Blame platform-demos/ca/checkbutton.js.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="checkbutton.js" xml:lang="ca">
Packit 1470ea
  <info>
Packit 1470ea
  <title type="text">CheckButton (JavaScript)</title>
Packit 1470ea
    <link type="guide" xref="beginner.js#buttons"/>
Packit 1470ea
    <revision version="0.1" date="2012-06-12" 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 box which can be checked or unchecked</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>CheckButton</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/checkbutton.png"/>
Packit 1470ea
  

This application has a CheckButton. Whether the box is checked dictates whether the window's title bar shows anything.

Packit 1470ea
  

A CheckButton sends the "toggled" signal when it's checked or unchecked. While it's checked, the "active" property is true. While it's not, "active" tests as false.

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
imports.gi.versions.Gtk = '3.0';
Packit 1470ea
Packit 1470ea
const Gio = imports.gi.Gio;
Packit 1470ea
const Gtk = imports.gi.Gtk;
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
class CheckButtonExample {
Packit 1470ea
    // Create the application itself
Packit 1470ea
    constructor() {
Packit 1470ea
        this.application = new Gtk.Application({
Packit 1470ea
            application_id: 'org.example.jscheckbutton',
Packit 1470ea
            flags: Gio.ApplicationFlags.FLAGS_NONE
Packit 1470ea
        });
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 window 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
    

All the code for this sample goes in the CheckButtonExample 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
    
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
            default_height: 100,
Packit 1470ea
            default_width: 300,
Packit 1470ea
            border_width: 10,
Packit 1470ea
            title: "CheckButton Example"});
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 href="GtkApplicationWindow.js.page">Gtk.ApplicationWindow</link> to put all our widgets into.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="button">
Packit 1470ea
    <title>Creating the checkbutton</title>
Packit 1470ea
    
Packit 1470ea
        // Create the check button
Packit 1470ea
        this._button = new Gtk.CheckButton ({label: "Show Title"});
Packit 1470ea
        this._window.add (this._button);
Packit 1470ea
Packit 1470ea
        // Have the check button be checked by default
Packit 1470ea
        this._button.set_active (true);
Packit 1470ea
Packit 1470ea
        // Connect the button to a function that does something when it's toggled
Packit 1470ea
        this._button.connect ("toggled", this._toggledCB.bind(this));
Packit 1470ea
]]>
Packit 1470ea
    

This code creates the checkbutton itself. The label next to the checkbutton is created by giving the checkbutton the "label" property and assigning a string value to it. Since this checkbutton toggles whether the window title is on or off, and the window title will be on to start with, we want the box to be checked by default. Whenever the user checks or unchecks the box, we call the _toggledCB function.

Packit 1470ea
    
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    }
Packit 1470ea
]]>
Packit 1470ea
    

This code finishes up creating the UI, by telling the window to show itself and all child widgets (which is just the checkbutton in this case).

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="function">
Packit 1470ea
    <title>Function which handles the checkbutton's toggling</title>
Packit 1470ea
    
Packit 1470ea
    _toggledCB() {
Packit 1470ea
Packit 1470ea
        // Make the window title appear or disappear when the checkbox is toggled
Packit 1470ea
        if (this._button.get_active() == true)
Packit 1470ea
            this._window.set_title ("CheckButton Example");
Packit 1470ea
        else
Packit 1470ea
            this._window.set_title ("");
Packit 1470ea
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
};
Packit 1470ea
]]>
Packit 1470ea
    

If the checkbutton is toggled from on to off, we want the window title to disappear. If it's toggled from off to on, we want it to reappear. We can tell which way it was toggled by testing to see whether it's active (checked) or not afterwards. A simple if / else statement which calls the checkbutton's get_active() method will work for this.

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

Finally, we create a new instance of the finished CheckButtonExample 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
#!/usr/bin/gjs
Packit 1470ea
Packit 1470ea
imports.gi.versions.Gtk = '3.0';
Packit 1470ea
Packit 1470ea
const Gio = imports.gi.Gio;
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
Packit 1470ea
class CheckButtonExample {
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    constructor() {
Packit 1470ea
        this.application = new Gtk.Application({
Packit 1470ea
            application_id: 'org.example.jscheckbutton',
Packit 1470ea
            flags: Gio.ApplicationFlags.FLAGS_NONE
Packit 1470ea
        });
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 window 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
            default_height: 100,
Packit 1470ea
            default_width: 300,
Packit 1470ea
            border_width: 10,
Packit 1470ea
            title: "CheckButton Example"});
Packit 1470ea
Packit 1470ea
        // Create the check button
Packit 1470ea
        this._button = new Gtk.CheckButton ({label: "Show Title"});
Packit 1470ea
        this._window.add (this._button);
Packit 1470ea
Packit 1470ea
        // Have the check button be checked by default
Packit 1470ea
        this._button.set_active (true);
Packit 1470ea
Packit 1470ea
        // Connect the button to a function that does something when it's toggled
Packit 1470ea
        this._button.connect ("toggled", this._toggledCB.bind(this));
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    _toggledCB() {
Packit 1470ea
Packit 1470ea
        // Make the window title appear or disappear when the checkbox is toggled
Packit 1470ea
        if (this._button.get_active() == true)
Packit 1470ea
            this._window.set_title ("CheckButton Example");
Packit 1470ea
        else
Packit 1470ea
            this._window.set_title ("");
Packit 1470ea
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
};
Packit 1470ea
Packit 1470ea
// Run the application
Packit 1470ea
let app = new CheckButtonExample ();
Packit 1470ea
app.application.run (ARGV);
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="in-depth">
Packit 1470ea
    <title>In-depth documentation</title>
Packit 1470ea
<list>
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.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.CheckButton.html">Gtk.CheckButton</link>

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