Struct gtk4::Dialog [−][src]
pub struct Dialog(_);
Expand description
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 Dialog
is called the “content area”, and is yours
to populate with widgets such a Label
or Entry
, 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 Dialog
using
with_buttons()
, or use
DialogExt::add_button()
, DialogExtManual::add_buttons()
,
or DialogExt::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 ResponseType::Close
or ResponseType::Cancel
, the
close button is omitted.
Clicking a button that was added as an action widget will emit the
signal::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 ResponseType
enumeration (these all have values
less than zero). If a dialog receives a delete event, the
signal::Dialog::response
signal will be emitted with the
ResponseType::DeleteEvent
response ID.
Dialogs are created with a call to new()
or
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 GtkWindowExt::set_modal()
on the dialog. When using with_buttons()
, you can also
pass the DialogFlags::MODAL
flag to make a dialog modal.
For the simple dialog in the following example, a 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 Dialog
usage:
⚠️ The following code is in c ⚠️
// 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 Dialog
implementation of the Buildable
interface exposes the
content_area
as an internal child with the name “content_area”.
Dialog
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.
Dialog
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 Dialog
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
Dialog
uses the AccessibleRole::Dialog
role.
Implements
DialogExt
, GtkWindowExt
, WidgetExt
, glib::ObjectExt
, AccessibleExt
, BuildableExt
, ConstraintTargetExt
, NativeExt
, RootExt
, ShortcutManagerExt
, DialogExtManual
, WidgetExtManual
, AccessibleExtManual
Implementations
Creates a new builder-pattern struct instance to construct Dialog
objects.
This method returns an instance of DialogBuilder
which can be used to create Dialog
objects.
Creates a new Dialog
with the given title and transient parent.
The flags
argument can be used to make the dialog modal, have it
destroyed along with its transient parent, or make it use a headerbar.
Button text/response ID pairs should be listed in pairs, with a None
pointer ending the list. Button text can be arbitrary text. A response
ID can be any positive number, or one of the values in the
ResponseType
enumeration. If the user clicks one of these
buttons, Dialog
will emit the signal::Dialog::response
signal
with the corresponding response ID.
If a Dialog
receives a delete event, it will emit ::response with a
response ID of ResponseType::DeleteEvent
.
However, destroying a dialog does not emit the ::response signal;
so be careful relying on ::response when using the
DialogFlags::DESTROY_WITH_PARENT
flag.
Here’s a simple example: ⚠️ The following code is in c ⚠️
GtkWindow *main_app_window; // Window the dialog should show up on
GtkWidget *dialog;
GtkDialogFlags flags = GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT;
dialog = gtk_dialog_new_with_buttons ("My dialog",
main_app_window,
flags,
_("_OK"),
GTK_RESPONSE_ACCEPT,
_("_Cancel"),
GTK_RESPONSE_REJECT,
NULL);
title
Title of the dialog
parent
Transient parent of the dialog
flags
from DialogFlags
first_button_text
text to go in first button
Returns
a new Dialog
Trait Implementations
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Returns the type identifier of Self
.
Auto Trait Implementations
impl RefUnwindSafe for Dialog
impl UnwindSafe for Dialog
Blanket Implementations
Mutably borrows from an owned value. Read more
Upcasts an object to a superclass or interface T
. Read more
Upcasts an object to a reference of its superclass or interface T
. Read more
Tries to downcast to a subclass or interface implementor T
. Read more
Tries to downcast to a reference of its subclass or interface implementor T
. Read more
Tries to cast to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Tries to cast to reference to an object of type T
. This handles upcasting, downcasting
and casting between interface and interface implementors. All checks are performed at
runtime, while downcast
and upcast
will do many checks at compile-time already. Read more
Casts to T
unconditionally. Read more
Casts to &T
unconditionally. Read more
Returns true
if the object is an instance of (can be cast to) T
.
pub fn set_property<'a, N, V>(
&self,
property_name: N,
value: V
) -> Result<(), BoolError> where
N: Into<&'a str>,
V: ToValue,
pub fn set_property_from_value<'a, N>(
&self,
property_name: N,
value: &Value
) -> Result<(), BoolError> where
N: Into<&'a str>,
pub fn set_properties_from_value(
&self,
property_values: &[(&str, Value)]
) -> Result<(), BoolError>
pub fn has_property<'a, N>(&self, property_name: N, type_: Option<Type>) -> bool where
N: Into<&'a str>,
pub fn find_property<'a, N>(&self, property_name: N) -> Option<ParamSpec> where
N: Into<&'a str>,
Safety Read more
Safety Read more
Safety Read more
Safety Read more
pub fn connect<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
N: Into<&'a str>,
F: 'static + Fn(&[Value]) -> Option<Value> + Send + Sync,
Same as connect
but takes a SignalId
instead of a signal name.
pub fn connect_local<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
N: Into<&'a str>,
F: 'static + Fn(&[Value]) -> Option<Value>,
Same as connect_local
but takes a SignalId
instead of a signal name.
pub unsafe fn connect_unsafe<'a, N, F>(
&self,
signal_name: N,
after: bool,
callback: F
) -> Result<SignalHandlerId, BoolError> where
N: Into<&'a str>,
F: Fn(&[Value]) -> Option<Value>,
Same as connect_unsafe
but takes a SignalId
instead of a signal name.
Emit signal by signal id.
Same as emit
but takes Value
for the arguments.
Emit signal by its name.
Same as emit_by_name
but takes Value
for the arguments.
Emit signal with details by signal id.
Same as emit_with_details
but takes Value
for the arguments.
pub fn connect_notify<F>(&self, name: Option<&str>, f: F) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec) + Send + Sync,
pub fn connect_notify_local<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: 'static + Fn(&T, &ParamSpec),
pub unsafe fn connect_notify_unsafe<F>(
&self,
name: Option<&str>,
f: F
) -> SignalHandlerId where
F: Fn(&T, &ParamSpec),
pub fn bind_property<'a, O, N, M>(
&'a self,
source_property: N,
target: &'a O,
target_property: M
) -> BindingBuilder<'a> where
O: ObjectType,
N: Into<&'a str>,
M: Into<&'a str>,
Returns a SendValue
clone of self
.