31 #include <string.h> |
31 #include <string.h> |
32 |
32 |
33 #include "../ui/window.h" |
33 #include "../ui/window.h" |
34 #include "../ui/properties.h" |
34 #include "../ui/properties.h" |
35 #include "../common/context.h" |
35 #include "../common/context.h" |
|
36 #include "../common/menu.h" |
|
37 #include "../common/toolbar.h" |
|
38 |
|
39 #include <cx/mempool.h> |
36 |
40 |
37 #include "menu.h" |
41 #include "menu.h" |
38 #include "toolbar.h" |
42 #include "toolbar.h" |
39 #include "container.h" |
43 #include "container.h" |
|
44 #include "headerbar.h" |
|
45 #include "button.h" |
40 |
46 |
41 static int nwindows = 0; |
47 static int nwindows = 0; |
42 |
48 |
43 static int window_default_width = 650; |
49 static int window_default_width = 650; |
44 static int window_default_height = 550; |
50 static int window_default_height = 550; |
45 |
51 |
46 void ui_exit_event(GtkWidget *widget, gpointer data) { |
52 static gboolean ui_window_destroy(void *data) { |
47 UiObject *obj = data; |
53 UiObject *obj = data; |
48 UiEvent ev; |
54 uic_object_destroy(obj); |
49 ev.window = obj->window; |
|
50 ev.document = obj->ctx->document; |
|
51 ev.obj = obj; |
|
52 ev.eventdata = NULL; |
|
53 ev.intval = 0; |
|
54 |
|
55 if(obj->ctx->close_callback) { |
|
56 obj->ctx->close_callback(&ev, obj->ctx->close_data); |
|
57 } |
|
58 // TODO: free UiObject |
|
59 |
55 |
60 nwindows--; |
56 nwindows--; |
61 #ifdef UI_GTK2 |
57 #ifdef UI_GTK2 |
62 if(nwindows == 0) { |
58 if(nwindows == 0) { |
63 gtk_main_quit(); |
59 gtk_main_quit(); |
64 } |
60 } |
65 #endif |
61 #endif |
66 } |
62 |
67 |
63 return FALSE; |
68 static UiObject* create_window(char *title, void *window_data, UiBool simple) { |
64 } |
69 UcxMempool *mp = ucx_mempool_new(256); |
65 |
70 UiObject *obj = ucx_mempool_calloc(mp, 1, sizeof(UiObject)); |
66 void ui_window_widget_destroy(UiObject *obj) { |
71 |
67 #if GTK_MAJOR_VERSION >= 4 |
72 #ifndef UI_GTK2 |
68 gtk_window_destroy(GTK_WINDOW(obj->widget)); |
|
69 #else |
|
70 gtk_widget_destroy(obj->widget); |
|
71 #endif |
|
72 } |
|
73 |
|
74 void ui_exit_event(GtkWidget *widget, gpointer data) { |
|
75 // delay exit handler |
|
76 g_idle_add(ui_window_destroy, data); |
|
77 } |
|
78 |
|
79 static gboolean ui_window_close_request(UiObject *obj) { |
|
80 uic_context_prepare_close(obj->ctx); |
|
81 obj->ref--; |
|
82 if(obj->ref > 0) { |
|
83 #if GTK_CHECK_VERSION(2, 18, 0) |
|
84 gtk_widget_set_visible(obj->widget, FALSE); |
|
85 #else |
|
86 gtk_widget_hide(obj->widget); |
|
87 #endif |
|
88 return TRUE; |
|
89 } else { |
|
90 return FALSE; |
|
91 } |
|
92 } |
|
93 |
|
94 #if GTK_MAJOR_VERSION >= 4 |
|
95 static gboolean close_request(GtkWindow* self, UiObject *obj) { |
|
96 return ui_window_close_request(obj); |
|
97 } |
|
98 #else |
|
99 static gboolean close_request(GtkWidget* self, GdkEvent* event, UiObject *obj) { |
|
100 return ui_window_close_request(obj); |
|
101 } |
|
102 #endif |
|
103 |
|
104 static UiObject* create_window(const char *title, void *window_data, UiBool sidebar, UiBool simple) { |
|
105 CxMempool *mp = cxBasicMempoolCreate(256); |
|
106 UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject)); |
|
107 obj->ref = 0; |
|
108 |
|
109 #ifdef UI_LIBADWAITA |
|
110 obj->widget = adw_application_window_new(ui_get_application()); |
|
111 #elif !defined(UI_GTK2) |
73 obj->widget = gtk_application_window_new(ui_get_application()); |
112 obj->widget = gtk_application_window_new(ui_get_application()); |
74 #else |
113 #else |
75 obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
114 obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
76 #endif |
115 #endif |
77 |
116 |
78 |
117 |
79 obj->ctx = uic_context(obj, mp); |
118 obj->ctx = uic_context(obj, mp); |
80 obj->window = window_data; |
119 obj->window = window_data; |
81 |
120 |
|
121 #if GTK_CHECK_VERSION(4, 0, 0) |
|
122 obj->ctx->action_map = G_ACTION_MAP(obj->widget); |
|
123 #endif |
|
124 |
82 if(title != NULL) { |
125 if(title != NULL) { |
83 gtk_window_set_title(GTK_WINDOW(obj->widget), title); |
126 gtk_window_set_title(GTK_WINDOW(obj->widget), title); |
84 } |
127 } |
85 |
128 |
86 char *width = ui_get_property("ui.window.width"); |
129 const char *width = ui_get_property("ui.window.width"); |
87 char *height = ui_get_property("ui.window.height"); |
130 const char *height = ui_get_property("ui.window.height"); |
88 if(width && height) { |
131 if(width && height) { |
89 gtk_window_set_default_size( |
132 gtk_window_set_default_size( |
90 GTK_WINDOW(obj->widget), |
133 GTK_WINDOW(obj->widget), |
91 atoi(width), |
134 atoi(width), |
92 atoi(height)); |
135 atoi(height)); |
93 } else { |
136 } else { |
94 gtk_window_set_default_size( |
137 gtk_window_set_default_size( |
95 GTK_WINDOW(obj->widget), |
138 GTK_WINDOW(obj->widget), |
96 window_default_width, |
139 window_default_width + sidebar*250, |
97 window_default_height); |
140 window_default_height); |
98 } |
141 } |
99 |
142 |
|
143 obj->destroy = ui_window_widget_destroy; |
100 g_signal_connect( |
144 g_signal_connect( |
101 obj->widget, |
145 obj->widget, |
102 "destroy", |
146 "destroy", |
103 G_CALLBACK(ui_exit_event), |
147 G_CALLBACK(ui_exit_event), |
104 obj); |
148 obj); |
|
149 #if GTK_MAJOR_VERSION >= 4 |
|
150 g_signal_connect( |
|
151 obj->widget, |
|
152 "close-request", |
|
153 G_CALLBACK(close_request), |
|
154 obj); |
|
155 #else |
|
156 g_signal_connect( |
|
157 obj->widget, |
|
158 "delete-event", |
|
159 G_CALLBACK(close_request), |
|
160 obj); |
|
161 #endif |
105 |
162 |
106 GtkWidget *vbox = ui_gtk_vbox_new(0); |
163 GtkWidget *vbox = ui_gtk_vbox_new(0); |
107 gtk_container_add(GTK_CONTAINER(obj->widget), vbox); |
164 #ifdef UI_LIBADWAITA |
108 |
165 GtkWidget *toolbar_view = adw_toolbar_view_new(); |
|
166 adw_toolbar_view_set_content(ADW_TOOLBAR_VIEW(toolbar_view), vbox); |
|
167 |
|
168 GtkWidget *content_box = ui_gtk_vbox_new(0); |
|
169 BOX_ADD_EXPAND(GTK_BOX(vbox), content_box); |
|
170 |
|
171 if(sidebar) { |
|
172 GtkWidget *splitview = adw_overlay_split_view_new(); |
|
173 adw_application_window_set_content(ADW_APPLICATION_WINDOW(obj->widget), splitview); |
|
174 |
|
175 GtkWidget *sidebar_toolbar_view = adw_toolbar_view_new(); |
|
176 adw_overlay_split_view_set_sidebar(ADW_OVERLAY_SPLIT_VIEW(splitview), sidebar_toolbar_view); |
|
177 GtkWidget *sidebar_headerbar = adw_header_bar_new(); |
|
178 adw_toolbar_view_add_top_bar(ADW_TOOLBAR_VIEW(sidebar_toolbar_view), sidebar_headerbar); |
|
179 |
|
180 adw_overlay_split_view_set_content(ADW_OVERLAY_SPLIT_VIEW(splitview), toolbar_view); |
|
181 |
|
182 g_object_set_data(G_OBJECT(obj->widget), "ui_sidebar", sidebar_toolbar_view); |
|
183 } else { |
|
184 adw_application_window_set_content(ADW_APPLICATION_WINDOW(obj->widget), toolbar_view); |
|
185 } |
|
186 |
|
187 |
|
188 GtkWidget *headerbar = adw_header_bar_new(); |
|
189 adw_toolbar_view_add_top_bar(ADW_TOOLBAR_VIEW(toolbar_view), headerbar); |
|
190 g_object_set_data(G_OBJECT(obj->widget), "ui_headerbar", headerbar); |
|
191 |
|
192 if(!simple) { |
|
193 ui_fill_headerbar(obj, headerbar); |
|
194 } |
|
195 #elif GTK_MAJOR_VERSION >= 4 |
|
196 GtkWidget *content_box = ui_gtk_vbox_new(0); |
|
197 WINDOW_SET_CONTENT(obj->widget, vbox); |
|
198 if(sidebar) { |
|
199 GtkWidget *paned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL); |
|
200 GtkWidget *sidebar_vbox = ui_gtk_vbox_new(0); |
|
201 gtk_paned_set_start_child(GTK_PANED(paned), sidebar_vbox); |
|
202 gtk_paned_set_end_child(GTK_PANED(paned), content_box); |
|
203 BOX_ADD_EXPAND(GTK_BOX(vbox), paned); |
|
204 g_object_set_data(G_OBJECT(obj->widget), "ui_sidebar", sidebar_vbox); |
|
205 } else { |
|
206 BOX_ADD_EXPAND(GTK_BOX(vbox), content_box); |
|
207 } |
|
208 #else |
109 if(!simple) { |
209 if(!simple) { |
110 // menu |
210 // menu |
111 GtkWidget *mb = ui_create_menubar(obj); |
211 if(uic_get_menu_list()) { |
112 if(mb) { |
212 GtkWidget *mb = ui_create_menubar(obj); |
113 gtk_box_pack_start(GTK_BOX(vbox), mb, FALSE, FALSE, 0); |
213 if(mb) { |
|
214 gtk_box_pack_start(GTK_BOX(vbox), mb, FALSE, FALSE, 0); |
|
215 } |
114 } |
216 } |
115 |
217 |
116 // toolbar |
218 // toolbar |
117 GtkWidget *tb = ui_create_toolbar(obj); |
219 if(uic_toolbar_isenabled()) { |
118 if(tb) { |
220 GtkWidget *tb = ui_create_toolbar(obj); |
119 gtk_box_pack_start(GTK_BOX(vbox), tb, FALSE, FALSE, 0); |
221 if(tb) { |
120 } |
222 gtk_box_pack_start(GTK_BOX(vbox), tb, FALSE, FALSE, 0); |
121 } |
223 } |
|
224 } |
|
225 |
|
226 //GtkWidget *hb = ui_create_headerbar(obj); |
|
227 //gtk_window_set_titlebar(GTK_WINDOW(obj->widget), hb); |
|
228 } |
|
229 |
|
230 GtkWidget *content_box = ui_gtk_vbox_new(0); |
|
231 WINDOW_SET_CONTENT(obj->widget, vbox); |
|
232 if(sidebar) { |
|
233 GtkWidget *paned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL); |
|
234 GtkWidget *sidebar_vbox = ui_gtk_vbox_new(0); |
|
235 gtk_paned_add1(GTK_PANED(paned), sidebar_vbox); |
|
236 gtk_paned_add2(GTK_PANED(paned), content_box); |
|
237 BOX_ADD_EXPAND(GTK_BOX(vbox), paned); |
|
238 g_object_set_data(G_OBJECT(obj->widget), "ui_sidebar", sidebar_vbox); |
|
239 gtk_paned_set_position (GTK_PANED(paned), 200); |
|
240 } else { |
|
241 BOX_ADD_EXPAND(GTK_BOX(vbox), content_box); |
|
242 } |
|
243 |
|
244 #endif |
122 |
245 |
123 // window content |
246 // window content |
124 // the content has a (TODO: not yet) configurable frame |
247 // the content has a (TODO: not yet) configurable frame |
|
248 // TODO: really? why |
|
249 /* |
125 GtkWidget *frame = gtk_frame_new(NULL); |
250 GtkWidget *frame = gtk_frame_new(NULL); |
126 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE); |
251 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE); |
127 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0); |
252 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0); |
128 |
253 |
129 // content vbox |
254 // content vbox |
130 GtkWidget *content_box = ui_gtk_vbox_new(0); |
255 GtkWidget *content_box = ui_gtk_vbox_new(0); |
131 gtk_container_add(GTK_CONTAINER(frame), content_box); |
256 gtk_container_add(GTK_CONTAINER(frame), content_box); |
132 obj->container = ui_box_container(obj, content_box); |
257 obj->container = ui_box_container(obj, content_box); |
|
258 */ |
|
259 obj->container = ui_box_container(obj, content_box, UI_CONTAINER_VBOX); |
133 |
260 |
134 nwindows++; |
261 nwindows++; |
135 return obj; |
262 return obj; |
136 } |
263 } |
137 |
264 |
138 |
265 |
139 UiObject* ui_window(char *title, void *window_data) { |
266 UiObject* ui_window(const char *title, void *window_data) { |
140 return create_window(title, window_data, FALSE); |
267 return create_window(title, window_data, FALSE, FALSE); |
141 } |
268 } |
142 |
269 |
143 UiObject* ui_simplewindow(char *title, void *window_data) { |
270 UiObject *ui_sidebar_window(const char *title, void *window_data) { |
144 return create_window(title, window_data, TRUE); |
271 return create_window(title, window_data, TRUE, FALSE); |
145 } |
272 } |
146 |
273 |
147 static char* ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action) { |
274 UiObject* ui_simple_window(const char *title, void *window_data) { |
|
275 return create_window(title, window_data, FALSE, TRUE); |
|
276 } |
|
277 |
|
278 void ui_window_size(UiObject *obj, int width, int height) { |
|
279 gtk_window_set_default_size( |
|
280 GTK_WINDOW(obj->widget), |
|
281 width, |
|
282 height); |
|
283 } |
|
284 |
|
285 #ifdef UI_LIBADWAITA |
|
286 |
|
287 static void dialog_response(AdwAlertDialog *self, gchar *response, UiEventData *data) { |
|
288 UiEvent evt; |
|
289 evt.obj = data->obj; |
|
290 evt.document = evt.obj->ctx->document; |
|
291 evt.window = evt.obj->window; |
|
292 evt.eventdata = NULL; |
|
293 evt.intval = 0; |
|
294 |
|
295 if(!strcmp(response, "btn1")) { |
|
296 evt.intval = 1; |
|
297 } else if(!strcmp(response, "btn2")) { |
|
298 evt.intval = 2; |
|
299 } |
|
300 |
|
301 if(data->customdata) { |
|
302 GtkWidget *entry = data->customdata; |
|
303 evt.eventdata = (void*)ENTRY_GET_TEXT(GTK_ENTRY(entry)); |
|
304 } |
|
305 |
|
306 if(data->callback) { |
|
307 data->callback(&evt, data->userdata); |
|
308 } |
|
309 } |
|
310 |
|
311 void ui_dialog_create(UiObject *parent, UiDialogArgs args) { |
|
312 AdwDialog *dialog = adw_alert_dialog_new(args.title, args.content); |
|
313 UiEventData *event = malloc(sizeof(UiEventData)); |
|
314 event->callback = args.result; |
|
315 event->userdata = args.resultdata; |
|
316 event->customdata = NULL; |
|
317 event->value = 0; |
|
318 event->obj = parent; |
|
319 |
|
320 if(args.button1_label) { |
|
321 adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "btn1", args.button1_label); |
|
322 } |
|
323 if(args.button2_label) { |
|
324 adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "btn2", args.button2_label); |
|
325 } |
|
326 if(args.closebutton_label) { |
|
327 adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "close", args.closebutton_label); |
|
328 adw_alert_dialog_set_close_response(ADW_ALERT_DIALOG(dialog), "close"); |
|
329 } |
|
330 |
|
331 GtkWidget *entry = NULL; |
|
332 if(args.input || args.password) { |
|
333 entry = gtk_entry_new(); |
|
334 if(args.password) { |
|
335 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE); |
|
336 } |
|
337 if(args.input_value) { |
|
338 ENTRY_SET_TEXT(entry, args.input_value); |
|
339 } |
|
340 adw_alert_dialog_set_extra_child(ADW_ALERT_DIALOG(dialog), entry); |
|
341 event->customdata = entry; |
|
342 } |
|
343 |
|
344 g_signal_connect( |
|
345 dialog, |
|
346 "destroy", |
|
347 G_CALLBACK(ui_destroy_userdata), |
|
348 event); |
|
349 |
|
350 g_signal_connect(dialog, "response", G_CALLBACK(dialog_response), event); |
|
351 adw_dialog_present(dialog, parent->widget); |
|
352 |
|
353 if(entry) { |
|
354 gtk_entry_grab_focus_without_selecting(GTK_ENTRY(entry)); |
|
355 } |
|
356 } |
|
357 #else |
|
358 |
|
359 static void ui_dialog_response (GtkDialog* self, gint response_id, gpointer user_data) { |
|
360 UiEventData *data = user_data; |
|
361 UiEvent evt; |
|
362 evt.obj = data->obj; |
|
363 evt.document = evt.obj->ctx->document; |
|
364 evt.window = evt.obj->window; |
|
365 evt.eventdata = NULL; |
|
366 evt.intval = 0; |
|
367 |
|
368 if(data->customdata) { |
|
369 GtkWidget *entry = data->customdata; |
|
370 evt.eventdata = (void*)ENTRY_GET_TEXT(GTK_ENTRY(entry)); |
|
371 |
|
372 } |
|
373 |
|
374 if(response_id == 1 || response_id == 2) { |
|
375 evt.intval = response_id; |
|
376 } |
|
377 |
|
378 |
|
379 if(data->callback) { |
|
380 data->callback(&evt, data->userdata); |
|
381 } |
|
382 |
|
383 WINDOW_DESTROY(GTK_WIDGET(self)); |
|
384 } |
|
385 |
|
386 void ui_dialog_create(UiObject *parent, UiDialogArgs args) { |
|
387 GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new()); |
|
388 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent->widget)); |
|
389 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); |
|
390 |
|
391 GtkWidget *dialog_w = GTK_WIDGET(dialog); |
|
392 if(args.title) { |
|
393 gtk_window_set_title(GTK_WINDOW(dialog), args.title); |
|
394 } |
|
395 if(args.button1_label) { |
|
396 gtk_dialog_add_button(dialog, args.button1_label, 1); |
|
397 } |
|
398 if(args.button2_label) { |
|
399 gtk_dialog_add_button(dialog, args.button2_label, 2); |
|
400 } |
|
401 if(args.closebutton_label) { |
|
402 gtk_dialog_add_button(dialog, args.closebutton_label, 0); |
|
403 } |
|
404 |
|
405 GtkWidget *content_area = gtk_dialog_get_content_area(dialog); |
|
406 if(args.content) { |
|
407 GtkWidget *label = gtk_label_new(args.content); |
|
408 BOX_ADD(content_area, label); |
|
409 } |
|
410 |
|
411 GtkWidget *textfield = NULL; |
|
412 if(args.input || args.password) { |
|
413 textfield = gtk_entry_new(); |
|
414 if(args.password) { |
|
415 gtk_entry_set_visibility(GTK_ENTRY(textfield), FALSE); |
|
416 } |
|
417 if(args.input_value) { |
|
418 ENTRY_SET_TEXT(textfield, args.input_value); |
|
419 } |
|
420 BOX_ADD(content_area, textfield); |
|
421 } |
|
422 |
|
423 UiEventData *event = malloc(sizeof(UiEventData)); |
|
424 event->obj = parent; |
|
425 event->callback = args.result; |
|
426 event->userdata = args.resultdata; |
|
427 event->value = 0; |
|
428 event->customdata = textfield; |
|
429 |
|
430 g_signal_connect(dialog_w, |
|
431 "response", |
|
432 G_CALLBACK(ui_dialog_response), |
|
433 event); |
|
434 |
|
435 WINDOW_SHOW(GTK_WIDGET(dialog_w)); |
|
436 } |
|
437 #endif |
|
438 |
|
439 |
|
440 #if GTK_MAJOR_VERSION >= 3 |
|
441 UiFileList listmodel2filelist(GListModel *selection) { |
|
442 UiFileList flist; |
|
443 flist.files = NULL; |
|
444 flist.nfiles = 0; |
|
445 flist.nfiles = g_list_model_get_n_items(selection); |
|
446 flist.files = calloc(flist.nfiles, sizeof(char*)); |
|
447 for(int i=0;i<flist.nfiles;i++) { |
|
448 GFile *file = g_list_model_get_item(selection, i); |
|
449 char *path = g_file_get_path(file); |
|
450 flist.files[i] = path ? strdup(path) : NULL; |
|
451 g_object_unref(file); |
|
452 } |
|
453 return flist; |
|
454 } |
|
455 #endif |
|
456 |
|
457 |
|
458 #if GTK_CHECK_VERSION(4, 10, 0) |
|
459 |
|
460 #define UI_GTK_FILEDIALOG_OPEN 16 |
|
461 #define UI_GTK_FILEDIALOG_SAVE 32 |
|
462 |
|
463 static void filechooser_opened(GObject *source, GAsyncResult *result, void *data) { |
|
464 UiEventData *event = data; |
|
465 |
|
466 GFile *file = NULL; |
|
467 GListModel *selection = NULL; |
|
468 GError *error = NULL; |
|
469 |
|
470 int mode = event->value; |
|
471 int multi = mode & UI_FILEDIALOG_SELECT_MULTI; |
|
472 if((mode & UI_FILEDIALOG_SELECT_FOLDER) == UI_FILEDIALOG_SELECT_FOLDER) { |
|
473 if(multi) { |
|
474 selection = gtk_file_dialog_select_multiple_folders_finish(GTK_FILE_DIALOG(source), result, &error); |
|
475 } else { |
|
476 file = gtk_file_dialog_select_folder_finish(GTK_FILE_DIALOG(source), result, &error); |
|
477 } |
|
478 } else if((mode & UI_GTK_FILEDIALOG_OPEN) == UI_GTK_FILEDIALOG_OPEN) { |
|
479 if(multi) { |
|
480 selection = gtk_file_dialog_open_multiple_finish(GTK_FILE_DIALOG(source), result, &error); |
|
481 } else { |
|
482 file = gtk_file_dialog_open_finish(GTK_FILE_DIALOG(source), result, &error); |
|
483 } |
|
484 } else { |
|
485 file = gtk_file_dialog_save_finish(GTK_FILE_DIALOG(source), result, &error); |
|
486 } |
|
487 |
|
488 UiEvent evt; |
|
489 evt.obj = event->obj; |
|
490 evt.document = evt.obj->ctx->document; |
|
491 evt.window = evt.obj->window; |
|
492 evt.intval = 0; |
|
493 |
|
494 UiFileList flist; |
|
495 flist.files = NULL; |
|
496 flist.nfiles = 0; |
|
497 evt.eventdata = &flist; |
|
498 |
|
499 if(selection) { |
|
500 flist = listmodel2filelist(selection); |
|
501 g_object_unref(selection); |
|
502 } else if(file) { |
|
503 char *path = g_file_get_path(file); |
|
504 if(path) { |
|
505 flist.nfiles = 1; |
|
506 flist.files = calloc(flist.nfiles, sizeof(char*)); |
|
507 flist.files[0] = strdup(path); |
|
508 } |
|
509 g_object_unref(file); |
|
510 } |
|
511 |
|
512 if(event->callback) { |
|
513 event->callback(&evt, event->userdata); |
|
514 } |
|
515 |
|
516 for(int i=0;i<flist.nfiles;i++) { |
|
517 free(flist.files[i]); |
|
518 } |
|
519 } |
|
520 |
|
521 static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, const char *name, ui_callback file_selected_callback, void *cbdata) { |
|
522 if(action == GTK_FILE_CHOOSER_ACTION_OPEN) { |
|
523 mode |= UI_GTK_FILEDIALOG_OPEN; |
|
524 } else { |
|
525 mode |= UI_GTK_FILEDIALOG_SAVE; |
|
526 } |
|
527 |
|
528 UiEventData *event = malloc(sizeof(UiEventData)); |
|
529 event->callback = file_selected_callback; |
|
530 event->userdata = cbdata; |
|
531 event->customdata = NULL; |
|
532 event->value = mode; |
|
533 event->obj = obj; |
|
534 |
|
535 GtkWindow *parent = GTK_WINDOW(gtk_widget_get_root(obj->widget)); |
|
536 GtkFileDialog *dialog = gtk_file_dialog_new(); |
|
537 if(name) { |
|
538 gtk_file_dialog_set_initial_name(dialog, name); |
|
539 } |
|
540 |
|
541 int multi = mode & UI_FILEDIALOG_SELECT_MULTI; |
|
542 if((mode & UI_FILEDIALOG_SELECT_FOLDER) == UI_FILEDIALOG_SELECT_FOLDER) { |
|
543 if(multi) { |
|
544 gtk_file_dialog_select_multiple_folders(dialog, parent, NULL, filechooser_opened, event); |
|
545 } else { |
|
546 gtk_file_dialog_select_folder(dialog, parent, NULL, filechooser_opened, event); |
|
547 } |
|
548 } else if(action == GTK_FILE_CHOOSER_ACTION_OPEN) { |
|
549 if(multi) { |
|
550 gtk_file_dialog_open_multiple(dialog, parent, NULL, filechooser_opened, event); |
|
551 } else { |
|
552 gtk_file_dialog_open(dialog, parent, NULL, filechooser_opened, event); |
|
553 } |
|
554 } else { |
|
555 gtk_file_dialog_save(dialog, parent, NULL, filechooser_opened, event); |
|
556 } |
|
557 |
|
558 g_object_unref(dialog); |
|
559 } |
|
560 #else |
|
561 |
|
562 |
|
563 |
|
564 static void filechooser_response(GtkDialog* self, gint response_id, UiEventData *data) { |
|
565 UiEvent evt; |
|
566 evt.obj = data->obj; |
|
567 evt.document = evt.obj->ctx->document; |
|
568 evt.window = evt.obj->window; |
|
569 evt.intval = 0; |
|
570 |
|
571 UiFileList flist; |
|
572 flist.files = NULL; |
|
573 flist.nfiles = 0; |
|
574 evt.eventdata = &flist; |
|
575 |
|
576 if(response_id == GTK_RESPONSE_ACCEPT) { |
|
577 #if GTK_CHECK_VERSION(4, 0, 0) |
|
578 GListModel *selection = gtk_file_chooser_get_files(GTK_FILE_CHOOSER(self)); |
|
579 flist = flist = listmodel2filelist(selection); |
|
580 g_object_unref(selection); |
|
581 #else |
|
582 GSList *selection = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(self)); |
|
583 flist.nfiles = g_slist_length(selection); |
|
584 flist.files = calloc(flist.nfiles, sizeof(char*)); |
|
585 int i = 0; |
|
586 while(selection) { |
|
587 char *file = selection->data; |
|
588 flist.files[i] = strdup(file); |
|
589 g_free(file); |
|
590 selection = selection->next; |
|
591 i++; |
|
592 } |
|
593 g_slist_free(selection); |
|
594 #endif |
|
595 } |
|
596 |
|
597 |
|
598 if(data->callback) { |
|
599 data->callback(&evt, data->userdata); |
|
600 } |
|
601 |
|
602 for(int i=0;i<flist.nfiles;i++) { |
|
603 free(flist.files[i]); |
|
604 } |
|
605 |
|
606 WINDOW_DESTROY(GTK_WIDGET(self)); |
|
607 } |
|
608 |
|
609 static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, const char *name, ui_callback file_selected_callback, void *cbdata) { |
148 char *button; |
610 char *button; |
149 char *title; |
611 char *title; |
150 |
612 |
151 if(action == GTK_FILE_CHOOSER_ACTION_OPEN) { |
613 GtkWidget *dialog; |
152 button = GTK_STOCK_OPEN; |
614 if((mode & UI_FILEDIALOG_SELECT_FOLDER) == UI_FILEDIALOG_SELECT_FOLDER) { |
153 title = "Datei öffnen..."; |
615 dialog = gtk_file_chooser_dialog_new ( |
154 } else { |
616 "Open Folder", |
155 button = GTK_STOCK_SAVE; |
617 GTK_WINDOW(obj->widget), |
156 title = "Datei speichern..."; |
618 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, |
157 } |
619 "Cancel", |
158 |
620 GTK_RESPONSE_CANCEL, |
159 GtkWidget *dialog = gtk_file_chooser_dialog_new( |
621 "Select Folder", |
160 title, |
622 GTK_RESPONSE_ACCEPT, |
161 GTK_WINDOW(obj->widget), |
623 NULL); |
162 action, |
624 } else if(action == GTK_FILE_CHOOSER_ACTION_OPEN) { |
163 GTK_STOCK_CANCEL, |
625 dialog = gtk_file_chooser_dialog_new ( |
164 GTK_RESPONSE_CANCEL, |
626 "Select Folder", |
165 button, |
627 GTK_WINDOW(obj->widget), |
166 GTK_RESPONSE_ACCEPT, |
628 action, |
167 NULL); |
629 "Cancel", |
168 if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { |
630 GTK_RESPONSE_CANCEL, |
169 char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); |
631 "Open File", |
170 gtk_widget_destroy(dialog); |
632 GTK_RESPONSE_ACCEPT, |
171 char *copy = strdup(file); |
633 NULL); |
172 g_free(file); |
634 } else { |
173 return copy; |
635 dialog = gtk_file_chooser_dialog_new ( |
174 } else { |
636 "Save File", |
175 gtk_widget_destroy(dialog); |
637 GTK_WINDOW(obj->widget), |
176 return NULL; |
638 action, |
177 } |
639 "Cancel", |
178 } |
640 GTK_RESPONSE_CANCEL, |
179 |
641 "Save File", |
180 char* ui_openfiledialog(UiObject *obj) { |
642 GTK_RESPONSE_ACCEPT, |
181 return ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_OPEN); |
643 NULL); |
182 } |
644 } |
183 |
645 |
184 char* ui_savefiledialog(UiObject *obj) { |
646 if((mode & UI_FILEDIALOG_SELECT_MULTI) == UI_FILEDIALOG_SELECT_MULTI) { |
185 return ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_SAVE); |
647 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); |
186 } |
648 } |
187 |
649 |
|
650 UiEventData *event = malloc(sizeof(UiEventData)); |
|
651 event->obj = obj; |
|
652 event->userdata = cbdata; |
|
653 event->callback = file_selected_callback; |
|
654 event->value = 0; |
|
655 event->customdata = NULL; |
|
656 |
|
657 g_signal_connect( |
|
658 dialog, |
|
659 "response", |
|
660 G_CALLBACK(filechooser_response), |
|
661 event); |
|
662 g_signal_connect( |
|
663 dialog, |
|
664 "destroy", |
|
665 G_CALLBACK(ui_destroy_userdata), |
|
666 event); |
|
667 |
|
668 |
|
669 UiEvent evt; |
|
670 evt.obj = obj; |
|
671 evt.document = evt.obj->ctx->document; |
|
672 evt.window = evt.obj->window; |
|
673 evt.intval = 0; |
|
674 |
|
675 UiFileList flist; |
|
676 flist.files = NULL; |
|
677 flist.nfiles = 0; |
|
678 evt.eventdata = &flist; |
|
679 |
|
680 gtk_widget_show(dialog); |
|
681 } |
|
682 #endif |
|
683 |
|
684 void ui_openfiledialog(UiObject *obj, unsigned int mode, ui_callback file_selected_callback, void *cbdata) { |
|
685 ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_OPEN, mode, NULL, file_selected_callback, cbdata); |
|
686 } |
|
687 |
|
688 void ui_savefiledialog(UiObject *obj, const char *name, ui_callback file_selected_callback, void *cbdata) { |
|
689 ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_SAVE, 0, name, file_selected_callback, cbdata); |
|
690 } |
|
691 |
|
692 #if GTK_CHECK_VERSION(4, 10, 0) |
|
693 #define DIALOG_NEW() gtk_window_new() |
|
694 #else |
|
695 #define DIALOG_NEW() gtk_dialog_new() |
|
696 |
|
697 static void ui_dialogwindow_response(GtkDialog* self, gint response_id, gpointer user_data) { |
|
698 UiEventData *event = user_data; |
|
699 // TODO: do we need to check if response_id == GTK_RESPONSE_DELETE_EVENT? |
|
700 if(event->callback) { |
|
701 UiEvent e; |
|
702 e.obj = event->obj; |
|
703 e.window = event->obj->window; |
|
704 e.document = event->obj->ctx->document; |
|
705 e.eventdata = NULL; |
|
706 e.intval = event->value; |
|
707 event->callback(&e, event->userdata); |
|
708 } |
|
709 } |
|
710 |
|
711 #endif |
|
712 |
|
713 #if GTK_CHECK_VERSION(4, 0, 0) |
|
714 #define HEADERBAR_SHOW_CLOSEBUTTON(headerbar, set) gtk_header_bar_set_show_title_buttons(GTK_HEADER_BAR(headerbar), set) |
|
715 #define DEFAULT_BUTTON(window, button) gtk_window_set_default_widget(GTK_WINDOW(window), button) |
|
716 #else |
|
717 #define HEADERBAR_SHOW_CLOSEBUTTON(headerbar, set) gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(headerbar), set) |
|
718 #define DEFAULT_BUTTON(window, button) gtk_widget_set_can_default(button, TRUE); gtk_window_set_default(GTK_WINDOW(window), button) |
|
719 #endif |
|
720 |
|
721 |
|
722 |
|
723 UiObject* ui_dialog_window_create(UiObject *parent, UiDialogWindowArgs args) { |
|
724 GtkWidget *dialog = DIALOG_NEW(); |
|
725 if(args.width > 0 || args.height > 0) { |
|
726 gtk_window_set_default_size( |
|
727 GTK_WINDOW(dialog), |
|
728 args.width, |
|
729 args.height); |
|
730 } |
|
731 |
|
732 |
|
733 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent->widget)); |
|
734 if(args.modal != UI_OFF) { |
|
735 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); |
|
736 } |
|
737 |
|
738 CxMempool *mp = cxBasicMempoolCreate(256); |
|
739 UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject)); |
|
740 obj->ctx = uic_context(obj, mp); |
|
741 obj->widget = dialog; |
|
742 obj->ref = 0; |
|
743 obj->destroy = ui_window_widget_destroy; |
|
744 nwindows++; |
|
745 |
|
746 if(args.title != NULL) { |
|
747 gtk_window_set_title(GTK_WINDOW(dialog), args.title); |
|
748 } |
|
749 |
|
750 #if ! GTK_CHECK_VERSION(4, 10, 0) |
|
751 UiEventData *event = malloc(sizeof(UiEventData)); |
|
752 event->obj = obj; |
|
753 event->userdata = args.onclickdata; |
|
754 event->callback = args.onclick; |
|
755 event->value = 0; |
|
756 event->customdata = NULL; |
|
757 |
|
758 g_signal_connect(dialog, "response", G_CALLBACK(ui_dialogwindow_response), event); |
|
759 g_signal_connect( |
|
760 dialog, |
|
761 "destroy", |
|
762 G_CALLBACK(ui_destroy_userdata), |
|
763 event); |
|
764 #endif |
|
765 |
|
766 g_signal_connect( |
|
767 dialog, |
|
768 "destroy", |
|
769 G_CALLBACK(ui_exit_event), |
|
770 obj); |
|
771 #if GTK_MAJOR_VERSION >= 4 |
|
772 g_signal_connect( |
|
773 obj->widget, |
|
774 "close-request", |
|
775 G_CALLBACK(close_request), |
|
776 obj); |
|
777 #else |
|
778 g_signal_connect( |
|
779 obj->widget, |
|
780 "delete-event", |
|
781 G_CALLBACK(close_request), |
|
782 obj); |
|
783 #endif |
|
784 |
|
785 #if GTK_MAJOR_VERSION < 4 |
|
786 GtkWidget *c = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); |
|
787 gtk_container_remove(GTK_CONTAINER(dialog), c); |
|
788 #endif |
|
789 |
|
790 GtkWidget *content_vbox = ui_gtk_vbox_new(0); |
|
791 obj->container = ui_box_container(obj, content_vbox, UI_CONTAINER_VBOX); |
|
792 if(args.lbutton1 || args.lbutton2 || args.rbutton3 || args.rbutton4) { |
|
793 #if GTK_CHECK_VERSION(3, 10, 0) |
|
794 if(args.titlebar_buttons != UI_OFF) { |
|
795 GtkWidget *headerbar = gtk_header_bar_new(); |
|
796 gtk_window_set_titlebar(GTK_WINDOW(dialog), headerbar); |
|
797 if(args.show_closebutton == UI_OFF) { |
|
798 HEADERBAR_SHOW_CLOSEBUTTON(headerbar, FALSE); |
|
799 } |
|
800 |
|
801 if(args.lbutton1) { |
|
802 GtkWidget *button = ui_create_button(obj, args.lbutton1, NULL, args.onclick, args.onclickdata, 1, args.default_button == 1); |
|
803 gtk_header_bar_pack_start(GTK_HEADER_BAR(headerbar), button); |
|
804 if(args.default_button == 1) { |
|
805 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); |
|
806 DEFAULT_BUTTON(dialog, button); |
|
807 } |
|
808 } |
|
809 if(args.lbutton2) { |
|
810 GtkWidget *button = ui_create_button(obj, args.lbutton2, NULL, args.onclick, args.onclickdata, 2, args.default_button == 2); |
|
811 gtk_header_bar_pack_start(GTK_HEADER_BAR(headerbar), button); |
|
812 if(args.default_button == 2) { |
|
813 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); |
|
814 DEFAULT_BUTTON(dialog, button); |
|
815 } |
|
816 } |
|
817 |
|
818 if(args.rbutton4) { |
|
819 GtkWidget *button = ui_create_button(obj, args.rbutton4, NULL, args.onclick, args.onclickdata, 4, args.default_button == 4); |
|
820 gtk_header_bar_pack_end(GTK_HEADER_BAR(headerbar), button); |
|
821 if(args.default_button == 4) { |
|
822 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); |
|
823 DEFAULT_BUTTON(dialog, button); |
|
824 } |
|
825 } |
|
826 if(args.rbutton3) { |
|
827 GtkWidget *button = ui_create_button(obj, args.rbutton3, NULL, args.onclick, args.onclickdata, 3, args.default_button == 3); |
|
828 gtk_header_bar_pack_end(GTK_HEADER_BAR(headerbar), button); |
|
829 if(args.default_button == 3) { |
|
830 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); |
|
831 DEFAULT_BUTTON(dialog, button); |
|
832 } |
|
833 } |
|
834 WINDOW_SET_CONTENT(obj->widget, content_vbox); |
|
835 return obj; |
|
836 } |
|
837 #endif |
|
838 GtkWidget *vbox = ui_gtk_vbox_new(0); |
|
839 WINDOW_SET_CONTENT(obj->widget, vbox); |
|
840 |
|
841 GtkWidget *separator = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL); |
|
842 |
|
843 GtkWidget *grid = ui_create_grid_widget(10, 10); |
|
844 GtkWidget *widget = ui_box_set_margin(grid, 16); |
|
845 gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE); |
|
846 |
|
847 if(args.lbutton1) { |
|
848 GtkWidget *button = ui_create_button(obj, args.lbutton1, NULL, args.onclick, args.onclickdata, 1, args.default_button == 1); |
|
849 gtk_grid_attach(GTK_GRID(grid), button, 0, 0, 1, 1); |
|
850 if(args.default_button == 1) { |
|
851 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); |
|
852 DEFAULT_BUTTON(dialog, button); |
|
853 } |
|
854 } |
|
855 if(args.lbutton2) { |
|
856 GtkWidget *button = ui_create_button(obj, args.lbutton2, NULL, args.onclick, args.onclickdata, 2, args.default_button == 2); |
|
857 gtk_grid_attach(GTK_GRID(grid), button, 1, 0, 1, 1); |
|
858 if(args.default_button == 2) { |
|
859 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); |
|
860 DEFAULT_BUTTON(dialog, button); |
|
861 } |
|
862 } |
|
863 GtkWidget *space = gtk_label_new(NULL); |
|
864 gtk_widget_set_hexpand(space, TRUE); |
|
865 gtk_grid_attach(GTK_GRID(grid), space, 2, 0, 1, 1); |
|
866 if(args.rbutton3) { |
|
867 GtkWidget *button = ui_create_button(obj, args.rbutton3, NULL, args.onclick, args.onclickdata, 3, args.default_button == 3); |
|
868 gtk_grid_attach(GTK_GRID(grid), button, 3, 0, 1, 1); |
|
869 if(args.default_button == 3) { |
|
870 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); |
|
871 DEFAULT_BUTTON(dialog, button); |
|
872 } |
|
873 } |
|
874 if(args.rbutton4) { |
|
875 GtkWidget *button = ui_create_button(obj, args.rbutton4, NULL, args.onclick, args.onclickdata, 4, args.default_button == 4); |
|
876 gtk_grid_attach(GTK_GRID(grid), button, 4, 0, 1, 1); |
|
877 if(args.default_button == 4) { |
|
878 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); |
|
879 DEFAULT_BUTTON(dialog, button); |
|
880 } |
|
881 } |
|
882 |
|
883 BOX_ADD_EXPAND(vbox, content_vbox); |
|
884 BOX_ADD_NO_EXPAND(vbox, separator); |
|
885 BOX_ADD_NO_EXPAND(vbox, widget); |
|
886 } else { |
|
887 WINDOW_SET_CONTENT(obj->widget, content_vbox); |
|
888 } |
|
889 |
|
890 return obj; |
|
891 } |