Blame platform-demos/C/hello-world.vala.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="hello-world.vala">
Packit 1470ea
Packit 1470ea
  <info>
Packit 1470ea
  <title type="text">Hello World (Vala)</title>
Packit 1470ea
    <link type="guide" xref="beginner.vala#tutorials" group="#first"/>
Packit 1470ea
Packit 1470ea
    <revision version="0.1" date="2013-06-17" status="review"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Susanna Huhtanen</name>
Packit 1470ea
      <email its:translate="no">ihmis.suski@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
    <credit type="editor">
Packit 1470ea
      <name>Tiffany Antopolski</name>
Packit 1470ea
      <email its:translate="no">tiffany.antopolski@gmail.com</email>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>A basic "hello, world" application</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>How to build, install and create a <file>tar.xz</file> of a Hello World program</title>
Packit 1470ea
    <media type="image" mime="image/png" style="floatend" src="media/hello-world.png"/>
Packit 1470ea
    <synopsis>
Packit 1470ea
      

This tutorial will demonstrate how to:

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

create a small "Hello, World" application using GTK+

</item>
Packit 1470ea
        <item>

make the <file>.desktop</file> file

</item>
Packit 1470ea
        <item>

how to set up the build system

</item>
Packit 1470ea
      </list>
Packit 1470ea
    </synopsis>
Packit 1470ea
Packit 1470ea
  <links type="section" />
Packit 1470ea
Packit 1470ea
  <section id="hello-world"><title>Create the program</title>
Packit 1470ea
Packit 1470ea
    <links type="section" />
Packit 1470ea
Packit 1470ea
    <section id="application"><title>Creating the main window for the application</title>
Packit 1470ea
      
Packit 1470ea
        protected override void activate () {
Packit 1470ea
                var window = new Gtk.ApplicationWindow (this);
Packit 1470ea
                window.set_title ("Welcome to GNOME");
Packit 1470ea
                window.set_default_size (200, 100);
Packit 1470ea
                window.show_all ();
Packit 1470ea
        }
Packit 1470ea
}]]>
Packit 1470ea
Packit 1470ea
    

Gtk.Application initializes GTK+. It also connects the <gui>x</gui> button that's automatically generated along with the window to the "destroy" signal.

Packit 1470ea
    

We can start building our first window. We do this by creating a variable called window and assigning it a new Gtk.ApplicationWindow.

Packit 1470ea
    

We give the window a title using set_title. The title can be any string you want it to be. To be on the safe side, it's best to stick to UTF-8 encoding.

Packit 1470ea
    

Now we have a window which has a title and a working "close" button. Let's add the actual "Hello World" text.

Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
    <section id="label"><title>Label for the window</title>
Packit 1470ea
      
Packit 1470ea
                window.add (label);
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
      

Finally, we create and run the application:

Packit 1470ea
Packit 1470ea
      
Packit 1470ea
        return new MyApplication ().run (args);
Packit 1470ea
}]]>
Packit 1470ea
Packit 1470ea
      

Gtk.ApplicationWindow can only hold one widget at a time. To construct more elaborate programs you need to create a holder widget like Gtk.Grid inside the window, and then add all the other widgets to it.

Packit 1470ea
   </section>
Packit 1470ea
Packit 1470ea
Packit 1470ea
    <section id="vala"><title>hello-world.vala</title>
Packit 1470ea
      

The complete file:

Packit 1470ea
      <xi:include href="samples/hello-in-vala/hello-world.vala" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
    <section id="terminal"><title>Running the application from terminal</title>
Packit 1470ea
      

To run this application, first save it as hello-world.vala. Then open Terminal, go to the folder where your application is stored.

Packit 1470ea
      

Compile the program:

Packit 1470ea
           <screen>valac --pkg gtk+-3.0 <file>hello-world.vala</file></screen>
Packit 1470ea
      

Run the program:

Packit 1470ea
           <screen>./hello-world</screen>
