ui/motif/tree.c

changeset 44
a1571777eff2
parent 43
157a21a914ac
child 45
cfeb2d5f1332
equal deleted inserted replaced
43:157a21a914ac 44:a1571777eff2
73 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData)); 73 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
74 event->obj = obj; 74 event->obj = obj;
75 event->activate = modelinfo->activate; 75 event->activate = modelinfo->activate;
76 event->selection = modelinfo->selection; 76 event->selection = modelinfo->selection;
77 event->userdata = modelinfo->userdata; 77 event->userdata = modelinfo->userdata;
78 event->last_selection = NULL;
78 if(modelinfo->selection) { 79 if(modelinfo->selection) {
79 XtAddCallback( 80 XtAddCallback(
80 container, 81 container,
81 XmNselectionCallback, 82 XmNselectionCallback,
82 (XtCallbackProc)ui_table_select_callback, 83 (XtCallbackProc)ui_table_select_callback,
183 void ui_table_action_callback( 184 void ui_table_action_callback(
184 Widget widget, 185 Widget widget,
185 UiTreeEventData *event, 186 UiTreeEventData *event,
186 XmContainerSelectCallbackStruct *sel) 187 XmContainerSelectCallbackStruct *sel)
187 { 188 {
188 UiListSelection *selection = malloc(sizeof(UiListSelection)); 189 UiListSelection *selection = ui_list_selection(sel);
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 190
197 UiEvent e; 191 UiEvent e;
198 e.obj = event->obj; 192 e.obj = event->obj;
199 e.window = event->obj->window; 193 e.window = event->obj->window;
200 e.document = event->obj->document; 194 e.document = event->obj->document;
201 e.eventdata = selection; 195 e.eventdata = selection;
202 e.intval = selection->count > 0 ? selection->rows[0] : -1; 196 e.intval = selection->count > 0 ? selection->rows[0] : -1;
203 event->activate(&e, event->userdata); 197 event->activate(&e, event->userdata);
204 198
205 free(selection); 199 free(event->last_selection->rows);
200 free(event->last_selection);
201 event->last_selection = selection;
206 } 202 }
207 203
208 void ui_table_select_callback( 204 void ui_table_select_callback(
209 Widget widget, 205 Widget widget,
210 UiTreeEventData *event, 206 UiTreeEventData *event,
211 XmContainerSelectCallbackStruct *sel) 207 XmContainerSelectCallbackStruct *sel)
212 { 208 {
209 UiListSelection *selection = ui_list_selection(sel);
210 if(!ui_compare_list_selection(selection, event->last_selection)) {
211 UiEvent e;
212 e.obj = event->obj;
213 e.window = event->obj->window;
214 e.document = event->obj->document;
215 e.eventdata = selection;
216 e.intval = selection->count > 0 ? selection->rows[0] : -1;
217 event->selection(&e, event->userdata);
218 }
219 if(event->last_selection) {
220 free(event->last_selection->rows);
221 free(event->last_selection);
222 }
223 event->last_selection = selection;
224 }
225
226 UiListSelection* ui_list_selection(XmContainerSelectCallbackStruct *xs) {
213 UiListSelection *selection = malloc(sizeof(UiListSelection)); 227 UiListSelection *selection = malloc(sizeof(UiListSelection));
214 selection->count = sel->selected_item_count; 228 selection->count = xs->selected_item_count;
215 selection->rows = calloc(selection->count, sizeof(int)); 229 selection->rows = calloc(selection->count, sizeof(int));
216 for(int i=0;i<selection->count;i++) { 230 for(int i=0;i<selection->count;i++) {
217 intptr_t index; 231 intptr_t index;
218 XtVaGetValues(sel->selected_items[i], XmNuserData, &index, NULL); 232 XtVaGetValues(xs->selected_items[i], XmNuserData, &index, NULL);
219 selection->rows[i] = index; 233 selection->rows[i] = index;
220 } 234 }
221 235 return selection;
222 UiEvent e; 236 }
223 e.obj = event->obj; 237
224 e.window = event->obj->window; 238 Boolean ui_compare_list_selection(UiListSelection *s1, UiListSelection *s2) {
225 e.document = event->obj->document; 239 if(!s1 || !s2) {
226 e.eventdata = selection; 240 return FALSE;
227 e.intval = selection->count > 0 ? selection->rows[0] : -1; 241 }
228 event->selection(&e, event->userdata); 242 if(s1->count != s2->count) {
229 243 return FALSE;
230 free(selection); 244 }
231 } 245 for(int i=0;i<s1->count;i++) {
246 if(s1->rows[i] != s2->rows[i]) {
247 return FALSE;
248 }
249 }
250 return TRUE;
251 }

mercurial