1 #include <gtk/gtk.h>
2 
3 /* Callback function in which reacts to the "response" signal from the user in
4  * the message dialog window.
5  * This function is used to destroy the dialog window.
6  */
7 static void
on_close(GtkDialog * dialog,gint response_id,gpointer user_data)8 on_close (GtkDialog *dialog,
9           gint       response_id,
10           gpointer   user_data)
11 {
12   /* This will cause the dialog to be destroyed */
13   gtk_widget_destroy (GTK_WIDGET (dialog));
14 }
15 
16 /* Callback function for the response signal "activate" related to the SimpleAction
17  * "about_action".
18  * This function is used to cause the about dialog window to popup.
19  */
20 static void
about_cb(GSimpleAction * simple,GVariant * parameter,gpointer user_data)21 about_cb (GSimpleAction *simple,
22           GVariant      *parameter,
23           gpointer       user_data)
24 {
25   GtkWidget *about_dialog;
26 
27   about_dialog = gtk_about_dialog_new ();
28 
29   /* Lists of authors/ documenters to be used later, they must be initialized
30    * in a null terminated array of strings.
31    */
32   const gchar *authors[] = {"GNOME Documentation Team", NULL};
33   const gchar *documenters[] = {"GNOME Documentation Team", NULL};
34 
35   /* We fill in the information for the about dialog */
36   gtk_about_dialog_set_program_name (GTK_ABOUT_DIALOG (about_dialog), "AboutDialog Example");
37   gtk_about_dialog_set_copyright (GTK_ABOUT_DIALOG (about_dialog), "Copyright \xc2\xa9 2012 GNOME Documentation Team");
38   gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG (about_dialog), authors);
39   gtk_about_dialog_set_documenters (GTK_ABOUT_DIALOG (about_dialog), documenters);
40   gtk_about_dialog_set_website_label (GTK_ABOUT_DIALOG (about_dialog), "GNOME Developer Website");
41   gtk_about_dialog_set_website (GTK_ABOUT_DIALOG (about_dialog), "http://developer.gnome.org");
42 
43   /* We do not wish to show the title, which in this case would be
44    * "AboutDialog Example". We have to reset the title of the messagedialog
45    * window after setting the program name.
46    */
47   gtk_window_set_title (GTK_WINDOW (about_dialog), "");
48 
49   /* To close the aboutdialog when "close" is clicked we connect the response
50    * signal to on_close
51    */
52   g_signal_connect (GTK_DIALOG (about_dialog), "response",
53                     G_CALLBACK (on_close), NULL);
54 
55   /* Show the about dialog */
56   gtk_widget_show (about_dialog);
57 }
58 
59 static void
activate(GtkApplication * app,gpointer user_data)60 activate (GtkApplication *app,
61           gpointer        user_data)
62 {
63   GtkWidget *window;
64 
65   GSimpleAction *about_action;
66 
67   /* Create a window with a title and a default size */
68   window = gtk_application_window_new (app);
69   gtk_window_set_title (GTK_WINDOW (window), "AboutDialog Example");
70   gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
71 
72   /* Create a new simple action, giving it a NULL parameter type. It will
73    * always be NULL for actions invoked from a menu. (e.g clicking on an "ok"
74    * or "cancel" button)
75    */
76   about_action = g_simple_action_new ("about", NULL);
77 
78   /* Connect the "activate" signal to the appropriate callback function.
79    * It will indicate that the action was just activated.
80    */
81   g_signal_connect (about_action, "activate", G_CALLBACK (about_cb),
82                     GTK_WINDOW (window));
83 
84   /* Adds the about_action to the overall action map. An Action map is an
85    * interface that contains a number of named GAction instances
86    * (such as about_action)
87    */
88   g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (about_action));
89 
90   gtk_widget_show_all (window);
91 }
92 
93 /* Callback function for the response signal "activate" from the "quit" action
94  * found in the function directly below.
95  */
96 static void
quit_cb(GSimpleAction * simple,GVariant * parameter,gpointer user_data)97 quit_cb (GSimpleAction *simple,
98          GVariant      *parameter,
99          gpointer       user_data)
100 {
101   GApplication *application = user_data;
102 
103   g_application_quit (application);
104 }
105 
106 /* Startup function for the menu we are creating in this sample */
107 static void
startup(GApplication * app,gpointer user_data)108 startup (GApplication *app,
109          gpointer      user_data)
110 {
111   GMenu *menu;
112   GSimpleAction *quit_action;
113 
114   /* Initialize the GMenu, and add a menu item with label "About" and action
115    * "win.about". Also add another menu item with label "Quit" and action
116    * "app.quit"
117    */
118   menu = g_menu_new ();
119   g_menu_append (menu, "About", "win.about");
120   g_menu_append (menu, "Quit", "app.quit");
121 
122   /* Create a new simple action for the application. (In this case it is the
123    * "quit" action.
124    */
125   quit_action = g_simple_action_new ("quit", NULL);
126 
127   /* Ensure that the menu we have just created is set for the overall application */
128   gtk_application_set_app_menu (GTK_APPLICATION (app), G_MENU_MODEL (menu));
129 
130   g_signal_connect (quit_action,
131                     "activate",
132                     G_CALLBACK (quit_cb),
133                     app);
134 
135   g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit_action));
136 }
137 
138 /* Startup function for the application */
139 int
main(int argc,char ** argv)140 main (int argc, char **argv)
141 {
142   GtkApplication *app;
143   int status;
144 
145   app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
146   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
147   g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
148   status = g_application_run (G_APPLICATION (app), argc, argv);
149   g_object_unref (app);
150 
151   return status;
152 }
153