Blame platform-demos/C/02_welcome_to_the_grid.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="topic" style="task"
Packit 1470ea
      id="02_welcome_to_the_grid.js">
Packit 1470ea
  <info>
Packit 1470ea
    <link type="guide" xref="beginner.js#tutorials" />
Packit 1470ea
    <link type="seealso" xref="grid.js" />
Packit 1470ea
    <link type="seealso" xref="image.js" />
Packit 1470ea
    <link type="seealso" xref="label.js" />
Packit 1470ea
    <revision version="0.1" date="2012-07-28" 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>Learn how to lay out UI components, like Images and Labels.</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>2. Welcome to the Grid</title>
Packit 1470ea
  <synopsis>
Packit 1470ea
    

This tutorial will show you how to create basic widgets, or parts of the GNOME user interface, like Images and Labels. You'll then learn how to arrange them all in a Grid, which lets you put widgets exactly where you want them.

Packit 1470ea
    <note style="warning">

Have you taken <link xref="hellognome.js">the first tutorial in this series</link> already? You'll want to do so before continuing.

</note>
Packit 1470ea
  </synopsis>
Packit 1470ea
Packit 1470ea
  <links type="section" />
Packit 1470ea
Packit 1470ea
  <section id="native">
Packit 1470ea
    <title>Going native</title>
Packit 1470ea
Packit 1470ea
    

In the last tutorial, we created what was basically a GNOME window frame for a web app. All the GNOME-specific code we needed to learn revolved around putting the WebView -- the widget containing our application -- into an ApplicationWindow, and telling it to display. The application itself was written in HTML and JavaScript, just like most pages on the web.

Packit 1470ea
    

This time, we're going to use only native GNOME widgets. A widget is just a thing, like a checkbox or picture, and GNOME has a wide variety of them to choose from. We call them "native" widgets to distinguish them from things like the button and header in the web app we wrote. Because instead of using web code, these are going to be 100 percent GNOME, using GTK+.

Packit 1470ea
    <note style="tip">

GTK+ stands for "GIMP Toolkit". It's like a toolbox of widgets that you can reach into, while building your applications. It was originally written for <link href="http://www.gimp.org/">the GIMP</link>, which is a free software image editor.

</note>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="setup">
Packit 1470ea
    <title>Setting up our application</title>
Packit 1470ea
Packit 1470ea
    

Before we dig out any widgets from the GTK+ toolbox, we first need to write the basic boilerplate code that our application requires.

Packit 1470ea
    
Packit 1470ea
#!/usr/bin/gjs
Packit 1470ea
Packit 1470ea
imports.gi.versions.Gtk = '3.0';
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
]]>
Packit 1470ea
    

This part always goes at the start of your code. Depending on what you'll be doing with it, you may want to declare more imports here. What we're writing today is pretty basic, so these are all we need; Gtk for the widgets, using the stable '3.0' API.

Packit 1470ea
    

Speaking of which:

Packit 1470ea
    
Packit 1470ea
class WelcomeToTheGrid {
Packit 1470ea
    // Create the application itself
Packit 1470ea
    constructor() {
Packit 1470ea
        this.application = new Gtk.Application();
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
    

This is the start of the application itself, and the _init function which creates it. It tells _buildUI to create an ApplicationWindow, which we're going to call _window, and it tells our window to present itself whenever needed.

Packit 1470ea
    

This part, again, is pretty much copy-and-paste, but you always want to give your application a unique name.

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
            border_width: 10,
Packit 1470ea
            title: "Welcome to the Grid"});
Packit 1470ea
]]>
Packit 1470ea
    

Finally, we start off the _buildUI function by creating a new ApplicationWindow. We specify that it goes with this application, that it should appear in the center of the screen, and that there should be at least 10 pixels between the outside edge and any widgets inside of it. We also give it a title, which will appear at the top of the window.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="toolbox">
Packit 1470ea
    <title>Reaching into the GTK+ toolbox</title>
Packit 1470ea
    

What widgets should we use? Well, let's say we want to write an application that looks like this:

