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 |
static void |
19 |
change_fullscreen_state (GSimpleAction *action, |
20 |
GVariant *state, |
21 |
gpointer user_data) |
22 |
{ |
23 |
if (g_variant_get_boolean (state)) |
24 |
gtk_window_fullscreen (user_data); |
25 |
else |
26 |
gtk_window_unfullscreen (user_data); |
27 |
|
28 |
g_simple_action_set_state (action, state); |
29 |
} |
30 |
|
31 |
static GtkClipboard * |
32 |
get_clipboard (GtkWidget *widget) |
33 |
{ |
34 |
return gtk_widget_get_clipboard (widget, gdk_atom_intern_static_string ("CLIPBOARD")); |
35 |
} |
36 |
|
37 |
static void |
38 |
window_copy (GSimpleAction *action, |
39 |
GVariant *parameter, |
40 |
gpointer user_data) |
41 |
{ |
42 |
GtkWindow *window = GTK_WINDOW (user_data); |
43 |
} |
44 |
|
45 |
static void |
46 |
window_paste (GSimpleAction *action, |
47 |
GVariant *parameter, |
48 |
gpointer user_data) |
49 |
{ |
50 |
GtkWindow *window = GTK_WINDOW (user_data); |
51 |
} |
52 |
|
53 |
static GActionEntry win_entries[] = { |
54 |
{ "copy", window_copy, NULL, NULL, NULL }, |
55 |
{ "paste", window_paste, NULL, NULL, NULL }, |
56 |
{ "fullscreen", activate_toggle, NULL, "false", change_fullscreen_state } |
57 |
}; |
58 |
|
59 |
static void |
60 |
new_window (GApplication *app, |
61 |
GFile *file) |
62 |
{ |
63 |
GtkWidget *window, *grid, *scrolled; |
64 |
GtkWidget *toolbar; |
65 |
GtkToolItem *button; |
66 |
GtkWidget *sw, *box, *label; |
67 |
|
68 |
window = gtk_application_window_new (GTK_APPLICATION (app)); |
69 |
gtk_window_set_default_size ((GtkWindow*)window, 640, 480); |
70 |
g_action_map_add_action_entries (G_ACTION_MAP (window), win_entries, G_N_ELEMENTS (win_entries), window); |
71 |
gtk_window_set_title (GTK_WINDOW (window), "Paths"); |
72 |
|
73 |
grid = gtk_grid_new (); |
74 |
gtk_container_add (GTK_CONTAINER (window), grid); |
75 |
|
76 |
toolbar = gtk_toolbar_new (); |
77 |
|
78 |
button = gtk_separator_tool_item_new (); |
79 |
gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (button), FALSE); |
80 |
gtk_tool_item_set_expand (GTK_TOOL_ITEM (button), TRUE); |
81 |
gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET (button)); |
82 |
|
83 |
button = gtk_tool_item_new (); |
84 |
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); |
85 |
gtk_container_add (GTK_CONTAINER (button), box); |
86 |
label = gtk_label_new ("Fullscreen:"); |
87 |
gtk_container_add (GTK_CONTAINER (box), label); |
88 |
sw = gtk_switch_new (); |
89 |
gtk_actionable_set_action_name (GTK_ACTIONABLE (sw), "win.fullscreen"); |
90 |
gtk_container_add (GTK_CONTAINER (box), sw); |
91 |
gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET (button)); |
92 |
|
93 |
gtk_grid_attach (GTK_GRID (grid), toolbar, 0, 0, 1, 1); |
94 |
|
95 |
scrolled = gtk_scrolled_window_new (NULL, NULL); |
96 |
gtk_widget_set_hexpand (scrolled, TRUE); |
97 |
gtk_widget_set_vexpand (scrolled, TRUE); |
98 |
|
99 |
gtk_grid_attach (GTK_GRID (grid), scrolled, 0, 1, 1, 1); |
100 |
|
101 |
if (file != NULL) |
102 |
{ |
103 |
gchar *contents; |
104 |
gsize length; |
105 |
|
106 |
if (g_file_load_contents (file, NULL, &contents, &length, NULL, NULL)) |
107 |
{ |
108 |
GtkTextBuffer *buffer; |
109 |
|
110 |
g_free (contents); |
111 |
} |
112 |
} |
113 |
|
114 |
gtk_widget_show_all (GTK_WIDGET (window)); |
115 |
} |
116 |
|
117 |
static void |
118 |
paths_activate (GApplication *application) |
119 |
{ |
120 |
new_window (application, NULL); |
121 |
} |
122 |
|
123 |
static void |
124 |
paths_open (GApplication *application, |
125 |
GFile **files, |
126 |
gint n_files, |
127 |
const gchar *hint) |
128 |
{ |
129 |
gint i; |
130 |
|
131 |
for (i = 0; i < n_files; i++) |
132 |
new_window (application, files[i]); |
133 |
} |
134 |
|
135 |
static void |
136 |
paths_finalize (GObject *object) |
137 |
{ |
138 |
G_OBJECT_CLASS (paths_parent_class)->finalize (object); |
139 |
} |
140 |
|
141 |
static void |
142 |
new_activated (GSimpleAction *action, |
143 |
GVariant *parameter, |
144 |
gpointer user_data) |
145 |
{ |
146 |
GApplication *app = user_data; |
147 |
|
148 |
g_application_activate (app); |
149 |
} |
150 |
|
151 |
static void |
152 |
about_activated (GSimpleAction *action, |
153 |
GVariant *parameter, |
154 |
gpointer user_data) |
155 |
{ |
156 |
gtk_show_about_dialog (NULL, |
157 |
"program-name", "Paths", |
158 |
"title", "About Paths", |
159 |
"comments", "A graph-based musical composition tool.", |
160 |
NULL); |
161 |
} |
162 |
|
163 |
static void |
164 |
quit_activated (GSimpleAction *action, |
165 |
GVariant *parameter, |
166 |
gpointer user_data) |
167 |
{ |
168 |
GApplication *app = user_data; |
169 |
|
170 |
g_application_quit (app); |
171 |
} |
172 |
|
173 |
static GActionEntry app_entries[] = { |
174 |
{ "new", new_activated, NULL, NULL, NULL }, |
175 |
{ "about", about_activated, NULL, NULL, NULL }, |
176 |
{ "quit", quit_activated, NULL, NULL, NULL }, |
177 |
}; |
178 |
|
179 |
static void |
180 |
paths_startup (GApplication *application) |
181 |
{ |
182 |
GtkBuilder *builder; |
183 |
|
184 |
G_APPLICATION_CLASS (paths_parent_class) |
185 |
->startup (application); |
186 |
|
187 |
g_action_map_add_action_entries (G_ACTION_MAP (application), app_entries, G_N_ELEMENTS (app_entries), application); |
188 |
|
189 |
builder = gtk_builder_new (); |
190 |
gtk_builder_add_from_string (builder, |
191 |
"<interface>" |
192 |
" <menu id='app-menu'>" |
193 |
" <section>" |
194 |
" <item>" |
195 |
" <attribute name='label' translatable='yes'>_New Window</attribute>" |
196 |
" <attribute name='action'>app.new</attribute>" |
197 |
" <attribute name='accel'><Primary>n</attribute>" |
198 |
" </item>" |
199 |
" </section>" |
200 |
" <section>" |
201 |
" <item>" |
202 |
" <attribute name='label' translatable='yes'>_About Paths</attribute>" |
203 |
" <attribute name='action'>app.about</attribute>" |
204 |
" </item>" |
205 |
" </section>" |
206 |
" <section>" |
207 |
" <item>" |
208 |
" <attribute name='label' translatable='yes'>_Quit</attribute>" |
209 |
" <attribute name='action'>app.quit</attribute>" |
210 |
" <attribute name='accel'><Primary>q</attribute>" |
211 |
" </item>" |
212 |
" </section>" |
213 |
" </menu>" |
214 |
" <menu id='menubar'>" |
215 |
" <submenu>" |
216 |
" <attribute name='label' translatable='yes'>_Edit</attribute>" |
217 |
" <section>" |
218 |
" <item>" |
219 |
" <attribute name='label' translatable='yes'>_Copy</attribute>" |
220 |
" <attribute name='action'>win.copy</attribute>" |
221 |
" <attribute name='accel'><Primary>c</attribute>" |
222 |
" </item>" |
223 |
" <item>" |
224 |
" <attribute name='label' translatable='yes'>_Parse</attribute>" |
225 |
" <attribute name='action'>win.parse</attribute>" |
226 |
" <attribute name='accel'><Primary>v</attribute>" |
227 |
" </item>" |
228 |
" </section>" |
229 |
" </submenu>" |
230 |
" <submenu>" |
231 |
" <attribute name='label' translatable='yes'>_View</attribute>" |
232 |
" <section>" |
233 |
" <item>" |
234 |
" <attribute name='label' translatable='yes'>_Fullscreen</attribute>" |
235 |
" <attribute name='action'>win.fullscreen</attribute>" |
236 |
" <attribute name='accel'>F11</attribute>" |
237 |
" </item>" |
238 |
" </section>" |
239 |
" </submenu>" |
240 |
" </menu>" |
241 |
"</interface>", -1, NULL); |
242 |
gtk_application_set_app_menu (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"))); |
243 |
gtk_application_set_menubar (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "menubar"))); |
244 |
g_object_unref (builder); |
245 |
} |
246 |
|
247 |
static void |
248 |
paths_init (Paths *app) |
249 |
{ |
250 |
} |
251 |
|
252 |
static void |
253 |
paths_class_init (PathsClass *class) |
254 |
{ |
255 |
GApplicationClass *application_class = G_APPLICATION_CLASS (class); |
256 |
GObjectClass *object_class = G_OBJECT_CLASS (class); |
257 |
|
258 |
application_class->startup = paths_startup; |
259 |
application_class->activate = paths_activate; |
260 |
application_class->open = paths_open; |
261 |
|
262 |
object_class->finalize = paths_finalize; |
263 |
|
264 |
} |
265 |
|
266 |
Paths * |
267 |
paths_new (void) |
268 |
{ |
269 |
GtkApplication *paths; |
270 |
|
271 |
g_type_init (); |
272 |
|
273 |
g_set_application_name ("Paths"); |
274 |
|
275 |
paths = g_object_new (paths_get_type (), |
276 |
"application-id", "com.servebeer.herewe.paths", |
277 |
"flags", G_APPLICATION_HANDLES_OPEN, |
278 |
"inactivity-timeout", 30000, |
279 |
"register-session", TRUE, |
280 |
NULL); |
281 |
|
282 |
return paths; |
283 |
} |
284 |
|