Packit 1470ea
    </section>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="desktop.in"><title>The <file>.desktop.in</file> file</title>
Packit 1470ea
      

Running applications from the Terminal is useful at the beginning of the application making process. To have fully working <link href= "https://developer.gnome.org/integration-guide/stable/mime.html.en">application integration</link> in GNOME 3 requires a desktop launcher. For this you need to create a <file>.desktop</file> file. The <file>.desktop</file> file describes the application name, the used icon and various integration bits. A deeper insight into the <file>.desktop</file> file can be found <link href= "http://developer.gnome.org/desktop-entry-spec/">here</link>. The <file>.desktop.in</file> file will create the <file>.desktop</file>.

Packit 1470ea
Packit 1470ea
    

The example shows you the minimum requirements for a .desktop.in file.

Packit 1470ea
    <xi:include href="samples/hello-in-vala/hello-world.desktop.in" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
Packit 1470ea
    

Now let's go through some parts of the .desktop.in file.

Packit 1470ea
    <terms>
Packit 1470ea
      <item><title>Name</title>

The application name.

</item>
Packit 1470ea
      <item><title>Comment</title>

A short description of the application.

</item>
Packit 1470ea
      <item><title>Exec</title>

Specifies a command to execute when you choose the application from the menu. In this example exec just tells where to find the <file>hello-world</file> file and the file takes care of the rest.

</item>
Packit 1470ea
      <item><title>Terminal</title>

Specifies whether the command in the Exec key runs in a terminal window.

</item>
Packit 1470ea
    </terms>
Packit 1470ea
Packit 1470ea
    

To put your application into the appropriate category, you need to add the necessary categories to the Categories line. More information on the different categories can be found in the <link href = "http://standards.freedesktop.org/menu-spec/latest/apa.html">menu specification</link>.

Packit 1470ea
    

In this example we use an existing icon. For a custom icon you need to have a .svg file of your icon, stored in <file>/usr/share/icons/hicolor/scalable/apps</file>. Write the name of your icon file to the .desktop.in file, on line 7. More information on icons in: <link href="https://live.gnome.org/GnomeGoals/AppIcon">Installing Icons for Themes</link> and <link href="http://freedesktop.org/wiki/Specifications/icon-theme-spec">on freedesktop.org: Specifications/icon-theme-spec</link>.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="autotools"><title>The build system</title>
Packit 1470ea
    

To make your application truly a part of the GNOME 3 system you need to install it with the help of autotools. The autotools build will install all the necessary files to all the right places.

Packit 1470ea
    

For this you need to have the following files:

Packit 1470ea
    <links type="section"/>
Packit 1470ea
Packit 1470ea
      <section id="autogen"><title>autogen.sh</title>
Packit 1470ea
        <xi:include href="samples/hello-in-vala/autogen.sh" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
Packit 1470ea
      

After the <file>autogen.sh</file> file is ready and saved, run:

Packit 1470ea
      <screen><output style="prompt">$ </output><input>chmod +x autogen.sh</input></screen>
Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
Packit 1470ea
    <section id="makefile"><title>Makefile.am</title>
Packit 1470ea
      <xi:include href="samples/hello-in-vala/Makefile.am" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
Packit 1470ea
    <section id="configure"><title>configure.ac</title>
Packit 1470ea
      <xi:include href="samples/hello-in-vala/configure.ac" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
Packit 1470ea
    <section id="readme"><title>README</title>
Packit 1470ea
       

Information users should read first. This file can be blank.

Packit 1470ea
Packit 1470ea
       

When you have the <file>hello-world.c</file>, <file>hello-world.desktop.in</file>, <file>Makefile.am</file>, <file>configure.ac</file> and <file>autogen.sh</file> files with correct information and rights, the <file>README</file> file can include the following instructions:

Packit 1470ea
      <xi:include href="samples/hello-in-vala/README" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
    
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
</page>