66 Widget parent = ct->add(ct, args, &n); |
67 Widget parent = ct->add(ct, args, &n); |
67 |
68 |
68 Widget container = XmCreateContainer(parent, "table", args, n); |
69 Widget container = XmCreateContainer(parent, "table", args, n); |
69 XtManageChild(container); |
70 XtManageChild(container); |
70 |
71 |
|
72 // add callbacks |
|
73 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData)); |
|
74 event->obj = obj; |
|
75 event->activate = modelinfo->activate; |
|
76 event->selection = modelinfo->selection; |
|
77 event->userdata = modelinfo->userdata; |
|
78 if(modelinfo->selection) { |
|
79 XtAddCallback( |
|
80 container, |
|
81 XmNselectionCallback, |
|
82 (XtCallbackProc)ui_table_select_callback, |
|
83 event); |
|
84 } |
|
85 if(modelinfo->activate) { |
|
86 XtAddCallback( |
|
87 container, |
|
88 XmNdefaultActionCallback, |
|
89 (XtCallbackProc)ui_table_action_callback, |
|
90 event); |
|
91 } |
|
92 |
71 // add initial data |
93 // add initial data |
72 void *data = model->first(model); |
94 void *data = model->first(model); |
|
95 int i = 0; |
73 while(data) { |
96 while(data) { |
74 ui_add_icon_gadget(container, modelinfo, data); |
97 ui_add_icon_gadget(container, modelinfo, data, i); |
75 data = model->next(model); |
98 data = model->next(model); |
|
99 i++; |
76 } |
100 } |
77 |
101 |
78 // cleanup |
102 // cleanup |
79 for(int i=0;i<modelinfo->columns;i++) { |
103 for(int i=0;i<modelinfo->columns;i++) { |
80 XmStringFree(header[i]); |
104 XmStringFree(header[i]); |
82 XtFree((char*)header); |
106 XtFree((char*)header); |
83 |
107 |
84 return container; |
108 return container; |
85 } |
109 } |
86 |
110 |
87 void ui_add_icon_gadget(Widget container, UiModelInfo *modelinfo, void *data) { |
111 void ui_add_icon_gadget( |
|
112 Widget container, |
|
113 UiModelInfo *modelinfo, |
|
114 void *data, |
|
115 int index) |
|
116 { |
88 if(modelinfo->columns == 0) { |
117 if(modelinfo->columns == 0) { |
89 return; |
118 return; |
90 } |
119 } |
91 |
120 |
92 XmString label = NULL; |
121 XmString label = NULL; |
120 free(str); |
149 free(str); |
121 } |
150 } |
122 } |
151 } |
123 XtSetArg(args[1], XmNdetail, details); |
152 XtSetArg(args[1], XmNdetail, details); |
124 XtSetArg(args[2], XmNdetailCount, modelinfo->columns - 1); |
153 XtSetArg(args[2], XmNdetailCount, modelinfo->columns - 1); |
125 |
154 XtSetArg(args[3], XmNshadowThickness, 0); |
|
155 intptr_t userdata = index; |
|
156 XtSetArg(args[4], XmNuserData, userdata); |
126 // create widget |
157 // create widget |
127 Widget item = XmCreateIconGadget(container, "table_item", args, 3); |
158 Widget item = XmCreateIconGadget(container, "table_item", args, 5); |
128 XtManageChild(item); |
159 XtManageChild(item); |
129 |
160 |
130 // cleanup |
161 // cleanup |
131 XmStringFree(label); |
162 XmStringFree(label); |
132 for(int i=0;i<modelinfo->columns-1;i++) { |
163 for(int i=0;i<modelinfo->columns-1;i++) { |
146 } |
177 } |
147 } |
178 } |
148 *free = FALSE; |
179 *free = FALSE; |
149 return NULL; |
180 return NULL; |
150 } |
181 } |
|
182 |
|
183 void ui_table_action_callback( |
|
184 Widget widget, |
|
185 UiTreeEventData *event, |
|
186 XmContainerSelectCallbackStruct *sel) |
|
187 { |
|
188 UiListSelection *selection = malloc(sizeof(UiListSelection)); |
|
189 selection->count = sel->selected_item_count; |
|
190 selection->rows = calloc(selection->count, sizeof(int)); |
|
191 for(int i=0;i<selection->count;i++) { |
|
192 intptr_t index; |
|
193 XtVaGetValues(sel->selected_items[i], XmNuserData, &index, NULL); |
|
194 selection->rows[i] = index; |
|
195 } |
|
196 |
|
197 UiEvent e; |
|
198 e.obj = event->obj; |
|
199 e.window = event->obj->window; |
|
200 e.document = event->obj->document; |
|
201 e.eventdata = selection; |
|
202 e.intval = selection->count > 0 ? selection->rows[0] : -1; |
|
203 event->activate(&e, event->userdata); |
|
204 |
|
205 free(selection); |
|
206 } |
|
207 |
|
208 void ui_table_select_callback( |
|
209 Widget widget, |
|
210 UiTreeEventData *event, |
|
211 XmContainerSelectCallbackStruct *sel) |
|
212 { |
|
213 UiListSelection *selection = malloc(sizeof(UiListSelection)); |
|
214 selection->count = sel->selected_item_count; |
|
215 selection->rows = calloc(selection->count, sizeof(int)); |
|
216 for(int i=0;i<selection->count;i++) { |
|
217 intptr_t index; |
|
218 XtVaGetValues(sel->selected_items[i], XmNuserData, &index, NULL); |
|
219 selection->rows[i] = index; |
|
220 } |
|
221 |
|
222 UiEvent e; |
|
223 e.obj = event->obj; |
|
224 e.window = event->obj->window; |
|
225 e.document = event->obj->document; |
|
226 e.eventdata = selection; |
|
227 e.intval = selection->count > 0 ? selection->rows[0] : -1; |
|
228 event->selection(&e, event->userdata); |
|
229 |
|
230 free(selection); |
|
231 } |