1 /* Example for use of GNU gettext.
2 This file is in the public domain.
3
4 Source code of the C program. */
5
6
7 /* Get GTK declarations. */
8 #include <gtk/gtk.h>
9 #include <glib/gi18n.h>
10
11 /* Get getpid() declaration. */
12 #if HAVE_UNISTD_H
13 # include <unistd.h>
14 #endif
15
16 #define UI_PATH "/org/gnu/gettext/examples/hello/hello.ui"
17 #define APPLICATION_ID "org.gnu.gettext.examples.hello"
18 #define GSETTINGS_SCHEMA "org.gnu.gettext.examples.hello"
19
20 /* Forward declaration of GObject types. */
21
22 #define HELLO_TYPE_APPLICATION_WINDOW (hello_application_window_get_type ())
23 #define HELLO_APPLICATION_WINDOW(obj) \
24 (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
25 HELLO_TYPE_APPLICATION_WINDOW, \
26 HelloApplicationWindow))
27
28 typedef struct _HelloApplicationWindow HelloApplicationWindow;
29 typedef struct _HelloApplicationWindowClass HelloApplicationWindowClass;
30
31 #define HELLO_TYPE_APPLICATION (hello_application_get_type ())
32 #define HELLO_APPLICATION(obj) \
33 (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
34 HELLO_TYPE_APPLICATION, \
35 HelloApplication))
36
37 typedef struct _HelloApplication HelloApplication;
38 typedef struct _HelloApplicationClass HelloApplicationClass;
39
40 /* Custom application window implementation. */
41
42 struct _HelloApplicationWindow
43 {
44 GtkApplicationWindow parent;
45 GtkWidget *label;
46 GtkWidget *button;
47 GSettings *settings;
48 gsize label_id;
49 gchar *labels[3];
50 };
51
52 struct _HelloApplicationWindowClass
53 {
54 GtkApplicationWindowClass parent_class;
55 };
56
57 G_DEFINE_TYPE (HelloApplicationWindow, hello_application_window,
58 GTK_TYPE_APPLICATION_WINDOW);
59
60 static void
update_content(HelloApplicationWindow * window)61 update_content (HelloApplicationWindow *window)
62 {
63 gtk_label_set_label (GTK_LABEL (window->label),
64 window->labels[window->label_id]);
65 window->label_id = (window->label_id + 1) % G_N_ELEMENTS (window->labels);
66 }
67
68 static void
hello_application_window_init(HelloApplicationWindow * window)69 hello_application_window_init (HelloApplicationWindow *window)
70 {
71 gtk_widget_init_template (GTK_WIDGET (window));
72
73 window->settings = g_settings_new (GSETTINGS_SCHEMA);
74 g_settings_bind (window->settings, "use-markup",
75 window->label, "use-markup",
76 G_SETTINGS_BIND_DEFAULT);
77
78 window->labels[0]
79 = g_strdup_printf (_("<big>Hello world!</big>\n"
80 "This program is running as "
81 "process number <b>%d</b>."),
82 getpid ());
83 window->labels[1]
84 = g_strdup (_("<big><u>This is another text</u></big>"));
85 window->labels[2]
86 = g_strdup (_("<big><i>This is yet another text</i></big>"));
87
88 update_content (window);
89 }
90
91 static void
hello_application_window_dispose(GObject * object)92 hello_application_window_dispose (GObject *object)
93 {
94 HelloApplicationWindow *window = HELLO_APPLICATION_WINDOW (object);
95 g_clear_object (&window->settings);
96 }
97
98 static void
hello_application_window_class_init(HelloApplicationWindowClass * klass)99 hello_application_window_class_init (HelloApplicationWindowClass *klass)
100 {
101 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
102
103 gobject_class->dispose = hello_application_window_dispose;
104
105 gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass),
106 UI_PATH);
107 gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (klass),
108 HelloApplicationWindow, label);
109 gtk_widget_class_bind_template_child (GTK_WIDGET_CLASS (klass),
110 HelloApplicationWindow, button);
111 }
112
113 static HelloApplicationWindow *
hello_application_window_new(HelloApplication * application)114 hello_application_window_new (HelloApplication *application)
115 {
116 return g_object_new (HELLO_TYPE_APPLICATION_WINDOW,
117 "application", application,
118 NULL);
119 }
120
121 /* Custom application implementation. */
122
123 struct _HelloApplication
124 {
125 GtkApplication parent;
126 };
127
128 struct _HelloApplicationClass
129 {
130 GtkApplicationClass parent_class;
131 };
132
133 G_DEFINE_TYPE (HelloApplication, hello_application, GTK_TYPE_APPLICATION);
134
135 static void
hello_application_init(HelloApplication * application)136 hello_application_init (HelloApplication *application)
137 {
138 }
139
140 static void
clicked_callback(GtkWidget * widget,void * data)141 clicked_callback (GtkWidget *widget, void *data)
142 {
143 update_content (HELLO_APPLICATION_WINDOW (data));
144 }
145
146 static void
hello_application_activate(GApplication * application)147 hello_application_activate (GApplication *application)
148 {
149 HelloApplicationWindow *window;
150
151 window = hello_application_window_new (HELLO_APPLICATION (application));
152 g_signal_connect (window->button, "clicked",
153 G_CALLBACK (clicked_callback), window);
154 gtk_window_present (GTK_WINDOW (window));
155 }
156
157 static void
hello_application_class_init(HelloApplicationClass * klass)158 hello_application_class_init (HelloApplicationClass *klass)
159 {
160 G_APPLICATION_CLASS (klass)->activate = hello_application_activate;
161 }
162
163 static HelloApplication *
hello_application_new(void)164 hello_application_new (void)
165 {
166 return g_object_new (HELLO_TYPE_APPLICATION,
167 "application-id", APPLICATION_ID,
168 NULL);
169 }
170
171 int
main(int argc,char * argv[])172 main (int argc, char *argv[])
173 {
174 GApplication *application;
175 int status;
176
177 /* Load the GSettings schema from the current directory. */
178 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", FALSE);
179
180 /* Initializations. */
181 textdomain ("hello-c-gnome3");
182 bindtextdomain ("hello-c-gnome3", LOCALEDIR);
183
184 /* Create application. */
185 application = G_APPLICATION (hello_application_new ());
186
187 /* Start the application. */
188 status = g_application_run (application, argc, argv);
189 g_object_unref (application);
190
191 return status;
192 }
193