[]Struct gtk::Dialog

pub struct Dialog(_, _);

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 VBox, and is where widgets such as a Label or a Entry 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.

Dialog boxes are created with a call to Dialog::new or Dialog::new_with_buttons. 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 DialogExt::get_content_area and 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 GtkWindowExt::set_modal on the dialog. Use the GTK_WINDOW() macro to cast the widget returned from Dialog::new into a Window. When using Dialog::new_with_buttons you can also pass the DialogFlags::Modal flag to make a dialog modal.

If you add buttons to Dialog using Dialog::new_with_buttons, DialogExt::add_button, DialogExt::add_buttons, or DialogExt::add_action_widget, clicking the button will emit a signal called Dialog::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 ResponseType enumeration (these all have values less than zero). If a dialog receives a delete event, the Dialog::response signal will be emitted with a response ID of ResponseType::DeleteEvent.

If you want to block waiting for a dialog to return before returning control flow to your code, you can call DialogExt::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 MessageDialog 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 Dialog usage:

// 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);
}

Dialog as Buildable

The Dialog implementation of the Buildable interface exposes the vbox and action_area as internal children with the names “vbox” and “action_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">
      <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>

Implements

DialogExt, GtkWindowExt, BinExt, ContainerExt, WidgetExt, glib::object::ObjectExt, BuildableExt, DialogExtManual, GtkWindowExtManual, WidgetExtManual, BuildableExtManual

Methods

impl Dialog[src]

pub fn new() -> Dialog[src]

Creates a new dialog box.

Widgets should not be packed into this Window directly, but into the vbox and action_area, as described above.

Returns

the new dialog as a Widget

impl Dialog[src]

pub fn new_with_buttons<T: IsA<Window>>(
    title: Option<&str>,
    parent: Option<&T>,
    flags: DialogFlags,
    buttons: &[(&str, ResponseType)]
) -> Dialog
[src]

Creates a new Dialog with title title (or None for the default title; see GtkWindowExt::set_title) and transient parent parent (or None for none; see GtkWindowExt::set_transient_for). The flags argument can be used to make the dialog modal (DialogFlags::Modal) and/or to have it destroyed along with its transient parent (DialogFlags::DestroyWithParent). After flags, button text/response ID pairs should be listed, 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 dialog buttons, Dialog will emit the Dialog::response signal with the corresponding response ID. If a Dialog receives the Widget::delete-event signal, 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::DestroyWithParent flag. Buttons are from left to right, so the first button in the list will be the leftmost button in the dialog.

Here’s a simple example:

 GtkWidget *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, or None

parent

Transient parent of the dialog, or None

flags

from DialogFlags

first_button_text

text to go in first button, or None

Returns

a new Dialog

Trait Implementations

impl Clone for Dialog

impl Debug for Dialog

impl Default for Dialog[src]

impl Display for Dialog[src]

impl Eq for Dialog

impl Hash for Dialog

impl IsA<Bin> for Dialog

impl IsA<Buildable> for Dialog

impl IsA<Container> for Dialog

impl IsA<Dialog> for AboutDialog

impl IsA<Dialog> for AppChooserDialog

impl IsA<Dialog> for ColorChooserDialog

impl IsA<Dialog> for FileChooserDialog

impl IsA<Dialog> for FontChooserDialog

impl IsA<Dialog> for MessageDialog

impl IsA<Dialog> for RecentChooserDialog

impl IsA<Widget> for Dialog

impl IsA<Window> for Dialog

impl Ord for Dialog

impl<T: ObjectType> PartialEq<T> for Dialog

impl<T: ObjectType> PartialOrd<T> for Dialog

impl StaticType for Dialog

Auto Trait Implementations

impl RefUnwindSafe for Dialog

impl !Send for Dialog

impl !Sync for Dialog

impl Unpin for Dialog

impl UnwindSafe for Dialog

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<Super, Sub> CanDowncast<Sub> for Super where
    Sub: IsA<Super>,
    Super: IsA<Super>, 

impl<T> Cast for T where
    T: ObjectType, 

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ObjectExt for T where
    T: ObjectType, 

impl<'a, T> ToGlibContainerFromSlice<'a, *const GList> for T where
    T: GlibPtrDefault + ToGlibPtr<'a, <T as GlibPtrDefault>::GlibType>, 

type Storage = (Option<List>, Vec<Stash<'a, <T as GlibPtrDefault>::GlibType, T>>)

impl<'a, T> ToGlibContainerFromSlice<'a, *mut GArray> for T where
    T: GlibPtrDefault + ToGlibPtr<'a, <T as GlibPtrDefault>::GlibType>, 

type Storage = (Option<Array>, Vec<Stash<'a, <T as GlibPtrDefault>::GlibType, T>>)

impl<'a, T> ToGlibContainerFromSlice<'a, *mut GList> for T where
    T: GlibPtrDefault + ToGlibPtr<'a, <T as GlibPtrDefault>::GlibType>, 

type Storage = (Option<List>, Vec<Stash<'a, <T as GlibPtrDefault>::GlibType, T>>)

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> ToValue for T where
    T: SetValue + ?Sized

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.