Blame platform-demos/C/samples/combobox.js

Packit 1470ea
#!/usr/bin/gjs
Packit 1470ea
Packit 1470ea
imports.gi.versions.Gtk = '3.0';
Packit 1470ea
Packit 1470ea
const GObject = imports.gi.GObject;
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
Packit 1470ea
class ComboBoxExample {
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    constructor() {
Packit 1470ea
        this.application = new Gtk.Application ({
Packit 1470ea
            application_id: 'org.example.jscombobox'});
Packit 1470ea
Packit 1470ea
        // Connect 'activate' and 'startup' signals to the callback functions
Packit 1470ea
        this.application.connect('activate', this._onActivate.bind(this));
Packit 1470ea
        this.application.connect('startup', this._onStartup.bind(this));
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Callback function for 'activate' signal presents windows when active
Packit 1470ea
    _onActivate() {
Packit 1470ea
        this._window.present ();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Callback function for 'startup' signal builds the UI
Packit 1470ea
    _onStartup() {
Packit 1470ea
        this._buildUI();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Build the application's UI
Packit 1470ea
    _buildUI() {
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: "Welcome to GNOME",
Packit 1470ea
            default_width: 200,
Packit 1470ea
            border_width: 10 });
Packit 1470ea
Packit 1470ea
        // Create the liststore to put our options in
Packit 1470ea
        this._listStore = new Gtk.ListStore();
Packit 1470ea
        this._listStore.set_column_types ([
Packit 1470ea
            GObject.TYPE_STRING,
Packit 1470ea
            GObject.TYPE_STRING]);
Packit 1470ea
Packit 1470ea
        // This array holds our list of options and their icons
Packit 1470ea
        let options = [{ name: "Select" },
Packit 1470ea
            { name: "New", icon: Gtk.STOCK_NEW },
Packit 1470ea
            { name: "Open", icon: Gtk.STOCK_OPEN },
Packit 1470ea
            { name: "Save", icon: Gtk.STOCK_SAVE }];
Packit 1470ea
Packit 1470ea
        // Put the options in the liststore
Packit 1470ea
        for (let i = 0; i < options.length; i++ ) {
Packit 1470ea
            let option = options[i];
Packit 1470ea
            let iter = this._listStore.append();
Packit 1470ea
            this._listStore.set (iter, [0], [option.name]);
Packit 1470ea
            if ('icon' in option)
Packit 1470ea
                this._listStore.set (iter, [1], [option.icon]);
Packit 1470ea
        }
Packit 1470ea
Packit 1470ea
        // Create the combobox
Packit 1470ea
        this._comboBox = new Gtk.ComboBox({
Packit 1470ea
            model: this._listStore});
Packit 1470ea
Packit 1470ea
        // Create some cellrenderers for the items in each column
Packit 1470ea
        let rendererPixbuf = new Gtk.CellRendererPixbuf();
Packit 1470ea
        let rendererText = new Gtk.CellRendererText();
Packit 1470ea
Packit 1470ea
        // Pack the renderers into the combobox in the order we want to see
Packit 1470ea
        this._comboBox.pack_start (rendererPixbuf, false);
Packit 1470ea
        this._comboBox.pack_start (rendererText, false);
Packit 1470ea
Packit 1470ea
        // Set the renderers to use the information from our liststore
Packit 1470ea
        this._comboBox.add_attribute (rendererText, "text", 0);
Packit 1470ea
        this._comboBox.add_attribute (rendererPixbuf, "stock_id", 1);
Packit 1470ea
Packit 1470ea
        // Set the first row in the combobox to be active on startup
Packit 1470ea
        this._comboBox.set_active (0);
Packit 1470ea
Packit 1470ea
        // Connect the combobox's 'changed' signal to our callback function
Packit 1470ea
        this._comboBox.connect ('changed', this._onComboChanged.bind(this));
Packit 1470ea
Packit 1470ea
        // Add the combobox to the window
Packit 1470ea
        this._window.add (this._comboBox);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    _onComboChanged() {
Packit 1470ea
Packit 1470ea
        // The silly pseudohaiku that we'll use for our messagedialog
Packit 1470ea
        let haiku = ["",
Packit 1470ea
            "You ask for the new\nwith no thought for the aged\nlike fallen leaves trod.",
Packit 1470ea
            "Like a simple clam\nrevealing a lustrous pearl\nit opens for you.",
Packit 1470ea
            "A moment in time\na memory on the breeze\nthese things can't be saved."];
Packit 1470ea
Packit 1470ea
        // Which combobox item is active?
Packit 1470ea
        let activeItem = this._comboBox.get_active();
Packit 1470ea
Packit 1470ea
        // No messagedialog if you choose "Select"
Packit 1470ea
        if (activeItem != 0) {
Packit 1470ea
            this._popUp = new Gtk.MessageDialog ({
Packit 1470ea
                transient_for: this._window,
Packit 1470ea
                modal: true,
Packit 1470ea
                buttons: Gtk.ButtonsType.OK,
Packit 1470ea
                message_type: Gtk.MessageType.INFO,
Packit 1470ea
                text: haiku[activeItem]});
Packit 1470ea
Packit 1470ea
            // Connect the OK button to a handler function
Packit 1470ea
            this._popUp.connect ('response', this._onDialogResponse.bind(this));
Packit 1470ea
Packit 1470ea
            // Show the messagedialog
Packit 1470ea
            this._popUp.show();
Packit 1470ea
        }
Packit 1470ea
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    _onDialogResponse() {
Packit 1470ea
Packit 1470ea
        this._popUp.destroy ();
Packit 1470ea
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
};
Packit 1470ea
Packit 1470ea
// Run the application
Packit 1470ea
let app = new ComboBoxExample ();
Packit 1470ea
app.application.run (ARGV);