1 #include <gtk/gtk.h>
2
3 /* Declare these two Toolbuttons, as they will be used in both the fullscreen
4 * action callback as well as the activate function.
5 */
6 GtkToolItem *fullscreen_button;
7 GtkToolItem *leavefullscreen_button;
8
9
10
11 /* Callback function for the undo action */
12 static void
undo_callback(GSimpleAction * simple,GVariant * parameter,gpointer user_data)13 undo_callback (GSimpleAction *simple,
14 GVariant *parameter,
15 gpointer user_data)
16 {
17 g_print ("You clicked \"Undo\".\n");
18 }
19
20
21
22 /* Callback function for the fullscreen action */
23 static void
fullscreen_callback(GSimpleAction * simple,GVariant * parameter,gpointer user_data)24 fullscreen_callback (GSimpleAction *simple,
25 GVariant *parameter,
26 gpointer user_data)
27 {
28 GdkWindow *window = gtk_widget_get_window (GTK_WIDGET (user_data));
29
30 GdkWindowState current_state = gdk_window_get_state (window);
31
32 /* If the window is currently in fullscreen mode */
33 if ( (current_state & GDK_WINDOW_STATE_FULLSCREEN) != 0)
34 {
35 /* Minimize the window and change to the fullscreen button */
36 gdk_window_unfullscreen (window);
37 gtk_widget_hide (GTK_WIDGET(leavefullscreen_button));
38 gtk_widget_show (GTK_WIDGET(fullscreen_button));
39 }
40 else
41 {
42 /* Maximize the window, and change to the unfullscreen button */
43 gdk_window_fullscreen (window);
44 gtk_widget_hide (GTK_WIDGET (fullscreen_button));
45 gtk_widget_show (GTK_WIDGET (leavefullscreen_button));
46 }
47 }
48
49
50 static void
activate(GtkApplication * app,gpointer user_data)51 activate (GtkApplication *app,
52 gpointer user_data)
53 {
54 /* Initialize variables */
55 GtkWidget *window;
56 GtkWidget *grid;
57 GtkWidget *toolbar;
58
59 GtkToolItem *new_button;
60 GtkToolItem *open_button;
61 GtkToolItem *undo_button;
62
63 GtkStyleContext *style_context;
64
65 GSimpleAction *undo_action;
66 GSimpleAction *fullscreen_action;
67 GSimpleAction *leavefullscreen_action;
68
69 /* Create a window with a title and a default size */
70 window = gtk_application_window_new (app);
71 gtk_window_set_title (GTK_WINDOW (window), "Toolbar Example");
72 gtk_window_set_default_size (GTK_WINDOW (window), 400, 200);
73
74 /* Here we begin to create the toolbar */
75 toolbar = gtk_toolbar_new ();
76 /* Set the toolbar to be the primary toolbar of the application */
77 style_context = gtk_widget_get_style_context (toolbar);
78 gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_PRIMARY_TOOLBAR);
79
80 /* Create a button for the "new" action, with a stock image */
81 new_button = gtk_tool_button_new_from_stock (GTK_STOCK_NEW);
82 /* Show the "new" button's label */
83 gtk_tool_item_set_is_important (new_button, TRUE);
84 /* Insert the button in the desired position within the toolbar */
85 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), new_button, 0);
86 /* Show the button */
87 gtk_widget_show (GTK_WIDGET (new_button));
88 /* Set the action name for the "new" action. We use "app.new" to
89 * indicate that the action controls the application.
90 */
91 gtk_actionable_set_action_name (GTK_ACTIONABLE (new_button), "app.new");
92
93 /* Repeat the same steps for the "open" action */
94 open_button = gtk_tool_button_new_from_stock (GTK_STOCK_OPEN);
95 gtk_tool_item_set_is_important (open_button, TRUE);
96 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), open_button, 1);
97 gtk_widget_show (GTK_WIDGET (open_button));
98 gtk_actionable_set_action_name (GTK_ACTIONABLE (open_button), "app.open");
99
100 /* Repeat the same steps for the "undo" action */
101 undo_button = gtk_tool_button_new_from_stock (GTK_STOCK_UNDO);
102 gtk_tool_item_set_is_important (undo_button, TRUE);
103 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), undo_button, 2);
104 gtk_widget_show (GTK_WIDGET (undo_button));
105 /* In this case, we use "win.undo" to indicate that
106 * the action controls only the window
107 */
108 gtk_actionable_set_action_name (GTK_ACTIONABLE (undo_button), "win.undo");
109
110 /* Repeat the same steps for the "fullscreen" action */
111 fullscreen_button = gtk_tool_button_new_from_stock (GTK_STOCK_FULLSCREEN);
112 gtk_tool_item_set_is_important (fullscreen_button, TRUE);
113 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), fullscreen_button, 3);
114 gtk_widget_show (GTK_WIDGET (fullscreen_button));
115 gtk_actionable_set_action_name (GTK_ACTIONABLE (fullscreen_button),
116 "win.fullscreen");
117
118 /*Repeat the same steps for the "leavefullscreen" action */
119 leavefullscreen_button = gtk_tool_button_new_from_stock (GTK_STOCK_LEAVE_FULLSCREEN);
120 gtk_tool_item_set_is_important (leavefullscreen_button, TRUE);
121 gtk_toolbar_insert (GTK_TOOLBAR (toolbar), leavefullscreen_button, 3);
122 /* The only difference here is that we don't show the leavefullscreen button,
123 * as it will later replace the fullscreen button.
124 */
125 gtk_actionable_set_action_name (GTK_ACTIONABLE (leavefullscreen_button),
126 "win.leavefullscreen");
127
128 /* Once we've created the bare-bones of the toolbar, we make
129 * sure it has enough horizontal space.
130 */
131 gtk_widget_set_hexpand (toolbar, TRUE);
132 gtk_widget_show (toolbar);
133
134 /* Attach the toolbar to the grid and add it to the overall window */
135 grid = gtk_grid_new ();
136 gtk_grid_attach (GTK_GRID (grid), toolbar, 0, 0, 1, 1);
137 gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (grid));
138 gtk_widget_show (GTK_WIDGET (grid));
139
140 /* Use the action names to create the actions that control the window, and
141 * connect them to the appropriate callbackfunctions.
142 */
143 undo_action = g_simple_action_new ("undo", NULL);
144 g_signal_connect (undo_action, "activate", G_CALLBACK (undo_callback),
145 GTK_WINDOW (window));
146 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (undo_action));
147
148 fullscreen_action = g_simple_action_new ("fullscreen", NULL);
149 g_signal_connect (fullscreen_action, "activate", G_CALLBACK (fullscreen_callback),
150 GTK_WINDOW (window));
151 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (fullscreen_action));
152
153 leavefullscreen_action = g_simple_action_new ("leavefullscreen", NULL);
154 g_signal_connect (leavefullscreen_action, "activate", G_CALLBACK (fullscreen_callback),
155 GTK_WINDOW (window));
156 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (leavefullscreen_action));
157
158 gtk_widget_show (window);
159 }
160
161
162
163 /* Callback function for the new action */
164 static void
new_callback(GSimpleAction * simple,GVariant * parameter,gpointer user_data)165 new_callback (GSimpleAction *simple,
166 GVariant *parameter,
167 gpointer user_data)
168 {
169 g_print ("You clicked \"New\".\n");
170 }
171
172
173
174 /* Callback function for the open action */
175 static void
open_callback(GSimpleAction * simple,GVariant * parameter,gpointer user_data)176 open_callback (GSimpleAction *simple,
177 GVariant *parameter,
178 gpointer user_data)
179 {
180 g_print ("You clicked \"Open\".\n");
181 }
182
183
184
185 /* In this function, we create the actions in which control the window, and
186 * connect their signals to the appropriate callback function.
187 */
188 static void
startup(GApplication * app,gpointer user_data)189 startup (GApplication *app,
190 gpointer user_data)
191 {
192 GSimpleAction *new_action;
193 GSimpleAction *open_action;
194
195 new_action = g_simple_action_new ("new", NULL);
196 g_signal_connect (new_action, "activate", G_CALLBACK (new_callback), app);
197 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (new_action));
198
199 open_action = g_simple_action_new ("open", NULL);
200 g_signal_connect (open_action, "activate", G_CALLBACK (open_callback), app);
201 g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (open_action));
202 }
203
204
205
206 /* Startup function for the application */
207 int
main(int argc,char ** argv)208 main (int argc, char **argv)
209 {
210 GtkApplication *app;
211 int status;
212
213 app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
214 g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
215 g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
216 status = g_application_run (G_APPLICATION (app), argc, argv);
217 g_object_unref (app);
218
219 return status;
220 }
221