|
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="hello-world.vala" xml:lang="gl">
|
|
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 |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Fran Dieguez</mal:name>
|
|
Packit |
1470ea |
<mal:email>frandieguez@gnome.org</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012-2013.</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
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>Crear a xanela principal para o aplicativo</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 |
public class MyApplication : Gtk.Application {
|
|
Packit |
1470ea |
protected override void activate () {
|
|
Packit |
1470ea |
var window = new Gtk.ApplicationWindow (this);
|
|
Packit |
1470ea |
var label = new Gtk.Label ("Hello GNOME!");
|
|
Packit |
1470ea |
window.add (label);
|
|
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 |
public int main (string[] args) {
|
|
Packit |
1470ea |
return new MyApplication ().run (args);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
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 |
[Desktop Entry]
|
|
Packit |
1470ea |
Version=1.0
|
|
Packit |
1470ea |
Encoding=UTF-8
|
|
Packit |
1470ea |
Name=Hello World
|
|
Packit |
1470ea |
Comment=Say Hello
|
|
Packit |
1470ea |
Exec=@prefix@/bin/hello-world
|
|
Packit |
1470ea |
Icon=application-default-icon
|
|
Packit |
1470ea |
Terminal=false
|
|
Packit |
1470ea |
Type=Application
|
|
Packit |
1470ea |
StartupNotify=true
|
|
Packit |
1470ea |
Categories=GNOME;GTK;Utility;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Now let's go through some parts of the .desktop.in file.
|
|
Packit |
1470ea |
<terms>
|
|
Packit |
1470ea |
<item><title>Nome</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 |
#!/bin/sh
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
set -e
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
test -n "$srcdir" || srcdir=`dirname "$0"`
|
|
Packit |
1470ea |
test -n "$srcdir" || srcdir=.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
olddir=`pwd`
|
|
Packit |
1470ea |
cd "$srcdir"
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# This will run autoconf, automake, etc. for us
|
|
Packit |
1470ea |
autoreconf --force --install
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
cd "$olddir"
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if test -z "$NOCONFIGURE"; then
|
|
Packit |
1470ea |
"$srcdir"/configure "$@"
|
|
Packit |
1470ea |
fi
|
|
Packit |
1470ea |
|
|
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 |
# The actual runnable program is set to the SCRIPTS primitive.
|
|
Packit |
1470ea |
# # Prefix bin_ tells where to copy this
|
|
Packit |
1470ea |
bin_PROGRAMS = hello-world
|
|
Packit |
1470ea |
hello_world_CFLAGS = $(gtk_CFLAGS)
|
|
Packit |
1470ea |
hello_world_LDADD = $(gtk_LIBS)
|
|
Packit |
1470ea |
hello_world_VALAFLAGS = --pkg gtk+-3.0
|
|
Packit |
1470ea |
hello_world_SOURCES = hello-world.vala
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
desktopdir = $(datadir)/applications
|
|
Packit |
1470ea |
desktop_DATA = \
|
|
Packit |
1470ea |
hello-world.desktop
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="configure"><title>configure.ac</title>
|
|
Packit |
1470ea |
# This file is processed by autoconf to create a configure script
|
|
Packit |
1470ea |
AC_INIT([Hello World], 1.0)
|
|
Packit |
1470ea |
AM_INIT_AUTOMAKE([1.10 no-define foreign dist-xz no-dist-gzip])
|
|
Packit |
1470ea |
AC_PROG_CC
|
|
Packit |
1470ea |
AM_PROG_VALAC([0.16])
|
|
Packit |
1470ea |
PKG_CHECK_MODULES(gtk, gtk+-3.0)
|
|
Packit |
1470ea |
AC_CONFIG_FILES([Makefile hello-world.desktop])
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
AC_OUTPUT
|
|
Packit |
1470ea |
|
|
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 |
To build and install this program:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
./autogen.sh --prefix=/home/your_username/.local
|
|
Packit |
1470ea |
make
|
|
Packit |
1470ea |
make install
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
-------------
|
|
Packit |
1470ea |
Running the first line above creates the following files:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
aclocal.m4
|
|
Packit |
1470ea |
autom4te.cache
|
|
Packit |
1470ea |
config.log
|
|
Packit |
1470ea |
config.status
|
|
Packit |
1470ea |
configure
|
|
Packit |
1470ea |
depcomp
|
|
Packit |
1470ea |
hello-world
|
|
Packit |
1470ea |
hello-world.c
|
|
Packit |
1470ea |
hello-world.desktop
|
|
Packit |
1470ea |
hello_world-hello-world.o
|
|
Packit |
1470ea |
hello_world_vala.stamp
|
|
Packit |
1470ea |
install-sh
|
|
Packit |
1470ea |
missing
|
|
Packit |
1470ea |
Makefile.in
|
|
Packit |
1470ea |
Makefile
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Running "make" links all the appropriate libraries.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Running "make install", installs the application in /home/your_username/.local/bin
|
|
Packit |
1470ea |
and installs the hello-world.desktop file in /home/your_username/.local/share/applications
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
You can now run the application by typing "Hello World" in the Overview.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
----------------
|
|
Packit |
1470ea |
To uninstall, type:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
make uninstall
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
----------------
|
|
Packit |
1470ea |
To create a tarball type:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
make distcheck
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
This will create hello-world-1.0.tar.xz
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|