ui/motif/list.c

changeset 34
0ec8a5f17782
child 36
e4198fc2ead4
equal deleted inserted replaced
33:458831c574f4 34:0ec8a5f17782
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "container.h"
33
34 #include "list.h"
35 #include "../common/object.h"
36
37
38 void* ui_strmodel_getvalue(void *elm, int column) {
39 return column == 0 ? elm : NULL;
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, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
48 UiList *ls = list->list;
49
50 //int count;
51 //XmStringTable items = ui_create_stringlist(list->list, getvalue, &count);
52 int count = list->list->count(list->list);
53 XmStringTable items = (XmStringTable)XtMalloc(count * sizeof(XmString));
54 void *data = list->list->first(list->list);
55 for(int i=0;i<count;i++) {
56 items[i] = XmStringCreateLocalized(getvalue(data, 0));
57 data = list->list->next(list->list);
58 }
59
60 Arg args[8];
61 int n = 0;
62 XtSetArg(args[n], XmNitemCount, count);
63 n++;
64 XtSetArg(args[n], XmNitems, NULL);
65 n++;
66
67 UiContainer *ct = uic_get_current_container(obj);
68 Widget parent = ct->add(ct, args, &n);
69 Widget widget= XmCreateScrolledList(parent, "listview", args, n);
70 XtManageChild(widget);
71
72 UiListView *listview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListView));
73 listview->widget = widget;
74 listview->list = list;
75 listview->getvalue = getvalue;
76
77 list->list->observers = ui_add_observer(
78 list->list->observers,
79 (ui_callback)ui_listview_update,
80 listview);
81
82 for (int i=0;i<count;i++) {
83 XmStringFree(items[i]);
84 }
85 XtFree((char *)items);
86
87 if(f) {
88 UiListViewEventData *event = ucx_mempool_malloc(
89 obj->ctx->mempool,
90 sizeof(UiEventData));
91 event->event.obj = obj;
92 event->event.user_data = udata;
93 event->event.callback = f;
94 event->event.value = 0;
95 event->list = list;
96 XtAddCallback(
97 widget,
98 XmNdefaultActionCallback,
99 (XtCallbackProc)ui_list_selection_callback,
100 event);
101 }
102
103 return widget;
104 }
105
106 UIWIDGET ui_listview(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
107 UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
108 listptr->list = list;
109 return ui_listview_var(obj, listptr, getvalue, f, udata);
110 }
111
112 UIWIDGET ui_listview_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
113 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
114 if(var) {
115 UiListVar *value = var->value;
116 return ui_listview_var(obj, value->listptr, getvalue, f, udata);
117 } else {
118 // TODO: error
119 }
120 return NULL;
121 }
122
123
124 XmStringTable ui_create_stringlist(UiList *list, ui_model_getvalue_f getvalue, int *count) {
125 int num = list->count(list);
126 XmStringTable items = (XmStringTable)XtMalloc(num * sizeof(XmString));
127 void *data = list->first(list);
128 for(int i=0;i<num;i++) {
129 items[i] = XmStringCreateLocalized(getvalue(data, 0));
130 data = list->next(list);
131 }
132
133 *count = num;
134 return items;
135 }
136
137
138 void ui_listview_update(UiEvent *event, UiListView *view) {
139 int count;
140 XmStringTable items = ui_create_stringlist(
141 view->list->list,
142 view->getvalue,
143 &count);
144
145 XtVaSetValues(view->widget, XmNitems, items, XmNitemCount, count, NULL);
146
147 for (int i=0;i<count;i++) {
148 XmStringFree(items[i]);
149 }
150 XtFree((char *)items);
151 }
152
153 void ui_list_selection_callback (Widget widget, UiListViewEventData *event, XtPointer data) {
154 XmListCallbackStruct *cbs = (XmListCallbackStruct *) data;
155
156 UiEvent e;
157 e.obj = event->event.obj;
158 e.window = event->event.obj->window;
159 e.document = event->event.obj->document;
160 e.eventdata = event->list->list->get(event->list->list, cbs->item_position - 1);
161 e.intval = cbs->item_position - 1;
162 event->event.callback(&e, event->event.user_data);
163 }

mercurial