DialogProtocol
public protocol DialogProtocol : WindowProtocol
Dialogs are a convenient way to prompt the user for a small amount of input.
Typical uses are to display a message, ask a question, or anything else that does not require extensive effort on the user’s part.
The main area of a GtkDialog
is called the “content area”, and is yours
to populate with widgets such a GtkLabel
or GtkEntry
, to present
your information, questions, or tasks to the user.
In addition, dialogs allow you to add “action widgets”. Most commonly,
action widgets are buttons. Depending on the platform, action widgets may
be presented in the header bar at the top of the window, or at the bottom
of the window. To add action widgets, create your GtkDialog
using
[ctorGtk.Dialog.new_with_buttons
], or use
[methodGtk.Dialog.add_button
], [methodGtk.Dialog.add_buttons
],
or [methodGtk.Dialog.add_action_widget
].
GtkDialogs
uses some heuristics to decide whether to add a close
button to the window decorations. If any of the action buttons use
the response ID GTK_RESPONSE_CLOSE
or GTK_RESPONSE_CANCEL
, the
close button is omitted.
Clicking a button that was added as an action widget will emit the
[signalGtk.Dialog::response
] signal 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 [enumGtk.ResponseType
] enumeration (these all have values
less than zero). If a dialog receives a delete event, the
[signalGtk.Dialog::response
] signal will be emitted with the
GTK_RESPONSE_DELETE_EVENT
response ID.
Dialogs are created with a call to [ctorGtk.Dialog.new
] or
[ctorGtk.Dialog.new_with_buttons
]. The latter is recommended; it allows
you to set the dialog title, some convenient flags, and add buttons.
A “modal” dialog (that is, one which freezes the rest of the application
from user input), can be created by calling [methodGtk.Window.set_modal
]
on the dialog. When using [ctorGtk.Dialog.new_with_buttons
], you can also
pass the GTK_DIALOG_MODAL
flag to make a dialog modal.
For the simple dialog in the following example, a [classGtk.MessageDialog
]
would save 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:
// Function to open a dialog box with a message
void
quick_message (GtkWindow *parent, char *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_window_destroy),
dialog);
// Add the label, and show everything we’ve added
gtk_box_append (GTK_BOX (content_area), label);
gtk_widget_show (dialog);
}
GtkDialog as GtkBuildable
The GtkDialog
implementation of the GtkBuildable
interface exposes the
content_area
as an internal child with the name “content_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">
</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>
Accessibility
GtkDialog
uses the GTK_ACCESSIBLE_ROLE_DIALOG
role.
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
GtkDialog
instance.Declaration
Swift
var ptr: UnsafeMutableRawPointer! { get }
-
dialog_ptr
Default implementationTyped pointer to the underlying
GtkDialog
instance.Default Implementation
Return the stored, untyped pointer as a typed pointer to the
GtkDialog
instance.Declaration
Swift
var dialog_ptr: UnsafeMutablePointer<GtkDialog>! { get }
-
Required Initialiser for types conforming to
DialogProtocol
Declaration
Swift
init(raw: UnsafeMutableRawPointer)
-
bind(property:
Extension methodto: _: flags: transformFrom: transformTo: ) Bind a
DialogPropertyName
source 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 : ObjectProtocol
Parameters
source_property
the source property to bind
target
the target object to bind to
target_property
the target property to bind to
flags
the flags to pass to the
Binding
transform_from
ValueTransformer
to use for forward transformationtransform_to
ValueTransformer
to use for backwards transformationReturn Value
binding reference or
nil
in case of an error -
get(property:
Extension method) Get the value of a Dialog property
Declaration
Swift
@inlinable func get(property: DialogPropertyName) -> GLibObject.Value
Parameters
property
the 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
property
the 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
DialogSignalName
signalDeclaration
Swift
@discardableResult @inlinable func connect(signal s: DialogSignalName, flags f: ConnectFlags = ConnectFlags(0), handler h: @escaping SignalHandler) -> Int
Parameters
signal
The signal to connect
flags
The connection flags to use
data
A pointer to user data to provide to the callback
destroyData
A
GClosureNotify
C function to destroy the data pointed to byuserData
handler
The 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
DialogSignalName
signalDeclaration
Swift
@discardableResult @inlinable func connect(signal s: DialogSignalName, flags f: ConnectFlags = ConnectFlags(0), data userData: gpointer!, destroyData destructor: GClosureNotify? = nil, signalHandler h: @escaping GCallback) -> Int
Parameters
signal
The signal to connect
flags
The connection flags to use
data
A pointer to user data to provide to the callback
destroyData
A
GClosureNotify
C function to destroy the data pointed to byuserData
signalHandler
The 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: ) Emitted when the user uses a keybinding to close the dialog.
This is a keybinding signal.
The default binding for this signal is the Escape key.
Note
This represents the underlyingclose
signalDeclaration
Swift
@discardableResult @inlinable func onClose(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: DialogRef) -> Void) -> Int
Parameters
flags
Flags
unownedSelf
Reference to instance of self
handler
The signal handler to call Run the given callback whenever the
close
signal is emitted -
closeSignal
Extension methodTyped
close
signal for using theconnect(signal:)
methodsDeclaration
Swift
static var closeSignal: DialogSignalName { get }
-
onResponse(flags:
Extension methodhandler: ) Emitted when an action widget is clicked.
The signal is also emitted when the dialog receives a delete event, and when [method
Gtk.Dialog.response
] is called. On a delete event, the response ID isGTK_RESPONSE_DELETE_EVENT
. Otherwise, it depends on which action widget was clicked.Note
This represents the underlyingresponse
signalDeclaration
Swift
@discardableResult @inlinable func onResponse(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: DialogRef, _ responseId: Int) -> Void) -> Int
Parameters
flags
Flags
unownedSelf
Reference to instance of self
responseId
the response ID
handler
The signal handler to call Run the given callback whenever the
response
signal is emitted -
responseSignal
Extension methodTyped
response
signal 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 innotify
being 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-bar
signalDeclaration
Swift
@discardableResult @inlinable func onNotifyUseHeaderBar(flags: ConnectFlags = ConnectFlags(0), handler: @escaping (_ unownedSelf: DialogRef, _ pspec: ParamSpecRef) -> Void) -> Int
Parameters
flags
Flags
unownedSelf
Reference to instance of self
pspec
the
GParamSpec
of the property which changed.handler
The signal handler to call Run the given callback whenever the
notifyUseHeaderBar
signal is emitted -
notifyUseHeaderBarSignal
Extension methodTyped
notify::use-header-bar
signal 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
.GTK connects a signal handler that will emit the [signal
Gtk.Dialog::response
] signal 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 the
action_area
field of theGtkDialog
struct.Declaration
Swift
@inlinable func addActionWidget<WidgetT>(child: WidgetT, responseId: Int) where WidgetT : WidgetProtocol
-
addButton(buttonText:
Extension methodresponseId: ) Adds a button with the given text.
GTK arranges things so that clicking the button will emit the [signal
Gtk.Dialog::response
] signal 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<CChar>!, responseId: Int) -> WidgetRef!
-
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 the [property
Gtk.Dialog:use-header-bar
] property 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
response
signal with the given response ID.Used to indicate that the user has responded to the dialog in some way.
Declaration
Swift
@inlinable func response(responseId: Int)
-
setDefaultResponse(responseId:
Extension method) Sets the default widget for the dialog based on the response ID.
Pressing “Enter” normally activates the default widget.
Declaration
Swift
@inlinable func setDefaultResponse(responseId: Int)
-
setResponseSensitive(responseId:
Extension methodsetting: ) A convenient way to sensitize/desensitize dialog buttons.
Calls
gtk_widget_set_sensitive (widget,
setting)
for each widget in the dialog’s action area with the givenresponse_id
.Declaration
Swift
@inlinable func setResponseSensitive(responseId: Int, setting: Bool)
-
contentArea
Extension methodReturns the content area of
dialog
.Declaration
Swift
@inlinable var contentArea: BoxRef! { get }
-
headerBar
Extension methodReturns the header bar of
dialog
.Note that the headerbar is only used by the dialog if the [property
Gtk.Dialog:use-header-bar
] property istrue
.Declaration
Swift
@inlinable var headerBar: HeaderBarRef! { get }
-
parentInstance
Extension methodUndocumented
Declaration
Swift
@inlinable var parentInstance: GtkWindow { get }