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