The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

DESCRIPTION

Dialog boxes are a convenient way to prompt the user for a small amount of input, eg. 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 Gtk2::VBox, and is where widgets such as a Gtk2::Label or a Gtk2::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. The two areas are separated by a Gtk2::HSeparator.

GtkDialog boxes are created with a call to Gtk2::Dialog->new. The multi-argument form (and its alias, new_with_buttons is recommended; it allows you to set the dialog title, some convenient flags, and add simple buttons all in one go.

If $dialog is a newly created dialog, the two primary areas of the window can be accessed as $dialog->vbox and $dialog->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 the Gtk2::Window method set_modal on the dialog. You can also pass the 'modal' flag to new.

If you add buttons to GtkDialog using new, new_with_buttons, add_button, add_buttons, or add_action_widget, clicking the button will emit a signal called "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 Gtk2::ResponseType enumeration. If a dialog receives a delete event, the "response" signal will be emitted with a response ID of 'GTK_RESPONSE_NONE' (except within run -- see below).

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

 # Function to open a dialog box displaying the message provided.
 
 sub quick_message {
    my $message = shift;
    my $dialog = Gtk2::Dialog->new ('Message', $main_app_window,
                                    'destroy-with-parent',
                                    'gtk-ok' => 'none');
    my $label = Gtk2::Label->new (message);
    $dialog->vbox->add ($label);

    # Ensure that the dialog box is destroyed when the user responds.
    $dialog->signal_connect (response => sub { $_[0]->destroy });

    $dialog->show_all;
 }

The response type is somewhat abnormal as far as gtk2-perl enums go. In C, this enum lists named, predefined integer values for a field that is other composed of whatever integer values you like. In Perl, we allow this to be either one of the string constants listed here or any positive integer value. For example, 'ok', 'cancel', 4, and 42 are all valid response ids. You cannot use arbitrary string values, they must be integers. Be careful, because unknown string values tend to be mapped to 0.

Alias for the multi-argument version of Gtk2::Dialog->new.

The multi-argument form takes the same list of text => response-id pairs as $dialog->add_buttons. Do not pack widgets directly into the window; add them to $dialog->vbox.

Here's a simple example:

 $dialog = Gtk2::Dialog->new ('A cool dialog',
                              $main_app_window,
                              [qw/modal destroy-with-parent/],
                              'gtk-ok'     => 'accept',
                              'gtk-cancel' => 'reject');

During run, the default behavior of "delete_event" is disabled; if the dialog receives "delete_event", it will not be destroyed as windows usually are, and run will return 'GTK_RESPONSE_DELETE_EVENT' ('delete-event'). Also, during run the dialog will be modal. You can force run to return at any time by calling $dialog->response to emit the "response" signal. Destroying the dialog during run is a very bad idea, because your post-run code won't know whether the dialog was destroyed or not.

After run returns, you are responsible for hiding or destroying the dialog if you wish to do so.

Typical usage of this function might be:

  if ('accept' eq $dialog->run) {
         do_application_specific_something ();
  } else {
         do_nothing_since_dialog_was_cancelled ();
  }
  $dialog->destroy;