ui/motif/tree.c

changeset 45
cfeb2d5f1332
parent 44
a1571777eff2
child 52
25e5390cce41
equal deleted inserted replaced
44:a1571777eff2 45:cfeb2d5f1332
41 // TODO: check if modelinfo is complete 41 // TODO: check if modelinfo is complete
42 42
43 Arg args[16]; 43 Arg args[16];
44 int n = 0; 44 int n = 0;
45 45
46 // create scrolled window
47 UiContainer *ct = uic_get_current_container(obj);
48 Widget parent = ct->add(ct, args, &n);
49
50 XtSetArg(args[n], XmNscrollingPolicy, XmAUTOMATIC);
51 n++;
52 XtSetArg(args[n], XmNshadowThickness, 0);
53 n++;
54 Widget scrollw = XmCreateScrolledWindow(parent, "scroll_win", args, n);
55 XtManageChild(scrollw);
56
46 // create table headers 57 // create table headers
47 XmStringTable header = (XmStringTable)XtMalloc( 58 XmStringTable header = (XmStringTable)XtMalloc(
48 modelinfo->columns * sizeof(XmString)); 59 modelinfo->columns * sizeof(XmString));
49 for(int i=0;i<modelinfo->columns;i++) { 60 for(int i=0;i<modelinfo->columns;i++) {
50 header[i] = XmStringCreateLocalized(modelinfo->titles[i]); 61 header[i] = XmStringCreateLocalized(modelinfo->titles[i]);
51 } 62 }
63 n = 0;
52 XtSetArg(args[n], XmNdetailColumnHeading, header); 64 XtSetArg(args[n], XmNdetailColumnHeading, header);
53 n++; 65 n++;
54 XtSetArg(args[n], XmNdetailColumnHeadingCount, modelinfo->columns); 66 XtSetArg(args[n], XmNdetailColumnHeadingCount, modelinfo->columns);
55 n++; 67 n++;
56 68
59 n++; 71 n++;
60 XtSetArg(args[n], XmNentryViewType, XmSMALL_ICON); 72 XtSetArg(args[n], XmNentryViewType, XmSMALL_ICON);
61 n++; 73 n++;
62 XtSetArg(args[n], XmNselectionPolicy, XmSINGLE_SELECT); 74 XtSetArg(args[n], XmNselectionPolicy, XmSINGLE_SELECT);
63 n++; 75 n++;
76 XtSetArg(args[n], XmNwidth, 600);
77 n++;
64 78
65 // create widget 79 // create widget
66 UiContainer *ct = uic_get_current_container(obj); 80 //UiContainer *ct = uic_get_current_container(obj);
67 Widget parent = ct->add(ct, args, &n); 81 //Widget parent = ct->add(ct, args, &n);
68 82
69 Widget container = XmCreateContainer(parent, "table", args, n); 83 Widget container = XmCreateContainer(scrollw, "table", args, n);
70 XtManageChild(container); 84 XtManageChild(container);
71 85
72 // add callbacks 86 // add callbacks
73 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData)); 87 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
74 event->obj = obj; 88 event->obj = obj;
91 event); 105 event);
92 } 106 }
93 107
94 // add initial data 108 // add initial data
95 void *data = model->first(model); 109 void *data = model->first(model);
96 int i = 0; 110 int width = 0;
97 while(data) { 111 while(data) {
98 ui_add_icon_gadget(container, modelinfo, data, i); 112 int w = ui_add_icon_gadget(container, modelinfo, data);
113 if(w > width) {
114 width = w;
115 }
99 data = model->next(model); 116 data = model->next(model);
100 i++; 117 }
101 } 118
119 // set new XmContainer width
120 XtVaSetValues(container, XmNwidth, width, NULL);
102 121
103 // cleanup 122 // cleanup
104 for(int i=0;i<modelinfo->columns;i++) { 123 for(int i=0;i<modelinfo->columns;i++) {
105 XmStringFree(header[i]); 124 XmStringFree(header[i]);
106 } 125 }
107 XtFree((char*)header); 126 XtFree((char*)header);
108 127
109 return container; 128 return scrollw;
110 } 129 }
111 130
112 void ui_add_icon_gadget( 131 #define UI_COL_CHAR_WIDTH 12
113 Widget container, 132
114 UiModelInfo *modelinfo, 133 int ui_add_icon_gadget(Widget container, UiModelInfo *modelinfo, void *data) {
115 void *data, 134 int width = 50;
116 int index) 135
117 {
118 if(modelinfo->columns == 0) { 136 if(modelinfo->columns == 0) {
119 return; 137 return width;
120 } 138 }
121 139
122 XmString label = NULL; 140 XmString label = NULL;
123 Arg args[8]; 141 Arg args[8];
124 Boolean f; 142 Boolean f;
126 if(modelinfo->types[0] != 12345678) { // TODO: icon/label type 144 if(modelinfo->types[0] != 12345678) { // TODO: icon/label type
127 char *str = ui_type_to_string( 145 char *str = ui_type_to_string(
128 modelinfo->types[0], 146 modelinfo->types[0],
129 modelinfo->getvalue(data, 0), 147 modelinfo->getvalue(data, 0),
130 &f); 148 &f);
149
150 // column width
151 width += strlen(str) * UI_COL_CHAR_WIDTH;
152
153
131 XmString label = XmStringCreateLocalized(str); 154 XmString label = XmStringCreateLocalized(str);
132 XtSetArg(args[0], XmNlabelString, label); 155 XtSetArg(args[0], XmNlabelString, label);
133 if(f) { 156 if(f) {
134 free(str); 157 free(str);
135 } 158 }
143 for(int i=1;i<modelinfo->columns;i++) { 166 for(int i=1;i<modelinfo->columns;i++) {
144 char *str = ui_type_to_string( 167 char *str = ui_type_to_string(
145 modelinfo->types[i], 168 modelinfo->types[i],
146 modelinfo->getvalue(data, i), 169 modelinfo->getvalue(data, i),
147 &f); 170 &f);
171
172 // column width
173 width += strlen(str) * UI_COL_CHAR_WIDTH;
174
148 details[i - 1] = XmStringCreateLocalized(str); 175 details[i - 1] = XmStringCreateLocalized(str);
149 if(f) { 176 if(f) {
150 free(str); 177 free(str);
151 } 178 }
152 } 179 }
153 XtSetArg(args[1], XmNdetail, details); 180 XtSetArg(args[1], XmNdetail, details);
154 XtSetArg(args[2], XmNdetailCount, modelinfo->columns - 1); 181 XtSetArg(args[2], XmNdetailCount, modelinfo->columns - 1);
155 XtSetArg(args[3], XmNshadowThickness, 0); 182 XtSetArg(args[3], XmNshadowThickness, 0);
156 intptr_t userdata = index;
157 XtSetArg(args[4], XmNuserData, userdata);
158 // create widget 183 // create widget
159 Widget item = XmCreateIconGadget(container, "table_item", args, 5); 184 Widget item = XmCreateIconGadget(container, "table_item", args, 4);
160 XtManageChild(item); 185 XtManageChild(item);
161 186
162 // cleanup 187 // cleanup
163 XmStringFree(label); 188 XmStringFree(label);
164 for(int i=0;i<modelinfo->columns-1;i++) { 189 for(int i=0;i<modelinfo->columns-1;i++) {
165 XmStringFree(details[i]); 190 XmStringFree(details[i]);
166 } 191 }
167 XtFree((char*)details); 192 XtFree((char*)details);
193
194 return width;
168 } 195 }
169 196
170 char* ui_type_to_string(UiModelType type, void *data, Boolean *free) { 197 char* ui_type_to_string(UiModelType type, void *data, Boolean *free) {
171 switch(type) { 198 switch(type) {
172 case UI_STRING: *free = FALSE; return data; 199 case UI_STRING: *free = FALSE; return data;
226 UiListSelection* ui_list_selection(XmContainerSelectCallbackStruct *xs) { 253 UiListSelection* ui_list_selection(XmContainerSelectCallbackStruct *xs) {
227 UiListSelection *selection = malloc(sizeof(UiListSelection)); 254 UiListSelection *selection = malloc(sizeof(UiListSelection));
228 selection->count = xs->selected_item_count; 255 selection->count = xs->selected_item_count;
229 selection->rows = calloc(selection->count, sizeof(int)); 256 selection->rows = calloc(selection->count, sizeof(int));
230 for(int i=0;i<selection->count;i++) { 257 for(int i=0;i<selection->count;i++) {
231 intptr_t index; 258 int index;
232 XtVaGetValues(xs->selected_items[i], XmNuserData, &index, NULL); 259 XtVaGetValues(xs->selected_items[i], XmNpositionIndex, &index, NULL);
233 selection->rows[i] = index; 260 selection->rows[i] = index;
234 } 261 }
235 return selection; 262 return selection;
236 } 263 }
237 264

mercurial