File: hello-world.vala.page

package info (click to toggle)
gnome-devel-docs 40.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 79,188 kB
  • sloc: javascript: 2,514; xml: 2,407; ansic: 2,229; python: 1,854; makefile: 805; sh: 499; cpp: 131
file content (254 lines) | stat: -rw-r--r-- 10,525 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?xml version="1.0" encoding="utf-8"?>
<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="sv">

  <info>
  <title type="text">Hej världen (Vala)</title>
    <link type="guide" xref="beginner.vala#tutorials" group="#first"/>

    <revision version="0.1" date="2013-06-17" status="review"/>

    <credit type="author copyright">
      <name>Susanna Huhtanen</name>
      <email its:translate="no">ihmis.suski@gmail.com</email>
      <years>2012</years>
    </credit>
    <credit type="editor">
      <name>Tiffany Antopolski</name>
      <email its:translate="no">tiffany.antopolski@gmail.com</email>
    </credit>

    <desc>Ett grundläggande ”Hej världen”-program</desc>
  
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
      <mal:name>Sebastian Rasmussen</mal:name>
      <mal:email>sebras@gmail.com</mal:email>
      <mal:years>2019</mal:years>
    </mal:credit>
  
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
      <mal:name>Anders Jonsson</mal:name>
      <mal:email>anders.jonsson@norsjovallen.se</mal:email>
      <mal:years>2021</mal:years>
    </mal:credit>
  </info>

  <title>Hur du bygger, installerar och skapar en <file>tar.xz</file> för ett ”Hej världen”-program</title>
    <media type="image" mime="image/png" style="floatend" src="media/hello-world.png"/>
    <synopsis>
      <p>Denna handledning kommer demonstrera hur du:</p>
      <list style="numbered">
        <item><p>skapa ett litet ”Hej världen”-program med GTK+</p></item>
        <item><p>gör <file>.desktop</file>-filen</p></item>
        <item><p>konfigurerar byggsystemet</p></item>
      </list>
    </synopsis>

  <links type="section"/>

  <section id="hello-world"><title>Skapa programmet</title>

    <links type="section"/>

    <section id="application"><title>Creating the main window for the application</title>
      <code mime="text/x-csharp">class MyApplication : Gtk.Application {
        protected override void activate () {
                var window = new Gtk.ApplicationWindow (this);
                window.set_title ("Välkommen till GNOME");
                window.set_default_size (200, 100);
                window.show_all ();
        }
}</code>

    <p>Gtk.Application initierar GTK+. Det ansluter också knappen <gui>x</gui> som genereras automatiskt tillsammans med fönstret till signalen ”destroy”.</p>
    <p>Vi kan börja med att bygga vårt första fönster. Vi gör detta genom att skapa en variabel med namnet <var>window</var> och tilldela den ett nytt Gtk.ApplicationWindow.</p>
    <p>Vi ger fönstret en titel med <code>set_title</code>. Titeln kan vara vilken sträng som du vill. För att vara på den säkra sidan är det bäst att hålla sig till UTF-8-kodning.</p>
    <p>Nu har vi ett fönster som har en titel och en fungerande ”stäng”-knapp. Låt oss lägga till den faktiska texten ”Hej världen”.</p>
    </section>

    <section id="label"><title>Etikett för fönstret</title>
      <code mime="text/x-csharp">var label = new Gtk.Label ("Hej GNOME!");
                window.add (label);
</code>

      <p>Slutligen skapar och kör vi programmet:</p>

      <code mime="text/x-csharp">int main (string[] args) {
        return new MyApplication ().run (args);
}</code>

      <p>Gtk.ApplicationWindow kan endast innehålla en komponent åt gången. För att konstruera mer avancerade program behöver du skapa en behållarkomponent som Gtk.Grid inuti fönstret, och sedan lägga till alla andra komponenter till den.</p>
   </section>


    <section id="vala"><title>hello-world.vala</title>
      <p>Den fullständiga filen:</p>
      <code mime="text/x-csharp" style="numbered">public class MyApplication : Gtk.Application {
	protected override void activate () {
		var window = new Gtk.ApplicationWindow (this);
		var label = new Gtk.Label ("Hej GNOME!");
		window.add (label);
		window.set_title ("Välkommen till GNOME");
		window.set_default_size (200, 100);
		window.show_all ();
	}
}

