1 #include <gtk/gtk.h>
2 
3 
4 
5 /* Callback function in which reacts to the "response" signal from the user in
6  * the message dialog window.
7  * This function is used to interact with the user in the terminal.
8  */
9 static void
on_response(GtkDialog * dialog,gint response_id,gpointer user_data)10 on_response (GtkDialog *dialog,
11              gint       response_id,
12              gpointer   user_data)
13 {
14   /* If the button clicked gives response OK (response_id being -5) */
15   if (response_id == GTK_RESPONSE_OK)
16      g_print ("*boom*\n");
17 
18   /* If the button clicked gives response CANCEL (response_id being -6) */
19   else if (response_id == GTK_RESPONSE_CANCEL)
20      g_print ("good choice\n");
21 
22   /* If the message dialog is destroyed (for example by pressing escape) */
23   else if (response_id == GTK_RESPONSE_DELETE_EVENT)
24      g_print ("dialog closed or cancelled\n");
25 
26   /* Destroy the dialog after one of the above actions have taken place */
27   gtk_widget_destroy (GTK_WIDGET (dialog));
28 
29 }
30 
31 
32 
33 /* Callback function for the response signal "activate" related to the SimpleAction
34  * message_action.
35  * This function is used to cause the message dialog window to popup.
36  */
37 static void
message_cb(GSimpleAction * simple,GVariant * parameter,gpointer user_data)38 message_cb (GSimpleAction *simple,
39             GVariant      *parameter,
40             gpointer       user_data)
41 {
42    /* the parent variable in this case represents the window */
43    GtkWidget *message_dialog;
44    GtkWindow *parent = user_data;
45 
46    /* Create a new message dialog, and set the parameters as follows:
47     * Dialog Flags - make the constructed dialog modal
48     * (modal windows prevent interaction with other windows in the application)
49     * Message Type - nonfatal warning message
50     * Buttons Type - use the ok and cancel buttons
51     * message_format - text that you want the user to see in the window
52     */
53    message_dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
54                                             GTK_MESSAGE_WARNING,
55                                             GTK_BUTTONS_OK_CANCEL,
56                                             "This action will cause the universe to stop existing.");
57 
58    gtk_widget_show_all (message_dialog);
59 
60    g_signal_connect (GTK_DIALOG (message_dialog), "response",
61                     G_CALLBACK (on_response), NULL);
62 
63 }
64 
65 
66 
67 static void
activate(GtkApplication * app,gpointer user_data)68 activate (GtkApplication *app,
69           gpointer        user_data)
70 {
71   GtkWidget *window;
72   GtkWidget *label;
73 
74   GSimpleAction *message_action;
75 
76   /* Create a window with a title and a default size */
77   window = gtk_application_window_new (app);
78   gtk_window_set_title (GTK_WINDOW (window), "GMenu Example");
79   gtk_window_set_default_size (GTK_WINDOW (window), 400, 200);
80 
81   /* Create a label and add it to the window */
82   label = gtk_label_new ("This application goes boom!");
83   gtk_container_add (GTK_CONTAINER (window), label);
84 
85   /* Create a new simple action, giving it a NULL parameter type. It will
86    * always be NULL for actions invoked from a menu. (e.g clicking on an "ok"
87    * or "cancel" button)
88    */
89   message_action = g_simple_action_new ("message", NULL);
90 
91   /* Connect the "activate" signal to the appropriate callback function */
92   g_signal_connect (message_action, "activate", G_CALLBACK (message_cb),
93                     GTK_WINDOW (window));
94 
95   /* Adds the message_action to the overall action map. An Action map is an
96    * interface that contains a number of named GAction instances
97    * (such as message_action)
98    */
99   g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (message_action));
100 
101   gtk_widget_show_all (window);
102 }
103 
104 
105 
106 /* Callback function for the response signal "activate" from the "quit" action
107  * in the function directly below.
108  */
109 static void
quit_cb(GSimpleAction * simple,GVariant * parameter,gpointer user_data)110 quit_cb (GSimpleAction *simple,
111          GVariant      *parameter,
112          gpointer       user_data)
113 {
114   GApplication *application = user_data;
115 
116   g_application_quit (application);
117 }
118 
119 
120 
121 /* Startup function for the menu we are creating in this sample */
122 static void
startup(GApplication * app,gpointer user_data)123 startup (GApplication *app,
124          gpointer      user_data)
125 {
126   GMenu *menu;
127   GSimpleAction *quit_action;
128 
129   /* Initialize the GMenu, and add a menu item with label "Message" and action
130    * "win.message". Also add another menu item with label "Quit" and action
131    * "app.quit"
132    */
133   menu = g_menu_new ();
134   g_menu_append (menu, "Message", "win.message");
135   g_menu_append (menu, "Quit", "app.quit");
136 
137   /* Create a new simple action for the application. (In this case it is the
138    * "quit" action.
139    */
140   quit_action = g_simple_action_new ("quit", NULL);
141 
142   /* Ensure that the menu we have just created is set for the overall application */
143   gtk_application_set_app_menu (GTK_APPLICATION (app), G_MENU_MODEL (menu));
144 
145   g_signal_connect (quit_action,
146                     "activate",
147                     G_CALLBACK (quit_cb),
148                     app);
149 
150   g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (quit_action));
151 
152 }
153 
154 
155 
156 /* Startup function for the application */
157 int
main(int argc,char ** argv)158 main (int argc, char **argv)
159 {
160   GtkApplication *app;
161   int status;
162 
163   app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
164   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
165   g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
166   status = g_application_run (G_APPLICATION (app), argc, argv);
167   g_object_unref (app);
168 
169   return status;
170 }
171