ui/gtk/container.c

changeset 108
77254bd6dccb
parent 103
6606616eca9f
child 110
c00e968d018b
equal deleted inserted replaced
107:b34bd1557c6c 108:77254bd6dccb
118 ct->container.add = ui_box_container_add; 118 ct->container.add = ui_box_container_add;
119 ct->type = type; 119 ct->type = type;
120 return (UiContainer*)ct; 120 return (UiContainer*)ct;
121 } 121 }
122 122
123 void ui_box_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 123 void ui_box_container_add(UiContainer *ct, GtkWidget *widget) {
124 UiBoxContainer *bc = (UiBoxContainer*)ct; 124 UiBoxContainer *bc = (UiBoxContainer*)ct;
125 if(ct->layout.fill != UI_LAYOUT_UNDEFINED) { 125 UiBool fill = ct->layout.fill;
126 fill = ui_lb2bool(ct->layout.fill);
127 }
128
129 if(bc->has_fill && fill) { 126 if(bc->has_fill && fill) {
130 fprintf(stderr, "UiError: container has 2 filled widgets"); 127 fprintf(stderr, "UiError: container has 2 filled widgets");
131 fill = FALSE; 128 fill = FALSE;
132 } 129 }
133 if(fill) { 130 if(fill) {
178 UI_GTK_V2(ct->height = 1); 175 UI_GTK_V2(ct->height = 1);
179 return (UiContainer*)ct; 176 return (UiContainer*)ct;
180 } 177 }
181 178
182 #if GTK_MAJOR_VERSION >= 3 179 #if GTK_MAJOR_VERSION >= 3
183 void ui_grid_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 180 void ui_grid_container_add(UiContainer *ct, GtkWidget *widget) {
184 UiGridContainer *grid = (UiGridContainer*)ct; 181 UiGridContainer *grid = (UiGridContainer*)ct;
185 182
186 if(ct->layout.newline) { 183 if(ct->layout.newline) {
187 grid->x = 0; 184 grid->x = 0;
188 grid->y++; 185 grid->y++;
206 } else if(grid->def_vfill) { 203 } else if(grid->def_vfill) {
207 vfill = TRUE; 204 vfill = TRUE;
208 } 205 }
209 } 206 }
210 207
211 if(ct->layout.fill != UI_LAYOUT_UNDEFINED) { 208 UiBool fill = ct->layout.fill;
212 fill = ui_lb2bool(ct->layout.fill);
213 }
214 if(ct->layout.hexpand) { 209 if(ct->layout.hexpand) {
215 hexpand = TRUE; 210 hexpand = TRUE;
216 hfill = TRUE; 211 hfill = TRUE;
217 } else if(ct->layout.hfill) { 212 } else if(ct->layout.hfill) {
218 hfill = TRUE; 213 hfill = TRUE;
247 ui_reset_layout(ct->layout); 242 ui_reset_layout(ct->layout);
248 ct->current = widget; 243 ct->current = widget;
249 } 244 }
250 #endif 245 #endif
251 #ifdef UI_GTK2 246 #ifdef UI_GTK2
252 void ui_grid_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 247 void ui_grid_container_add(UiContainer *ct, GtkWidget *widget) {
253 UiGridContainer *grid = (UiGridContainer*)ct; 248 UiGridContainer *grid = (UiGridContainer*)ct;
254 249
255 if(ct->layout.newline) { 250 if(ct->layout.newline) {
256 grid->x = 0; 251 grid->x = 0;
257 grid->y++; 252 grid->y++;
275 } else if(grid->def_vfill) { 270 } else if(grid->def_vfill) {
276 vfill = TRUE; 271 vfill = TRUE;
277 } 272 }
278 } 273 }
279 274
280 if(ct->layout.fill != UI_LAYOUT_UNDEFINED) { 275 UiBool fill = ct->layout.fill;
281 fill = ui_lb2bool(ct->layout.fill);
282 }
283 if(ct->layout.hexpand) { 276 if(ct->layout.hexpand) {
284 hexpand = TRUE; 277 hexpand = TRUE;
285 hfill = TRUE; 278 hfill = TRUE;
286 } else if(ct->layout.hfill) { 279 } else if(ct->layout.hfill) {
287 hfill = TRUE; 280 hfill = TRUE;
338 ct->widget = frame; 331 ct->widget = frame;
339 ct->add = ui_frame_container_add; 332 ct->add = ui_frame_container_add;
340 return ct; 333 return ct;
341 } 334 }
342 335
343 void ui_frame_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 336 void ui_frame_container_add(UiContainer *ct, GtkWidget *widget) {
344 FRAME_SET_CHILD(ct->widget, widget); 337 FRAME_SET_CHILD(ct->widget, widget);
345 } 338 }
346 339
347 UiContainer* ui_expander_container(UiObject *obj, GtkWidget *expander) { 340 UiContainer* ui_expander_container(UiObject *obj, GtkWidget *expander) {
348 UiContainer *ct = cxCalloc( 341 UiContainer *ct = cxCalloc(
352 ct->widget = expander; 345 ct->widget = expander;
353 ct->add = ui_expander_container_add; 346 ct->add = ui_expander_container_add;
354 return ct; 347 return ct;
355 } 348 }
356 349
357 void ui_expander_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 350 void ui_expander_container_add(UiContainer *ct, GtkWidget *widget) {
358 EXPANDER_SET_CHILD(ct->widget, widget); 351 EXPANDER_SET_CHILD(ct->widget, widget);
359 } 352 }
360 353
361 void ui_scrolledwindow_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 354 void ui_scrolledwindow_container_add(UiContainer *ct, GtkWidget *widget) {
362 // TODO: check if the widget implements GtkScrollable 355 // TODO: check if the widget implements GtkScrollable
363 SCROLLEDWINDOW_SET_CHILD(ct->widget, widget); 356 SCROLLEDWINDOW_SET_CHILD(ct->widget, widget);
364 ui_reset_layout(ct->layout); 357 ui_reset_layout(ct->layout);
365 ct->current = widget; 358 ct->current = widget;
366 } 359 }
383 ct->container.widget = tabview; 376 ct->container.widget = tabview;
384 ct->container.add = ui_tabview_container_add; 377 ct->container.add = ui_tabview_container_add;
385 return (UiContainer*)ct; 378 return (UiContainer*)ct;
386 } 379 }
387 380
388 void ui_tabview_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 381 void ui_tabview_container_add(UiContainer *ct, GtkWidget *widget) {
389 UiGtkTabView *data = ui_widget_get_tabview_data(ct->widget); 382 UiGtkTabView *data = ui_widget_get_tabview_data(ct->widget);
390 if(!data) { 383 if(!data) {
391 fprintf(stderr, "UI Error: widget is not a tabview"); 384 fprintf(stderr, "UI Error: widget is not a tabview");
392 return; 385 return;
393 } 386 }
418 ret = a; 411 ret = a;
419 #endif 412 #endif
420 return ret; 413 return ret;
421 } 414 }
422 415
423 UIWIDGET ui_box_create(UiObject *obj, UiContainerArgs args, UiSubContainerType type) { 416 UIWIDGET ui_box_create(UiObject *obj, UiContainerArgs *args, UiSubContainerType type) {
424 UiObject *current = uic_current_obj(obj); 417 UiObject *current = uic_current_obj(obj);
425 UiContainer *ct = current->container; 418 UiContainer *ct = current->container;
426 UI_APPLY_LAYOUT1(current, args); 419 UI_APPLY_LAYOUT2(current, args);
427 420
428 GtkWidget *box = type == UI_CONTAINER_VBOX ? ui_gtk_vbox_new(args.spacing) : ui_gtk_hbox_new(args.spacing); 421 GtkWidget *box = type == UI_CONTAINER_VBOX ? ui_gtk_vbox_new(args->spacing) : ui_gtk_hbox_new(args->spacing);
429 ui_set_name_and_style(box, args.name, args.style_class); 422 ui_set_name_and_style(box, args->name, args->style_class);
430 GtkWidget *widget = args.margin > 0 ? ui_box_set_margin(box, args.margin) : box; 423 GtkWidget *widget = args->margin > 0 ? ui_box_set_margin(box, args->margin) : box;
431 ct->add(ct, widget, TRUE); 424 ct->add(ct, widget);
432 425
433 UiObject *newobj = uic_object_new(obj, box); 426 UiObject *newobj = uic_object_new(obj, box);
434 newobj->container = ui_box_container(obj, box, type); 427 newobj->container = ui_box_container(obj, box, type);
435 uic_obj_add(obj, newobj); 428 uic_obj_add(obj, newobj);
436 429
437 return widget; 430 return widget;
438 } 431 }
439 432
440 UIEXPORT UIWIDGET ui_vbox_create(UiObject *obj, UiContainerArgs args) { 433 UIEXPORT UIWIDGET ui_vbox_create(UiObject *obj, UiContainerArgs *args) {
441 return ui_box_create(obj, args, UI_CONTAINER_VBOX); 434 return ui_box_create(obj, args, UI_CONTAINER_VBOX);
442 } 435 }
443 436
444 UIEXPORT UIWIDGET ui_hbox_create(UiObject *obj, UiContainerArgs args) { 437 UIEXPORT UIWIDGET ui_hbox_create(UiObject *obj, UiContainerArgs *args) {
445 return ui_box_create(obj, args, UI_CONTAINER_HBOX); 438 return ui_box_create(obj, args, UI_CONTAINER_HBOX);
446 } 439 }
447 440
448 GtkWidget* ui_create_grid_widget(int colspacing, int rowspacing) { 441 GtkWidget* ui_create_grid_widget(int colspacing, int rowspacing) {
449 #if GTK_MAJOR_VERSION >= 3 442 #if GTK_MAJOR_VERSION >= 3
456 gtk_table_set_row_spacings(GTK_TABLE(grid), rowspacing); 449 gtk_table_set_row_spacings(GTK_TABLE(grid), rowspacing);
457 #endif 450 #endif
458 return grid; 451 return grid;
459 } 452 }
460 453
461 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs args) { 454 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs *args) {
462 UiObject* current = uic_current_obj(obj); 455 UiObject* current = uic_current_obj(obj);
463 UI_APPLY_LAYOUT1(current, args); 456 UI_APPLY_LAYOUT2(current, args);
464 GtkWidget *widget; 457 GtkWidget *widget;
465 458
466 GtkWidget *grid = ui_create_grid_widget(args.columnspacing, args.rowspacing); 459 GtkWidget *grid = ui_create_grid_widget(args->columnspacing, args->rowspacing);
467 ui_set_name_and_style(grid, args.name, args.style_class); 460 ui_set_name_and_style(grid, args->name, args->style_class);
468 widget = ui_box_set_margin(grid, args.margin); 461 widget = ui_box_set_margin(grid, args->margin);
469 current->container->add(current->container, widget, TRUE); 462 current->container->add(current->container, widget);
470 463
471 UiObject *newobj = uic_object_new(obj, grid); 464 UiObject *newobj = uic_object_new(obj, grid);
472 newobj->container = ui_grid_container(obj, grid, args.def_hexpand, args.def_vexpand, args.def_hfill, args.def_vfill); 465 newobj->container = ui_grid_container(obj, grid, args->def_hexpand, args->def_vexpand, args->def_hfill, args->def_vfill);
473 uic_obj_add(obj, newobj); 466 uic_obj_add(obj, newobj);
474 467
475 return widget; 468 return widget;
476 } 469 }
477 470
478 UIWIDGET ui_frame_create(UiObject *obj, UiFrameArgs args) { 471 UIWIDGET ui_frame_create(UiObject *obj, UiFrameArgs *args) {
479 UiObject* current = uic_current_obj(obj); 472 UiObject* current = uic_current_obj(obj);
480 UI_APPLY_LAYOUT1(current, args); 473 UI_APPLY_LAYOUT2(current, args);
481 474
482 GtkWidget *frame = gtk_frame_new(args.label); 475 GtkWidget *frame = gtk_frame_new(args->label);
483 UiObject *newobj = uic_object_new(obj, frame); 476 UiObject *newobj = uic_object_new(obj, frame);
484 GtkWidget *sub = ui_subcontainer_create(args.subcontainer, newobj, args.spacing, args.columnspacing, args.rowspacing, args.margin); 477 GtkWidget *sub = ui_subcontainer_create(args->subcontainer, newobj, args->spacing, args->columnspacing, args->rowspacing, args->margin);
485 if(sub) { 478 if(sub) {
486 FRAME_SET_CHILD(frame, sub); 479 FRAME_SET_CHILD(frame, sub);
487 } else { 480 } else {
488 newobj->widget = frame; 481 newobj->widget = frame;
489 newobj->container = ui_frame_container(obj, frame); 482 newobj->container = ui_frame_container(obj, frame);
490 } 483 }
491 current->container->add(current->container, frame, FALSE); 484 current->container->add(current->container, frame);
492 uic_obj_add(obj, newobj); 485 uic_obj_add(obj, newobj);
493 486
494 return frame; 487 return frame;
495 } 488 }
496 489
497 UIEXPORT UIWIDGET ui_expander_create(UiObject *obj, UiFrameArgs args) { 490 UIEXPORT UIWIDGET ui_expander_create(UiObject *obj, UiFrameArgs *args) {
498 UiObject* current = uic_current_obj(obj); 491 UiObject* current = uic_current_obj(obj);
499 UI_APPLY_LAYOUT1(current, args); 492 UI_APPLY_LAYOUT2(current, args);
500 493
501 GtkWidget *expander = gtk_expander_new(args.label); 494 GtkWidget *expander = gtk_expander_new(args->label);
502 gtk_expander_set_expanded(GTK_EXPANDER(expander), args.isexpanded); 495 gtk_expander_set_expanded(GTK_EXPANDER(expander), args->isexpanded);
503 UiObject *newobj = uic_object_new(obj, expander); 496 UiObject *newobj = uic_object_new(obj, expander);
504 GtkWidget *sub = ui_subcontainer_create(args.subcontainer, newobj, args.spacing, args.columnspacing, args.rowspacing, args.margin); 497 GtkWidget *sub = ui_subcontainer_create(args->subcontainer, newobj, args->spacing, args->columnspacing, args->rowspacing, args->margin);
505 if(sub) { 498 if(sub) {
506 EXPANDER_SET_CHILD(expander, sub); 499 EXPANDER_SET_CHILD(expander, sub);
507 } else { 500 } else {
508 newobj->widget = expander; 501 newobj->widget = expander;
509 newobj->container = ui_expander_container(obj, expander); 502 newobj->container = ui_expander_container(obj, expander);
510 } 503 }
511 current->container->add(current->container, expander, FALSE); 504 current->container->add(current->container, expander);
512 uic_obj_add(obj, newobj); 505 uic_obj_add(obj, newobj);
513 506
514 return expander; 507 return expander;
515 } 508 }
516 509
517 510
518 UIWIDGET ui_scrolledwindow_create(UiObject* obj, UiFrameArgs args) { 511 UIWIDGET ui_scrolledwindow_create(UiObject* obj, UiFrameArgs *args) {
519 UiObject* current = uic_current_obj(obj); 512 UiObject* current = uic_current_obj(obj);
520 UI_APPLY_LAYOUT1(current, args); 513 UI_APPLY_LAYOUT2(current, args);
521 514
522 GtkWidget *sw = SCROLLEDWINDOW_NEW(); 515 GtkWidget *sw = SCROLLEDWINDOW_NEW();
523 ui_set_name_and_style(sw, args.name, args.style_class); 516 ui_set_name_and_style(sw, args->name, args->style_class);
524 GtkWidget *widget = ui_box_set_margin(sw, args.margin); 517 GtkWidget *widget = ui_box_set_margin(sw, args->margin);
525 current->container->add(current->container, widget, TRUE); 518 current->container->add(current->container, widget);
526 519
527 UiObject *newobj = uic_object_new(obj, sw); 520 UiObject *newobj = uic_object_new(obj, sw);
528 GtkWidget *sub = ui_subcontainer_create(args.subcontainer, newobj, args.spacing, args.columnspacing, args.rowspacing, args.margin); 521 GtkWidget *sub = ui_subcontainer_create(args->subcontainer, newobj, args->spacing, args->columnspacing, args->rowspacing, args->margin);
529 if(sub) { 522 if(sub) {
530 SCROLLEDWINDOW_SET_CHILD(sw, sub); 523 SCROLLEDWINDOW_SET_CHILD(sw, sub);
531 } else { 524 } else {
532 newobj->widget = sw; 525 newobj->widget = sw;
533 newobj->container = ui_scrolledwindow_container(obj, sw); 526 newobj->container = ui_scrolledwindow_container(obj, sw);
687 680
688 UiGtkTabView* ui_widget_get_tabview_data(UIWIDGET tabview) { 681 UiGtkTabView* ui_widget_get_tabview_data(UIWIDGET tabview) {
689 return g_object_get_data(G_OBJECT(tabview), "ui_tabview"); 682 return g_object_get_data(G_OBJECT(tabview), "ui_tabview");
690 } 683 }
691 684
685 static void tabview_switch_page(
686 GtkNotebook *self,
687 GtkWidget *page,
688 guint page_num,
689 gpointer userdata)
690 {
691 UiGtkTabView *tabview = userdata;
692 if(!tabview->onchange) {
693 return;
694 }
695
696 UiEvent event;
697 event.obj = tabview->obj;
698 event.window = event.obj->window;
699 event.document = event.obj->ctx->document;
700 event.set = ui_get_setop();
701 event.eventdata = NULL;
702 event.eventdatatype = 0;
703 event.intval = page_num;
704
705 tabview->onchange(&event, tabview->onchange);
706 }
707
708 #if GTK_CHECK_VERSION(3, 10, 0)
709
710 static void tabview_stack_changed(
711 GObject *object,
712 GParamSpec *pspec,
713 UiGtkTabView *tabview)
714 {
715 if(!tabview->onchange) {
716 return;
717 }
718
719 UiEvent event;
720 event.obj = tabview->obj;
721 event.window = event.obj->window;
722 event.document = event.obj->ctx->document;
723 event.set = ui_get_setop();
724 event.eventdata = NULL;
725 event.eventdatatype = 0;
726 event.intval = 0;
727
728 tabview->onchange(&event, tabview->onchange);
729 }
730
731 #endif
732
692 typedef int64_t(*ui_tabview_get_func)(UiInteger*); 733 typedef int64_t(*ui_tabview_get_func)(UiInteger*);
693 typedef void (*ui_tabview_set_func)(UiInteger*, int64_t); 734 typedef void (*ui_tabview_set_func)(UiInteger*, int64_t);
694 735
695 UIWIDGET ui_tabview_create(UiObject* obj, UiTabViewArgs args) { 736 UIWIDGET ui_tabview_create(UiObject* obj, UiTabViewArgs *args) {
696 UiGtkTabView *data = malloc(sizeof(UiGtkTabView)); 737 UiGtkTabView *data = malloc(sizeof(UiGtkTabView));
697 data->margin = args.margin; 738 memset(data, 0, sizeof(UiGtkTabView));
698 data->spacing = args.spacing; 739 data->margin = args->margin;
699 data->columnspacing = args.columnspacing; 740 data->spacing = args->spacing;
700 data->rowspacing = args.rowspacing; 741 data->columnspacing = args->columnspacing;
742 data->rowspacing = args->rowspacing;
701 743
702 ui_tabview_get_func getfunc = NULL; 744 ui_tabview_get_func getfunc = NULL;
703 ui_tabview_set_func setfunc = NULL; 745 ui_tabview_set_func setfunc = NULL;
704 746
705 GtkWidget *widget = NULL; 747 GtkWidget *widget = NULL;
706 GtkWidget *data_widget = NULL; 748 GtkWidget *data_widget = NULL;
707 switch(args.tabview) { 749 switch(args->tabview) {
708 case UI_TABVIEW_DOC: { 750 case UI_TABVIEW_DOC: {
709 // TODO 751 // TODO
710 break; 752 break;
711 } 753 }
712 case UI_TABVIEW_NAVIGATION_SIDE: { 754 case UI_TABVIEW_NAVIGATION_SIDE: {
713 #if GTK_CHECK_VERSION(3, 10, 0) 755 #if GTK_CHECK_VERSION(3, 10, 0)
714 widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); 756 widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
715 GtkWidget *sidebar = gtk_stack_sidebar_new(); 757 GtkWidget *sidebar = gtk_stack_sidebar_new();
716 BOX_ADD(widget, sidebar); 758 BOX_ADD(widget, sidebar);
717 GtkWidget *stack = gtk_stack_new(); 759 GtkWidget *stack = gtk_stack_new();
760 g_signal_connect(stack, "notify::visible-child", G_CALLBACK(tabview_stack_changed), data);
718 gtk_stack_set_transition_type (GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_SLIDE_UP_DOWN); 761 gtk_stack_set_transition_type (GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_SLIDE_UP_DOWN);
719 gtk_stack_sidebar_set_stack(GTK_STACK_SIDEBAR(sidebar), GTK_STACK(stack)); 762 gtk_stack_sidebar_set_stack(GTK_STACK_SIDEBAR(sidebar), GTK_STACK(stack));
720 BOX_ADD_EXPAND(widget, stack); 763 BOX_ADD_EXPAND(widget, stack);
721 data->select_tab = ui_stack_tab_select; 764 data->select_tab = ui_stack_tab_select;
722 data->remove_tab = ui_stack_tab_remove; 765 data->remove_tab = ui_stack_tab_remove;
732 case UI_TABVIEW_DEFAULT: /* fall through */ 775 case UI_TABVIEW_DEFAULT: /* fall through */
733 case UI_TABVIEW_NAVIGATION_TOP: /* fall through */ 776 case UI_TABVIEW_NAVIGATION_TOP: /* fall through */
734 case UI_TABVIEW_INVISIBLE: /* fall through */ 777 case UI_TABVIEW_INVISIBLE: /* fall through */
735 case UI_TABVIEW_NAVIGATION_TOP2: { 778 case UI_TABVIEW_NAVIGATION_TOP2: {
736 widget = gtk_notebook_new(); 779 widget = gtk_notebook_new();
780 g_signal_connect(
781 widget,
782 "switch-page",
783 G_CALLBACK(tabview_switch_page),
784 data);
737 data_widget = widget; 785 data_widget = widget;
738 data->select_tab = ui_notebook_tab_select; 786 data->select_tab = ui_notebook_tab_select;
739 data->remove_tab = ui_notebook_tab_remove; 787 data->remove_tab = ui_notebook_tab_remove;
740 data->add_tab = ui_notebook_tab_add; 788 data->add_tab = ui_notebook_tab_add;
741 getfunc = ui_notebook_get; 789 getfunc = ui_notebook_get;
742 setfunc = ui_notebook_set; 790 setfunc = ui_notebook_set;
743 if(args.tabview == UI_TABVIEW_INVISIBLE) { 791 if(args->tabview == UI_TABVIEW_INVISIBLE) {
744 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(widget), FALSE); 792 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(widget), FALSE);
745 gtk_notebook_set_show_border(GTK_NOTEBOOK(widget), FALSE); 793 gtk_notebook_set_show_border(GTK_NOTEBOOK(widget), FALSE);
746 } 794 }
747 break; 795 break;
748 } 796 }
749 } 797 }
750 798
751 UiObject* current = uic_current_obj(obj); 799 UiObject* current = uic_current_obj(obj);
752 if(args.value || args.varname) { 800 if(args->value || args->varname) {
753 UiVar *var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_INTEGER); 801 UiVar *var = uic_widget_var(obj->ctx, current->ctx, args->value, args->varname, UI_VAR_INTEGER);
754 UiInteger *i = var->value; 802 UiInteger *i = var->value;
755 i->get = getfunc; 803 i->get = getfunc;
756 i->set = setfunc; 804 i->set = setfunc;
757 i->obj = data_widget; 805 i->obj = data_widget;
758 } 806 }
759 807
760 g_object_set_data(G_OBJECT(widget), "ui_tabview", data); 808 g_object_set_data(G_OBJECT(widget), "ui_tabview", data);
761 data->widget = data_widget; 809 data->widget = data_widget;
762 data->subcontainer = args.subcontainer; 810 data->subcontainer = args->subcontainer;
763 811
764 UI_APPLY_LAYOUT1(current, args); 812 UI_APPLY_LAYOUT2(current, args);
765 current->container->add(current->container, widget, TRUE); 813 current->container->add(current->container, widget);
766 814
767 UiObject *newobj = uic_object_new(obj, widget); 815 UiObject *newobj = uic_object_new(obj, widget);
768 newobj->container = ui_tabview_container(obj, widget); 816 newobj->container = ui_tabview_container(obj, widget);
769 uic_obj_add(obj, newobj); 817 uic_obj_add(obj, newobj);
770 data->obj = newobj; 818 data->obj = newobj;
870 918
871 void ui_headerbar_end_create(UiObject *obj) { 919 void ui_headerbar_end_create(UiObject *obj) {
872 hb_set_part(obj, 1); 920 hb_set_part(obj, 1);
873 } 921 }
874 922
875 UIWIDGET ui_headerbar_fallback_create(UiObject *obj, UiHeaderbarArgs args) { 923 UIWIDGET ui_headerbar_fallback_create(UiObject *obj, UiHeaderbarArgs *args) {
876 UiObject *current = uic_current_obj(obj); 924 UiObject *current = uic_current_obj(obj);
877 UiContainer *ct = current->container; 925 UiContainer *ct = current->container;
878 UI_APPLY_LAYOUT1(current, args); 926 UI_APPLY_LAYOUT2(current, args);
879 927
880 GtkWidget *box = ui_gtk_hbox_new(args.alt_spacing); 928 GtkWidget *box = ui_gtk_hbox_new(args->alt_spacing);
881 ui_set_name_and_style(box, args.name, args.style_class); 929 ui_set_name_and_style(box, args->name, args->style_class);
882 ct->add(ct, box, FALSE); 930 ct->add(ct, box);
883 931
884 UiObject *newobj = uic_object_new(obj, box); 932 UiObject *newobj = uic_object_new(obj, box);
885 newobj->container = ui_headerbar_fallback_container(obj, box); 933 newobj->container = ui_headerbar_fallback_container(obj, box);
886 uic_obj_add(obj, newobj); 934 uic_obj_add(obj, newobj);
887 935
908 ct->container.widget = headerbar; 956 ct->container.widget = headerbar;
909 ct->container.add = ui_headerbar_fallback_container_add; 957 ct->container.add = ui_headerbar_fallback_container_add;
910 return (UiContainer*)ct; 958 return (UiContainer*)ct;
911 } 959 }
912 960
913 void ui_headerbar_fallback_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 961 void ui_headerbar_fallback_container_add(UiContainer *ct, GtkWidget *widget) {
914 UiHeaderbarContainer *hb = (UiHeaderbarContainer*)ct; 962 UiHeaderbarContainer *hb = (UiHeaderbarContainer*)ct;
915 BOX_ADD(ct->widget, widget); 963 BOX_ADD(ct->widget, widget);
916 } 964 }
917 965
918 #if GTK_CHECK_VERSION(3, 10, 0) 966 #if GTK_CHECK_VERSION(3, 10, 0)
919 967
920 UIWIDGET ui_headerbar_create(UiObject *obj, UiHeaderbarArgs args) { 968 UIWIDGET ui_headerbar_create(UiObject *obj, UiHeaderbarArgs *args) {
921 GtkWidget *headerbar = g_object_get_data(G_OBJECT(obj->widget), "ui_headerbar"); 969 GtkWidget *headerbar = g_object_get_data(G_OBJECT(obj->widget), "ui_headerbar");
922 if(!headerbar) { 970 if(!headerbar) {
923 return ui_headerbar_fallback_create(obj, args); 971 return ui_headerbar_fallback_create(obj, args);
924 } 972 }
925 973
938 ct->container.widget = headerbar; 986 ct->container.widget = headerbar;
939 ct->container.add = ui_headerbar_container_add; 987 ct->container.add = ui_headerbar_container_add;
940 return (UiContainer*)ct; 988 return (UiContainer*)ct;
941 } 989 }
942 990
943 void ui_headerbar_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 991 void ui_headerbar_container_add(UiContainer *ct, GtkWidget *widget) {
944 UiHeaderbarContainer *hb = (UiHeaderbarContainer*)ct; 992 UiHeaderbarContainer *hb = (UiHeaderbarContainer*)ct;
945 if(hb->part == 0) { 993 if(hb->part == 0) {
946 UI_HEADERBAR_PACK_START(ct->widget, widget); 994 UI_HEADERBAR_PACK_START(ct->widget, widget);
947 } else if(hb->part == 1) { 995 } else if(hb->part == 1) {
948 UI_HEADERBAR_PACK_END(ct->widget, widget); 996 UI_HEADERBAR_PACK_END(ct->widget, widget);
956 } 1004 }
957 } 1005 }
958 1006
959 #else 1007 #else
960 1008
961 UIWIDGET ui_headerbar_create(UiObject *obj, UiHeaderbarArgs args) { 1009 UIWIDGET ui_headerbar_create(UiObject *obj, UiHeaderbarArgs *args) {
962 return ui_headerbar_fallback_create(obj, args); 1010 return ui_headerbar_fallback_create(obj, args);
963 } 1011 }
964 1012
965 #endif 1013 #endif
966 1014
967 /* -------------------- Sidebar -------------------- */ 1015 /* -------------------- Sidebar -------------------- */
968 1016
969 #ifdef UI_LIBADWAITA 1017 #ifdef UI_LIBADWAITA
970 UIWIDGET ui_sidebar_create(UiObject *obj, UiSidebarArgs args) { 1018 UIWIDGET ui_sidebar_create(UiObject *obj, UiSidebarArgs *args) {
971 GtkWidget *sidebar_toolbar_view = g_object_get_data(G_OBJECT(obj->widget), "ui_sidebar"); 1019 GtkWidget *sidebar_toolbar_view = g_object_get_data(G_OBJECT(obj->widget), "ui_sidebar");
972 if(!sidebar_toolbar_view) { 1020 if(!sidebar_toolbar_view) {
973 fprintf(stderr, "Error: window is not configured for sidebar\n"); 1021 fprintf(stderr, "Error: window is not configured for sidebar\n");
974 return NULL; 1022 return NULL;
975 } 1023 }
976 1024
977 GtkWidget *box = ui_gtk_vbox_new(args.spacing); 1025 GtkWidget *box = ui_gtk_vbox_new(args->spacing);
978 ui_box_set_margin(box, args.margin); 1026 ui_box_set_margin(box, args->margin);
979 adw_toolbar_view_set_content(ADW_TOOLBAR_VIEW(sidebar_toolbar_view), box); 1027 adw_toolbar_view_set_content(ADW_TOOLBAR_VIEW(sidebar_toolbar_view), box);
980 1028
981 UiObject *newobj = uic_object_new(obj, box); 1029 UiObject *newobj = uic_object_new(obj, box);
982 newobj->container = ui_box_container(obj, box, UI_CONTAINER_VBOX); 1030 newobj->container = ui_box_container(obj, box, UI_CONTAINER_VBOX);
983 uic_obj_add(obj, newobj); 1031 uic_obj_add(obj, newobj);
984 1032
985 return box; 1033 return box;
986 } 1034 }
987 #else 1035 #else
988 UIWIDGET ui_sidebar_create(UiObject *obj, UiSidebarArgs args) { 1036 UIWIDGET ui_sidebar_create(UiObject *obj, UiSidebarArgs *args) {
989 GtkWidget *sidebar_vbox = g_object_get_data(G_OBJECT(obj->widget), "ui_sidebar"); 1037 GtkWidget *sidebar_vbox = g_object_get_data(G_OBJECT(obj->widget), "ui_sidebar");
990 1038
991 GtkWidget *box = ui_gtk_vbox_new(args.spacing); 1039 GtkWidget *box = ui_gtk_vbox_new(args->spacing);
992 ui_box_set_margin(box, args.margin); 1040 ui_box_set_margin(box, args->margin);
993 BOX_ADD_EXPAND(sidebar_vbox, box); 1041 BOX_ADD_EXPAND(sidebar_vbox, box);
994 1042
995 UiObject *newobj = uic_object_new(obj, box); 1043 UiObject *newobj = uic_object_new(obj, box);
996 newobj->container = ui_box_container(obj, box, UI_CONTAINER_VBOX); 1044 newobj->container = ui_box_container(obj, box, UI_CONTAINER_VBOX);
997 uic_obj_add(obj, newobj); 1045 uic_obj_add(obj, newobj);
1017 return NULL; 1065 return NULL;
1018 } 1066 }
1019 1067
1020 1068
1021 1069
1022 static UIWIDGET splitpane_create(UiObject *obj, UiOrientation orientation, UiSplitPaneArgs args) { 1070 static UIWIDGET splitpane_create(UiObject *obj, UiOrientation orientation, UiSplitPaneArgs *args) {
1023 UiObject* current = uic_current_obj(obj); 1071 UiObject* current = uic_current_obj(obj);
1024 1072
1025 GtkWidget *pane0 = create_paned(orientation); 1073 GtkWidget *pane0 = create_paned(orientation);
1026 1074
1027 UI_APPLY_LAYOUT1(current, args); 1075 UI_APPLY_LAYOUT2(current, args);
1028 current->container->add(current->container, pane0, TRUE); 1076 current->container->add(current->container, pane0);
1029 1077
1030 int max = args.max_panes == 0 ? 2 : args.max_panes; 1078 int max = args->max_panes == 0 ? 2 : args->max_panes;
1031 1079
1032 UiObject *newobj = uic_object_new(obj, pane0); 1080 UiObject *newobj = uic_object_new(obj, pane0);
1033 newobj->container = ui_splitpane_container(obj, pane0, orientation, max, args.initial_position); 1081 newobj->container = ui_splitpane_container(obj, pane0, orientation, max, args->initial_position);
1034 uic_obj_add(obj, newobj); 1082 uic_obj_add(obj, newobj);
1035 1083
1036 g_object_set_data(G_OBJECT(pane0), "ui_splitpane", newobj->container); 1084 g_object_set_data(G_OBJECT(pane0), "ui_splitpane", newobj->container);
1037 1085
1038 return pane0; 1086 return pane0;
1039 } 1087 }
1040 1088
1041 UIWIDGET ui_hsplitpane_create(UiObject *obj, UiSplitPaneArgs args) { 1089 UIWIDGET ui_hsplitpane_create(UiObject *obj, UiSplitPaneArgs *args) {
1042 return splitpane_create(obj, UI_HORIZONTAL, args); 1090 return splitpane_create(obj, UI_HORIZONTAL, args);
1043 } 1091 }
1044 1092
1045 UIWIDGET ui_vsplitpane_create(UiObject *obj, UiSplitPaneArgs args) { 1093 UIWIDGET ui_vsplitpane_create(UiObject *obj, UiSplitPaneArgs *args) {
1046 return splitpane_create(obj, UI_VERTICAL, args); 1094 return splitpane_create(obj, UI_VERTICAL, args);
1047 } 1095 }
1048 1096
1049 UiContainer* ui_splitpane_container(UiObject *obj, GtkWidget *pane, UiOrientation orientation, int max, int init) { 1097 UiContainer* ui_splitpane_container(UiObject *obj, GtkWidget *pane, UiOrientation orientation, int max, int init) {
1050 UiSplitPaneContainer *ct = ui_calloc(obj->ctx, 1, sizeof(UiSplitPaneContainer)); 1098 UiSplitPaneContainer *ct = ui_calloc(obj->ctx, 1, sizeof(UiSplitPaneContainer));
1056 ct->initial_position = init; 1104 ct->initial_position = init;
1057 ct->children = cxArrayListCreateSimple(CX_STORE_POINTERS, 4); 1105 ct->children = cxArrayListCreateSimple(CX_STORE_POINTERS, 4);
1058 return (UiContainer*)ct; 1106 return (UiContainer*)ct;
1059 } 1107 }
1060 1108
1061 void ui_splitpane_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) { 1109 void ui_splitpane_container_add(UiContainer *ct, GtkWidget *widget) {
1062 UiSplitPaneContainer *s = (UiSplitPaneContainer*)ct; 1110 UiSplitPaneContainer *s = (UiSplitPaneContainer*)ct;
1063 1111
1064 if(s->nchildren >= s->max) { 1112 if(s->nchildren >= s->max) {
1065 fprintf(stderr, "splitpane: maximum number of children reached\n"); 1113 fprintf(stderr, "splitpane: maximum number of children reached\n");
1066 return; 1114 return;
1149 while(elm) { 1197 while(elm) {
1150 CxHashKey key = cx_hash_key(&elm, sizeof(void*)); 1198 CxHashKey key = cx_hash_key(&elm, sizeof(void*));
1151 UiObject *item_obj = cxMapGet(ct->current_items, key); 1199 UiObject *item_obj = cxMapGet(ct->current_items, key);
1152 if(item_obj) { 1200 if(item_obj) {
1153 // re-add previously created widget 1201 // re-add previously created widget
1154 ui_box_container_add(ct->container, item_obj->widget, FALSE); 1202 ui_box_container_add(ct->container, item_obj->widget);
1155 } else { 1203 } else {
1156 // create new widget and object for this list element 1204 // create new widget and object for this list element
1157 CxMempool *mp = cxMempoolCreateSimple(256); 1205 CxMempool *mp = cxMempoolCreateSimple(256);
1158 const CxAllocator *a = mp->allocator; 1206 const CxAllocator *a = mp->allocator;
1159 UiObject *obj = cxCalloc(a, 1, sizeof(UiObject)); 1207 UiObject *obj = cxCalloc(a, 1, sizeof(UiObject));
1164 obj, 1212 obj,
1165 ct->spacing, 1213 ct->spacing,
1166 ct->columnspacing, 1214 ct->columnspacing,
1167 ct->rowspacing, 1215 ct->rowspacing,
1168 ct->margin); 1216 ct->margin);
1169 ui_box_container_add(ct->container, obj->widget, FALSE); 1217 ui_box_container_add(ct->container, obj->widget);
1170 if(ct->create_ui) { 1218 if(ct->create_ui) {
1171 ct->create_ui(obj, index, elm, ct->userdata); 1219 ct->create_ui(obj, index, elm, ct->userdata);
1172 } 1220 }
1173 cxMapPut(new_items, key, obj); 1221 cxMapPut(new_items, key, obj);
1174 } 1222 }
1175 elm = list->next(list); 1223 elm = list->next(list);
1176 index++; 1224 index++;
1177 } 1225 }
1226
1227 #if GTK_MAJOR_VERSION < 4
1228 gtk_widget_show_all(ct->widget);
1229 #endif
1178 } 1230 }
1179 1231
1180 static void destroy_itemlist_container(GtkWidget *w, UiGtkItemListContainer *container) { 1232 static void destroy_itemlist_container(GtkWidget *w, UiGtkItemListContainer *container) {
1181 container->remove_items = FALSE; 1233 container->remove_items = FALSE;
1182 cxMapFree(container->current_items); 1234 cxMapFree(container->current_items);
1183 free(container); 1235 free(container);
1184 } 1236 }
1185 1237
1186 UIWIDGET ui_itemlist_create(UiObject *obj, UiItemListContainerArgs args) { 1238 UIWIDGET ui_itemlist_create(UiObject *obj, UiItemListContainerArgs *args) {
1187 UiObject *current = uic_current_obj(obj); 1239 UiObject *current = uic_current_obj(obj);
1188 UiContainer *ct = current->container; 1240 UiContainer *ct = current->container;
1189 UI_APPLY_LAYOUT1(current, args); 1241 UI_APPLY_LAYOUT2(current, args);
1190 1242
1191 GtkWidget *box = args.container == UI_CONTAINER_VBOX ? ui_gtk_vbox_new(args.spacing) : ui_gtk_hbox_new(args.spacing); 1243 GtkWidget *box = args->container == UI_CONTAINER_VBOX ? ui_gtk_vbox_new(args->spacing) : ui_gtk_hbox_new(args->spacing);
1192 ui_set_name_and_style(box, args.name, args.style_class); 1244 ui_set_name_and_style(box, args->name, args->style_class);
1193 GtkWidget *widget = args.margin > 0 ? ui_box_set_margin(box, args.margin) : box; 1245 GtkWidget *widget = args->margin > 0 ? ui_box_set_margin(box, args->margin) : box;
1194 ct->add(ct, widget, TRUE); 1246 ct->add(ct, widget);
1195 1247
1196 UiGtkItemListContainer *container = malloc(sizeof(UiGtkItemListContainer)); 1248 UiGtkItemListContainer *container = malloc(sizeof(UiGtkItemListContainer));
1197 container->parent = obj; 1249 container->parent = obj;
1198 container->widget = box; 1250 container->widget = box;
1199 container->container = ui_box_container(current, box, args.container); 1251 container->container = ui_box_container(current, box, args->container);
1200 container->create_ui = args.create_ui; 1252 container->create_ui = args->create_ui;
1201 container->userdata = args.userdata; 1253 container->userdata = args->userdata;
1202 container->subcontainer = args.subcontainer; 1254 container->subcontainer = args->subcontainer;
1203 container->current_items = cxHashMapCreateSimple(CX_STORE_POINTERS); 1255 container->current_items = cxHashMapCreateSimple(CX_STORE_POINTERS);
1204 container->current_items->collection.advanced_destructor = remove_item; 1256 container->current_items->collection.advanced_destructor = remove_item;
1205 container->current_items->collection.destructor_data = container; 1257 container->current_items->collection.destructor_data = container;
1206 container->margin = args.sub_margin; 1258 container->margin = args->sub_margin;
1207 container->spacing = args.sub_spacing; 1259 container->spacing = args->sub_spacing;
1208 container->columnspacing = args.sub_columnspacing; 1260 container->columnspacing = args->sub_columnspacing;
1209 container->rowspacing = args.sub_rowspacing; 1261 container->rowspacing = args->sub_rowspacing;
1210 container->remove_items = TRUE; 1262 container->remove_items = TRUE;
1211 1263
1212 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_LIST); 1264 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args->value, args->varname, UI_VAR_LIST);
1213 if(var) { 1265 if(var) {
1214 UiList *list = var->value; 1266 UiList *list = var->value;
1215 list->obj = container; 1267 list->obj = container;
1216 list->update = update_itemlist; 1268 list->update = update_itemlist;
1217 update_itemlist(list, 0); 1269 update_itemlist(list, 0);
1234 * 1286 *
1235 */ 1287 */
1236 1288
1237 void ui_layout_fill(UiObject *obj, UiBool fill) { 1289 void ui_layout_fill(UiObject *obj, UiBool fill) {
1238 UiContainer *ct = uic_get_current_container(obj); 1290 UiContainer *ct = uic_get_current_container(obj);
1239 ct->layout.fill = ui_bool2lb(fill); 1291 ct->layout.fill = fill;
1240 } 1292 }
1241 1293
1242 void ui_layout_hexpand(UiObject *obj, UiBool expand) { 1294 void ui_layout_hexpand(UiObject *obj, UiBool expand) {
1243 UiContainer *ct = uic_get_current_container(obj); 1295 UiContainer *ct = uic_get_current_container(obj);
1244 ct->layout.hexpand = expand; 1296 ct->layout.hexpand = expand;

mercurial