ui/motif/container.c

changeset 431
bb7da585debc
parent 426
3eb26df703bf
equal deleted inserted replaced
169:fe49cff3c571 431:bb7da585debc
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2014 Olaf Wintermann. All rights reserved. 4 * Copyright 2024 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
32 32
33 #include "container.h" 33 #include "container.h"
34 #include "../common/context.h" 34 #include "../common/context.h"
35 #include "../common/object.h" 35 #include "../common/object.h"
36 36
37 #define UI_GRID_MAX_COLUMNS 512 37 #include "Grid.h"
38 38
39 static UiBool ui_lb2bool(UiLayoutBool b) { 39 /* ---------------------------- Box Container ---------------------------- */
40 return b == UI_LAYOUT_TRUE ? TRUE : FALSE; 40
41 } 41 static UIWIDGET box_create(UiObject *obj, UiContainerArgs args, UiBoxOrientation orientation) {
42 42 UiContainerPrivate *ctn = ui_obj_container(obj);
43 static UiLayoutBool ui_bool2lb(UiBool b) { 43 UI_APPLY_LAYOUT(ctn->layout, args);
44 return b ? UI_LAYOUT_TRUE : UI_LAYOUT_FALSE; 44
45 } 45 Arg xargs[16];
46 46 int n = 0;
47 47
48 UiContainer* ui_frame_container(UiObject *obj, Widget frame) { 48 if(orientation == UI_BOX_VERTICAL) {
49 UiContainer *ct = ucx_mempool_calloc( 49 //XtSetArg(xargs[n], gridRowSpacing, args.spacing); n++;
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 { 50 } else {
119 f1 = XmNtopAttachment; 51 //XtSetArg(xargs[n], gridColumnSpacing, args.spacing); n++;
120 f2 = XmNbottomAttachment; 52 }
121 d1 = XmNleftAttachment; 53
122 d2 = XmNrightAttachment; 54 Widget parent = ctn->prepare(ctn, xargs, &n);
123 w1 = XmNleftWidget; 55 Widget grid = XtCreateManagedWidget(args.name ? args.name : "boxcontainer", gridClass, parent, xargs, n);
124 w2 = XmNrightWidget; 56 ctn->add(ctn, grid);
125 57
126 // margin/spacing 58 UiContainerX *container = ui_box_container(obj, grid, orientation);
127 XtSetArg(args[a], XmNtopOffset, bc->margin); a++; 59 uic_object_push_container(obj, container);
128 XtSetArg(args[a], XmNbottomOffset, bc->margin); a++; 60
129 61 return grid;
130 XtSetArg(args[a], XmNleftOffset, bc->prev_widget ? bc->spacing : bc->margin); a++; 62 }
131 } 63
132 XtSetArg(args[a], f1, XmATTACH_FORM); a++; 64 // public
133 XtSetArg(args[a], f2, XmATTACH_FORM); a++; 65 UIWIDGET ui_vbox_create(UiObject *obj, UiContainerArgs args) {
134 66 return box_create(obj, args, UI_BOX_VERTICAL);
135 if(fill) { 67 }
136 XtSetArg(args[a], d2, XmATTACH_FORM); a++; 68
137 } 69 // public
138 if(bc->prev_widget) { 70 UIWIDGET ui_hbox_create(UiObject *obj, UiContainerArgs args) {
139 XtSetArg(args[a], d1, XmATTACH_WIDGET); a++; 71 return box_create(obj, args, UI_BOX_HORIZONTAL);
140 XtSetArg(args[a], w1, bc->prev_widget); a++; 72 }
141 } else { 73
142 XtSetArg(args[a], d1, XmATTACH_FORM); a++; 74 UiContainerX* ui_box_container(UiObject *obj, Widget grid, UiBoxOrientation orientation) {
143 } 75 UiBoxContainer *ctn = ui_malloc(obj->ctx, sizeof(UiBoxContainer));
144 76 memset(ctn, 0, sizeof(UiBoxContainer));
77 ctn->container.prepare = orientation == UI_BOX_VERTICAL ? ui_vbox_prepare : ui_hbox_prepare;
78 ctn->container.add = ui_box_container_add;
79 ctn->container.widget = grid;
80 ctn->n = 0;
81 return (UiContainerX*)ctn;
82 }
83
84 static Widget ui_box_container_prepare(UiBoxContainer *box, Arg *args, int *n) {
85 int a = *n;
86 box->n++;
87 return box->container.widget;
88 }
89
90 Widget ui_vbox_prepare(UiContainerPrivate *ctn, Arg *args, int *n) {
91 UiBoxContainer *box = (UiBoxContainer*)ctn;
92 int a = *n;
93 XtSetArg(args[a], gridRow, box->n); a++;
94 if(box->container.layout.fill == UI_ON) {
95 XtSetArg(args[a], gridVExpand, TRUE); a++;
96 XtSetArg(args[a], gridVFill, TRUE); a++;
97 }
98 XtSetArg(args[a], gridHExpand, TRUE); a++;
99 XtSetArg(args[a], gridHFill, TRUE); a++;
145 *n = a; 100 *n = a;
146 return ct->widget; 101 return ui_box_container_prepare(box, args, n);
147 } 102 }
148 103
149 void ui_box_container_add(UiContainer *ct, Widget widget) { 104 Widget ui_hbox_prepare(UiContainerPrivate *ctn, Arg *args, int *n) {
150 UiBoxContainer *bc = (UiBoxContainer*)ct; 105 UiBoxContainer *box = (UiBoxContainer*)ctn;
151 // determine dynamic attachments 106 int a = *n;
152 void *d1; 107 XtSetArg(args[a], gridColumn, box->n); a++;
153 void *d2; 108 if(box->container.layout.fill == UI_ON) {
154 void *w1; 109 XtSetArg(args[a], gridHExpand, TRUE); a++;
155 void *w2; 110 XtSetArg(args[a], gridHFill, TRUE); a++;
156 if(bc->orientation == UI_BOX_VERTICAL) { 111 }
157 d1 = XmNtopAttachment; 112 XtSetArg(args[a], gridVExpand, TRUE); a++;
158 d2 = XmNbottomAttachment; 113 XtSetArg(args[a], gridVFill, TRUE); a++;
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; 114 *n = a;
363 return ct->widget; 115 return ui_box_container_prepare(box, args, n);
364 } 116 }
365 117
366 void ui_tabview_container_add(UiContainer *ct, Widget widget) { 118 void ui_box_container_add(UiContainerPrivate *ctn, Widget widget) {
367 UiTabViewContainer *tabview = (UiTabViewContainer*)ct; 119 ui_reset_layout(ctn->layout);
368 120
369 if(tabview->current) { 121 }
370 XtUnmanageChild(tabview->current); 122
371 } 123
372 124 /* ---------------------------- Grid Container ---------------------------- */
373 tabview->current = widget; 125
374 tabview->tabs = ucx_list_append(tabview->tabs, widget); 126 // public
375 127 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs args) {
376 ui_select_tab(ct->widget, 0); 128 Arg xargs[16];
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; 129 int n = 0;
386 Widget parent = ct->prepare(ct, args, &n, TRUE); 130
387 Widget form = XmCreateForm(parent, "vbox", args, n); 131 UiContainerPrivate *ctn = ui_obj_container(obj);
388 ct->add(ct, form); 132 UI_APPLY_LAYOUT(ctn->layout, args);
389 XtManageChild(form); 133
390 134 Widget parent = ctn->prepare(ctn, xargs, &n);
391 UiObject *newobj = uic_object_new(obj, form); 135 XtSetArg(xargs[n], gridMargin, args.margin); n++;
392 newobj->container = ui_box_container(obj, form, margin, spacing, orientation); 136 XtSetArg(xargs[n], gridColumnSpacing, args.columnspacing); n++;
393 uic_obj_add(obj, newobj); 137 XtSetArg(xargs[n], gridRowSpacing, args.rowspacing); n++;
394 138 Widget grid = XtCreateManagedWidget(args.name ? args.name : "gridcontainer", gridClass, parent, xargs, n);
395 return form; 139 ctn->add(ctn, grid);
396 } 140
397 141 UiContainerX *container = ui_grid_container(obj, grid);
398 UIWIDGET ui_vbox(UiObject *obj) { 142 uic_object_push_container(obj, container);
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 143
434 return grid; 144 return grid;
435 } 145 }
436 146
437 UIWIDGET ui_scrolledwindow(UiObject *obj) { 147 UiContainerX* ui_grid_container(UiObject *obj, Widget grid) {
438 UiContainer *ct = uic_get_current_container(obj); 148 UiGridContainer *ctn = ui_malloc(obj->ctx, sizeof(UiGridContainer));
439 149 memset(ctn, 0, sizeof(UiBoxContainer));
440 Arg args[16]; 150 ctn->container.prepare = ui_grid_container_prepare;
441 int n = 0; 151 ctn->container.add = ui_grid_container_add;
442 XtSetArg(args[n], XmNscrollingPolicy, XmAUTOMATIC); // TODO: dosn't work, use XmAPPLICATION_DEFINED 152 ctn->container.widget = grid;
443 n++; 153 ctn->x = 0;
444 Widget parent = ct->prepare(ct, args, &n, TRUE); 154 ctn->y = 0;
445 Widget scrolledwindow = XmCreateScrolledWindow(parent, "scrolledwindow", args, n); 155 return (UiContainerX*)ctn;
446 ct->add(ct, scrolledwindow); 156 }
447 XtManageChild(scrolledwindow); 157
448 158 Widget ui_grid_container_prepare(UiContainerPrivate *ctn, Arg *args, int *n) {
449 UiObject *newobj = uic_object_new(obj, scrolledwindow); 159 UiGridContainer *grid = (UiGridContainer*)ctn;
450 newobj->container = ui_scrolledwindow_container(obj, scrolledwindow); 160 if(ctn->layout.newline) {
451 uic_obj_add(obj, newobj); 161 grid->y++;
452 162 grid->x = 0;
453 return scrolledwindow; 163 }
454 } 164
455 165 int a = *n;
456 UIWIDGET ui_sidebar(UiObject *obj) { 166 XtSetArg(args[a], gridColumn, grid->x); a++;
457 UiContainer *ct = uic_get_current_container(obj); 167 XtSetArg(args[a], gridRow, grid->y); a++;
458 168 if(ctn->layout.colspan > 0) {
459 Arg args[16]; 169 XtSetArg(args[a], gridColspan, ctn->layout.colspan); a++;
460 int n = 0; 170 }
461 171 if(ctn->layout.rowspan > 0) {
462 XtSetArg(args[n], XmNorientation, XmHORIZONTAL); 172 XtSetArg(args[a], gridRowspan, ctn->layout.rowspan); a++;
463 n++; 173 }
464 174
465 Widget parent = ct->prepare(ct, args, &n, TRUE); 175 if(grid->container.layout.fill == UI_ON) {
466 Widget pane = XmCreatePanedWindow(parent, "pane", args, n); 176 grid->container.layout.hfill = TRUE;
467 ct->add(ct, pane); 177 grid->container.layout.vfill = TRUE;
468 XtManageChild(pane); 178 grid->container.layout.hexpand = TRUE;
469 179 grid->container.layout.vexpand = TRUE;
470 // add sidebar widget 180 }
471 Widget sidebar = XmCreateForm(pane, "sidebar", args, 0); 181
472 XtManageChild(sidebar); 182 if(grid->container.layout.hfill) {
473 183 XtSetArg(args[a], gridHFill, TRUE); a++;
474 UiObject *left = uic_object_new(obj, sidebar); 184 }
475 left->container = ui_box_container(left, sidebar, 0, 0, UI_BOX_VERTICAL); 185 if(grid->container.layout.vfill) {
476 186 XtSetArg(args[a], gridVFill, TRUE); a++;
477 // add content widget 187 }
478 XtSetArg (args[0], XmNpaneMaximum, 8000); 188 if(grid->container.layout.hexpand) {
479 Widget content = XmCreateForm(pane, "content_area", args, 1); 189 XtSetArg(args[a], gridHExpand, TRUE); a++;
480 XtManageChild(content); 190 }
481 191 if(grid->container.layout.vexpand) {
482 UiObject *right = uic_object_new(obj, content); 192 XtSetArg(args[a], gridVExpand, TRUE); a++;
483 right->container = ui_box_container(right, content, 0, 0, UI_BOX_VERTICAL); 193 }
484 194
485 uic_obj_add(obj, right); 195 *n = a;
486 uic_obj_add(obj, left); 196 return ctn->widget;
487 197 }
488 return sidebar; 198
489 } 199 void ui_grid_container_add(UiContainerPrivate *ctn, Widget widget) {
490 200 UiGridContainer *grid = (UiGridContainer*)ctn;
491 UIWIDGET ui_tabview(UiObject *obj) { 201 grid->x++;
492 UiContainer *ct = uic_get_current_container(obj); 202 ui_reset_layout(ctn->layout);
493 203 }
494 // create a simple frame as container widget 204
495 // when tabs are selected, the current child will be replaced by the 205
496 // the new tab widget 206 /* -------------------- Container Helper Functions -------------------- */
497 Arg args[16]; 207
498 int n = 0; 208 void ui_container_begin_close(UiObject *obj) {
499 XtSetArg(args[n], XmNshadowType, XmSHADOW_ETCHED_OUT); 209 UiContainerPrivate *ct = ui_obj_container(obj);
500 n++; 210 ct->container.close = 1;
501 XtSetArg(args[n], XmNshadowThickness, 0); 211 }
502 n++; 212
503 Widget parent = ct->prepare(ct, args, &n, TRUE); 213 int ui_container_finish(UiObject *obj) {
504 Widget form = XmCreateForm(parent, "tabview", args, n); 214 UiContainerPrivate *ct = ui_obj_container(obj);
505 ct->add(ct, form); 215 if(ct->container.close) {
506 XtManageChild(form); 216 ui_end_new(obj);
507 217 return 0;
508 UiObject *tabviewobj = uic_object_new(obj, form); 218 }
509 tabviewobj->container = ui_tabview_container(obj, form); 219 return 1;
510 uic_obj_add(obj, tabviewobj); 220 }
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 221
774 222
775 /* 223 /*
776 * -------------------- Layout Functions -------------------- 224 * -------------------- Layout Functions --------------------
777 * 225 *
778 * functions for setting layout attributes for the current container 226 * functions for setting layout attributes for the current container
779 * 227 *
780 */ 228 */
781 229
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) { 230 void ui_newline(UiObject *obj) {
803 UiContainer *ct = uic_get_current_container(obj); 231 UiContainerPrivate *ct = ui_obj_container(obj);
804 ct->layout.newline = TRUE; 232 ct->layout.newline = TRUE;
805 } 233 }

mercurial