42 |
42 |
43 UIWIDGET ui_listview_str(UiObject *obj, UiList *list, ui_callback f, void *udata) { |
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); |
44 return ui_listview(obj, list, ui_strmodel_getvalue, f, udata); |
45 } |
45 } |
46 |
46 |
47 UIWIDGET ui_listview_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { |
47 UIWIDGET ui_listview_var(UiObject *obj, UiVar *var, ui_getvaluefunc getvalue, ui_callback f, void *udata) { |
48 UiList *ls = list->list; |
|
49 |
|
50 int count; |
48 int count; |
51 XmStringTable items = ui_create_stringlist(list->list, getvalue, &count); |
49 XmStringTable items = ui_create_stringlist(var->value, getvalue, &count); |
52 |
50 |
53 Arg args[8]; |
51 Arg args[8]; |
54 int n = 0; |
52 int n = 0; |
55 XtSetArg(args[n], XmNitemCount, count); |
53 XtSetArg(args[n], XmNitemCount, count); |
56 n++; |
54 n++; |
63 ct->add(ct, XtParent(widget)); |
61 ct->add(ct, XtParent(widget)); |
64 XtManageChild(widget); |
62 XtManageChild(widget); |
65 |
63 |
66 UiListView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListView)); |
64 UiListView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListView)); |
67 listview->widget = widget; |
65 listview->widget = widget; |
68 listview->list = list; |
66 listview->list = var; |
69 listview->getvalue = getvalue; |
67 listview->getvalue = getvalue; |
70 |
|
71 list->list->observers = ui_add_observer( |
|
72 list->list->observers, |
|
73 (ui_callback)ui_listview_update, |
|
74 listview); |
|
75 |
68 |
76 for (int i=0;i<count;i++) { |
69 for (int i=0;i<count;i++) { |
77 XmStringFree(items[i]); |
70 XmStringFree(items[i]); |
78 } |
71 } |
79 XtFree((char *)items); |
72 XtFree((char *)items); |
84 sizeof(UiListViewEventData)); |
77 sizeof(UiListViewEventData)); |
85 event->event.obj = obj; |
78 event->event.obj = obj; |
86 event->event.userdata = udata; |
79 event->event.userdata = udata; |
87 event->event.callback = f; |
80 event->event.callback = f; |
88 event->event.value = 0; |
81 event->event.value = 0; |
89 event->list = list; |
82 event->var = var; |
90 XtAddCallback( |
83 XtAddCallback( |
91 widget, |
84 widget, |
92 XmNdefaultActionCallback, |
85 XmNdefaultActionCallback, |
93 (XtCallbackProc)ui_list_selection_callback, |
86 (XtCallbackProc)ui_list_selection_callback, |
94 event); |
87 event); |
95 } |
88 } |
96 |
89 |
97 return widget; |
90 return widget; |
98 } |
91 } |
99 |
92 |
100 UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { |
93 UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_getvaluefunc getvalue, ui_callback f, void *udata) { |
101 UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr)); |
94 UiVar *var = malloc(sizeof(UiVar)); |
102 listptr->list = list; |
95 var->value = list; |
103 return ui_listview_var(obj, listptr, getvalue, f, udata); |
96 var->type = UI_VAR_SPECIAL; |
104 } |
97 return ui_listview_var(obj, var, getvalue, f, udata); |
105 |
98 } |
106 UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { |
99 |
107 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST); |
100 UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_getvaluefunc getvalue, ui_callback f, void *udata) { |
|
101 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_LIST); |
108 if(var) { |
102 if(var) { |
109 UiListVar *value = var->value; |
103 UiListVar *value = var->value; |
110 return ui_listview_var(obj, value->listptr, getvalue, f, udata); |
104 return ui_listview_var(obj, var, getvalue, f, udata); |
111 } else { |
105 } else { |
112 // TODO: error |
106 // TODO: error |
113 } |
107 } |
114 return NULL; |
108 return NULL; |
115 } |
109 } |
116 |
110 |
117 |
111 |
118 XmStringTable ui_create_stringlist(UiList *list, ui_model_getvalue_f getvalue, int *count) { |
112 XmStringTable ui_create_stringlist(UiList *list, ui_getvaluefunc getvalue, int *count) { |
119 int num = list->count(list); |
113 int num = list->count(list); |
120 XmStringTable items = (XmStringTable)XtMalloc(num * sizeof(XmString)); |
114 XmStringTable items = (XmStringTable)XtMalloc(num * sizeof(XmString)); |
121 void *data = list->first(list); |
115 void *data = list->first(list); |
122 for(int i=0;i<num;i++) { |
116 for(int i=0;i<num;i++) { |
123 items[i] = XmStringCreateLocalized(getvalue(data, 0)); |
117 items[i] = XmStringCreateLocalized(getvalue(data, 0)); |
154 |
148 |
155 UiEvent e; |
149 UiEvent e; |
156 e.obj = event->event.obj; |
150 e.obj = event->event.obj; |
157 e.window = event->event.obj->window; |
151 e.window = event->event.obj->window; |
158 e.document = event->event.obj->ctx->document; |
152 e.document = event->event.obj->ctx->document; |
159 e.eventdata = event->list->list->get(event->list->list, cbs->item_position - 1); |
153 UiList *list = event->var->value; |
|
154 e.eventdata = list->get(list, cbs->item_position - 1); |
160 e.intval = cbs->item_position - 1; |
155 e.intval = cbs->item_position - 1; |
161 event->event.callback(&e, event->event.userdata); |
156 event->event.callback(&e, event->event.userdata); |
162 } |
157 } |
163 |
158 |
164 |
159 |
166 |
161 |
167 UIWIDGET ui_combobox_str(UiObject *obj, UiList *list, ui_callback f, void *udata) { |
162 UIWIDGET ui_combobox_str(UiObject *obj, UiList *list, ui_callback f, void *udata) { |
168 return ui_combobox(obj, list, ui_strmodel_getvalue, f, udata); |
163 return ui_combobox(obj, list, ui_strmodel_getvalue, f, udata); |
169 } |
164 } |
170 |
165 |
171 UIWIDGET ui_combobox(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { |
166 UIWIDGET ui_combobox(UiObject *obj, UiList *list, ui_getvaluefunc getvalue, ui_callback f, void *udata) { |
172 UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr)); |
167 UiVar *var = malloc(sizeof(UiVar)); |
173 listptr->list = list; |
168 var->value = list; |
174 return ui_combobox_var(obj, listptr, getvalue, f, udata); |
169 var->type = UI_VAR_SPECIAL; |
175 } |
170 return ui_combobox_var(obj, var, getvalue, f, udata); |
176 |
171 } |
177 UIWIDGET ui_combobox_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { |
172 |
178 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST); |
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); |
179 if(var) { |
175 if(var) { |
180 UiListVar *value = var->value; |
176 UiListVar *value = var->value; |
181 return ui_combobox_var(obj, value->listptr, getvalue, f, udata); |
177 return ui_combobox_var(obj, var, getvalue, f, udata); |
182 } else { |
178 } else { |
183 // TODO: error |
179 // TODO: error |
184 } |
180 } |
185 return NULL; |
181 return NULL; |
186 } |
182 } |
187 |
183 |
188 UIWIDGET ui_combobox_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) { |
184 UIWIDGET ui_combobox_var(UiObject *obj, UiVar *var, ui_getvaluefunc getvalue, ui_callback f, void *udata) { |
189 UiListView *listview = ucx_mempool_malloc( |
185 UiListView *listview = ucx_mempool_malloc( |
190 obj->ctx->mempool, |
186 obj->ctx->mempool, |
191 sizeof(UiListView)); |
187 sizeof(UiListView)); |
192 |
188 |
193 UiContainer *ct = uic_get_current_container(obj); |
189 UiContainer *ct = uic_get_current_container(obj); |
201 n++; |
197 n++; |
202 Widget parent = ct->prepare(ct, args, &n, FALSE); |
198 Widget parent = ct->prepare(ct, args, &n, FALSE); |
203 Widget combobox = XmCreateDropDownList(parent, "combobox", args, n); |
199 Widget combobox = XmCreateDropDownList(parent, "combobox", args, n); |
204 XtManageChild(combobox); |
200 XtManageChild(combobox); |
205 listview->widget = combobox; |
201 listview->widget = combobox; |
206 listview->list = list; |
202 listview->list = var; |
207 listview->getvalue = getvalue; |
203 listview->getvalue = getvalue; |
208 |
204 |
209 ui_listview_update(NULL, listview); |
205 ui_listview_update(NULL, listview); |
210 |
206 |
211 list->list->observers = ui_add_observer( |
207 } |
212 list->list->observers, |
|
213 (ui_callback)ui_listview_update, |
|
214 listview); |
|
215 |
|
216 } |
|