add existing code (build system, libs, initial mizucp code)
[mizunara.git] / ui / motif / container.c
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2014 Olaf Wintermann. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <inttypes.h>
32
33 #include "container.h"
34 #include "../common/context.h"
35 #include "../common/object.h"
36
37 #define UI_GRID_MAX_COLUMNS 512
38
39 static UiBool ui_lb2bool(UiLayoutBool b) {
40     return b == UI_LAYOUT_TRUE ? TRUE : FALSE;
41 }
42
43 static UiLayoutBool ui_bool2lb(UiBool b) {
44     return b ? UI_LAYOUT_TRUE : UI_LAYOUT_FALSE;
45 }
46
47
48 UiContainer* ui_frame_container(UiObject *obj, Widget frame) {
49     UiContainer *ct = ucx_mempool_calloc(
50             obj->ctx->mempool,
51             1,
52             sizeof(UiContainer));
53     ct->widget = frame;
54     ct->prepare = ui_frame_container_prepare;
55     ct->add = ui_frame_container_add;
56     return ct;
57 }
58
59 Widget ui_frame_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill) {
60     return ct->widget;
61 }
62
63 void ui_frame_container_add(UiContainer *ct, Widget widget) {
64     ui_reset_layout(ct->layout);
65     ct->current = widget;
66 }
67
68
69 UiContainer* ui_box_container(UiObject *obj, Widget box, int margin, int spacing, UiBoxOrientation orientation) {
70     UiBoxContainer *ct = ucx_mempool_calloc(
71             obj->ctx->mempool,
72             1,
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;
78     ct->margin = margin;
79     ct->spacing = spacing;
80     return (UiContainer*)ct;
81 }
82
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);
87     }
88     
89     if(bc->has_fill && fill) {
90         fprintf(stderr, "UiError: container has 2 filled widgets");
91         fill = FALSE;
92     }
93     if(fill) {
94         bc->has_fill = TRUE;
95     }
96     
97     int a = *n;
98     // determine fixed and dynamic attachments
99     void *f1;
100     void *f2;
101     void *d1;
102     void *d2;
103     void *w1;
104     void *w2;
105     if(bc->orientation == UI_BOX_VERTICAL) {
106         f1 = XmNleftAttachment;
107         f2 = XmNrightAttachment;
108         d1 = XmNtopAttachment;
109         d2 = XmNbottomAttachment;
110         w1 = XmNtopWidget;
111         w2 = XmNbottomWidget;
112         
113         // margin/spacing
114         XtSetArg(args[a], XmNleftOffset, bc->margin); a++;
115         XtSetArg(args[a], XmNrightOffset, bc->margin); a++;
116         
117         XtSetArg(args[a], XmNtopOffset, bc->prev_widget ? bc->spacing : bc->margin); a++;
118     } else {
119         f1 = XmNtopAttachment;
120         f2 = XmNbottomAttachment;
121         d1 = XmNleftAttachment;
122         d2 = XmNrightAttachment;
123         w1 = XmNleftWidget;
124         w2 = XmNrightWidget;
125         
126         // margin/spacing
127         XtSetArg(args[a], XmNtopOffset, bc->margin); a++;
128         XtSetArg(args[a], XmNbottomOffset, bc->margin); a++;
129         
130         XtSetArg(args[a], XmNleftOffset, bc->prev_widget ? bc->spacing : bc->margin); a++;
131     }
132     XtSetArg(args[a], f1, XmATTACH_FORM); a++;
133     XtSetArg(args[a], f2, XmATTACH_FORM); a++;
134
135     if(fill) {
136         XtSetArg(args[a], d2, XmATTACH_FORM); a++;
137     }
138     if(bc->prev_widget) {
139         XtSetArg(args[a], d1, XmATTACH_WIDGET); a++;
140         XtSetArg(args[a], w1, bc->prev_widget); a++;
141     } else {
142         XtSetArg(args[a], d1, XmATTACH_FORM); a++;
143     }
144     
145     *n = a;
146     return ct->widget;
147 }
148
149 void ui_box_container_add(UiContainer *ct, Widget widget) {
150     UiBoxContainer *bc = (UiBoxContainer*)ct;
151     // determine dynamic attachments
152     void *d1;
153     void *d2;
154     void *w1;
155     void *w2;
156     if(bc->orientation == UI_BOX_VERTICAL) {
157         d1 = XmNtopAttachment;
158         d2 = XmNbottomAttachment;
159         w1 = XmNtopWidget;
160         w2 = XmNbottomWidget;
161         
162     } else {
163         d1 = XmNleftAttachment;
164         d2 = XmNrightAttachment;
165         w1 = XmNleftWidget;
166         w2 = XmNrightWidget;
167     }
168     
169     if(bc->prev_widget) {
170         int v = 0;
171         XtVaGetValues(bc->prev_widget, d2, &v, NULL);
172         if(v == XmATTACH_FORM) {
173             XtVaSetValues(
174                     bc->prev_widget,
175                     d2,
176                     XmATTACH_WIDGET,
177                     w2,
178                     widget,
179                     NULL);
180             XtVaSetValues(
181                     widget,
182                     d1,
183                     XmATTACH_NONE,
184                     d2,
185                     XmATTACH_FORM,
186                     NULL);
187         }
188     }
189     bc->prev_widget = widget;
190     
191     ui_reset_layout(ct->layout);
192     ct->current = widget;
193 }
194
195 UiContainer* ui_grid_container(UiObject *obj, Widget form, int columnspacing, int rowspacing) {
196     UiGridContainer *ct = ucx_mempool_calloc(
197             obj->ctx->mempool,
198             1,
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;
206 }
207
208 void ui_grid_newline(UiGridContainer *grid) {
209     if(grid->current) {
210         grid->current = NULL;
211     }
212     grid->container.layout.newline = FALSE;
213 }
214
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);
219     }
220     return ct->widget;
221 }
222
223 void ui_grid_container_add(UiContainer *ct, Widget widget) {
224     UiGridContainer *grid = (UiGridContainer*)ct;
225     
226     if(grid->current) {
227         grid->current = ucx_list_append(grid->current, widget);
228     } else {
229         grid->current = ucx_list_append(grid->current, widget);
230         grid->lines = ucx_list_append(grid->lines, grid->current);
231     }
232     
233     ui_reset_layout(ct->layout);
234     ct->current = widget;
235 }
236
237 static void ui_grid_resize(Widget widget, XtPointer udata, XtPointer cdata) {
238     UiGridContainer *grid = udata;
239     
240     UcxList *rowdim = NULL;
241     int coldim[UI_GRID_MAX_COLUMNS];
242     memset(coldim, 0, UI_GRID_MAX_COLUMNS*sizeof(int));
243     int numcol = 0;
244     
245     // get the minimum size of the columns and rows
246     int sumw = 0;
247     int sumh = 0;
248     UCX_FOREACH(row, grid->lines) {
249         int rheight = 0;
250         int i=0;
251         int sum_width = 0;
252         UCX_FOREACH(elm, row->data) {
253             Widget w = elm->data;
254             int widget_width = 0;
255             int widget_height = 0;
256             XtVaGetValues(
257                     w,
258                     XmNwidth,
259                     &widget_width,
260                     XmNheight,
261                     &widget_height, 
262                     NULL);
263             
264             // get the maximum height in this row
265             if(widget_height > rheight) {
266                 rheight = widget_height;
267             }
268             
269             // get the maximum width in this column
270             if(widget_width > coldim[i]) {
271                 coldim[i] = widget_width;
272             }
273             sum_width += widget_width;
274             if(sum_width > sumw) {
275                 sumw = sum_width;
276             }
277             
278             i++;
279             if(i > numcol) {
280                 numcol = i;
281             }
282         }
283         rowdim = ucx_list_append(rowdim, (void*)(intptr_t)rheight);
284         sumh += rheight;
285     }
286     
287     // check container size
288     int gwidth = 0;
289     int gheight = 0;
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);
294         return;
295     }
296     
297     
298     // adjust the positions of all children
299     int y = 0;
300     UCX_FOREACH(row, grid->lines) {
301         int x = 0;       
302         int i=0;
303         UCX_FOREACH(elm, row->data) {
304             Widget w = elm->data;
305             XtVaSetValues(
306                     w,
307                     XmNx, x,
308                     XmNy, y,
309                     XmNwidth, coldim[i],
310                     XmNheight, rowdim->data,
311                     NULL);
312             
313             x += coldim[i];
314             i++;
315         }
316         y += (intptr_t)rowdim->data;
317         rowdim = rowdim->next;
318     }
319     
320     ucx_list_free(rowdim);
321 }
322
323 UiContainer* ui_scrolledwindow_container(UiObject *obj, Widget scrolledwindow) {
324     UiContainer *ct = ucx_mempool_calloc(
325             obj->ctx->mempool,
326             1,
327             sizeof(UiContainer));
328     ct->widget = scrolledwindow;
329     ct->prepare = ui_scrolledwindow_container_prepare;
330     ct->add = ui_scrolledwindow_container_add;
331     return ct;
332 }
333
334 Widget ui_scrolledwindow_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill) {
335     return ct->widget;
336 }
337
338 void ui_scrolledwindow_container_add(UiContainer *ct, Widget widget) {
339     ui_reset_layout(ct->layout);
340     ct->current = widget;
341 }
342
343
344 UiContainer* ui_tabview_container(UiObject *obj, Widget frame) {
345     UiTabViewContainer *ct = ucx_mempool_calloc(
346             obj->ctx->mempool,
347             1,
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;
354 }
355
356 Widget ui_tabview_container_prepare(UiContainer *ct, Arg *args, int *n, UiBool fill) {
357     int a = *n;
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++;
362     *n = a;
363     return ct->widget;
364 }
365
366 void ui_tabview_container_add(UiContainer *ct, Widget widget) {
367     UiTabViewContainer *tabview = (UiTabViewContainer*)ct; 
368     
369     if(tabview->current) {
370         XtUnmanageChild(tabview->current);
371     }
372
373     tabview->current = widget;
374     tabview->tabs = ucx_list_append(tabview->tabs, widget);
375     
376     ui_select_tab(ct->widget, 0);
377     ui_reset_layout(ct->layout);
378     ct->current = widget;
379 }
380
381 UIWIDGET ui_box(UiObject *obj, int margin, int spacing, UiBoxOrientation orientation) {
382     UiContainer *ct = uic_get_current_container(obj);
383     
384     Arg args[16];
385     int n = 0;
386     Widget parent = ct->prepare(ct, args, &n, TRUE);
387     Widget form = XmCreateForm(parent, "vbox", args, n);
388     ct->add(ct, form);
389     XtManageChild(form);
390     
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);
394     
395     return form;
396 }
397
398 UIWIDGET ui_vbox(UiObject *obj) {
399     return ui_box(obj, 0, 0, UI_BOX_VERTICAL);
400 }
401
402 UIWIDGET ui_hbox(UiObject *obj) {
403     return ui_box(obj, 0, 0, UI_BOX_HORIZONTAL);
404 }
405
406 UIWIDGET ui_vbox_sp(UiObject *obj, int margin, int spacing) {
407     return ui_box(obj, margin, spacing, UI_BOX_VERTICAL);
408 }
409
410 UIWIDGET ui_hbox_sp(UiObject *obj, int margin, int spacing) {
411     return ui_box(obj, margin, spacing, UI_BOX_HORIZONTAL);
412 }
413
414 UIWIDGET ui_grid(UiObject *obj) {
415     return ui_grid_sp(obj, 0, 0, 0);
416 }
417
418 UIWIDGET ui_grid_sp(UiObject *obj, int margin, int columnspacing, int rowspacing) {
419     UiContainer *ct = uic_get_current_container(obj);
420     
421     Arg args[16];
422     int n = 0;
423     Widget parent = ct->prepare(ct, args, &n, TRUE);
424     Widget grid = XmCreateDrawingArea(parent, "grid", args, n);
425     ct->add(ct, grid);
426     XtManageChild(grid);
427     
428     UiObject *newobj = uic_object_new(obj, grid);
429     newobj->container = ui_grid_container(obj, grid, columnspacing, rowspacing);
430     uic_obj_add(obj, newobj);
431     
432     XtAddCallback (grid, XmNresizeCallback , ui_grid_resize, newobj->container);
433     
434     return grid;
435 }
436
437 UIWIDGET ui_scrolledwindow(UiObject *obj) {
438     UiContainer *ct = uic_get_current_container(obj);
439     
440     Arg args[16];
441     int n = 0;
442     XtSetArg(args[n], XmNscrollingPolicy, XmAUTOMATIC); // TODO: dosn't work, use XmAPPLICATION_DEFINED
443     n++;
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);
448     
449     UiObject *newobj = uic_object_new(obj, scrolledwindow);
450     newobj->container = ui_scrolledwindow_container(obj, scrolledwindow);
451     uic_obj_add(obj, newobj);
452     
453     return scrolledwindow;
454 }
455
456 UIWIDGET ui_sidebar(UiObject *obj) {
457     UiContainer *ct = uic_get_current_container(obj);
458     
459     Arg args[16];
460     int n = 0;
461     
462     XtSetArg(args[n], XmNorientation, XmHORIZONTAL);
463     n++;
464     
465     Widget parent = ct->prepare(ct, args, &n, TRUE);
466     Widget pane = XmCreatePanedWindow(parent, "pane", args, n);
467     ct->add(ct, pane);
468     XtManageChild(pane);
469     
470     // add sidebar widget
471     Widget sidebar = XmCreateForm(pane, "sidebar", args, 0);
472     XtManageChild(sidebar);
473     
474     UiObject *left = uic_object_new(obj, sidebar);
475     left->container = ui_box_container(left, sidebar, 0, 0, UI_BOX_VERTICAL);
476     
477     // add content widget
478     XtSetArg (args[0], XmNpaneMaximum, 8000);
479     Widget content = XmCreateForm(pane, "content_area", args, 1);
480     XtManageChild(content);
481     
482     UiObject *right = uic_object_new(obj, content);
483     right->container = ui_box_container(right, content, 0, 0, UI_BOX_VERTICAL);
484     
485     uic_obj_add(obj, right);
486     uic_obj_add(obj, left);
487     
488     return sidebar;
489 }
490
491 UIWIDGET ui_tabview(UiObject *obj) {
492     UiContainer *ct = uic_get_current_container(obj);
493     
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
497     Arg args[16];
498     int n = 0;
499     XtSetArg(args[n], XmNshadowType, XmSHADOW_ETCHED_OUT);
500     n++;
501     XtSetArg(args[n], XmNshadowThickness, 0);
502     n++;
503     Widget parent = ct->prepare(ct, args, &n, TRUE);
504     Widget form = XmCreateForm(parent, "tabview", args, n);
505     ct->add(ct, form);
506     XtManageChild(form);
507     
508     UiObject *tabviewobj = uic_object_new(obj, form);
509     tabviewobj->container = ui_tabview_container(obj, form);
510     uic_obj_add(obj, tabviewobj);
511     
512     XtVaSetValues(form, XmNuserData, tabviewobj->container, NULL);
513     
514     return form;
515 }
516
517 void ui_tab(UiObject *obj, char *title) {
518     UiContainer *ct = uic_get_current_container(obj);
519     ct->layout.label = title;
520     
521     ui_vbox(obj);
522 }
523
524 void ui_select_tab(UIWIDGET tabview, int tab) {
525     UiTabViewContainer *ct = NULL;
526     XtVaGetValues(tabview, XmNuserData, &ct, NULL);
527     if(ct) {
528         XtUnmanageChild(ct->current);
529         UcxList *elm = ucx_list_get(ct->tabs, tab);
530         if(elm) {
531             XtManageChild(elm->data);
532             ct->current = elm->data;
533         } else {
534             fprintf(stderr, "UiError: front tab index: %d\n", tab);
535         }
536     } else {
537         fprintf(stderr, "UiError: widget is not a tabview\n");
538     }
539 }
540
541
542 /* document tabview */
543
544 static void ui_tabbar_resize(Widget widget, XtPointer udata, XtPointer cdata) {
545     MotifTabbedPane *v = (MotifTabbedPane*)udata;
546     
547     int width = 0;
548     int height = 0;
549     XtVaGetValues(widget, XmNwidth, &width, XmNheight, &height, NULL);
550     int button_width = width / 4;
551     int x = 0;
552     UCX_FOREACH(elm, v->tabs) {
553         UiTab *tab = elm->data;
554         XtVaSetValues(
555                 tab->tab_button,
556                 XmNx, x,
557                 XmNy, 0,
558                 XmNwidth,
559                 button_width,
560                 
561                 NULL);
562         x += button_width;
563     }
564     
565     if(height <= v->height) {
566         XtVaSetValues(widget, XmNheight, v->height + 4, NULL);
567     }
568 }
569
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); 
575     
576     XGCValues gcvals;
577     GC gc;
578     Pixel fgpix;
579     
580     int tab_x;
581     int tab_width;
582     XtVaGetValues(v->current->tab_button, XmNx, &tab_x, XmNwidth, &tab_width, XmNhighlightColor, &fgpix, NULL);
583     
584     gcvals.foreground = v->bg1;
585     gc = XCreateGC( dpy, XtWindow(widget), (GCForeground), &gcvals);
586       
587     int width = 0;
588     int height = 0;
589     XtVaGetValues(widget, XmNwidth, &width, XmNheight, &height, NULL);
590     XFillRectangle(dpy, XtWindow(widget), gc, 0, 0, width, height);
591     
592     gcvals.foreground = fgpix;
593     gc = XCreateGC( dpy, XtWindow(widget), (GCForeground), &gcvals);
594     
595     XFillRectangle(dpy, XtWindow(widget), gc, tab_x, 0, tab_width, height);
596     
597 }
598
599 UiTabbedPane* ui_tabbed_document_view(UiObject *obj) {
600     int n = 0;
601     Arg args[16];
602     
603     UiContainer *ct = uic_get_current_container(obj);
604     Widget parent = ct->prepare(ct, args, &n, TRUE);
605     
606     Widget tabview = XmCreateForm(parent, "tabview_form", args, n);
607     XtManageChild(tabview);
608     
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);
619     
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);
628     
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;
637     
638     XtAddCallback(tabbar, XmNresizeCallback , ui_tabbar_resize, tabbedpane);
639     XtAddCallback(tabbar, XmNexposeCallback, ui_tabbar_expose, tabbedpane);
640     
641     return &tabbedpane->view;
642 }
643
644 UiObject* ui_document_tab(UiTabbedPane *view) {
645     MotifTabbedPane *v = (MotifTabbedPane*)view;
646     int n = 0;
647     Arg args[16];
648     
649     // hide the current tab content
650     if(v->current) {
651         XtUnmanageChild(v->current->content->widget);
652     }
653     
654     UiTab *tab = ui_malloc(view->ctx, sizeof(UiTab));
655     
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);
665     
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;
676     
677     // add tab button
678     v->tabs = ucx_list_append_a(view->ctx->mempool->allocator, v->tabs, tab);
679     
680     XmString label = XmStringCreateLocalized("tab");
681     XtSetArg(args[0], XmNlabelString, label);
682     XtSetArg(args[1], XmNshadowThickness, 0);
683     XtSetArg(args[2], XmNhighlightThickness, 0);
684     
685     Widget button = XmCreatePushButton(v->tabbar, "tab_button", args, 3);
686     tab->tabbedpane = v;
687     tab->content = content;
688     tab->tab_button = button; 
689     XtManageChild(button);
690     XtAddCallback(
691         button,
692         XmNactivateCallback,
693         (XtCallbackProc)ui_tab_button_callback,
694         tab);
695     
696     if(v->height == 0) {
697         XtVaGetValues(
698                 button,
699                 XmNarmColor,
700                 &v->bg1,
701                 XmNbackground,
702                 &v->bg2,
703                 XmNheight,
704                 &v->height,
705                 NULL);
706         v->height += 2; // border
707     }
708     
709     ui_change_tab(v, tab);
710     ui_tabbar_resize(v->tabbar, v, NULL);
711     
712     return content;
713 }
714
715 void ui_tab_button_callback(Widget widget, UiTab *tab, XtPointer d) {  
716     MotifTabbedPane *t = tab->tabbedpane;
717     if(t->current) {
718         XtUnmanageChild(t->current->content->widget);
719         XtVaSetValues(t->current->tab_button, XmNset, 0, NULL);
720     }
721     XtManageChild(tab->content->widget);
722     
723     ui_change_tab(t, tab);
724     
725 }
726
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);
731     
732     if(pane->current) {
733         XtVaSetValues(pane->current->tab_button, XmNshadowThickness, 0, XmNbackground, pane->bg1, NULL);
734     }
735     XtVaSetValues(tab->tab_button, XmNshadowThickness, 1, XmNbackground, pane->bg2, NULL);
736     
737     pane->current = tab;
738     pane->index = ucx_list_find(pane->tabs, tab, NULL, NULL);
739     printf("index: %d\n", pane->index);
740     
741     // redraw tabbar
742     Display *dpy = XtDisplay(pane->tabbar);
743     Window window = XtWindow(pane->tabbar);
744     if(dpy && window) {
745         XClearArea(dpy, XtWindow(pane->tabbar), 0, 0, 0, 0, TRUE);
746         XFlush(dpy);
747     }
748 }
749
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);
753     }
754     uic_context_attach_document(ctx, document);
755     //uic_context_set_document(ctx->parent, document);
756     //ctx->parent->document = document;
757     
758     UiTab *tab = NULL;
759     XtVaGetValues(
760             ctx->obj->widget,
761             XmNuserData,
762             &tab,
763             NULL);
764     if(tab) {
765         if(tab->tabbedpane->current == tab) {
766             ctx->parent->attach_document(ctx->parent, ctx->document);
767         }
768     } else {
769         fprintf(stderr, "UiError: ui_bar_set_document: Cannot set document");
770     }
771 }
772
773
774
775 /*
776  * -------------------- Layout Functions --------------------
777  * 
778  * functions for setting layout attributes for the current container
779  *
780  */
781
782 void ui_layout_fill(UiObject *obj, UiBool fill) {
783     UiContainer *ct = uic_get_current_container(obj);
784     ct->layout.fill = ui_bool2lb(fill);
785 }
786
787 void ui_layout_hexpand(UiObject *obj, UiBool expand) {
788     UiContainer *ct = uic_get_current_container(obj);
789     ct->layout.hexpand = expand;
790 }
791
792 void ui_layout_vexpand(UiObject *obj, UiBool expand) {
793     UiContainer *ct = uic_get_current_container(obj);
794     ct->layout.vexpand = expand;
795 }
796
797 void ui_layout_gridwidth(UiObject *obj, int width) {
798     UiContainer *ct = uic_get_current_container(obj);
799     ct->layout.gridwidth = width;
800 }
801
802 void ui_newline(UiObject *obj) {
803     UiContainer *ct = uic_get_current_container(obj);
804     ct->layout.newline = TRUE;
805 }