MESSAGE DIALOGS

Message dialogs are similar to Dialogs and work in the same way but are easier to use in order to create the classic error/info message dialog of icon and text to alert user to something.

A Seed Javascript example is as follows:

   1 #!/usr/bin/env seed
   2 Gtk = imports.gi.Gtk;
   3 Gtk.init(null, null);
   4 
   5 var window = new Gtk.Window();
   6 window.signal.hide.connect(Gtk.main_quit);
   7 
   8 message_dialog = new Gtk.MessageDialog({message_type: Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: “Information only”});
   9 
  10 message_dialog.signal.response.connect(function(w, response_id) {
  11    message_dialog.hide();
  12 });
  13 window.show_all();
  14 message_dialog.show();
  15 
  16 Gtk.main();
  17 message_dialog.destroy();

The message Dialog works exactly the same way as a Dialog but only differs in the way it is created. The following piece of code, taken from above JavaScript Seed example, shows how it is created.

message_dialog = new Gtk.MessageDialog({message_type: Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: “Information only”});

The first JSON declaration, defines the icon to be used in the Message Dialog. The icon that is used is an Info ones. There are others including warning, question, error and other. The full list of these are as follows and are self explanatory:

Gtk.MessageType.INFO, Gtk.MessageType.WARNING, Gtk.MessageType.QUESTION, Gtk.MessageType.ERROR, Gtk.MessageType.OTHER

The second JSON declaration, defines the buttons to be used on the Message Dialog. The button that is being used is the Ok button. Other buttons can be used. The full list of these are as follows and are self-explanatory.

Gtk.ButtonsType.NONE, Gtk.ButtonsType.OK, Gtk.ButtonsType.CLOSE, Gtk.ButtonsType.CANCEL, Gtk.ButtonsType.YES_NO, Gtk.ButtonsType.OK_CANCEL

The last JSON declaration is text. This is the text that is to be displayed in the Message dialog.

A Seed JavaScript example of how a message dialog would be used in a real-life situation is as follows:

   1 #!/usr/bin/env seed
   2 
   3 Gtk = imports.gi.Gtk;
   4 Gtk.init(null, null);
   5 
   6 var window = new Gtk.Window({title: “Application”});
   7 window.signal.hide.connect(Gtk.main_quit);
   8 
   9 var entry = new Gtk.Entry();
  10 var button = new Gtk.Button({label: “Click Here”});
  11 var message_dialog = new Gtk.MessageDialog({message_type: Gtk.MessageType.ERROR, buttons : Gtk.ButtonsType.OK, text: “You need to entry text in entry box”});
  12 var vbox = new Gtk.VBox({homogenous: false, spacing : 5});
  13 vbox.pack_start(entry);
  14 vbox.pack_end(button);
  15 message_dialog.signal.response.connect(function(w, response_id) {
  16     message_dialog.hide();
  17 });
  18 
  19 button.signal.clicked.connect(function(w) {
  20    entry_text = entry.get_text();
  21    if (entry_text == ”) {
  22      message_dialog.show();
  23    }
  24    else {
  25      Seed.print(“Text entered : ” + entry_text)
  26    };
  27 
  28 });
  29 
  30 window.add(vbox);
  31 window.show_all();
  32 
  33 Gtk.main();

The above example displays an entry and button. If there is no text is the entry box an error message appears. If there is text in the entry, then it’s contents is displayed.

Projects/Seed/Tutorial/Gtk/message_dialog (last edited 2013-11-22 19:19:55 by WilliamJonMcCann)