ui/gtk/window.c

changeset 108
77254bd6dccb
parent 102
64ded9f6a6c6
child 109
c3dfcb8f0be7
equal deleted inserted replaced
107:b34bd1557c6c 108:77254bd6dccb
100 return ui_window_close_request(obj); 100 return ui_window_close_request(obj);
101 } 101 }
102 #endif 102 #endif
103 103
104 static UiObject* create_window(const char *title, void *window_data, UiBool sidebar, UiBool simple) { 104 static UiObject* create_window(const char *title, void *window_data, UiBool sidebar, UiBool simple) {
105 CxMempool *mp = cxMempoolCreateSimple(256); 105 UiObject *obj = uic_object_new_toplevel();
106 UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject));
107 obj->ref = 0;
108 106
109 #ifdef UI_LIBADWAITA 107 #ifdef UI_LIBADWAITA
110 obj->widget = adw_application_window_new(ui_get_application()); 108 obj->widget = adw_application_window_new(ui_get_application());
111 #elif !defined(UI_GTK2) 109 #elif !defined(UI_GTK2)
112 obj->widget = gtk_application_window_new(ui_get_application()); 110 obj->widget = gtk_application_window_new(ui_get_application());
113 #else 111 #else
114 obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL); 112 obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
115 #endif 113 #endif
116 114
117
118 obj->ctx = uic_context(obj, mp);
119 obj->window = window_data; 115 obj->window = window_data;
120 116
121 #if GTK_CHECK_VERSION(4, 0, 0) 117 #if GTK_CHECK_VERSION(4, 0, 0)
122 obj->ctx->action_map = G_ACTION_MAP(obj->widget); 118 obj->ctx->action_map = G_ACTION_MAP(obj->widget);
123 #endif 119 #endif
314 UiEvent evt; 310 UiEvent evt;
315 evt.obj = data->obj; 311 evt.obj = data->obj;
316 evt.document = evt.obj->ctx->document; 312 evt.document = evt.obj->ctx->document;
317 evt.window = evt.obj->window; 313 evt.window = evt.obj->window;
318 evt.eventdata = NULL; 314 evt.eventdata = NULL;
315 evt.eventdatatype = 0;
316 evt.eventdatatype = 0;
319 evt.intval = 0; 317 evt.intval = 0;
320 318
321 if(!strcmp(response, "btn1")) { 319 if(!strcmp(response, "btn1")) {
322 evt.intval = 1; 320 evt.intval = 1;
323 } else if(!strcmp(response, "btn2")) { 321 } else if(!strcmp(response, "btn2")) {
325 } 323 }
326 324
327 if(data->customdata) { 325 if(data->customdata) {
328 GtkWidget *entry = data->customdata; 326 GtkWidget *entry = data->customdata;
329 evt.eventdata = (void*)ENTRY_GET_TEXT(GTK_ENTRY(entry)); 327 evt.eventdata = (void*)ENTRY_GET_TEXT(GTK_ENTRY(entry));
328 evt.eventdatatype = UI_EVENT_DATA_STRING;
330 } 329 }
331 330
332 if(data->callback) { 331 if(data->callback) {
333 data->callback(&evt, data->userdata); 332 data->callback(&evt, data->userdata);
334 } 333 }
335 } 334 }
336 335
337 void ui_dialog_create(UiObject *parent, UiDialogArgs args) { 336 void ui_dialog_create(UiObject *parent, UiDialogArgs *args) {
338 AdwDialog *dialog = adw_alert_dialog_new(args.title, args.content); 337 AdwDialog *dialog = adw_alert_dialog_new(args->title, args->content);
339 UiEventData *event = malloc(sizeof(UiEventData)); 338 UiEventData *event = malloc(sizeof(UiEventData));
340 event->callback = args.result; 339 event->callback = args->result;
341 event->userdata = args.resultdata; 340 event->userdata = args->resultdata;
342 event->customdata = NULL; 341 event->customdata = NULL;
342 event->customint = 0;
343 event->value = 0; 343 event->value = 0;
344 event->obj = parent; 344 event->obj = parent;
345 345
346 if(args.button1_label) { 346 if(args->button1_label) {
347 adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "btn1", args.button1_label); 347 adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "btn1", args->button1_label);
348 } 348 }
349 if(args.button2_label) { 349 if(args->button2_label) {
350 adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "btn2", args.button2_label); 350 adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "btn2", args->button2_label);
351 } 351 }
352 if(args.closebutton_label) { 352 if(args->closebutton_label) {
353 adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "close", args.closebutton_label); 353 adw_alert_dialog_add_response(ADW_ALERT_DIALOG(dialog), "close", args->closebutton_label);
354 adw_alert_dialog_set_close_response(ADW_ALERT_DIALOG(dialog), "close"); 354 adw_alert_dialog_set_close_response(ADW_ALERT_DIALOG(dialog), "close");
355 } 355 }
356 356
357 GtkWidget *entry = NULL; 357 GtkWidget *entry = NULL;
358 if(args.input || args.password) { 358 if(args->input || args->password) {
359 entry = gtk_entry_new(); 359 entry = gtk_entry_new();
360 if(args.password) { 360 if(args->password) {
361 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE); 361 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
362 } 362 }
363 if(args.input_value) { 363 if(args->input_value) {
364 ENTRY_SET_TEXT(entry, args.input_value); 364 ENTRY_SET_TEXT(entry, args->input_value);
365 } 365 }
366 adw_alert_dialog_set_extra_child(ADW_ALERT_DIALOG(dialog), entry); 366 adw_alert_dialog_set_extra_child(ADW_ALERT_DIALOG(dialog), entry);
367 event->customdata = entry; 367 event->customdata = entry;
368 event->customint = 0;
368 } 369 }
369 370
370 g_signal_connect( 371 g_signal_connect(
371 dialog, 372 dialog,
372 "destroy", 373 "destroy",
413 GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new()); 414 GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new());
414 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent->widget)); 415 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent->widget));
415 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); 416 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
416 417
417 GtkWidget *dialog_w = GTK_WIDGET(dialog); 418 GtkWidget *dialog_w = GTK_WIDGET(dialog);
418 if(args.title) { 419 if(args->title) {
419 gtk_window_set_title(GTK_WINDOW(dialog), args.title); 420 gtk_window_set_title(GTK_WINDOW(dialog), args->title);
420 } 421 }
421 if(args.button1_label) { 422 if(args->button1_label) {
422 gtk_dialog_add_button(dialog, args.button1_label, 1); 423 gtk_dialog_add_button(dialog, args->button1_label, 1);
423 } 424 }
424 if(args.button2_label) { 425 if(args->button2_label) {
425 gtk_dialog_add_button(dialog, args.button2_label, 2); 426 gtk_dialog_add_button(dialog, args->button2_label, 2);
426 } 427 }
427 if(args.closebutton_label) { 428 if(args->closebutton_label) {
428 gtk_dialog_add_button(dialog, args.closebutton_label, 0); 429 gtk_dialog_add_button(dialog, args->closebutton_label, 0);
429 } 430 }
430 431
431 GtkWidget *content_area = gtk_dialog_get_content_area(dialog); 432 GtkWidget *content_area = gtk_dialog_get_content_area(dialog);
432 if(args.content) { 433 if(args->content) {
433 GtkWidget *label = gtk_label_new(args.content); 434 GtkWidget *label = gtk_label_new(args->content);
434 BOX_ADD(content_area, label); 435 BOX_ADD(content_area, label);
435 } 436 }
436 437
437 GtkWidget *textfield = NULL; 438 GtkWidget *textfield = NULL;
438 if(args.input || args.password) { 439 if(args->input || args->password) {
439 textfield = gtk_entry_new(); 440 textfield = gtk_entry_new();
440 if(args.password) { 441 if(args->password) {
441 gtk_entry_set_visibility(GTK_ENTRY(textfield), FALSE); 442 gtk_entry_set_visibility(GTK_ENTRY(textfield), FALSE);
442 } 443 }
443 if(args.input_value) { 444 if(args->input_value) {
444 ENTRY_SET_TEXT(textfield, args.input_value); 445 ENTRY_SET_TEXT(textfield, args->input_value);
445 } 446 }
446 BOX_ADD(content_area, textfield); 447 BOX_ADD(content_area, textfield);
447 } 448 }
448 449
449 UiEventData *event = malloc(sizeof(UiEventData)); 450 UiEventData *event = malloc(sizeof(UiEventData));
450 event->obj = parent; 451 event->obj = parent;
451 event->callback = args.result; 452 event->callback = args->result;
452 event->userdata = args.resultdata; 453 event->userdata = args->resultdata;
453 event->value = 0; 454 event->value = 0;
454 event->customdata = textfield; 455 event->customdata = textfield;
455 456
456 g_signal_connect(dialog_w, 457 g_signal_connect(dialog_w,
457 "response", 458 "response",
519 520
520 UiFileList flist; 521 UiFileList flist;
521 flist.files = NULL; 522 flist.files = NULL;
522 flist.nfiles = 0; 523 flist.nfiles = 0;
523 evt.eventdata = &flist; 524 evt.eventdata = &flist;
525 evt.eventdatatype = UI_EVENT_DATA_FILE_LIST;
524 526
525 if(selection) { 527 if(selection) {
526 flist = listmodel2filelist(selection); 528 flist = listmodel2filelist(selection);
527 g_object_unref(selection); 529 g_object_unref(selection);
528 } else if(file) { 530 } else if(file) {
553 555
554 UiEventData *event = malloc(sizeof(UiEventData)); 556 UiEventData *event = malloc(sizeof(UiEventData));
555 event->callback = file_selected_callback; 557 event->callback = file_selected_callback;
556 event->userdata = cbdata; 558 event->userdata = cbdata;
557 event->customdata = NULL; 559 event->customdata = NULL;
560 event->customint = 0;
558 event->value = mode; 561 event->value = mode;
559 event->obj = obj; 562 event->obj = obj;
560 563
561 GtkWindow *parent = GTK_WINDOW(gtk_widget_get_root(obj->widget)); 564 GtkWindow *parent = GTK_WINDOW(gtk_widget_get_root(obj->widget));
562 GtkFileDialog *dialog = gtk_file_dialog_new(); 565 GtkFileDialog *dialog = gtk_file_dialog_new();
744 #define DEFAULT_BUTTON(window, button) gtk_widget_set_can_default(button, TRUE); gtk_window_set_default(GTK_WINDOW(window), button) 747 #define DEFAULT_BUTTON(window, button) gtk_widget_set_can_default(button, TRUE); gtk_window_set_default(GTK_WINDOW(window), button)
745 #endif 748 #endif
746 749
747 750
748 751
749 UiObject* ui_dialog_window_create(UiObject *parent, UiDialogWindowArgs args) { 752 UiObject* ui_dialog_window_create(UiObject *parent, UiDialogWindowArgs *args) {
750 GtkWidget *dialog = DIALOG_NEW(); 753 GtkWidget *dialog = DIALOG_NEW();
751 if(args.width > 0 || args.height > 0) { 754 if(args->width > 0 || args->height > 0) {
752 gtk_window_set_default_size( 755 gtk_window_set_default_size(
753 GTK_WINDOW(dialog), 756 GTK_WINDOW(dialog),
754 args.width, 757 args->width,
755 args.height); 758 args->height);
756 } 759 }
757 760
758 761
759 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent->widget)); 762 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent->widget));
760 if(args.modal != UI_OFF) { 763 if(args->modal != UI_OFF) {
761 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE); 764 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
762 } 765 }
763 766
764 CxMempool *mp = cxMempoolCreateSimple(256); 767 UiObject *obj = uic_object_new_toplevel();
765 UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject));
766 obj->ctx = uic_context(obj, mp);
767 obj->widget = dialog; 768 obj->widget = dialog;
768 obj->ref = 0; 769 obj->ref = 0;
769 obj->destroy = ui_window_widget_destroy; 770 obj->destroy = ui_window_widget_destroy;
770 nwindows++; 771 nwindows++;
771 772
772 if(args.title != NULL) { 773 if(args->title != NULL) {
773 gtk_window_set_title(GTK_WINDOW(dialog), args.title); 774 gtk_window_set_title(GTK_WINDOW(dialog), args->title);
774 } 775 }
775 776
776 #if ! GTK_CHECK_VERSION(4, 10, 0) 777 #if ! GTK_CHECK_VERSION(4, 10, 0)
777 UiEventData *event = malloc(sizeof(UiEventData)); 778 UiEventData *event = malloc(sizeof(UiEventData));
778 event->obj = obj; 779 event->obj = obj;
779 event->userdata = args.onclickdata; 780 event->userdata = args->onclickdata;
780 event->callback = args.onclick; 781 event->callback = args->onclick;
781 event->value = 0; 782 event->value = 0;
782 event->customdata = NULL; 783 event->customdata = NULL;
783 784
784 g_signal_connect(dialog, "response", G_CALLBACK(ui_dialogwindow_response), event); 785 g_signal_connect(dialog, "response", G_CALLBACK(ui_dialogwindow_response), event);
785 g_signal_connect( 786 g_signal_connect(
813 gtk_container_remove(GTK_CONTAINER(dialog), c); 814 gtk_container_remove(GTK_CONTAINER(dialog), c);
814 #endif 815 #endif
815 816
816 GtkWidget *content_vbox = ui_gtk_vbox_new(0); 817 GtkWidget *content_vbox = ui_gtk_vbox_new(0);
817 obj->container = ui_box_container(obj, content_vbox, UI_CONTAINER_VBOX); 818 obj->container = ui_box_container(obj, content_vbox, UI_CONTAINER_VBOX);
818 if(args.lbutton1 || args.lbutton2 || args.rbutton3 || args.rbutton4) { 819 if(args->lbutton1 || args->lbutton2 || args->rbutton3 || args->rbutton4) {
819 #if GTK_CHECK_VERSION(3, 10, 0) 820 #if GTK_CHECK_VERSION(3, 10, 0)
820 if(args.titlebar_buttons != UI_OFF) { 821 if(args->titlebar_buttons != UI_OFF) {
821 GtkWidget *headerbar = gtk_header_bar_new(); 822 GtkWidget *headerbar = gtk_header_bar_new();
822 gtk_window_set_titlebar(GTK_WINDOW(dialog), headerbar); 823 gtk_window_set_titlebar(GTK_WINDOW(dialog), headerbar);
823 if(args.show_closebutton == UI_OFF) { 824 if(args->show_closebutton == UI_OFF) {
824 HEADERBAR_SHOW_CLOSEBUTTON(headerbar, FALSE); 825 HEADERBAR_SHOW_CLOSEBUTTON(headerbar, FALSE);
825 } 826 }
826 827
827 if(args.lbutton1) { 828 if(args->lbutton1) {
828 GtkWidget *button = ui_create_button(obj, args.lbutton1, NULL, args.onclick, args.onclickdata, 1, args.default_button == 1); 829 GtkWidget *button = ui_create_button(obj, args->lbutton1, NULL, args->onclick, args->onclickdata, 1, args->default_button == 1);
829 gtk_header_bar_pack_start(GTK_HEADER_BAR(headerbar), button); 830 gtk_header_bar_pack_start(GTK_HEADER_BAR(headerbar), button);
830 if(args.default_button == 1) { 831 if(args->default_button == 1) {
831 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); 832 WIDGET_ADD_CSS_CLASS(button, "suggested-action");
832 DEFAULT_BUTTON(dialog, button); 833 DEFAULT_BUTTON(dialog, button);
833 } 834 }
834 } 835 }
835 if(args.lbutton2) { 836 if(args->lbutton2) {
836 GtkWidget *button = ui_create_button(obj, args.lbutton2, NULL, args.onclick, args.onclickdata, 2, args.default_button == 2); 837 GtkWidget *button = ui_create_button(obj, args->lbutton2, NULL, args->onclick, args->onclickdata, 2, args->default_button == 2);
837 gtk_header_bar_pack_start(GTK_HEADER_BAR(headerbar), button); 838 gtk_header_bar_pack_start(GTK_HEADER_BAR(headerbar), button);
838 if(args.default_button == 2) { 839 if(args->default_button == 2) {
839 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); 840 WIDGET_ADD_CSS_CLASS(button, "suggested-action");
840 DEFAULT_BUTTON(dialog, button); 841 DEFAULT_BUTTON(dialog, button);
841 } 842 }
842 } 843 }
843 844
844 if(args.rbutton4) { 845 if(args->rbutton4) {
845 GtkWidget *button = ui_create_button(obj, args.rbutton4, NULL, args.onclick, args.onclickdata, 4, args.default_button == 4); 846 GtkWidget *button = ui_create_button(obj, args->rbutton4, NULL, args->onclick, args->onclickdata, 4, args->default_button == 4);
846 gtk_header_bar_pack_end(GTK_HEADER_BAR(headerbar), button); 847 gtk_header_bar_pack_end(GTK_HEADER_BAR(headerbar), button);
847 if(args.default_button == 4) { 848 if(args->default_button == 4) {
848 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); 849 WIDGET_ADD_CSS_CLASS(button, "suggested-action");
849 DEFAULT_BUTTON(dialog, button); 850 DEFAULT_BUTTON(dialog, button);
850 } 851 }
851 } 852 }
852 if(args.rbutton3) { 853 if(args->rbutton3) {
853 GtkWidget *button = ui_create_button(obj, args.rbutton3, NULL, args.onclick, args.onclickdata, 3, args.default_button == 3); 854 GtkWidget *button = ui_create_button(obj, args->rbutton3, NULL, args->onclick, args->onclickdata, 3, args->default_button == 3);
854 gtk_header_bar_pack_end(GTK_HEADER_BAR(headerbar), button); 855 gtk_header_bar_pack_end(GTK_HEADER_BAR(headerbar), button);
855 if(args.default_button == 3) { 856 if(args->default_button == 3) {
856 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); 857 WIDGET_ADD_CSS_CLASS(button, "suggested-action");
857 DEFAULT_BUTTON(dialog, button); 858 DEFAULT_BUTTON(dialog, button);
858 } 859 }
859 } 860 }
860 WINDOW_SET_CONTENT(obj->widget, content_vbox); 861 WINDOW_SET_CONTENT(obj->widget, content_vbox);
868 869
869 GtkWidget *grid = ui_create_grid_widget(10, 10); 870 GtkWidget *grid = ui_create_grid_widget(10, 10);
870 GtkWidget *widget = ui_box_set_margin(grid, 16); 871 GtkWidget *widget = ui_box_set_margin(grid, 16);
871 gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE); 872 gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE);
872 873
873 if(args.lbutton1) { 874 if(args->lbutton1) {
874 GtkWidget *button = ui_create_button(obj, args.lbutton1, NULL, args.onclick, args.onclickdata, 1, args.default_button == 1); 875 GtkWidget *button = ui_create_button(obj, args->lbutton1, NULL, args->onclick, args->onclickdata, 1, args->default_button == 1);
875 gtk_grid_attach(GTK_GRID(grid), button, 0, 0, 1, 1); 876 gtk_grid_attach(GTK_GRID(grid), button, 0, 0, 1, 1);
876 if(args.default_button == 1) { 877 if(args->default_button == 1) {
877 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); 878 WIDGET_ADD_CSS_CLASS(button, "suggested-action");
878 DEFAULT_BUTTON(dialog, button); 879 DEFAULT_BUTTON(dialog, button);
879 } 880 }
880 } 881 }
881 if(args.lbutton2) { 882 if(args->lbutton2) {
882 GtkWidget *button = ui_create_button(obj, args.lbutton2, NULL, args.onclick, args.onclickdata, 2, args.default_button == 2); 883 GtkWidget *button = ui_create_button(obj, args->lbutton2, NULL, args->onclick, args->onclickdata, 2, args->default_button == 2);
883 gtk_grid_attach(GTK_GRID(grid), button, 1, 0, 1, 1); 884 gtk_grid_attach(GTK_GRID(grid), button, 1, 0, 1, 1);
884 if(args.default_button == 2) { 885 if(args->default_button == 2) {
885 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); 886 WIDGET_ADD_CSS_CLASS(button, "suggested-action");
886 DEFAULT_BUTTON(dialog, button); 887 DEFAULT_BUTTON(dialog, button);
887 } 888 }
888 } 889 }
889 GtkWidget *space = gtk_label_new(NULL); 890 GtkWidget *space = gtk_label_new(NULL);
890 gtk_widget_set_hexpand(space, TRUE); 891 gtk_widget_set_hexpand(space, TRUE);
891 gtk_grid_attach(GTK_GRID(grid), space, 2, 0, 1, 1); 892 gtk_grid_attach(GTK_GRID(grid), space, 2, 0, 1, 1);
892 if(args.rbutton3) { 893 if(args->rbutton3) {
893 GtkWidget *button = ui_create_button(obj, args.rbutton3, NULL, args.onclick, args.onclickdata, 3, args.default_button == 3); 894 GtkWidget *button = ui_create_button(obj, args->rbutton3, NULL, args->onclick, args->onclickdata, 3, args->default_button == 3);
894 gtk_grid_attach(GTK_GRID(grid), button, 3, 0, 1, 1); 895 gtk_grid_attach(GTK_GRID(grid), button, 3, 0, 1, 1);
895 if(args.default_button == 3) { 896 if(args->default_button == 3) {
896 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); 897 WIDGET_ADD_CSS_CLASS(button, "suggested-action");
897 DEFAULT_BUTTON(dialog, button); 898 DEFAULT_BUTTON(dialog, button);
898 } 899 }
899 } 900 }
900 if(args.rbutton4) { 901 if(args->rbutton4) {
901 GtkWidget *button = ui_create_button(obj, args.rbutton4, NULL, args.onclick, args.onclickdata, 4, args.default_button == 4); 902 GtkWidget *button = ui_create_button(obj, args->rbutton4, NULL, args->onclick, args->onclickdata, 4, args->default_button == 4);
902 gtk_grid_attach(GTK_GRID(grid), button, 4, 0, 1, 1); 903 gtk_grid_attach(GTK_GRID(grid), button, 4, 0, 1, 1);
903 if(args.default_button == 4) { 904 if(args->default_button == 4) {
904 WIDGET_ADD_CSS_CLASS(button, "suggested-action"); 905 WIDGET_ADD_CSS_CLASS(button, "suggested-action");
905 DEFAULT_BUTTON(dialog, button); 906 DEFAULT_BUTTON(dialog, button);
906 } 907 }
907 } 908 }
908 909

mercurial