DialogProtocol
public protocol DialogProtocol : WindowProtocol
Dialog boxes are a convenient way to prompt the user for a small amount of input, e.g. to display a message, ask a question, or anything else that does not require extensive effort on the user’s part.
GTK+ treats a dialog as a window split vertically. The top section is a
GtkVBox, and is where widgets such as a GtkLabel or a GtkEntry should
be packed. The bottom area is known as the
“action area”. This is generally used for
packing buttons into the dialog which may perform functions such as
cancel, ok, or apply.
GtkDialog boxes are created with a call to gtk_dialog_new() or
gtk_dialog_new_with_buttons(). gtk_dialog_new_with_buttons() is
recommended; it allows you to set the dialog title, some convenient
flags, and add simple buttons.
If “dialog” is a newly created dialog, the two primary areas of the
window can be accessed through gtk_dialog_get_content_area() and
gtk_dialog_get_action_area(), as can be seen from the example below.
A “modal” dialog (that is, one which freezes the rest of the application
from user input), can be created by calling gtk_window_set_modal() on the
dialog. Use the GTK_WINDOW() macro to cast the widget returned from
gtk_dialog_new() into a GtkWindow. When using gtk_dialog_new_with_buttons()
you can also pass the GTK_DIALOG_MODAL flag to make a dialog modal.
If you add buttons to GtkDialog using gtk_dialog_new_with_buttons(),
gtk_dialog_add_button(), gtk_dialog_add_buttons(), or
gtk_dialog_add_action_widget(), clicking the button will emit a signal
called GtkDialog::response with a response ID that you specified. GTK+
will never assign a meaning to positive response IDs; these are entirely
user-defined. But for convenience, you can use the response IDs in the
GtkResponseType enumeration (these all have values less than zero). If
a dialog receives a delete event, the GtkDialog::response signal will
be emitted with a response ID of GTK_RESPONSE_DELETE_EVENT.
If you want to block waiting for a dialog to return before returning
control flow to your code, you can call gtk_dialog_run(). This function
enters a recursive main loop and waits for the user to respond to the
dialog, returning the response ID corresponding to the button the user
clicked.
For the simple dialog in the following example, in reality you’d probably
use GtkMessageDialog to save yourself some effort. But you’d need to
create the dialog contents manually if you had more than a simple message
in the dialog.
An example for simple GtkDialog usage: (C Language Example):
// Function to open a dialog box with a message
void
quick_message (GtkWindow *parent, gchar *message)
{
GtkWidget *dialog, *label, *content_area;
GtkDialogFlags flags;
// Create the widgets
flags = GTK_DIALOG_DESTROY_WITH_PARENT;
dialog = gtk_dialog_new_with_buttons ("Message",
parent,
flags,
_("_OK"),
GTK_RESPONSE_NONE,
NULL);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
label = gtk_label_new (message);
// Ensure that the dialog box is destroyed when the user responds
g_signal_connect_swapped (dialog,
"response",
G_CALLBACK (gtk_widget_destroy),
dialog);
// Add the label, and show everything we’ve added
gtk_container_add (GTK_CONTAINER (content_area), label);
gtk_widget_show_all (dialog);
}
GtkDialog as GtkBuildable
The GtkDialog implementation of the GtkBuildable interface exposes the
vbox and action_area as internal children with the names “vbox” and
“action_area”.
GtkDialog supports a custom <action-widgets> element, which can contain
multiple <action-widget> elements. The “response” attribute specifies a
numeric response, and the content of the element is the id of widget
(which should be a child of the dialogs action_area). To mark a response
as default, set the “default“ attribute of the <action-widget> element
to true.
GtkDialog supports adding action widgets by specifying “action“ as the “type“ attribute of a <child> element. The widget will be added either to the action area or the headerbar of the dialog, depending on the “use-header-bar“ property. The response id has to be associated with the action widget using the <action-widgets> element.
An example of a GtkDialog UI definition fragment:
<object class="GtkDialog" id="dialog1">
<child type="action">
<object class="GtkButton" id="button_cancel"/>
</child>
<child type="action">
<object class="GtkButton" id="button_ok">
<property name="can-default">True</property>
</object>
</child>
<action-widgets>
<action-widget response="cancel">button_cancel</action-widget>
<action-widget response="ok" default="true">button_ok</action-widget>
</action-widgets>
</object>
The DialogProtocol protocol exposes the methods and properties of an underlying GtkDialog instance.
The default implementation of these can be found in the protocol extension below.
For a concrete class that implements these methods and properties, see Dialog.
Alternatively, use DialogRef as a lighweight, unowned reference if you already have an instance you just want to use.
-
Untyped pointer to the underlying
GtkDialoginstance.Declaration
Swift
var ptr: UnsafeMutableRawPointer! { get } -
dialog_ptrDefault implementationTyped pointer to the underlying
GtkDialoginstance.Default Implementation
Return the stored, untyped pointer as a typed pointer to the
GtkDialoginstance.Declaration
Swift
var dialog_ptr: UnsafeMutablePointer<GtkDialog>! { get } -
Required Initialiser for types conforming to
DialogProtocolDeclaration
Swift
init(raw: UnsafeMutableRawPointer)
-
bind(property:Extension methodto: _: flags: transformFrom: transformTo: ) Bind a
DialogPropertyNamesource property to a given target object.Declaration
Swift
@discardableResult @inlinable func bind<Q, T>(property source_property: DialogPropertyName, to target: T, _ target_property: Q, flags f: BindingFlags = .default, transformFrom transform_from: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }, transformTo transform_to: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }) -> BindingRef! where Q : PropertyNameProtocol, T : ObjectProtocolParameters
source_propertythe source property to bind
targetthe target object to bind to
target_propertythe target property to bind to
flagsthe flags to pass to the
Bindingtransform_fromValueTransformerto use for forward transformationtransform_toValueTransformerto use for backwards transformationReturn Value
binding reference or
nilin case of an error -
get(property:Extension method) Get the value of a Dialog property
Declaration
Swift
@inlinable func get(property: DialogPropertyName) -> GLibObject.ValueParameters
propertythe property to get the value for
Return Value
the value of the named property
-
set(property:Extension methodvalue: ) Set the value of a Dialog property. Note that this will only have an effect on properties that are writable and not construct-only!
Declaration
Swift
@inlinable func set(property: DialogPropertyName, value v: GLibObject.Value)Parameters
propertythe property to get the value for
Return Value
the value of the named property
-
connect(signal:Extension methodflags: handler: ) Connect a Swift signal handler to the given, typed
DialogSignalNamesignalDeclaration
Swift
@discardableResult @inlinable func connect(signal s: DialogSignalName, flags f: ConnectFlags = ConnectFlags(0), handler h: @escaping SignalHandler) -> IntParameters
signalThe signal to connect
flagsThe connection flags to use
dataA pointer to user data to provide to the callback
destroyDataA
GClosureNotifyC function to destroy the data pointed to byuserDatahandlerThe Swift signal handler (function or callback) to invoke on the given signal
Return Value
The signal handler ID (always greater than 0 for successful connections)
-
connect(signal:Extension methodflags: data: destroyData: signalHandler: ) Connect a C signal handler to the given, typed
DialogSignalNamesignalDeclaration
Swift
@discardableResult @inlinable func connect(signal s: DialogSignalName, flags f: ConnectFlags = ConnectFlags(0), data userData: gpointer!, destroyData destructor: GClosureNotify? = nil, signalHandler h: @escaping GCallback) -> IntParameters
signalThe signal to connect
flagsThe connection flags to use
dataA pointer to user data to provide to the callback
destroyDataA
GClosureNotifyC function to destroy the data pointed to byuserDatasignalHandlerThe C function to be called on the given signal
Return Value
The signal handler ID (always greater than 0 for successful connections)
-
onClose(flags:Extension methodhandler: ) The
closesignal is a keybinding signal which gets emitted when the user uses a keybinding to close the dialog.The default binding for this signal is the Escape key.
Note
This represents the underlyingclosesignalDeclaration
Swift
@discardableResult @inlinable func onClose(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: DialogRef) -> Void) -> IntParameters
flagsFlags
unownedSelfReference to instance of self
handlerThe signal handler to call Run the given callback whenever the
closesignal is emitted -
closeSignalExtension methodTyped
closesignal for using theconnect(signal:)methodsDeclaration
Swift
static var closeSignal: DialogSignalName { get } -
onResponse(flags:Extension methodhandler: ) Emitted when an action widget is clicked, the dialog receives a delete event, or the application programmer calls
gtk_dialog_response(). On a delete event, the response ID isGTK_RESPONSE_DELETE_EVENT. Otherwise, it depends on which action widget was clicked.Note
This represents the underlyingresponsesignalDeclaration
Swift
@discardableResult @inlinable func onResponse(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: DialogRef, _ responseID: Int) -> Void) -> IntParameters
flagsFlags
unownedSelfReference to instance of self
responseIDthe response ID
handlerThe signal handler to call Run the given callback whenever the
responsesignal is emitted -
responseSignalExtension methodTyped
responsesignal for using theconnect(signal:)methodsDeclaration
Swift
static var responseSignal: DialogSignalName { get } -
onNotifyUseHeaderBar(flags:Extension methodhandler: ) The notify signal is emitted on an object when one of its properties has its value set through
g_object_set_property(),g_object_set(), et al.Note that getting this signal doesn’t itself guarantee that the value of the property has actually changed. When it is emitted is determined by the derived GObject class. If the implementor did not create the property with
G_PARAM_EXPLICIT_NOTIFY, then any call tog_object_set_property()results innotifybeing emitted, even if the new value is the same as the old. If they did passG_PARAM_EXPLICIT_NOTIFY, then this signal is emitted only when they explicitly callg_object_notify()org_object_notify_by_pspec(), and common practice is to do that only when the value has actually changed.This signal is typically used to obtain change notification for a single property, by specifying the property name as a detail in the
g_signal_connect()call, like this:(C Language Example):
g_signal_connect (text_view->buffer, "notify::paste-target-list", G_CALLBACK (gtk_text_view_target_list_notify), text_view)It is important to note that you must use canonical parameter names as detail strings for the notify signal.
Note
This represents the underlyingnotify::use-header-barsignalDeclaration
Swift
@discardableResult @inlinable func onNotifyUseHeaderBar(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: DialogRef, _ pspec: ParamSpecRef) -> Void) -> IntParameters
flagsFlags
unownedSelfReference to instance of self
pspecthe
GParamSpecof the property which changed.handlerThe signal handler to call Run the given callback whenever the
notifyUseHeaderBarsignal is emitted -
notifyUseHeaderBarSignalExtension methodTyped
notify::use-header-barsignal for using theconnect(signal:)methodsDeclaration
Swift
static var notifyUseHeaderBarSignal: DialogSignalName { get }
-
addActionWidget(child:Extension methodresponseID: ) Adds an activatable widget to the action area of a
GtkDialog, connecting a signal handler that will emit theGtkDialog::responsesignal on the dialog when the widget is activated. The widget is appended to the end of the dialog’s action area. If you want to add a non-activatable widget, simply pack it into theaction_areafield of theGtkDialogstruct.Declaration
Swift
@inlinable func addActionWidget<WidgetT>(child: WidgetT, responseID: Int) where WidgetT : WidgetProtocol -
addButton(buttonText:Extension methodresponseID: ) Adds a button with the given text and sets things up so that clicking the button will emit the
GtkDialog::responsesignal with the givenresponse_id. The button is appended to the end of the dialog’s action area. The button widget is returned, but usually you don’t need it.Declaration
Swift
@inlinable func addButton(buttonText: UnsafePointer<gchar>!, responseID: Int) -> WidgetRef! -
getActionArea()Extension methodReturns the action area of
dialog.get_action_area is deprecated: Direct access to the action area is discouraged; use gtk_dialog_add_button(), etc.
Declaration
Swift
@available(*, deprecated) @inlinable func getActionArea() -> BoxRef! -
getContentArea()Extension methodReturns the content area of
dialog.Declaration
Swift
@inlinable func getContentArea() -> BoxRef! -
getHeaderBar()Extension methodReturns the header bar of
dialog. Note that the headerbar is only used by the dialog if theGtkDialog:use-header-barproperty istrue.Declaration
Swift
@inlinable func getHeaderBar() -> HeaderBarRef! -
getResponseFor(widget:Extension method) Gets the response id of a widget in the action area of a dialog.
Declaration
Swift
@inlinable func getResponseFor<WidgetT>(widget: WidgetT) -> Int where WidgetT : WidgetProtocol -
getWidgetForResponse(responseID:Extension method) Gets the widget button that uses the given response ID in the action area of a dialog.
Declaration
Swift
@inlinable func getWidgetForResponse(responseID: Int) -> WidgetRef! -
response(responseID:Extension method) Emits the
GtkDialog::responsesignal with the given response ID. Used to indicate that the user has responded to the dialog in some way; typically either you orgtk_dialog_run()will be monitoring theresponsesignal and take appropriate action.Declaration
Swift
@inlinable func response(responseID: Int) -
run()Extension methodBlocks in a recursive main loop until the
dialogeither emits theGtkDialog::responsesignal, or is destroyed. If the dialog is destroyed during the call togtk_dialog_run(),gtk_dialog_run()returnsGTK_RESPONSE_NONE. Otherwise, it returns the response ID from theresponsesignal emission.Before entering the recursive main loop,
gtk_dialog_run()callsgtk_widget_show()on the dialog for you. Note that you still need to show any children of the dialog yourself.During
gtk_dialog_run(), the default behavior ofGtkWidget::delete-eventis disabled; if the dialog receivesdelete_event, it will not be destroyed as windows usually are, andgtk_dialog_run()will returnGTK_RESPONSE_DELETE_EVENT. Also, duringgtk_dialog_run()the dialog will be modal. You can forcegtk_dialog_run()to return at any time by callinggtk_dialog_response()to emit theresponsesignal. Destroying the dialog duringgtk_dialog_run()is a very bad idea, because your post-run code won’t know whether the dialog was destroyed or not.After
gtk_dialog_run()returns, you are responsible for hiding or destroying the dialog if you wish to do so.Typical usage of this function might be: (C Language Example):
GtkWidget *dialog = gtk_dialog_new (); // Set up dialog... int result = gtk_dialog_run (GTK_DIALOG (dialog)); switch (result) { case GTK_RESPONSE_ACCEPT: // do_application_specific_something (); break; default: // do_nothing_since_dialog_was_cancelled (); break; } gtk_widget_destroy (dialog);Note that even though the recursive main loop gives the effect of a modal dialog (it prevents the user from interacting with other windows in the same window group while the dialog is run), callbacks such as timeouts, IO channel watches, DND drops, etc, will be triggered during a
gtk_dialog_run()call.Declaration
Swift
@inlinable func run() -> Int -
setAlternativeButtonOrderFromArray(nParams:Extension methodnewOrder: ) Sets an alternative button order. If the
GtkSettings:gtk-alternative-button-ordersetting is set totrue, the dialog buttons are reordered according to the order of the response ids innew_order.See
gtk_dialog_set_alternative_button_order()for more information.This function is for use by language bindings.
set_alternative_button_order_from_array is deprecated: Deprecated
Declaration
Swift
@available(*, deprecated) @inlinable func setAlternativeButtonOrderFromArray(nParams: Int, newOrder: UnsafeMutablePointer<gint>!) -
setDefaultResponse(responseID:Extension method) Sets the last widget in the dialog’s action area with the given
response_idas the default widget for the dialog. Pressing “Enter” normally activates the default widget.Declaration
Swift
@inlinable func setDefaultResponse(responseID: Int) -
setResponseSensitive(responseID:Extension methodsetting: ) Calls
gtk_widget_set_sensitive (widget,setting)for each widget in the dialog’s action area with the givenresponse_id. A convenient way to sensitize/desensitize dialog buttons.Declaration
Swift
@inlinable func setResponseSensitive(responseID: Int, setting: Bool) -
actionAreaExtension methodReturns the action area of
dialog.get_action_area is deprecated: Direct access to the action area is discouraged; use gtk_dialog_add_button(), etc.
Declaration
Swift
@inlinable var actionArea: BoxRef! { get } -
contentAreaExtension methodReturns the content area of
dialog.Declaration
Swift
@inlinable var contentArea: BoxRef! { get } -
headerBarExtension methodReturns the header bar of
dialog. Note that the headerbar is only used by the dialog if theGtkDialog:use-header-barproperty istrue.Declaration
Swift
@inlinable var headerBar: HeaderBarRef! { get } -
windowExtension methodUndocumented
Declaration
Swift
@inlinable var window: GtkWindow { get }
View on GitHub
Install in Dash
DialogProtocol Protocol Reference