Packit 1470ea
Packit 1470ea
    <media type="image" mime="image/png" src="media/02_jsgrid_01.png"/>
Packit 1470ea
Packit 1470ea
    

We're going to need, at the very least, a picture and a text label to go with it. Let's start with the picture:

Packit 1470ea
    
Packit 1470ea
        // Create an image
Packit 1470ea
        this._image = new Gtk.Image ({ file: "gnome-image.png" });
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

You can download the image file used in this example <link href="https://live.gnome.org/TarynFox?action=AttachFile&do=get&target=gnome-image.png">here</link>. Be sure to put it in the same directory as the code you're writing.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Create a label
Packit 1470ea
        this._label = new Gtk.Label ({ label: "Welcome to GNOME, too!" });
Packit 1470ea
]]>
Packit 1470ea
    

That code adds in the label beneath. You can see how we create widgets, here; each one is a part of Gtk, and we can give it properties that customize how it behaves. In this case, we set the Image's file property to be the filename of the picture we want, and the Label's label property to be the sentence that we want beneath the picture.

Packit 1470ea
    <note style="tip">

Yes, it sounds redundant for a Label to have a label property, but it's not. Other widgets that contain text have a label property, so it's consistent for the Label widget to have one too.

</note>
Packit 1470ea
    

We can't just add these widgets to our window in order, though, the same way HTML elements appear in the order you write them. That's because an ApplicationWindow can only contain one widget.

Packit 1470ea
    

How do we get around that? By making that one widget a container widget, which can hold more than one widget and organize them inside it. Behold: The Grid.

Packit 1470ea
    
Packit 1470ea
        // Create the Grid
Packit 1470ea
        this._grid = new Gtk.Grid ();
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

We're not giving it any properties yet. Those will come later, as we learn how to use the Grid's powers. First, let's attach the Image and Label we made to our Grid.

Packit 1470ea
    
Packit 1470ea
        // Attach the image and label to the grid
Packit 1470ea
        this._grid.attach (this._image, 0, 0, 1, 1);
Packit 1470ea
        this._grid.attach (this._label, 0, 1, 1, 1);
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

This code looks awfully complicated, but it's not. Here's what those numbers mean:

Packit 1470ea
    <list type="numbered">
Packit 1470ea
      <item>

The first number is what left-to-right position to put things in, starting from 0. Any widget that uses a 0 here goes all the way to the left.

</item>
Packit 1470ea
      <item>

The second number is what top-to-bottom position to put a given widget in, starting from 0. The Label goes beneath the Image, so we give the Image a 0 and the Label a 1 here.

</item>
Packit 1470ea
      <item>

The third and fourth numbers are how many columns and rows a widget should take up. We'll see how these work in a minute.

</item>
Packit 1470ea
    </list>
Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Add the grid to the window
Packit 1470ea
        this._window.add (this._grid);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
};
Packit 1470ea
Packit 1470ea
// Run the application
Packit 1470ea
let app = new WelcomeToTheGrid ();
Packit 1470ea
app.application.run (ARGV);
Packit 1470ea
]]>
Packit 1470ea
    

Now that we've created the Grid and attached all our widgets to it, we add it to the window and tell the window to show itself, as the last part of the _buildUI function. As always, to finish up we create a new instance of the application's class and tell it to run.

Packit 1470ea
    

Save your application as welcome_to_the_grid.js. Then, to run your application just open a terminal, go to the directory where your application is at, and type

Packit 1470ea
      <screen> <output style="prompt">$ </output>gjs welcome_to_the_grid.js </screen>
Packit 1470ea
Packit 1470ea
    <media type="image" mime="image/png" src="media/02_jsgrid_02.png"/>
Packit 1470ea
Packit 1470ea
    

There we go! But wait. That doesn't look right. Why is the Label crammed up next to the Image like that? That doesn't look as nice, and it makes it harder to read. What can we do about this?

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="tweaking">
Packit 1470ea
    <title>Tweaking the Grid</title>
Packit 1470ea
Packit 1470ea
    

