2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
33 #include "container.h"
34 #include "../common/context.h"
35 #include "../common/object.h"
37 #define UI_GRID_MAX_COLUMNS 512
39 static UiBool ui_lb2bool(UiLayoutBool b) {
40 return b == UI_LAYOUT_TRUE ? TRUE : FALSE;
43 static UiLayoutBool ui_bool2lb(UiBool b) {
44 return b ? UI_LAYOUT_TRUE : UI_LAYOUT_FALSE;
48 UiContainer* ui_frame_container(UiObject *obj, Widget frame) {
49 UiContainer *ct = ucx_mempool_calloc(
54 ct->prepare = ui_frame_container_prepare;
55 ct->add = ui_frame_container_add;
59 Widget ui_frame_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill) {
63 void ui_frame_container_add(UiContainer *ct, Widget widget) {
64 ui_reset_layout(ct->layout);
69 UiContainer* ui_box_container(UiObject *obj, Widget box, int margin, int spacing, UiBoxOrientation orientation) {
70 UiBoxContainer *ct = ucx_mempool_calloc(
73 sizeof(UiBoxContainer));
74 ct->container.widget = box;
75 ct->container.prepare = ui_box_container_prepare;
76 ct->container.add = ui_box_container_add;
77 ct->orientation = orientation;
79 ct->spacing = spacing;
80 return (UiContainer*)ct;
83 Widget ui_box_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill) {
84 UiBoxContainer *bc = (UiBoxContainer*)ct;
85 if(ct->layout.fill != UI_LAYOUT_UNDEFINED) {
86 fill = ui_lb2bool(ct->layout.fill);
89 if(bc->has_fill && fill) {
90 fprintf(stderr, "UiError: container has 2 filled widgets");
98 // determine fixed and dynamic attachments
105 if(bc->orientation == UI_BOX_VERTICAL) {
106 f1 = XmNleftAttachment;
107 f2 = XmNrightAttachment;
108 d1 = XmNtopAttachment;
109 d2 = XmNbottomAttachment;
111 w2 = XmNbottomWidget;
114 XtSetArg(args[a], XmNleftOffset, bc->margin); a++;
115 XtSetArg(args[a], XmNrightOffset, bc->margin); a++;
117 XtSetArg(args[a], XmNtopOffset, bc->prev_widget ? bc->spacing : bc->margin); a++;
119 f1 = XmNtopAttachment;
120 f2 = XmNbottomAttachment;
121 d1 = XmNleftAttachment;
122 d2 = XmNrightAttachment;
127 XtSetArg(args[a], XmNtopOffset, bc->margin); a++;
128 XtSetArg(args[a], XmNbottomOffset, bc->margin); a++;
130 XtSetArg(args[a], XmNleftOffset, bc->prev_widget ? bc->spacing : bc->margin); a++;
132 XtSetArg(args[a], f1, XmATTACH_FORM); a++;
133 XtSetArg(args[a], f2, XmATTACH_FORM); a++;
136 XtSetArg(args[a], d2, XmATTACH_FORM); a++;
138 if(bc->prev_widget) {
139 XtSetArg(args[a], d1, XmATTACH_WIDGET); a++;
140 XtSetArg(args[a], w1, bc->prev_widget); a++;
142 XtSetArg(args[a], d1, XmATTACH_FORM); a++;
149 void ui_box_container_add(UiContainer *ct, Widget widget) {
150 UiBoxContainer *bc = (UiBoxContainer*)ct;
151 // determine dynamic attachments
156 if(bc->orientation == UI_BOX_VERTICAL) {
157 d1 = XmNtopAttachment;
158 d2 = XmNbottomAttachment;
160 w2 = XmNbottomWidget;
163 d1 = XmNleftAttachment;
164 d2 = XmNrightAttachment;
169 if(bc->prev_widget) {
171 XtVaGetValues(bc->prev_widget, d2, &v, NULL);
172 if(v == XmATTACH_FORM) {
189 bc->prev_widget = widget;
191 ui_reset_layout(ct->layout);
192 ct->current = widget;
195 UiContainer* ui_grid_container(UiObject *obj, Widget form, int columnspacing, int rowspacing) {
196 UiGridContainer *ct = ucx_mempool_calloc(
199 sizeof(UiGridContainer));
200 ct->container.widget = form;
201 ct->container.prepare = ui_grid_container_prepare;
202 ct->container.add = ui_grid_container_add;
203 ct->columnspacing = columnspacing;
204 ct->rowspacing = rowspacing;
205 return (UiContainer*)ct;
208 void ui_grid_newline(UiGridContainer *grid) {
210 grid->current = NULL;
212 grid->container.layout.newline = FALSE;
215 Widget ui_grid_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill) {
216 UiGridContainer *grid = (UiGridContainer*)ct;
217 if(ct->layout.newline) {
218 ui_grid_newline(grid);
223 void ui_grid_container_add(UiContainer *ct, Widget widget) {
224 UiGridContainer *grid = (UiGridContainer*)ct;
227 grid->current = ucx_list_append(grid->current, widget);
229 grid->current = ucx_list_append(grid->current, widget);
230 grid->lines = ucx_list_append(grid->lines, grid->current);
233 ui_reset_layout(ct->layout);
234 ct->current = widget;
237 static void ui_grid_resize(Widget widget, XtPointer udata, XtPointer cdata) {
238 UiGridContainer *grid = udata;
240 UcxList *rowdim = NULL;
241 int coldim[UI_GRID_MAX_COLUMNS];
242 memset(coldim, 0, UI_GRID_MAX_COLUMNS*sizeof(int));
245 // get the minimum size of the columns and rows
248 UCX_FOREACH(row, grid->lines) {
252 UCX_FOREACH(elm, row->data) {
253 Widget w = elm->data;
254 int widget_width = 0;
255 int widget_height = 0;
264 // get the maximum height in this row
265 if(widget_height > rheight) {
266 rheight = widget_height;
269 // get the maximum width in this column
270 if(widget_width > coldim[i]) {
271 coldim[i] = widget_width;
273 sum_width += widget_width;
274 if(sum_width > sumw) {
283 rowdim = ucx_list_append(rowdim, (void*)(intptr_t)rheight);
287 // check container size
290 XtVaGetValues(widget, XmNwidth, &gwidth, XmNheight, &gheight, NULL);
291 if(gwidth < sumw || gheight < sumh) {
292 XtVaSetValues(widget, XmNwidth, sumw, XmNheight, sumh, NULL);
293 ucx_list_free(rowdim);
298 // adjust the positions of all children
300 UCX_FOREACH(row, grid->lines) {
303 UCX_FOREACH(elm, row->data) {
304 Widget w = elm->data;
310 XmNheight, rowdim->data,
316 y += (intptr_t)rowdim->data;
317 rowdim = rowdim->next;
320 ucx_list_free(rowdim);
323 UiContainer* ui_scrolledwindow_container(UiObject *obj, Widget scrolledwindow) {
324 UiContainer *ct = ucx_mempool_calloc(
327 sizeof(UiContainer));
328 ct->widget = scrolledwindow;
329 ct->prepare = ui_scrolledwindow_container_prepare;
330 ct->add = ui_scrolledwindow_container_add;
334 Widget ui_scrolledwindow_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill) {
338 void ui_scrolledwindow_container_add(UiContainer *ct, Widget widget) {
339 ui_reset_layout(ct->layout);
340 ct->current = widget;
344 UiContainer* ui_tabview_container(UiObject *obj, Widget frame) {
345 UiTabViewContainer *ct = ucx_mempool_calloc(
348 sizeof(UiTabViewContainer));
349 ct->context = obj->ctx;
350 ct->container.widget = frame;
351 ct->container.prepare = ui_tabview_container_prepare;
352 ct->container.add = ui_tabview_container_add;
353 return (UiContainer*)ct;
356 Widget ui_tabview_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill) {
358 XtSetArg(args[a], XmNleftAttachment, XmATTACH_FORM); a++;
359 XtSetArg(args[a], XmNrightAttachment, XmATTACH_FORM); a++;
360 XtSetArg(args[a], XmNtopAttachment, XmATTACH_FORM); a++;
361 XtSetArg(args[a], XmNbottomAttachment, XmATTACH_FORM); a++;
366 void ui_tabview_container_add(UiContainer *ct, Widget widget) {
367 UiTabViewContainer *tabview = (UiTabViewContainer*)ct;
369 if(tabview->current) {
370 XtUnmanageChild(tabview->current);
373 tabview->current = widget;
374 tabview->tabs = ucx_list_append(tabview->tabs, widget);
376 ui_select_tab(ct->widget, 0);
377 ui_reset_layout(ct->layout);
378 ct->current = widget;
381 UIWIDGET ui_box(UiObject *obj, int margin, int spacing, UiBoxOrientation orientation) {
382 UiContainer *ct = uic_get_current_container(obj);
386 Widget parent = ct->prepare(ct, args, &n, TRUE);
387 Widget form = XmCreateForm(parent, "vbox", args, n);
391 UiObject *newobj = uic_object_new(obj, form);
392 newobj->container = ui_box_container(obj, form, margin, spacing, orientation);
393 uic_obj_add(obj, newobj);
398 UIWIDGET ui_vbox(UiObject *obj) {
399 return ui_box(obj, 0, 0, UI_BOX_VERTICAL);
402 UIWIDGET ui_hbox(UiObject *obj) {
403 return ui_box(obj, 0, 0, UI_BOX_HORIZONTAL);
406 UIWIDGET ui_vbox_sp(UiObject *obj, int margin, int spacing) {
407 return ui_box(obj, margin, spacing, UI_BOX_VERTICAL);
410 UIWIDGET ui_hbox_sp(UiObject *obj, int margin, int spacing) {
411 return ui_box(obj, margin, spacing, UI_BOX_HORIZONTAL);
414 UIWIDGET ui_grid(UiObject *obj) {
415 return ui_grid_sp(obj, 0, 0, 0);
418 UIWIDGET ui_grid_sp(UiObject *obj, int margin, int columnspacing, int rowspacing) {
419 UiContainer *ct = uic_get_current_container(obj);
423 Widget parent = ct->prepare(ct, args, &n, TRUE);
424 Widget grid = XmCreateDrawingArea(parent, "grid", args, n);
428 UiObject *newobj = uic_object_new(obj, grid);
429 newobj->container = ui_grid_container(obj, grid, columnspacing, rowspacing);
430 uic_obj_add(obj, newobj);
432 XtAddCallback (grid, XmNresizeCallback , ui_grid_resize, newobj->container);
437 UIWIDGET ui_scrolledwindow(UiObject *obj) {
438 UiContainer *ct = uic_get_current_container(obj);
442 XtSetArg(args[n], XmNscrollingPolicy, XmAUTOMATIC); // TODO: dosn't work, use XmAPPLICATION_DEFINED
444 Widget parent = ct->prepare(ct, args, &n, TRUE);
445 Widget scrolledwindow = XmCreateScrolledWindow(parent, "scrolledwindow", args, n);
446 ct->add(ct, scrolledwindow);
447 XtManageChild(scrolledwindow);
449 UiObject *newobj = uic_object_new(obj, scrolledwindow);
450 newobj->container = ui_scrolledwindow_container(obj, scrolledwindow);
451 uic_obj_add(obj, newobj);
453 return scrolledwindow;
456 UIWIDGET ui_sidebar(UiObject *obj) {
457 UiContainer *ct = uic_get_current_container(obj);
462 XtSetArg(args[n], XmNorientation, XmHORIZONTAL);
465 Widget parent = ct->prepare(ct, args, &n, TRUE);
466 Widget pane = XmCreatePanedWindow(parent, "pane", args, n);
470 // add sidebar widget
471 Widget sidebar = XmCreateForm(pane, "sidebar", args, 0);
472 XtManageChild(sidebar);
474 UiObject *left = uic_object_new(obj, sidebar);
475 left->container = ui_box_container(left, sidebar, 0, 0, UI_BOX_VERTICAL);
477 // add content widget
478 XtSetArg (args[0], XmNpaneMaximum, 8000);
479 Widget content = XmCreateForm(pane, "content_area", args, 1);
480 XtManageChild(content);
482 UiObject *right = uic_object_new(obj, content);
483 right->container = ui_box_container(right, content, 0, 0, UI_BOX_VERTICAL);
485 uic_obj_add(obj, right);
486 uic_obj_add(obj, left);
491 UIWIDGET ui_tabview(UiObject *obj) {
492 UiContainer *ct = uic_get_current_container(obj);
494 // create a simple frame as container widget
495 // when tabs are selected, the current child will be replaced by the
496 // the new tab widget
499 XtSetArg(args[n], XmNshadowType, XmSHADOW_ETCHED_OUT);
501 XtSetArg(args[n], XmNshadowThickness, 0);
503 Widget parent = ct->prepare(ct, args, &n, TRUE);
504 Widget form = XmCreateForm(parent, "tabview", args, n);
508 UiObject *tabviewobj = uic_object_new(obj, form);
509 tabviewobj->container = ui_tabview_container(obj, form);
510 uic_obj_add(obj, tabviewobj);
512 XtVaSetValues(form, XmNuserData, tabviewobj->container, NULL);
517 void ui_tab(UiObject *obj, char *title) {
518 UiContainer *ct = uic_get_current_container(obj);
519 ct->layout.label = title;
524 void ui_select_tab(UIWIDGET tabview, int tab) {
525 UiTabViewContainer *ct = NULL;
526 XtVaGetValues(tabview, XmNuserData, &ct, NULL);
528 XtUnmanageChild(ct->current);
529 UcxList *elm = ucx_list_get(ct->tabs, tab);
531 XtManageChild(elm->data);
532 ct->current = elm->data;
534 fprintf(stderr, "UiError: front tab index: %d\n", tab);
537 fprintf(stderr, "UiError: widget is not a tabview\n");
542 /* document tabview */
544 static void ui_tabbar_resize(Widget widget, XtPointer udata, XtPointer cdata) {
545 MotifTabbedPane *v = (MotifTabbedPane*)udata;
549 XtVaGetValues(widget, XmNwidth, &width, XmNheight, &height, NULL);
550 int button_width = width / 4;
552 UCX_FOREACH(elm, v->tabs) {
553 UiTab *tab = elm->data;
565 if(height <= v->height) {
566 XtVaSetValues(widget, XmNheight, v->height + 4, NULL);
570 static void ui_tabbar_expose(Widget widget, XtPointer udata, XtPointer cdata) {
571 MotifTabbedPane *v = (MotifTabbedPane*)udata;
572 XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct *)cdata;
573 XEvent *event = cbs->event;
574 Display *dpy = XtDisplay(widget);
582 XtVaGetValues(v->current->tab_button, XmNx, &tab_x, XmNwidth, &tab_width, XmNhighlightColor, &fgpix, NULL);
584 gcvals.foreground = v->bg1;
585 gc = XCreateGC( dpy, XtWindow(widget), (GCForeground), &gcvals);
589 XtVaGetValues(widget, XmNwidth, &width, XmNheight, &height, NULL);
590 XFillRectangle(dpy, XtWindow(widget), gc, 0, 0, width, height);
592 gcvals.foreground = fgpix;
593 gc = XCreateGC( dpy, XtWindow(widget), (GCForeground), &gcvals);
595 XFillRectangle(dpy, XtWindow(widget), gc, tab_x, 0, tab_width, height);
599 UiTabbedPane* ui_tabbed_document_view(UiObject *obj) {
603 UiContainer *ct = uic_get_current_container(obj);
604 Widget parent = ct->prepare(ct, args, &n, TRUE);
606 Widget tabview = XmCreateForm(parent, "tabview_form", args, n);
607 XtManageChild(tabview);
609 XtSetArg(args[0], XmNorientation, XmHORIZONTAL);
610 XtSetArg(args[1], XmNpacking, XmPACK_TIGHT);
611 XtSetArg(args[2], XmNspacing, 1);
612 XtSetArg(args[3], XmNleftAttachment, XmATTACH_FORM);
613 XtSetArg(args[4], XmNrightAttachment, XmATTACH_FORM);
614 XtSetArg(args[5], XmNtopAttachment, XmATTACH_FORM);
615 XtSetArg(args[6], XmNmarginWidth, 0);
616 XtSetArg(args[7], XmNmarginHeight, 0);
617 Widget tabbar = XmCreateDrawingArea(tabview, "tabbar", args, 8);
618 XtManageChild(tabbar);
620 XtSetArg(args[0], XmNleftAttachment, XmATTACH_FORM);
621 XtSetArg(args[1], XmNrightAttachment, XmATTACH_FORM);
622 XtSetArg(args[2], XmNtopAttachment, XmATTACH_WIDGET);
623 XtSetArg(args[3], XmNtopWidget, tabbar);
624 XtSetArg(args[4], XmNbottomAttachment, XmATTACH_FORM);
625 XtSetArg(args[5], XmNshadowThickness, 0);
626 Widget tabct = XmCreateForm(tabview, "tabview", args, 6);
627 XtManageChild(tabct);
629 MotifTabbedPane *tabbedpane = ui_malloc(obj->ctx, sizeof(MotifTabbedPane));
630 tabbedpane->view.ctx = uic_current_obj(obj)->ctx;
631 tabbedpane->view.widget = tabct;
632 tabbedpane->view.document = NULL;
633 tabbedpane->tabbar = tabbar;
634 tabbedpane->tabs = NULL;
635 tabbedpane->current = NULL;
636 tabbedpane->height = 0;
638 XtAddCallback(tabbar, XmNresizeCallback , ui_tabbar_resize, tabbedpane);
639 XtAddCallback(tabbar, XmNexposeCallback, ui_tabbar_expose, tabbedpane);
641 return &tabbedpane->view;
644 UiObject* ui_document_tab(UiTabbedPane *view) {
645 MotifTabbedPane *v = (MotifTabbedPane*)view;
649 // hide the current tab content
651 XtUnmanageChild(v->current->content->widget);
654 UiTab *tab = ui_malloc(view->ctx, sizeof(UiTab));
656 // create the new tab content
657 XtSetArg(args[0], XmNshadowThickness, 0);
658 XtSetArg(args[1], XmNleftAttachment, XmATTACH_FORM);
659 XtSetArg(args[2], XmNrightAttachment, XmATTACH_FORM);
660 XtSetArg(args[3], XmNtopAttachment, XmATTACH_FORM);
661 XtSetArg(args[4], XmNbottomAttachment, XmATTACH_FORM);
662 XtSetArg(args[5], XmNuserData, tab);
663 Widget frame = XmCreateFrame(view->widget, "tab", args, 6);
664 XtManageChild(frame);
666 UiObject *content = ui_malloc(view->ctx, sizeof(UiObject));
667 content->widget = NULL; // initialization for uic_context()
668 content->ctx = uic_context(content, view->ctx->mempool);
669 content->ctx->parent = view->ctx;
670 content->ctx->attach_document = uic_context_attach_document;
671 content->ctx->detach_document2 = uic_context_detach_document2;
672 content->widget = frame;
673 content->window = view->ctx->obj->window;
674 content->container = ui_frame_container(content, frame);
675 content->next = NULL;
678 v->tabs = ucx_list_append_a(view->ctx->mempool->allocator, v->tabs, tab);
680 XmString label = XmStringCreateLocalized("tab");
681 XtSetArg(args[0], XmNlabelString, label);
682 XtSetArg(args[1], XmNshadowThickness, 0);
683 XtSetArg(args[2], XmNhighlightThickness, 0);
685 Widget button = XmCreatePushButton(v->tabbar, "tab_button", args, 3);
687 tab->content = content;
688 tab->tab_button = button;
689 XtManageChild(button);
693 (XtCallbackProc)ui_tab_button_callback,
706 v->height += 2; // border
709 ui_change_tab(v, tab);
710 ui_tabbar_resize(v->tabbar, v, NULL);
715 void ui_tab_button_callback(Widget widget, UiTab *tab, XtPointer d) {
716 MotifTabbedPane *t = tab->tabbedpane;
718 XtUnmanageChild(t->current->content->widget);
719 XtVaSetValues(t->current->tab_button, XmNset, 0, NULL);
721 XtManageChild(tab->content->widget);
723 ui_change_tab(t, tab);
727 void ui_change_tab(MotifTabbedPane *pane, UiTab *tab) {
728 UiContext *ctx = tab->content->ctx;
729 ctx->parent->detach_document2(ctx->parent, pane->current->content->ctx->document);
730 ctx->parent->attach_document(ctx->parent, ctx->document);
733 XtVaSetValues(pane->current->tab_button, XmNshadowThickness, 0, XmNbackground, pane->bg1, NULL);
735 XtVaSetValues(tab->tab_button, XmNshadowThickness, 1, XmNbackground, pane->bg2, NULL);
738 pane->index = ucx_list_find(pane->tabs, tab, NULL, NULL);
739 printf("index: %d\n", pane->index);
742 Display *dpy = XtDisplay(pane->tabbar);
743 Window window = XtWindow(pane->tabbar);
745 XClearArea(dpy, XtWindow(pane->tabbar), 0, 0, 0, 0, TRUE);
750 void ui_tab_set_document(UiContext *ctx, void *document) {
751 if(ctx->parent->document) {
752 //ctx->parent->detach_document(ctx->parent, ctx->parent->document);
754 uic_context_attach_document(ctx, document);
755 //uic_context_set_document(ctx->parent, document);
756 //ctx->parent->document = document;
765 if(tab->tabbedpane->current == tab) {
766 ctx->parent->attach_document(ctx->parent, ctx->document);
769 fprintf(stderr, "UiError: ui_bar_set_document: Cannot set document");
776 * -------------------- Layout Functions --------------------
778 * functions for setting layout attributes for the current container
782 void ui_layout_fill(UiObject *obj, UiBool fill) {
783 UiContainer *ct = uic_get_current_container(obj);
784 ct->layout.fill = ui_bool2lb(fill);
787 void ui_layout_hexpand(UiObject *obj, UiBool expand) {
788 UiContainer *ct = uic_get_current_container(obj);
789 ct->layout.hexpand = expand;
792 void ui_layout_vexpand(UiObject *obj, UiBool expand) {
793 UiContainer *ct = uic_get_current_container(obj);
794 ct->layout.vexpand = expand;
797 void ui_layout_gridwidth(UiObject *obj, int width) {
798 UiContainer *ct = uic_get_current_container(obj);
799 ct->layout.gridwidth = width;
802 void ui_newline(UiObject *obj) {
803 UiContainer *ct = uic_get_current_container(obj);
804 ct->layout.newline = TRUE;