/[paths]/src/paths.c
ViewVC logotype

Contents of /src/paths.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (show annotations)
Sun Aug 19 03:20:49 2012 UTC (8 years, 1 month ago) by ben
File MIME type: text/plain
File size: 9380 byte(s)
do not use an ui file, take a gtk3 example
1 #include <stdlib.h>
2 #include <gtk/gtk.h>
3 #include "paths.h"
4
5 G_DEFINE_TYPE (Paths, paths, GTK_TYPE_APPLICATION)
6
7 static void
8 activate_toggle (GSimpleAction *action,
9 GVariant *parameter,
10 gpointer user_data)
11 {
12 GVariant *state;
13
14 state = g_action_get_state (G_ACTION (action));
15 g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
16 g_variant_unref (state);
17 }
18
19 static void
20 activate_radio (GSimpleAction *action,
21 GVariant *parameter,
22 gpointer user_data)
23 {
24 g_action_change_state (G_ACTION (action), parameter);
25 }
26
27 static void
28 change_fullscreen_state (GSimpleAction *action,
29 GVariant *state,
30 gpointer user_data)
31 {
32 if (g_variant_get_boolean (state))
33 gtk_window_fullscreen (user_data);
34 else
35 gtk_window_unfullscreen (user_data);
36
37 g_simple_action_set_state (action, state);
38 }
39
40 static void
41 change_justify_state (GSimpleAction *action,
42 GVariant *state,
43 gpointer user_data)
44 {
45 GtkTextView *text = g_object_get_data (user_data, "bloatpad-text");
46 const gchar *str;
47
48 str = g_variant_get_string (state, NULL);
49
50 if (g_str_equal (str, "left"))
51 gtk_text_view_set_justification (text, GTK_JUSTIFY_LEFT);
52 else if (g_str_equal (str, "center"))
53 gtk_text_view_set_justification (text, GTK_JUSTIFY_CENTER);
54 else if (g_str_equal (str, "right"))
55 gtk_text_view_set_justification (text, GTK_JUSTIFY_RIGHT);
56 else
57 /* ignore this attempted change */
58 return;
59
60 g_simple_action_set_state (action, state);
61 }
62
63 static GtkClipboard *
64 get_clipboard (GtkWidget *widget)
65 {
66 return gtk_widget_get_clipboard (widget, gdk_atom_intern_static_string ("CLIPBOARD"));
67 }
68
69 static void
70 window_copy (GSimpleAction *action,
71 GVariant *parameter,
72 gpointer user_data)
73 {
74 GtkWindow *window = GTK_WINDOW (user_data);
75 GtkTextView *text = g_object_get_data ((GObject*)window, "bloatpad-text");
76
77 gtk_text_buffer_copy_clipboard (gtk_text_view_get_buffer (text),
78 get_clipboard ((GtkWidget*) text));
79 }
80
81 static void
82 window_paste (GSimpleAction *action,
83 GVariant *parameter,
84 gpointer user_data)
85 {
86 GtkWindow *window = GTK_WINDOW (user_data);
87 GtkTextView *text = g_object_get_data ((GObject*)window, "bloatpad-text");
88
89 gtk_text_buffer_paste_clipboard (gtk_text_view_get_buffer (text),
90 get_clipboard ((GtkWidget*) text),
91 NULL,
92 TRUE);
93
94 }
95
96 static GActionEntry win_entries[] = {
97 { "copy", window_copy, NULL, NULL, NULL },
98 { "paste", window_paste, NULL, NULL, NULL },
99 { "fullscreen", activate_toggle, NULL, "false", change_fullscreen_state },
100 { "justify", activate_radio, "s", "'left'", change_justify_state }
101 };
102
103 static void
104 new_window (GApplication *app,
105 GFile *file)
106 {
107 GtkWidget *window, *grid, *scrolled, *view;
108 GtkWidget *toolbar;
109 GtkToolItem *button;
110 GtkWidget *sw, *box, *label;
111
112 window = gtk_application_window_new (GTK_APPLICATION (app));
113 gtk_window_set_default_size ((GtkWindow*)window, 640, 480);
114 g_action_map_add_action_entries (G_ACTION_MAP (window), win_entries, G_N_ELEMENTS (win_entries), window);
115 gtk_window_set_title (GTK_WINDOW (window), "Paths");
116
117 grid = gtk_grid_new ();
118 gtk_container_add (GTK_CONTAINER (window), grid);
119
120 toolbar = gtk_toolbar_new ();
121
122 button = gtk_toggle_tool_button_new_from_stock (GTK_STOCK_JUSTIFY_RIGHT);
123 gtk_actionable_set_detailed_action_name (GTK_ACTIONABLE (button), "win.justify::right");
124 gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET (button));
125
126 button = gtk_separator_tool_item_new ();
127 gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (button), FALSE);
128 gtk_tool_item_set_expand (GTK_TOOL_ITEM (button), TRUE);
129 gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET (button));
130
131 button = gtk_tool_item_new ();
132 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
133 gtk_container_add (GTK_CONTAINER (button), box);
134 label = gtk_label_new ("Fullscreen:");
135 gtk_container_add (GTK_CONTAINER (box), label);
136 sw = gtk_switch_new ();
137 gtk_actionable_set_action_name (GTK_ACTIONABLE (sw), "win.fullscreen");
138 gtk_container_add (GTK_CONTAINER (box), sw);
139 gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET (button));
140
141 gtk_grid_attach (GTK_GRID (grid), toolbar, 0, 0, 1, 1);
142
143 scrolled = gtk_scrolled_window_new (NULL, NULL);
144 gtk_widget_set_hexpand (scrolled, TRUE);
145 gtk_widget_set_vexpand (scrolled, TRUE);
146 view = gtk_text_view_new ();
147
148 g_object_set_data ((GObject*)window, "bloatpad-text", view);
149
150 gtk_container_add (GTK_CONTAINER (scrolled), view);
151
152 gtk_grid_attach (GTK_GRID (grid), scrolled, 0, 1, 1, 1);
153
154 if (file != NULL)
155 {
156 gchar *contents;
157 gsize length;
158
159 if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL))
160 {
161 GtkTextBuffer *buffer;
162
163 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
164 gtk_text_buffer_set_text (buffer, contents, length);
165 g_free (contents);
166 }
167 }
168
169 gtk_widget_show_all (GTK_WIDGET (window));
170 }
171
172 static void
173 paths_activate (GApplication *application)
174 {
175 new_window (application, NULL);
176 }
177
178 static void
179 paths_open (GApplication *application,
180 GFile **files,
181 gint n_files,
182 const gchar *hint)
183 {
184 gint i;
185
186 for (i = 0; i < n_files; i++)
187 new_window (application, files[i]);
188 }
189
190 static void
191 paths_finalize (GObject *object)
192 {
193 G_OBJECT_CLASS (paths_parent_class)->finalize (object);
194 }
195
196 static void
197 new_activated (GSimpleAction *action,
198 GVariant *parameter,
199 gpointer user_data)
200 {
201 GApplication *app = user_data;
202
203 g_application_activate (app);
204 }
205
206 static void
207 about_activated (GSimpleAction *action,
208 GVariant *parameter,
209 gpointer user_data)
210 {
211 gtk_show_about_dialog (NULL,
212 "program-name", "Paths",
213 "title", "About Paths",
214 "comments", "A graph-based musical composition tool.",
215 NULL);
216 }
217
218 static void
219 quit_activated (GSimpleAction *action,
220 GVariant *parameter,
221 gpointer user_data)
222 {
223 GApplication *app = user_data;
224
225 g_application_quit (app);
226 }
227
228 static GActionEntry app_entries[] = {
229 { "new", new_activated, NULL, NULL, NULL },
230 { "about", about_activated, NULL, NULL, NULL },
231 { "quit", quit_activated, NULL, NULL, NULL },
232 };
233
234 static void
235 paths_startup (GApplication *application)
236 {
237 GtkBuilder *builder;
238
239 G_APPLICATION_CLASS (paths_parent_class)
240 ->startup (application);
241
242 g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application);
243
244 builder = gtk_builder_new ();
245 gtk_builder_add_from_string (builder,
246 "<interface>"
247 " <menu id='app-menu'>"
248 " <section>"
249 " <item>"
250 " <attribute name='label' translatable='yes'>_New Window</attribute>"
251 " <attribute name='action'>app.new</attribute>"
252 " <attribute name='accel'>&lt;Primary&gt;n</attribute>"
253 " </item>"
254 " </section>"
255 " <section>"
256 " <item>"
257 " <attribute name='label' translatable='yes'>_About Paths</attribute>"
258 " <attribute name='action'>app.about</attribute>"
259 " </item>"
260 " </section>"
261 " <section>"
262 " <item>"
263 " <attribute name='label' translatable='yes'>_Quit</attribute>"
264 " <attribute name='action'>app.quit</attribute>"
265 " <attribute name='accel'>&lt;Primary&gt;q</attribute>"
266 " </item>"
267 " </section>"
268 " </menu>"
269 " <menu id='menubar'>"
270 " <submenu>"
271 " <attribute name='label' translatable='yes'>_Edit</attribute>"
272 " <section>"
273 " <item>"
274 " <attribute name='label' translatable='yes'>_Copy</attribute>"
275 " <attribute name='action'>win.copy</attribute>"
276 " <attribute name='accel'>&lt;Primary&gt;c</attribute>"
277 " </item>"
278 " <item>"
279 " <attribute name='label' translatable='yes'>_Parse</attribute>"
280 " <attribute name='action'>win.parse</attribute>"
281 " <attribute name='accel'>&lt;Primary&gt;v</attribute>"
282 " </item>"
283 " </section>"
284 " </submenu>"
285 " <submenu>"
286 " <attribute name='label' translatable='yes'>_View</attribute>"
287 " <section>"
288 " <item>"
289 " <attribute name='label' translatable='yes'>_Fullscreen</attribute>"
290 " <attribute name='action'>win.fullscreen</attribute>"
291 " <attribute name='accel'>F11</attribute>"
292 " </item>"
293 " </section>"
294 " </submenu>"
295 " </menu>"
296 "</interface>", -1, NULL);
297 gtk_application_set_app_menu (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu")));
298 gtk_application_set_menubar (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "menubar")));
299 g_object_unref (builder);
300 }
301
302 static void
303 paths_init (Paths *app)
304 {
305 }
306
307 static void
308 paths_class_init (PathsClass *class)
309 {
310 GApplicationClass *application_class = G_APPLICATION_CLASS (class);
311 GObjectClass *object_class = G_OBJECT_CLASS (class);
312
313 application_class->startup = paths_startup;
314 application_class->activate = paths_activate;
315 application_class->open = paths_open;
316
317 object_class->finalize = paths_finalize;
318
319 }
320
321 Paths *
322 paths_new (void)
323 {
324 GtkApplication *paths;
325
326 g_type_init ();
327
328 g_set_application_name ("Paths");
329
330 paths = g_object_new (paths_get_type (),
331 "application-id", "com.servebeer.herewe.paths",
332 "flags", G_APPLICATION_HANDLES_OPEN,
333 "inactivity-timeout", 30000,
334 "register-session", TRUE,
335 NULL);
336
337 return paths;
338 }
339

  ViewVC Help
Powered by ViewVC 1.1.26