One thing we can do, is we can give the Label a margin_top property when we create it. This works sort of like setting a margin for an HTML element using inline CSS styling.

Packit 1470ea
    
Packit 1470ea
        // Create a label
Packit 1470ea
        this._label = new Gtk.Label ({
Packit 1470ea
            label: "Welcome to GNOME, too!",
Packit 1470ea
            margin_top: 20 });
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

Of course, if we do that then if we replace the Label with something else -- or add in another widget -- then we have to repeat the margin_top on it too. Otherwise we end up with something like this:

Packit 1470ea
    <media type="image" mime="image/png" src="media/02_jsgrid_03.png"/>
Packit 1470ea
Packit 1470ea
    

We could give the Image a margin_bottom property, but that won't work when the new Label is in a separate column. So how about we try this instead:

Packit 1470ea
    
Packit 1470ea
        // Create the Grid
Packit 1470ea
        this._grid = new Gtk.Grid ({
Packit 1470ea
            row_spacing: 20 });
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

That makes it so that there are always 20 pixels of space in between each horizontal row.

Packit 1470ea
    <note style="tip">

Yes, you can also set the column_spacing property on a Grid, or the margin_left and margin_right properties on any widget. Try them out, if you like!

</note>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="adding">
Packit 1470ea
    <title>Adding more widgets</title>
Packit 1470ea
Packit 1470ea
    

If we did want to add a second Label, how would we do it so that it actually looked like it belonged there? One way is to center the Image on top, so that it's above both Labels instead of just the one on the left. That's where those other numbers in the Grid's attach method come in:

Packit 1470ea
    
Packit 1470ea
        // Create a second label
Packit 1470ea
        this._labelTwo = new Gtk.Label ({
Packit 1470ea
            label: "The cake is a pie." });
Packit 1470ea
Packit 1470ea
        // Attach the image and labels to the grid
Packit 1470ea
        this._grid.attach (this._image, 0, 0, 2, 1);
Packit 1470ea
        this._grid.attach (this._label, 0, 1, 1, 1);
Packit 1470ea
        this._grid.attach (this._labelTwo, 1, 1, 1, 1);
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

After we create the second Label, we attach it to the Grid to the right of the first Label. Remember, the first two numbers count columns and rows from left to right and top to bottom, starting with 0. So if the first Label is in column 0 and row 1, we can put the second in column 1 and row 1 to put it to the right of the first Label.

Packit 1470ea
    

Note the number 2 in the attach statement for the Image. That's what does the trick here. That number is how many columns the Image spans, remember? So when we put it together, we get something like this:

Packit 1470ea
    <media type="image" mime="image/png" src="media/02_jsgrid_04.png"/>
Packit 1470ea
Packit 1470ea
    

There are two things you should take note of, here.

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

Setting the Image to span two columns doesn't stretch the picture itself horizontally. Instead, it stretches the invisible box taken up by the Image widget to fill both columns, then places the Image in the center of that box.

</item>
Packit 1470ea
      <item>

Even though we've set the Grid's row_spacing and the ApplicationWindow's border_width properties, we haven't yet set anything that puts a border in between the two Labels. They were separate earlier when the Image was in only one column, but now that it spans both GNOME doesn't see a reason to keep them apart.

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

There are at least three ways we can get around that last one. First, we can set a margin_left or margin_right on one of the Labels:

Packit 1470ea
    <media type="image" mime="image/png" src="media/02_jsgrid_05.png"/>
Packit 1470ea
Packit 1470ea
    

Second, we can set the Grid's column_homogeneous property to true.

Packit 1470ea
    
Packit 1470ea
        // Create the Grid
Packit 1470ea
        this._grid = new Gtk.Grid ({
Packit 1470ea
            column_homogeneous: true,
Packit 1470ea
            row_spacing: 20 });
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

That makes it look something like this:

Packit 1470ea
    <media type="image" mime="image/png" src="media/02_jsgrid_06.png"/>
Packit 1470ea
Packit 1470ea
    

And third, we can set the Grid's column_spacing property, the same way we set its row_spacing.

