Blame platform-demos/C/messagedialog.js.page

Packit 1470ea
Packit 1470ea
Packit 1470ea
      xmlns:its="http://www.w3.org/2005/11/its"
Packit 1470ea
      xmlns:xi="http://www.w3.org/2001/XInclude"
Packit 1470ea
      type="guide" style="task"
Packit 1470ea
      id="messagedialog.js">
Packit 1470ea
  <info>
Packit 1470ea
  <title type="text">MessageDialog (JavaScript)</title>
Packit 1470ea
    <link type="guide" xref="beginner.js#windows"/>
Packit 1470ea
    <link type="seealso" xref="GtkApplicationWindow.js" />
Packit 1470ea
    <link type="seealso" xref="gmenu.js" />
Packit 1470ea
    <link type="seealso" xref="label.js" />
Packit 1470ea
    <revision version="0.2" date="2012-06-25" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Taryn Fox</name>
Packit 1470ea
      <email its:translate="no">jewelfox@fursona.net</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>A popup message attached to a window</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>MessageDialog</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/messagedialog.png"/>
Packit 1470ea
  

A MessageDialog is a modal message dialog, which means a popup that you have to respond to before you get back to what you were doing in the window that it's attached to. This one can cause the world to explode (or at least it says that it can). To make the popup appear when you run this sample, click on "Message" inside of its application menu -- that's the menu that appears when you click on an application's name in the upper-left screen corner, next to Activities.

Packit 1470ea
  <note>

The difference between a MessageDialog and a <link xref="dialog.js">Dialog</link> is that a Dialog can contain whatever widgets and content you want to put in it, whereas a MessageDialog is just a convenient way to make popups appear with a basic message and buttons.

</note>
Packit 1470ea
    <links type="section" />
Packit 1470ea
Packit 1470ea
  <section id="imports">
Packit 1470ea
    <title>Libraries to import</title>
Packit 1470ea
    
Packit 1470ea
#!/usr/bin/gjs
Packit 1470ea
Packit 1470ea
const Gio = imports.gi.Gio;
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
const Lang = imports.lang;
Packit 1470ea
]]>
Packit 1470ea
    

These are the libraries we need to import for this application to run. Remember that the line which tells GNOME that we're using Gjs always needs to go at the start.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="applicationwindow">
Packit 1470ea
    <title>Creating the application window</title>
Packit 1470ea
    
Packit 1470ea
const MessageDialogExample = new Lang.Class ({
Packit 1470ea
    Name: 'MessageDialog Example',
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    _init: function () {
Packit 1470ea
        this.application = new Gtk.Application ({
Packit 1470ea
            application_id: 'org.example.jsmessagedialog',
Packit 1470ea
            flags: Gio.ApplicationFlags.FLAGS_NONE });
Packit 1470ea
Packit 1470ea
        // Connect 'activate' and 'startup' signals to the callback functions
Packit 1470ea
        this.application.connect('activate', Lang.bind(this, this._onActivate));
Packit 1470ea
        this.application.connect('startup', Lang.bind(this, this._onStartup));
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    // Callback function for 'activate' signal presents windows when active
Packit 1470ea
    _onActivate: function () {
Packit 1470ea
        this._window.present ();
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    // Callback function for 'startup' signal initializes menus and builds the UI
Packit 1470ea
    _onStartup: function () {
Packit 1470ea
        this._initMenus();
Packit 1470ea
        this._buildUI ();
Packit 1470ea
    },
Packit 1470ea
]]>
Packit 1470ea
    

All the code for this sample goes in the MessageDialogExample class. The above code creates a <link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link> for our widgets and window to go in.

Packit 1470ea
    <note>

Before we call _buildUI to create the window and the widgets inside it, we need to call _initMenus, which tells GNOME to create the menu. We can put the actual code for _initMenus after the code for _buildUI, since it doesn't matter what order we put them in so long as _initMenus is called first in _onStartup.

</note>
Packit 1470ea
    
Packit 1470ea
    // Build the application's UI
Packit 1470ea
    _buildUI: function () {
Packit 1470ea
Packit 1470ea
        // Create the application window
Packit 1470ea
        this._window = new Gtk.ApplicationWindow  ({
Packit 1470ea
            application: this.application,
Packit 1470ea
            window_position: Gtk.WindowPosition.CENTER,
Packit 1470ea
            title: "Gtk.MessageDialog Example",
Packit 1470ea
            default_height: 200,
Packit 1470ea
            default_width: 400 });
Packit 1470ea
]]>
Packit 1470ea
    

The _buildUI function is where we put all the code to create the application's user interface. The first step is creating a new <link xref="GtkApplicationWindow.js">Gtk.ApplicationWindow</link> to put all our widgets into.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Create a silly warning message and add it to the window
Packit 1470ea
        this.warningLabel = new Gtk.Label ({
Packit 1470ea
            label: "This application goes boom! (Not really.)"});
Packit 1470ea
        this._window.add (this.warningLabel);
Packit 1470ea
]]>
Packit 1470ea
    

For this example, all that we have in the window the popup comes out of is a silly warning <link xref="label.js">Label</link>.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="menu">
Packit 1470ea
    <title>Creating the application's menu</title>
Packit 1470ea
    
Packit 1470ea
    // Build the application menu, including the button that calls the dialog
