ui/motif/list.c

branch
newapi
changeset 427
7ead63398a50
parent 406
0ebf9d7b23e8
equal deleted inserted replaced
426:3eb26df703bf 427:7ead63398a50
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 UIWIDGET ui_listview_create(UiObject* obj, UiListArgs args) {
38 Arg xargs[16];
39 int n = 0;
40
41 UiContainerPrivate *ctn = ui_obj_container(obj);
42 UI_APPLY_LAYOUT(ctn->layout, args);
43
44 if(args.multiselection) {
45 XtSetArg(xargs[n], XmNselectionPolicy, XmEXTENDED_SELECT); n++;
46 } else {
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);
53 XtManageChild(widget);
54
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;
60 listview->widget = widget;
61 listview->getvalue = args.getvalue ? args.getvalue : ui_strmodel_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;
67
68 if(var) {
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);
75 }
76
77 XtAddCallback(
78 widget,
79 XmNdestroyCallback,
80 (XtCallbackProc)ui_listview_destroy,
81 listview);
82
83 XtAddCallback(
84 widget,
85 XmNdefaultActionCallback,
86 (XtCallbackProc)ui_listview_activate,
87 listview);
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);
98
99 return widget;
100 }
101
102 void ui_listview_destroy(Widget w, UiListView *listview, XtPointer d) {
103 // TODO
104 }
105
106 static void list_callback(UiObject *obj, UiListSelection sel, ui_callback callback, void *userdata) {
107 UiEvent event;
108 event.obj = obj;
109 event.window = obj->window;
110 event.document = obj->ctx->document;
111 event.eventdata = &sel;
112 event.intval = sel.count > 0 ? sel.rows[0] : -1;
113 callback(&event, userdata);
114 }
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 }
127
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) {
143 int num = list->count(list);
144 XmStringTable items = (XmStringTable)XtMalloc(num * sizeof(XmString));
145 void *data = list->first(list);
146 for(int i=0;i<num;i++) {
147 char *s = getvalue(data, 0);
148 items[i] = XmStringCreateLocalized(s ? s : "");
149 data = list->next(list);
150 }
151
152 *count = num;
153 return items;
154 }
155
156 void ui_listview_update(UiList *list, int i) {
157 UiListView *listview = list->obj;
158
159 int count;
160 XmStringTable items = create_stringlist(
161 list,
162 listview->getvalue,
163 &count);
164
165 XtVaSetValues(
166 listview->widget,
167 XmNitems, count == 0 ? NULL : items,
168 XmNitemCount,
169 count,
170 NULL);
171
172 for (int i=0;i<count;i++) {
173 XmStringFree(items[i]);
174 }
175 XtFree((char *)items);
176 }
177
178 UiListSelection ui_listview_getselection(UiList *list) {
179 UiListView *listview = list->obj;
180 UiListSelection sel = { listview->current_selection.count, NULL };
181 if(sel.count > 0) {
182 sel.rows = calloc(sel.count, sizeof(int));
183 memcpy(sel.rows, listview->current_selection.rows, sel.count*sizeof(int));
184 }
185 return sel;
186 }
187
188 void ui_listview_setselection(UiList *list, UiListSelection selection) {
189 UiListView *listview = list->obj;
190 XmListDeselectAllItems(listview->widget);
191 for(int i=0;i<selection.count;i++) {
192 XmListSelectPos(listview->widget, selection.rows[i]+1, False);
193 }
194 }
195
196 void* ui_strmodel_getvalue(void *elm, int column) {
197 return column == 0 ? elm : NULL;
198 }

mercurial