5 activate_toggle (GSimpleAction *action,
11 state = g_action_get_state (G_ACTION (action));
12 g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
13 g_variant_unref (state);
17 activate_radio (GSimpleAction *action,
21 g_action_change_state (G_ACTION (action), parameter);
25 change_fullscreen_state (GSimpleAction *action,
29 if (g_variant_get_boolean (state))
30 gtk_window_fullscreen (user_data);
32 gtk_window_unfullscreen (user_data);
34 g_simple_action_set_state (action, state);
38 change_justify_state (GSimpleAction *action,
42 GtkTextView *text = g_object_get_data (user_data, "bloatpad-text");
45 str = g_variant_get_string (state, NULL);
47 if (g_str_equal (str, "left"))
48 gtk_text_view_set_justification (text, GTK_JUSTIFY_LEFT);
49 else if (g_str_equal (str, "center"))
50 gtk_text_view_set_justification (text, GTK_JUSTIFY_CENTER);
51 else if (g_str_equal (str, "right"))
52 gtk_text_view_set_justification (text, GTK_JUSTIFY_RIGHT);
54 /* ignore this attempted change */
57 g_simple_action_set_state (action, state);
61 get_clipboard (GtkWidget *widget)
63 return gtk_widget_get_clipboard (widget, gdk_atom_intern_static_string ("CLIPBOARD"));
67 window_copy (GSimpleAction *action,
71 GtkWindow *window = GTK_WINDOW (user_data);
72 GtkTextView *text = g_object_get_data ((GObject*)window, "bloatpad-text");
74 gtk_text_buffer_copy_clipboard (gtk_text_view_get_buffer (text),
75 get_clipboard ((GtkWidget*) text));
79 window_paste (GSimpleAction *action,
83 GtkWindow *window = GTK_WINDOW (user_data);
84 GtkTextView *text = g_object_get_data ((GObject*)window, "bloatpad-text");
86 gtk_text_buffer_paste_clipboard (gtk_text_view_get_buffer (text),
87 get_clipboard ((GtkWidget*) text),
93 static GActionEntry win_entries[] = {
94 { "copy", window_copy, NULL, NULL, NULL },
95 { "paste", window_paste, NULL, NULL, NULL },
96 { "fullscreen", activate_toggle, NULL, "false", change_fullscreen_state },
97 { "justify", activate_radio, "s", "'left'", change_justify_state }
101 new_window (GApplication *app,
104 GtkWidget *window, *grid, *scrolled, *view;
107 GtkWidget *sw, *box, *label;
109 window = gtk_application_window_new (GTK_APPLICATION (app));
110 gtk_window_set_default_size ((GtkWindow*)window, 640, 480);
111 g_action_map_add_action_entries (G_ACTION_MAP (window), win_entries, G_N_ELEMENTS (win_entries), window);
112 gtk_window_set_title (GTK_WINDOW (window), "Bloatpad");
114 grid = gtk_grid_new ();
115 gtk_container_add (GTK_CONTAINER (window), grid);
117 toolbar = gtk_toolbar_new ();
118 button = gtk_toggle_tool_button_new_from_stock (GTK_STOCK_JUSTIFY_LEFT);
119 gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button), "win.justify::left");
120 gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET (button));
122 button = gtk_toggle_tool_button_new_from_stock (GTK_STOCK_JUSTIFY_CENTER);
123 gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button), "win.justify::center");
124 gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET (button));
126 button = gtk_toggle_tool_button_new_from_stock (GTK_STOCK_JUSTIFY_RIGHT);
127 gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button), "win.justify::right");
128 gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET (button));
130 button = gtk_separator_tool_item_new ();
131 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (button), FALSE);
132 gtk_tool_item_set_expand (GTK_TOOL_ITEM (button), TRUE);
133 gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET (button));
135 button = gtk_tool_item_new ();
136 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
137 gtk_container_add (GTK_CONTAINER (button), box);
138 label = gtk_label_new ("Fullscreen:");
139 gtk_container_add (GTK_CONTAINER (box), label);
140 sw = gtk_switch_new ();
141 gtk_actionable_set_action_name (GTK_ACTIONABLE (sw), "win.fullscreen");
142 gtk_container_add (GTK_CONTAINER (box), sw);
143 gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET (button));
145 gtk_grid_attach (GTK_GRID (grid), toolbar, 0, 0, 1, 1);
147 scrolled = gtk_scrolled_window_new (NULL, NULL);
148 gtk_widget_set_hexpand (scrolled, TRUE);
149 gtk_widget_set_vexpand (scrolled, TRUE);
150 view = gtk_text_view_new ();
152 g_object_set_data ((GObject*)window, "bloatpad-text", view);
154 gtk_container_add (GTK_CONTAINER (scrolled), view);
156 gtk_grid_attach (GTK_GRID (grid), scrolled, 0, 1, 1, 1);
163 if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL))
165 GtkTextBuffer *buffer;
167 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
168 gtk_text_buffer_set_text (buffer, contents, length);
173 gtk_widget_show_all (GTK_WIDGET (window));
177 bloat_pad_activate (GApplication *application)
179 new_window (application, NULL);
183 bloat_pad_open (GApplication *application,
190 for (i = 0; i < n_files; i++)
191 new_window (application, files[i]);
196 GtkApplication parent_instance;
202 typedef GtkApplicationClass BloatPadClass;
204 G_DEFINE_TYPE (BloatPad, bloat_pad, GTK_TYPE_APPLICATION)
207 bloat_pad_finalize (GObject *object)
209 G_OBJECT_CLASS (bloat_pad_parent_class)->finalize (object);
213 new_activated (GSimpleAction *action,
217 GApplication *app = user_data;
219 g_application_activate (app);
223 about_activated (GSimpleAction *action,
227 gtk_show_about_dialog (NULL,
228 "program-name", "Bloatpad",
229 "title", "About Bloatpad",
230 "comments", "Not much to say, really.",
235 quit_activated (GSimpleAction *action,
239 GApplication *app = user_data;
241 g_application_quit (app);
245 update_time (gpointer user_data)
247 BloatPad *bloatpad = user_data;
251 while (g_menu_model_get_n_items (G_MENU_MODEL (bloatpad->time)))
252 g_menu_remove (bloatpad->time, 0);
254 g_message ("Updating the time menu (which should be open now)...");
256 now = g_date_time_new_now_local ();
257 time = g_date_time_format (now, "%c");
258 g_menu_append (bloatpad->time, time, NULL);
259 g_date_time_unref (now);
262 return G_SOURCE_CONTINUE;
266 time_active_changed (GSimpleAction *action,
270 BloatPad *bloatpad = user_data;
272 if (g_variant_get_boolean (state))
274 if (!bloatpad->timeout)
276 bloatpad->timeout = g_timeout_add (1000, update_time, bloatpad);
277 update_time (bloatpad);
282 if (bloatpad->timeout)
284 g_source_remove (bloatpad->timeout);
285 bloatpad->timeout = 0;
289 g_simple_action_set_state (action, state);
292 static GActionEntry app_entries[] = {
293 { "new", new_activated, NULL, NULL, NULL },
294 { "about", about_activated, NULL, NULL, NULL },
295 { "quit", quit_activated, NULL, NULL, NULL },
296 { "time-active", NULL, NULL, "false", time_active_changed }
300 bloat_pad_startup (GApplication *application)
302 BloatPad *bloatpad = (BloatPad*) application;
305 G_APPLICATION_CLASS (bloat_pad_parent_class)
306 ->startup (application);
308 g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application);
310 builder = gtk_builder_new ();
311 gtk_builder_add_from_string (builder,
313 " <menu id='app-menu'>"
316 " <attribute name='label' translatable='yes'>_New Window</attribute>"
317 " <attribute name='action'>app.new</attribute>"
318 " <attribute name='accel'><Primary>n</attribute>"
323 " <attribute name='label' translatable='yes'>_About Bloatpad</attribute>"
324 " <attribute name='action'>app.about</attribute>"
329 " <attribute name='label' translatable='yes'>_Quit</attribute>"
330 " <attribute name='action'>app.quit</attribute>"
331 " <attribute name='accel'><Primary>q</attribute>"
335 " <menu id='menubar'>"
337 " <attribute name='label' translatable='yes'>_Edit</attribute>"
340 " <attribute name='label' translatable='yes'>_Copy</attribute>"
341 " <attribute name='action'>win.copy</attribute>"
342 " <attribute name='accel'><Primary>c</attribute>"
345 " <attribute name='label' translatable='yes'>_Paste</attribute>"
346 " <attribute name='action'>win.paste</attribute>"
347 " <attribute name='accel'><Primary>v</attribute>"
352 " <attribute name='label' translatable='yes'>_View</attribute>"
355 " <attribute name='label' translatable='yes'>_Fullscreen</attribute>"
356 " <attribute name='action'>win.fullscreen</attribute>"
357 " <attribute name='accel'>F11</attribute>"
361 " <submenu id='time-menu'>"
362 " <attribute name='label' translatable='yes'>Time</attribute>"
363 " <attribute name='submenu-action'>app.time-active</attribute>"
366 "</interface>", -1, NULL);
367 gtk_application_set_app_menu (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
368 gtk_application_set_menubar (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
369 //gtk_application_set_menubar (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
370 bloatpad->time = G_MENU (gtk_builder_get_object (builder, "time-menu"));
371 g_object_unref (builder);
375 bloat_pad_shutdown (GApplication *application)
377 BloatPad *bloatpad = (BloatPad *) application;
379 if (bloatpad->timeout)
381 g_source_remove (bloatpad->timeout);
382 bloatpad->timeout = 0;
385 G_APPLICATION_CLASS (bloat_pad_parent_class)
386 ->shutdown (application);
390 bloat_pad_init (BloatPad *app)
395 bloat_pad_class_init (BloatPadClass *class)
397 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
398 GObjectClass *object_class = G_OBJECT_CLASS (class);
400 application_class->startup = bloat_pad_startup;
401 application_class->shutdown = bloat_pad_shutdown;
402 application_class->activate = bloat_pad_activate;
403 application_class->open = bloat_pad_open;
405 object_class->finalize = bloat_pad_finalize;
414 g_set_application_name ("Bloatpad");
416 bloat_pad = g_object_new (bloat_pad_get_type (),
417 "application-id", "org.gtk.Test.bloatpad",
418 "flags", G_APPLICATION_HANDLES_OPEN,
419 "inactivity-timeout", 30000,
420 "register-session", TRUE,
427 main (int argc, char **argv)
432 bloat_pad = bloat_pad_new ();
434 gtk_application_add_accelerator (GTK_APPLICATION (bloat_pad),
435 "F11", "win.fullscreen", NULL);
437 status = g_application_run (G_APPLICATION (bloat_pad), argc, argv);
439 g_object_unref (bloat_pad);