Packit 1470ea
    _initMenus: function() {
Packit 1470ea
        let menu = new Gio.Menu();
Packit 1470ea
        menu.append("Message",'app.message');
Packit 1470ea
        menu.append("Quit",'app.quit');
Packit 1470ea
        this.application.set_app_menu(menu);
Packit 1470ea
Packit 1470ea
        // This pops up a MessageDialog when "Message" is clicked in the menu
Packit 1470ea
        let messageAction = new Gio.SimpleAction ({ name: 'message' });
Packit 1470ea
        messageAction.connect('activate', Lang.bind(this,
Packit 1470ea
            function() {
Packit 1470ea
                this._showMessageDialog();
Packit 1470ea
            }));
Packit 1470ea
        this.application.add_action(messageAction);
Packit 1470ea
Packit 1470ea
        // This closes the window when "Quit" is clicked in the menu
Packit 1470ea
        let quitAction = new Gio.SimpleAction ({ name: 'quit' });
Packit 1470ea
        quitAction.connect('activate', Lang.bind(this,
Packit 1470ea
            function() {
Packit 1470ea
                this._window.destroy();
Packit 1470ea
            }));
Packit 1470ea
        this.application.add_action(quitAction);
Packit 1470ea
    },
Packit 1470ea
]]>
Packit 1470ea
    

Here, we build the <link xref="gmenu.js">GMenu</link> where we'll be putting the "Message" button which triggers the popup MessageDialog. The GMenu is the menu that appears when you click the application's name in the upper-left corner of the screen, next to the Activities menu. Our menu only has two options in it: Message, and Quit.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="messagedialog">
Packit 1470ea
    <title>Creating the MessageDialog</title>
Packit 1470ea
    
Packit 1470ea
    _showMessageDialog: function () {
Packit 1470ea
Packit 1470ea
        // Create a modal MessageDialog whose parent is the window
Packit 1470ea
        this._messageDialog = new Gtk.MessageDialog ({
Packit 1470ea
            transient_for: this._window,
Packit 1470ea
            modal: true,
Packit 1470ea
            buttons: Gtk.ButtonsType.OK_CANCEL,
Packit 1470ea
            message_type: Gtk.MessageType.WARNING,
Packit 1470ea
            text: "This action will cause the universe to stop existing." });
Packit 1470ea
Packit 1470ea
        this._messageDialog.connect ('response', Lang.bind(this, this._response_cb));
Packit 1470ea
        this._messageDialog.show();
Packit 1470ea
    },
Packit 1470ea
]]>
Packit 1470ea
    

To make our MessageDialog a popup attached to the main window, we set its modal property to true and set it to be "transient_for" _window. After that, we can set what kind of buttons it has and what kind of message it is (which determines what icon appears next to the message), and write out the text inside it, before connecting its "response" signal to the callback function which handles it.

Packit 1470ea
    <note>

Here are some resources for making your own MessageDialogs:

Packit 1470ea
      <list>
Packit 1470ea
        <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkMessageDialog.html#GtkButtonsType">List of button types</link>

</item>
Packit 1470ea
        <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkMessageDialog.html#GtkMessageType">List of message types</link>

</item>
Packit 1470ea
      </list>
Packit 1470ea
    </note>
Packit 1470ea
Packit 1470ea
    
Packit 1470ea
    // Callback function (aka signal handler) for the response signal
Packit 1470ea
    _response_cb: function (messagedialog, response_id) {
Packit 1470ea
Packit 1470ea
        // A simple switch that changes the main window's label
Packit 1470ea
        switch (response_id) {
Packit 1470ea
            case Gtk.ResponseType.OK:
Packit 1470ea
                this.warningLabel.set_label ("*BOOM*\n");
Packit 1470ea
                break;
Packit 1470ea
            case Gtk.ResponseType.CANCEL:
Packit 1470ea
                this.warningLabel.set_label ("Good choice!\n");
Packit 1470ea
                break;
Packit 1470ea
            case Gtk.ResponseType.DELETE_EVENT:
Packit 1470ea
                this.warningLabel.set_label ("Dialog closed or cancelled.\n");
Packit 1470ea
                break;
Packit 1470ea
        }
Packit 1470ea
Packit 1470ea
        this._messageDialog.destroy();
Packit 1470ea
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
});
Packit 1470ea
]]>
Packit 1470ea
    

This function takes two parameters, the MessageDialog and its response_id, both of which are automatically supplied (you don't have to manually pass them to it for it to work). Here we use a simple switch to change the "warning label"'s text, depending on which option you select. The DELETE_EVENT occurs if you press Escape to cancel the MessageDialog, instead of clicking OK or Cancel. Whatever you select, the popup is destroyed afterwards.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
// Run the application
Packit 1470ea
let app = new MessageDialogExample ();
Packit 1470ea
app.application.run (ARGV);
Packit 1470ea
]]>
Packit 1470ea
    

Finally, we create a new instance of the finished MessageDialogExample class, and set the application running.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="complete">
Packit 1470ea
    <title>Complete code sample</title>
Packit 1470ea
<xi:include href="samples/messagedialog.js" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="in-depth">
Packit 1470ea
    <title>In-depth documentation</title>
Packit 1470ea

Packit 1470ea
  In this sample we used the following:
Packit 1470ea

Packit 1470ea
<list>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gio/unstable/GMenu.html">GMenu</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gio/stable/GSimpleAction.html">GSimpleAction</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkApplicationWindow.html">Gtk.ApplicationWindow</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.MessageDialog.html">Gtk.MessageDialog</link>

</item>
Packit 1470ea
</list>
Packit 1470ea
  </section>
Packit 1470ea
</page>