32 #include "container.h" |
32 #include "container.h" |
33 |
33 |
34 #include "list.h" |
34 #include "list.h" |
35 #include "../common/object.h" |
35 #include "../common/object.h" |
36 |
36 |
37 |
37 UIWIDGET ui_listview_create(UiObject* obj, UiListArgs args) { |
38 void* ui_strmodel_getvalue(void *elm, int column) { |
38 Arg xargs[16]; |
39 return column == 0 ? elm : NULL; |
39 int n = 0; |
40 } |
|
41 |
|
42 |
|
43 UIWIDGET ui_listview_str(UiObject *obj, UiList *list, ui_callback f, void *udata) { |
|
44 return ui_listview(obj, list, ui_strmodel_getvalue, f, udata); |
|
45 } |
|
46 |
|
47 UIWIDGET ui_listview_var(UiObject *obj, UiVar *var, ui_getvaluefunc getvalue, ui_callback f, void *udata) { |
|
48 int count; |
|
49 XmStringTable items = ui_create_stringlist(var->value, getvalue, &count); |
|
50 |
40 |
51 Arg args[8]; |
41 UiContainerPrivate *ctn = ui_obj_container(obj); |
52 int n = 0; |
42 UI_APPLY_LAYOUT(ctn->layout, args); |
53 XtSetArg(args[n], XmNitemCount, count); |
|
54 n++; |
|
55 XtSetArg(args[n], XmNitems, count == 0 ? NULL : items); |
|
56 n++; |
|
57 |
43 |
58 UiContainer *ct = uic_get_current_container(obj); |
44 if(args.multiselection) { |
59 Widget parent = ct->prepare(ct, args, &n, TRUE); |
45 XtSetArg(xargs[n], XmNselectionPolicy, XmEXTENDED_SELECT); n++; |
60 Widget widget = XmCreateScrolledList(parent, "listview", args, n); |
46 } else { |
61 ct->add(ct, XtParent(widget)); |
47 XtSetArg(xargs[n], XmNselectionPolicy, XmSINGLE_SELECT); n++; |
|
48 } |
|
49 |
|
50 char *name = args.name ? (char*)args.name : "listview"; |
|
51 Widget parent = ctn->prepare(ctn, xargs, &n); |
|
52 Widget widget = XmCreateScrolledList(parent, name, xargs, n); |
62 XtManageChild(widget); |
53 XtManageChild(widget); |
63 |
54 |
64 UiListView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListView)); |
55 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.list, args.varname, UI_VAR_LIST); |
|
56 |
|
57 UiListView *listview = malloc(sizeof(UiListView)); |
|
58 memset(listview, 0, sizeof(UiListView)); |
|
59 listview->obj = obj; |
65 listview->widget = widget; |
60 listview->widget = widget; |
66 listview->list = var; |
61 listview->getvalue = args.getvalue ? args.getvalue : ui_strmodel_getvalue; |
67 listview->getvalue = getvalue; |
62 listview->var = var; |
|
63 listview->onactivate = args.onactivate; |
|
64 listview->onactivatedata = args.onactivatedata; |
|
65 listview->onselection = args.onselection; |
|
66 listview->onselectiondata = args.onselectiondata; |
68 |
67 |
69 for (int i=0;i<count;i++) { |
68 if(var) { |
70 XmStringFree(items[i]); |
69 UiList *list = var->value; |
|
70 list->obj = listview; |
|
71 list->update = ui_listview_update; |
|
72 list->getselection = ui_listview_getselection; |
|
73 list->setselection = ui_listview_setselection; |
|
74 ui_listview_update(list, 0); |
71 } |
75 } |
72 XtFree((char *)items); |
|
73 |
76 |
74 if(f) { |
77 XtAddCallback( |
75 UiListViewEventData *event = ucx_mempool_malloc( |
78 widget, |
76 obj->ctx->mempool, |
79 XmNdestroyCallback, |
77 sizeof(UiListViewEventData)); |
80 (XtCallbackProc)ui_listview_destroy, |
78 event->event.obj = obj; |
81 listview); |
79 event->event.userdata = udata; |
82 |
80 event->event.callback = f; |
83 XtAddCallback( |
81 event->event.value = 0; |
|
82 event->var = var; |
|
83 XtAddCallback( |
|
84 widget, |
84 widget, |
85 XmNdefaultActionCallback, |
85 XmNdefaultActionCallback, |
86 (XtCallbackProc)ui_list_selection_callback, |
86 (XtCallbackProc)ui_listview_activate, |
87 event); |
87 listview); |
88 } |
88 XtAddCallback( |
|
89 widget, |
|
90 XmNextendedSelectionCallback, |
|
91 (XtCallbackProc)ui_listview_selection, |
|
92 listview); |
|
93 XtAddCallback( |
|
94 widget, |
|
95 XmNsingleSelectionCallback, |
|
96 (XtCallbackProc)ui_listview_selection, |
|
97 listview); |
89 |
98 |
90 return widget; |
99 return widget; |
91 } |
100 } |
92 |
101 |
93 UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_getvaluefunc getvalue, ui_callback f, void *udata) { |
102 void ui_listview_destroy(Widget w, UiListView *listview, XtPointer d) { |
94 UiVar *var = malloc(sizeof(UiVar)); |
103 // TODO |
95 var->value = list; |
|
96 var->type = UI_VAR_SPECIAL; |
|
97 return ui_listview_var(obj, var, getvalue, f, udata); |
|
98 } |
104 } |
99 |
105 |
100 UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_getvaluefunc getvalue, ui_callback f, void *udata) { |
106 static void list_callback(UiObject *obj, UiListSelection sel, ui_callback callback, void *userdata) { |
101 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_LIST); |
107 UiEvent event; |
102 if(var) { |
108 event.obj = obj; |
103 UiListVar *value = var->value; |
109 event.window = obj->window; |
104 return ui_listview_var(obj, var, getvalue, f, udata); |
110 event.document = obj->ctx->document; |
105 } else { |
111 event.eventdata = &sel; |
106 // TODO: error |
112 event.intval = sel.count > 0 ? sel.rows[0] : -1; |
107 } |
113 callback(&event, userdata); |
108 return NULL; |
|
109 } |
114 } |
110 |
115 |
|
116 static void listview_save_selection(UiListView *listview, XmListCallbackStruct *cb) { |
|
117 UiListSelection sel = { cb->selected_item_count, NULL }; |
|
118 if(sel.count > 0) { |
|
119 sel.rows = calloc(sel.count, sizeof(int)); |
|
120 for(int i=0;i<sel.count;i++) { |
|
121 sel.rows[i] = cb->selected_item_positions[i]-1; |
|
122 } |
|
123 } |
|
124 free(listview->current_selection.rows); |
|
125 listview->current_selection = sel; |
|
126 } |
111 |
127 |
112 XmStringTable ui_create_stringlist(UiList *list, ui_getvaluefunc getvalue, int *count) { |
128 void ui_listview_activate(Widget w, UiListView *listview, XmListCallbackStruct *cb) { |
|
129 listview_save_selection(listview, cb); |
|
130 if(listview->onactivate) { |
|
131 list_callback(listview->obj, listview->current_selection, listview->onactivate, listview->onactivatedata); |
|
132 } |
|
133 } |
|
134 |
|
135 void ui_listview_selection(Widget w, UiListView *listview, XmListCallbackStruct *cb) { |
|
136 listview_save_selection(listview, cb); |
|
137 if(listview->onselection) { |
|
138 list_callback(listview->obj, listview->current_selection, listview->onselection, listview->onselectiondata); |
|
139 } |
|
140 } |
|
141 |
|
142 static XmStringTable create_stringlist(UiList *list, ui_getvaluefunc getvalue, int *count) { |
113 int num = list->count(list); |
143 int num = list->count(list); |
114 XmStringTable items = (XmStringTable)XtMalloc(num * sizeof(XmString)); |
144 XmStringTable items = (XmStringTable)XtMalloc(num * sizeof(XmString)); |
115 void *data = list->first(list); |
145 void *data = list->first(list); |
116 for(int i=0;i<num;i++) { |
146 for(int i=0;i<num;i++) { |
117 items[i] = XmStringCreateLocalized(getvalue(data, 0)); |
147 char *s = getvalue(data, 0); |
|
148 items[i] = XmStringCreateLocalized(s ? s : ""); |
118 data = list->next(list); |
149 data = list->next(list); |
119 } |
150 } |
120 |
151 |
121 *count = num; |
152 *count = num; |
122 return items; |
153 return items; |
123 } |
154 } |
124 |
155 |
125 |
156 void ui_listview_update(UiList *list, int i) { |
126 void ui_listview_update(UiEvent *event, UiListView *view) { |
157 UiListView *listview = list->obj; |
|
158 |
127 int count; |
159 int count; |
128 XmStringTable items = ui_create_stringlist( |
160 XmStringTable items = create_stringlist( |
129 view->list->value, |
161 list, |
130 view->getvalue, |
162 listview->getvalue, |
131 &count); |
163 &count); |
132 |
164 |
133 XtVaSetValues( |
165 XtVaSetValues( |
134 view->widget, |
166 listview->widget, |
135 XmNitems, count == 0 ? NULL : items, |
167 XmNitems, count == 0 ? NULL : items, |
136 XmNitemCount, |
168 XmNitemCount, |
137 count, |
169 count, |
138 NULL); |
170 NULL); |
139 |
171 |
141 XmStringFree(items[i]); |
173 XmStringFree(items[i]); |
142 } |
174 } |
143 XtFree((char *)items); |
175 XtFree((char *)items); |
144 } |
176 } |
145 |
177 |
146 void ui_list_selection_callback (Widget widget, UiListViewEventData *event, XtPointer data) { |
178 UiListSelection ui_listview_getselection(UiList *list) { |
147 XmListCallbackStruct *cbs = (XmListCallbackStruct *)data; |
179 UiListView *listview = list->obj; |
148 |
180 UiListSelection sel = { listview->current_selection.count, NULL }; |
149 UiEvent e; |
181 if(sel.count > 0) { |
150 e.obj = event->event.obj; |
182 sel.rows = calloc(sel.count, sizeof(int)); |
151 e.window = event->event.obj->window; |
183 memcpy(sel.rows, listview->current_selection.rows, sel.count*sizeof(int)); |
152 e.document = event->event.obj->ctx->document; |
184 } |
153 UiList *list = event->var->value; |
185 return sel; |
154 e.eventdata = list->get(list, cbs->item_position - 1); |
|
155 e.intval = cbs->item_position - 1; |
|
156 event->event.callback(&e, event->event.userdata); |
|
157 } |
186 } |
158 |
187 |
159 |
188 void ui_listview_setselection(UiList *list, UiListSelection selection) { |
160 /* --------------------------- ComboBox --------------------------- */ |
189 UiListView *listview = list->obj; |
161 |
190 XmListDeselectAllItems(listview->widget); |
162 UIWIDGET ui_combobox_str(UiObject *obj, UiList *list, ui_callback f, void *udata) { |
191 for(int i=0;i<selection.count;i++) { |
163 return ui_combobox(obj, list, ui_strmodel_getvalue, f, udata); |
192 XmListSelectPos(listview->widget, selection.rows[i]+1, False); |
|
193 } |
164 } |
194 } |
165 |
195 |
166 UIWIDGET ui_combobox(UiObject *obj, UiList *list, ui_getvaluefunc getvalue, ui_callback f, void *udata) { |
196 void* ui_strmodel_getvalue(void *elm, int column) { |
167 UiVar *var = malloc(sizeof(UiVar)); |
197 return column == 0 ? elm : NULL; |
168 var->value = list; |
|
169 var->type = UI_VAR_SPECIAL; |
|
170 return ui_combobox_var(obj, var, getvalue, f, udata); |
|
171 } |
198 } |
172 |
|
173 UIWIDGET ui_combobox_nv(UiObject *obj, char *varname, ui_getvaluefunc getvalue, ui_callback f, void *udata) { |
|
174 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_LIST); |
|
175 if(var) { |
|
176 UiListVar *value = var->value; |
|
177 return ui_combobox_var(obj, var, getvalue, f, udata); |
|
178 } else { |
|
179 // TODO: error |
|
180 } |
|
181 return NULL; |
|
182 } |
|
183 |
|
184 UIWIDGET ui_combobox_var(UiObject *obj, UiVar *var, ui_getvaluefunc getvalue, ui_callback f, void *udata) { |
|
185 UiListView *listview = ucx_mempool_malloc( |
|
186 obj->ctx->mempool, |
|
187 sizeof(UiListView)); |
|
188 |
|
189 UiContainer *ct = uic_get_current_container(obj); |
|
190 Arg args[16]; |
|
191 int n = 0; |
|
192 XtSetArg(args[n], XmNindicatorOn, XmINDICATOR_NONE); |
|
193 n++; |
|
194 XtSetArg(args[n], XmNtraversalOn, FALSE); |
|
195 n++; |
|
196 XtSetArg(args[n], XmNwidth, 160); |
|
197 n++; |
|
198 Widget parent = ct->prepare(ct, args, &n, FALSE); |
|
199 Widget combobox = XmCreateDropDownList(parent, "combobox", args, n); |
|
200 XtManageChild(combobox); |
|
201 listview->widget = combobox; |
|
202 listview->list = var; |
|
203 listview->getvalue = getvalue; |
|
204 |
|
205 ui_listview_update(NULL, listview); |
|
206 |
|
207 } |
|