]> Pileus Git - ~andy/gtk/blob - examples/bloatpad.c
74eac3174f538ddc05dcb4060a4da8e70cc426e3
[~andy/gtk] / examples / bloatpad.c
1 #include <stdlib.h>
2 #include <gtk/gtk.h>
3
4 static void
5 show_about (GSimpleAction *action,
6             GVariant      *parameter,
7             gpointer       user_data)
8 {
9   GtkWindow *window = user_data;
10
11   gtk_show_about_dialog (window,
12                          "program-name", "Bloatpad",
13                          "title", "About Bloatpad",
14                          "comments", "Not much to say, really.",
15                          NULL);
16 }
17
18 static void
19 activate_toggle (GSimpleAction *action,
20                  GVariant      *parameter,
21                  gpointer       user_data)
22 {
23   GVariant *state;
24
25   state = g_action_get_state (G_ACTION (action));
26   g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
27   g_object_unref (state);
28 }
29
30 static void
31 change_fullscreen_state (GSimpleAction *action,
32                          GVariant      *state,
33                          gpointer       user_data)
34 {
35   if (g_variant_get_boolean (state))
36     gtk_window_fullscreen (user_data);
37   else
38     gtk_window_unfullscreen (user_data);
39
40   g_simple_action_set_state (action, state);
41 }
42
43 static GActionEntry win_entries[] = {
44   { "about", show_about },
45   { "fullscreen", activate_toggle, NULL, "false", change_fullscreen_state }
46 };
47
48 static void
49 new_window (GApplication *app,
50             GFile        *file)
51 {
52   GtkWidget *window, *button, *grid, *scrolled, *view;
53   GtkWidget *menu;
54
55   window = gtk_application_window_new (GTK_APPLICATION (app));
56   g_action_map_add_action_entries (G_ACTION_MAP (window), win_entries, G_N_ELEMENTS (win_entries), window);
57   gtk_window_set_title (GTK_WINDOW (window), "Bloatpad");
58
59   grid = gtk_grid_new ();
60   gtk_container_add (GTK_CONTAINER (window), grid);
61
62   button = gtk_application_menu_button_new ();
63   gtk_button_set_image (GTK_BUTTON (button), gtk_image_new_from_icon_name ("help-about", GTK_ICON_SIZE_MENU));
64   gtk_widget_set_halign (button, GTK_ALIGN_START);
65   gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
66
67   scrolled = gtk_scrolled_window_new (NULL, NULL);
68   gtk_widget_set_hexpand (scrolled, TRUE);
69   gtk_widget_set_vexpand (scrolled, TRUE);
70   view = gtk_text_view_new ();
71
72   gtk_container_add (GTK_CONTAINER (scrolled), view);
73
74   gtk_grid_attach (GTK_GRID (grid), scrolled, 0, 1, 1, 1);
75
76   if (file != NULL)
77     {
78       gchar *contents;
79       gsize length;
80
81       if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL))
82         {
83           GtkTextBuffer *buffer;
84
85           buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
86           gtk_text_buffer_set_text (buffer, contents, length);
87           g_free (contents);
88         }
89     }
90
91   gtk_widget_show_all (GTK_WIDGET (window));
92 }
93
94 static void
95 bloat_pad_activate (GApplication *application)
96 {
97   new_window (application, NULL);
98 }
99
100 static void
101 bloat_pad_open (GApplication  *application,
102                 GFile        **files,
103                 gint           n_files,
104                 const gchar   *hint)
105 {
106   gint i;
107
108   for (i = 0; i < n_files; i++)
109     new_window (application, files[i]);
110 }
111
112 typedef GtkApplication BloatPad;
113 typedef GtkApplicationClass BloatPadClass;
114
115 G_DEFINE_TYPE (BloatPad, bloat_pad, GTK_TYPE_APPLICATION)
116
117 static void
118 bloat_pad_finalize (GObject *object)
119 {
120   G_OBJECT_CLASS (bloat_pad_parent_class)->finalize (object);
121 }
122
123 static void
124 show_help (GSimpleAction *action,
125            GVariant      *parameter,
126            gpointer       user_data)
127 {
128   g_print ("Want help, eh ?!\n");
129 }
130
131 static void
132 quit_app (GSimpleAction *action,
133           GVariant      *parameter,
134           gpointer       user_data)
135 {
136   GList *list, *next;
137   GtkWindow *win;
138
139   g_print ("Going down...\n");
140
141   list = gtk_application_get_windows (GTK_APPLICATION (g_application_get_default ()));
142   while (list)
143     {
144       win = list->data;
145       next = list->next;
146
147       gtk_widget_destroy (GTK_WIDGET (win));
148
149       list = next;
150     }
151 }
152
153 static GSimpleActionGroup *actions = NULL;
154 static GMenu *menu = NULL;
155
156 static void
157 remove_action (GSimpleAction *action,
158                GVariant      *parameter,
159                gpointer       user_data)
160 {
161   GAction *add;
162
163   g_menu_remove (menu, g_menu_model_get_n_items (G_MENU_MODEL (menu)) - 1);
164   g_simple_action_set_enabled (action, FALSE);
165   add = g_simple_action_group_lookup (actions, "add");
166   g_simple_action_set_enabled (G_SIMPLE_ACTION (add), TRUE);
167 }
168
169 static void
170 add_action (GSimpleAction *action,
171             GVariant      *parameter,
172             gpointer       user_data)
173 {
174   GAction *remove;
175
176   g_menu_append (menu, "Remove", "app.remove");
177   g_simple_action_set_enabled (action, FALSE);
178   remove = g_simple_action_group_lookup (actions, "remove");
179   g_simple_action_set_enabled (G_SIMPLE_ACTION (remove), TRUE);
180 }
181
182 static GActionEntry app_entries[] = {
183   { "help", show_help, NULL, NULL, NULL },
184   { "quit", quit_app, NULL, NULL, NULL },
185   { "add", add_action, NULL, NULL, NULL },
186   { "remove", remove_action, NULL, NULL, NULL }
187 };
188
189 static GActionGroup *
190 get_actions (void)
191 {
192   actions = g_simple_action_group_new ();
193   g_simple_action_group_add_entries (actions,
194                                      app_entries, G_N_ELEMENTS (app_entries),
195                                      NULL);
196
197   return G_ACTION_GROUP (actions);
198 }
199
200 static GMenuModel *
201 get_menu (void)
202 {
203   menu = g_menu_new ();
204   g_menu_append (menu, "Help", "app.help");
205   g_menu_append (menu, "About Bloatpad", "win.about");
206   g_menu_append (menu, "Fullscreen", "win.fullscreen");
207   g_menu_append (menu, "Quit", "app.quit");
208   g_menu_append (menu, "Add", "app.add");
209
210   return G_MENU_MODEL (menu);
211 }
212
213 static void
214 bloat_pad_init (BloatPad *app)
215 {
216   g_application_set_action_group (G_APPLICATION (app), get_actions ());
217   g_application_set_menu (G_APPLICATION (app), get_menu ());
218 }
219
220 static void
221 bloat_pad_class_init (BloatPadClass *class)
222 {
223   G_OBJECT_CLASS (class)->finalize= bloat_pad_finalize;
224
225   G_APPLICATION_CLASS (class)->activate = bloat_pad_activate;
226   G_APPLICATION_CLASS (class)->open = bloat_pad_open;
227 }
228
229 BloatPad *
230 bloat_pad_new (void)
231 {
232   g_type_init ();
233
234   return g_object_new (bloat_pad_get_type (),
235                        "application-id", "org.gtk.Test.bloatpad",
236                        "flags", G_APPLICATION_HANDLES_OPEN,
237                        NULL);
238 }
239
240 int
241 main (int argc, char **argv)
242 {
243   BloatPad *bloat_pad;
244   int status;
245
246   bloat_pad = bloat_pad_new ();
247   status = g_application_run (G_APPLICATION (bloat_pad), argc, argv);
248   g_object_unref (bloat_pad);
249
250   return status;
251 }