ui/motif/tree.c

changeset 39
4e66271541e8
child 43
157a21a914ac
equal deleted inserted replaced
38:8ccdde37275b 39:4e66271541e8
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 <string.h>
32
33 #include "tree.h"
34
35 #include "container.h"
36 #include "../common/object.h"
37 #include "../../ucx/utils.h"
38
39 UIWIDGET ui_table(UiObject *obj, UiList *model, UiModelInfo *modelinfo) {
40 // TODO: check if modelinfo is complete
41
42 Arg args[16];
43 int n = 0;
44
45 // create table headers
46 XmStringTable header = (XmStringTable)XtMalloc(
47 modelinfo->columns * sizeof(XmString));
48 for(int i=0;i<modelinfo->columns;i++) {
49 header[i] = XmStringCreateLocalized(modelinfo->titles[i]);
50 }
51 XtSetArg(args[n], XmNdetailColumnHeading, header);
52 n++;
53 XtSetArg(args[n], XmNdetailColumnHeadingCount, modelinfo->columns);
54 n++;
55
56 // set res
57 XtSetArg(args[n], XmNlayoutType, XmDETAIL);
58 n++;
59 XtSetArg(args[n], XmNentryViewType, XmSMALL_ICON);
60 n++;
61 XtSetArg(args[n], XmNselectionPolicy, XmSINGLE_SELECT);
62 n++;
63
64 // create widget
65 UiContainer *ct = uic_get_current_container(obj);
66 Widget parent = ct->add(ct, args, &n);
67
68 Widget container = XmCreateContainer(parent, "table", args, n);
69 XtManageChild(container);
70
71 // add initial data
72 void *data = model->first(model);
73 while(data) {
74 ui_add_icon_gadget(container, modelinfo, data);
75 data = model->next(model);
76 }
77
78 // cleanup
79 for(int i=0;i<modelinfo->columns;i++) {
80 XmStringFree(header[i]);
81 }
82 XtFree((char*)header);
83
84 return container;
85 }
86
87 void ui_add_icon_gadget(Widget container, UiModelInfo *modelinfo, void *data) {
88 if(modelinfo->columns == 0) {
89 return;
90 }
91
92 XmString label = NULL;
93 Arg args[8];
94 Boolean f;
95 // first column
96 if(modelinfo->types[0] != 12345678) { // TODO: icon/label type
97 char *str = ui_type_to_string(
98 modelinfo->types[0],
99 modelinfo->getvalue(data, 0),
100 &f);
101 XmString label = XmStringCreateLocalized(str);
102 XtSetArg(args[0], XmNlabelString, label);
103 if(f) {
104 free(str);
105 }
106 } else {
107 // TODO
108 }
109
110 // remaining columns are the icon gadget details
111 XmStringTable details = (XmStringTable)XtMalloc(
112 (modelinfo->columns - 1) * sizeof(XmString));
113 for(int i=1;i<modelinfo->columns;i++) {
114 char *str = ui_type_to_string(
115 modelinfo->types[i],
116 modelinfo->getvalue(data, i),
117 &f);
118 details[i - 1] = XmStringCreateLocalized(str);
119 if(f) {
120 free(str);
121 }
122 }
123 XtSetArg(args[1], XmNdetail, details);
124 XtSetArg(args[2], XmNdetailCount, modelinfo->columns - 1);
125
126 // create widget
127 Widget item = XmCreateIconGadget(container, "table_item", args, 3);
128 XtManageChild(item);
129
130 // cleanup
131 XmStringFree(label);
132 for(int i=0;i<modelinfo->columns-1;i++) {
133 XmStringFree(details[i]);
134 }
135 XtFree((char*)details);
136 }
137
138 char* ui_type_to_string(UiModelType type, void *data, Boolean *free) {
139 switch(type) {
140 case UI_STRING: *free = FALSE; return data;
141 case UI_INTEGER: {
142 *free = TRUE;
143 int *val = data;
144 sstr_t str = ucx_asprintf(ucx_default_allocator(), "%d", *val);
145 return str.ptr;
146 }
147 }
148 *free = FALSE;
149 return NULL;
150 }

mercurial