Packit 1470ea
    
Packit 1470ea
        // Create the Grid
Packit 1470ea
        this._grid = new Gtk.Grid ({
Packit 1470ea
            column_spacing: 20,
Packit 1470ea
            row_spacing: 20 });
Packit 1470ea
]]>
Packit 1470ea
    

That makes it look like this:

Packit 1470ea
    <media type="image" mime="image/png" src="media/02_jsgrid_07.png"/>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="stock">
Packit 1470ea
    <title>Using stock images</title>
Packit 1470ea
Packit 1470ea
    

GNOME has a lot of stock images on hand already, that we can use if we don't feel like creating our own or if we want a universally-recognized icon. Here's how we create a stock image, compared to how we create a normal one:

Packit 1470ea
    
Packit 1470ea
        // Create an image
Packit 1470ea
        this._image = new Gtk.Image ({ file: "gnome-image.png" });
Packit 1470ea
Packit 1470ea
        // Create a second image using a stock icon
Packit 1470ea
        this._icon = new Gtk.Image ({ stock: 'gtk-about' });
Packit 1470ea
]]>
Packit 1470ea
    

After that, we attach it to the Grid to the left of the first Label. (We aren't using the second one for this example.)

Packit 1470ea
    
Packit 1470ea
        // Attach the images and label to the grid
Packit 1470ea
        this._grid.attach (this._image, 0, 0, 2, 1);
Packit 1470ea
        this._grid.attach (this._icon,  0, 1, 1, 1);
Packit 1470ea
        this._grid.attach (this._label, 1, 1, 1, 1);
Packit 1470ea
]]>
Packit 1470ea
    

That gives us this, when we run it:

Packit 1470ea
    <media type="image" mime="image/png" src="media/02_jsgrid_08.png"/>
Packit 1470ea
Packit 1470ea
    

That's what the stock "About" icon looks like. You can see a list of all the stock items starting with gtk-about in <link href="https://developer.gnome.org/gtk3/3.4/gtk3-Stock-Items.html#GTK-STOCK-ABOUT:CAPS">GNOME's developer documentation</link>. It was written for C programmers, but you don't need to know C to use it; just look at the part in quotation marks, like "gtk-about", and copy that part to use the icon next to it.

Packit 1470ea
    <note style="tip">

We put single quotes around 'gtk-about' here because, unlike text strings that have double quotes around them, that part will never need to be translated into another language. In fact, if it were translated it'd break the icon, because its name is still "gtk-about" no matter which language you speak.

</note>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
Packit 1470ea
  <section id="next">
Packit 1470ea
    <title>What's next?</title>
Packit 1470ea
    

Before we go on to the next tutorial, let's try something a little different:

Packit 1470ea
    
Packit 1470ea
        // Create a button
Packit 1470ea
        this._button = new Gtk.Button ({
Packit 1470ea
            label: "Welcome to GNOME, too!"});
Packit 1470ea
Packit 1470ea
        // Attach the images and button to the grid
Packit 1470ea
        this._grid.attach (this._image,  0, 0, 2, 1);
Packit 1470ea
        this._grid.attach (this._icon,   0, 1, 1, 1);
Packit 1470ea
        this._grid.attach (this._button, 1, 1, 1, 1);
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

That's right, we turned the Label into a Button just by changing the name! If you run the application and click on it, though, you'll find that it doesn't do anything. How do we make our Button do something? That's what we'll find out, in <link xref="03_getting_the_signal.js">our next tutorial</link>.

Packit 1470ea
    

If you like, feel free to spend some time experimenting with Grids, Labels, and Images, including stock images.

Packit 1470ea
    <note style="tip">

One trick you can use to make more complex layouts is to nest Grids inside of each other. This lets you group together related widgets, and rearrange them easily. Take a look at the <link xref="radiobutton.js">RadioButton</link> code sample if you'd like to see how this is done.

</note>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="complete">
Packit 1470ea
    <title>Complete code sample</title>
Packit 1470ea
<xi:include href="samples/02_welcome_to_the_grid.js" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
</page>