1 /* Example for use of GNU gettext.
2 This file is in the public domain.
4 Source code of the C program. */
7 /* Get GTK declarations. */
9 #include <glib/gi18n.h>
11 /* Get getpid() declaration. */
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"
20 /* Forward declaration of GObject types. */
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))
28 typedef struct _HelloApplicationWindow HelloApplicationWindow;
29 typedef struct _HelloApplicationWindowClass HelloApplicationWindowClass;
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, \
37 typedef struct _HelloApplication HelloApplication;
38 typedef struct _HelloApplicationClass HelloApplicationClass;
40 /* Custom application window implementation. */
42 struct _HelloApplicationWindow
44 GtkApplicationWindow parent;
52 struct _HelloApplicationWindowClass
54 GtkApplicationWindowClass parent_class;
57 G_DEFINE_TYPE (HelloApplicationWindow, hello_application_window,
58 GTK_TYPE_APPLICATION_WINDOW);
61 update_content (HelloApplicationWindow *window)
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);
69 hello_application_window_init (HelloApplicationWindow *window)
71 gtk_widget_init_template (GTK_WIDGET (window));
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);
79 = g_strdup_printf (_("<big>Hello world!</big>\n"
80 "This program is running as "
81 "process number <b>%d</b>."),
84 = g_strdup (_("<big><u>This is another text</u></big>"));
86 = g_strdup (_("<big><i>This is yet another text</i></big>"));
88 update_content (window);
92 hello_application_window_dispose (GObject *object)
94 HelloApplicationWindow *window = HELLO_APPLICATION_WINDOW (object);
95 g_clear_object (&window->settings);
99 hello_application_window_class_init (HelloApplicationWindowClass *klass)
101 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
103 gobject_class->dispose = hello_application_window_dispose;
105 gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass),
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);
113 static HelloApplicationWindow *
114 hello_application_window_new (HelloApplication *application)
116 return g_object_new (HELLO_TYPE_APPLICATION_WINDOW,
117 "application", application,
121 /* Custom application implementation. */
123 struct _HelloApplication
125 GtkApplication parent;
128 struct _HelloApplicationClass
130 GtkApplicationClass parent_class;
133 G_DEFINE_TYPE (HelloApplication, hello_application, GTK_TYPE_APPLICATION);
136 hello_application_init (HelloApplication *application)
141 clicked_callback (GtkWidget *widget, void *data)
143 update_content (HELLO_APPLICATION_WINDOW (data));
147 hello_application_activate (GApplication *application)
149 HelloApplicationWindow *window;
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));
158 hello_application_class_init (HelloApplicationClass *klass)
160 G_APPLICATION_CLASS (klass)->activate = hello_application_activate;
163 static HelloApplication *
164 hello_application_new (void)
166 return g_object_new (HELLO_TYPE_APPLICATION,
167 "application-id", APPLICATION_ID,
172 main (int argc, char *argv[])
174 GApplication *application;
177 /* Load the GSettings schema from the current directory. */
178 g_setenv ("GSETTINGS_SCHEMA_DIR", ".", FALSE);
180 /* Initializations. */
181 textdomain ("hello-c-gnome3");
182 bindtextdomain ("hello-c-gnome3", LOCALEDIR);
184 /* Create application. */
185 application = G_APPLICATION (hello_application_new ());
187 /* Start the application. */
188 status = g_application_run (application, argc, argv);
189 g_object_unref (application);