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