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