Snippet. Vala. Showing a Message Dialog

The class MessageDialog in the GTK package of Vala is a convenience widget that provides the possibility to present a dialog with an image representing the type of message (Error, Information, Question, etc.) alongside some message text.

var dialog = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "Hello from Vala!");
dialog.set_title("Message Dialog");;

The class MessageDialog is an extension of the class Dialog and can easily be created from there. However, MessageDialog is useful for routine messages and will save you lots of typing.

Possible Types of MessageDialog

Four different types of MessageDialog are readily available.

// Error
var dialog_error = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, "This is an error message");
// Info
var dialog_info = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "This is an info message");
// Question
var dialog_question = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, "This is a question message requiring a choice.");
// Warning
var dialog_warning = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.WARNING, Gtk.ButtonsType.OK, "This is a warning message");

Possible Flags of MessageDialog

Flags allow MessageDialog to have different behaviour. The following flags are available:

  • GTK_DIALOG_MODAL - Make the constructed dialog modal, see gtk_window_set_modal().
  • GTK_DIALOG_DESTROY_WITH_PARENT - Destroy the dialog when its parent is destroyed, see gtk_window_set_destroy_with_parent().
  • GTK_DIALOG_NO_SEPARATOR - Don't put a separator between the action area and the dialog content. This option has been deprecated in GTK+ 2.22. It will be removed in GTK+ 3
// Modal
var dialog_info = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "This is an info message");

// Destroyable with parent
var dialog_info = new Gtk.MessageDialog(parent,Gtk.DialogFlags.DESTROY_WITH_PARENT,Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "This is an info message");

// Modal and Destroyable
var dialog_info = new Gtk.MessageDialog(parent,Gtk.DialogFlags.DESTROY_WITH_PARENT | Gtk.DialogFlags.MODAL,Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "This is an info message");

Possible Buttons of MessageDialog

Different pre-ready buttons can be placed visible in MessageDialog.

// No Buttons
var dialog = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.INFO, Gtk.ButtonsType.NONE, "This is a dialog with no buttons");
// Button OK
var dialog_ok = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.INFO, Gtk.ButtonsType.OK, "This is a dialog with OK button");
// Button Close
var dialog_close = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.INFO, Gtk.ButtonsType.CLOSE, "This is a dialog with Close button");
// Button Cancel
var dialog_cancel = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.INFO, Gtk.ButtonsType.CANCEL, "This is a dialog with Cancel button");
// Buttons Yes and No
var dialog_yes_no = new Gtk.MessageDialog(null,Gtk.DialogFlags.MODAL,Gtk.MessageType.INFO, Gtk.ButtonsType.YES_NO, "This is a dialog with Yes and No buttons");

Updated on: 24 Apr 2024