public int main (string[] args) {
	return new MyApplication ().run (args);
}
</code>
    </section>

    <section id="terminal"><title>Köra programmet från terminalen</title>
      <p>För att köra detta program, spara det först som hello-world.vala. Öppna sedan Terminal och gå till mappen där ditt program lagrats.</p>
      <p>Kompilera programmet:</p>
           <screen>valac --pkg gtk+-3.0 <file>hello-world.vala</file></screen>
      <p>Kör programmet:</p>
           <screen>./<var>hello-world</var></screen>
    </section>
  </section>

  <section id="desktop.in"><title><file>.desktop.in</file>-filen</title>
      <p>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>.</p>

    <p>Exemplet visar dig minimikraven för en <code>.desktop.in</code>-fil.</p>
    <code mime="text/desktop" style="numbered">[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=Hello World
Comment=Säg hej
Exec=@prefix@/bin/hello-world
Icon=application-default-icon
Terminal=false
Type=Application
StartupNotify=true
Categories=GNOME;GTK;Utility;
</code>

    <p>Låt oss nu gå igenom några delar av <code>.desktop.in</code>-filen.</p>
    <terms>
      <item><title>Name</title><p>Programmets namn.</p></item>
      <item><title>Comment</title><p>En kort beskrivning av programmet.</p></item>
      <item><title>Exec</title><p>Anger ett kommando att köra när du väljer programmet från menyn. I detta exempel säger exec bara till var filen <file>hello-world</file> kan hittas, så tar filen hand om resten.</p></item>
      <item><title>Terminal</title><p>Anger huruvida kommandot i Exec-nyckeln körs i ett terminalfönster.</p></item>
    </terms>

    <p>För att lägga ditt program i rätt kategori behöver du lägga till de nödvändiga kategorierna till raden Categories. Mer information om det olika kategorierna kan hittas i <link href="http://standards.freedesktop.org/menu-spec/latest/apa.html">menyspecifikationen</link>.</p>
    <p>I detta exempel använder vi en befintlig ikon. För en anpassad ikon behöver du ha en .svg-fil av din ikon, lagrad i <file>/usr/share/icons/hicolor/scalable/apps</file>. Skriv namnet på din ikonfil i .desktop.in-filen, på rad 7. Mer information om ikoner finns i: <link href="https://wiki.gnome.org/Initiatives/GnomeGoals/AppIcon">Installing Icons for Themes</link> och <link href="http://freedesktop.org/wiki/Specifications/icon-theme-spec">på freedesktop.org: Specifications/icon-theme-spec</link>.</p>
  </section>

  <section id="autotools"><title>Byggsystemet</title>
    <p>För att verkligen göra ditt program till en del av GNOME 3-systemet behöver du installera det med hjälp av autotools. Detta autotools-bygge kommer installera alla nödvändiga filer till rätt plats.</p>
    <p>För detta behöver du ha följande filer:</p>
    <links type="section"/>

      <section id="autogen"><title>autogen.sh</title>
        <code mime="application/x-shellscript" style="numbered">#!/bin/sh

set -e

test -n "$srcdir" || srcdir=`dirname "$0"`
test -n "$srcdir" || srcdir=.

olddir=`pwd`
cd "$srcdir"

# Detta kommer köra autoconf, automake o.s.v. åt oss
autoreconf --force --install

cd "$olddir"

if test -z "$NOCONFIGURE"; then
  "$srcdir"/configure "$@"
fi
</code>

      <p>Efter att filen <file>autogen.sh</file> är klar och sparad, kör:</p>
      <screen><output style="prompt">$ </output><input>chmod +x autogen.sh</input></screen>
    </section>


    <section id="makefile"><title>Makefile.am</title>
      <code mime="application/x-shellscript" style="numbered"># The actual runnable program is set to the SCRIPTS primitive.
# # Prefix bin_ tells where to copy this
bin_PROGRAMS = hello-world
hello_world_CFLAGS = $(gtk_CFLAGS)
hello_world_LDADD = $(gtk_LIBS)
hello_world_VALAFLAGS = --pkg gtk+-3.0
hello_world_SOURCES = hello-world.vala

desktopdir = $(datadir)/applications
desktop_DATA = \
	hello-world.desktop
</code>
    </section>


    <section id="configure"><title>configure.ac</title>
      <code mime="application/x-shellscript" style="numbered"># This file is processed by autoconf to create a configure script
AC_INIT([Hello World], 1.0)
AM_INIT_AUTOMAKE([1.10 no-define foreign dist-xz no-dist-gzip])
AC_PROG_CC
AM_PROG_VALAC([0.16])
PKG_CHECK_MODULES(gtk, gtk+-3.0)
AC_CONFIG_FILES([Makefile hello-world.desktop])

AC_OUTPUT
</code>
    </section>


    <section id="readme"><title>README</title>
       <p>Information som användare bör läsa först. Denna fil kan vara tom.</p>

       <p>När du har filerna <file>hello-world.c</file>, <file>hello-world.desktop.in</file>, <file>Makefile.am</file>, <file>configure.ac</file> och <file>autogen.sh</file> med rätt information och rättigheter, kan filen <file>README</file> innehålla följande instruktioner:</p>
      <code mime="text/readme" style="numbered">To build and install this program:

./autogen.sh --prefix=/home/your_username/.local
make
make install

-------------
Running the first line above creates the following files:

aclocal.m4
autom4te.cache
config.log
config.status
configure
depcomp
hello-world
hello-world.c
hello-world.desktop
hello_world-hello-world.o
hello_world_vala.stamp
install-sh
missing
Makefile.in
Makefile

Running "make" links all the appropriate libraries.

Running "make install", installs the application in /home/your_username/.local/bin
and installs the hello-world.desktop file in /home/your_username/.local/share/applications

You can now run the application by typing "Hello World" in the Overview.

----------------
To uninstall, type:

make uninstall

----------------
To create a tarball type:

make distcheck

This will create hello-world-1.0.tar.xz
</code>
    </section>

    <!-- TODO: How to make a custom icon with autotools -->

  </section>
</page>