ui/motif/list.c

Mon, 06 Jan 2025 22:22:55 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 06 Jan 2025 22:22:55 +0100
changeset 101
7b3a3130be44
parent 100
d2bd73d28ff1
permissions
-rw-r--r--

update ucx, toolkit

0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include "container.h"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "list.h"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "../common/object.h"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
101
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
37 UIWIDGET ui_listview_create(UiObject* obj, UiListArgs args) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
38 Arg xargs[16];
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
39 int n = 0;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
40
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
41 UiContainerPrivate *ctn = ui_obj_container(obj);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
42 UI_APPLY_LAYOUT(ctn->layout, args);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
43
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
44 if(args.multiselection) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
45 XtSetArg(xargs[n], XmNselectionPolicy, XmEXTENDED_SELECT); n++;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
46 } else {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
47 XtSetArg(xargs[n], XmNselectionPolicy, XmSINGLE_SELECT); n++;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
48 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
49
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
50 char *name = args.name ? (char*)args.name : "listview";
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
51 Widget parent = ctn->prepare(ctn, xargs, &n);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
52 Widget widget = XmCreateScrolledList(parent, name, xargs, n);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
53 XtManageChild(widget);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
54
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
55 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args.list, args.varname, UI_VAR_LIST);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
56
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
57 UiListView *listview = malloc(sizeof(UiListView));
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
58 memset(listview, 0, sizeof(UiListView));
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
59 listview->obj = obj;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
60 listview->widget = widget;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
61 listview->getvalue = args.getvalue ? args.getvalue : ui_strmodel_getvalue;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
62 listview->var = var;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
63 listview->onactivate = args.onactivate;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
64 listview->onactivatedata = args.onactivatedata;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
65 listview->onselection = args.onselection;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
66 listview->onselectiondata = args.onselectiondata;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
67
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
68 if(var) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
69 UiList *list = var->value;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
70 list->obj = listview;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
71 list->update = ui_listview_update;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
72 list->getselection = ui_listview_getselection;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
73 list->setselection = ui_listview_setselection;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
74 ui_listview_update(list, 0);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
75 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
76
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
77 XtAddCallback(
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
78 widget,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
79 XmNdestroyCallback,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
80 (XtCallbackProc)ui_listview_destroy,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
81 listview);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
82
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
83 XtAddCallback(
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
84 widget,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
85 XmNdefaultActionCallback,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
86 (XtCallbackProc)ui_listview_activate,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
87 listview);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
88 XtAddCallback(
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
89 widget,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
90 XmNextendedSelectionCallback,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
91 (XtCallbackProc)ui_listview_selection,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
92 listview);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
93 XtAddCallback(
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
94 widget,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
95 XmNsingleSelectionCallback,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
96 (XtCallbackProc)ui_listview_selection,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
97 listview);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
98
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
99 return widget;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
100 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
101
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
102 void ui_listview_destroy(Widget w, UiListView *listview, XtPointer d) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
103 // TODO
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
104 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
105
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
106 static void list_callback(UiObject *obj, UiListSelection sel, ui_callback callback, void *userdata) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
107 UiEvent event;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
108 event.obj = obj;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
109 event.window = obj->window;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
110 event.document = obj->ctx->document;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
111 event.eventdata = &sel;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
112 event.intval = sel.count > 0 ? sel.rows[0] : -1;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
113 callback(&event, userdata);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
114 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
115
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
116 static void listview_save_selection(UiListView *listview, XmListCallbackStruct *cb) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
117 UiListSelection sel = { cb->selected_item_count, NULL };
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
118 if(sel.count > 0) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
119 sel.rows = calloc(sel.count, sizeof(int));
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
120 for(int i=0;i<sel.count;i++) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
121 sel.rows[i] = cb->selected_item_positions[i]-1;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
122 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
123 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
124 free(listview->current_selection.rows);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
125 listview->current_selection = sel;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
126 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
127
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
128 void ui_listview_activate(Widget w, UiListView *listview, XmListCallbackStruct *cb) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
129 listview_save_selection(listview, cb);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
130 if(listview->onactivate) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
131 list_callback(listview->obj, listview->current_selection, listview->onactivate, listview->onactivatedata);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
132 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
133 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
134
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
135 void ui_listview_selection(Widget w, UiListView *listview, XmListCallbackStruct *cb) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
136 listview_save_selection(listview, cb);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
137 if(listview->onselection) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
138 list_callback(listview->obj, listview->current_selection, listview->onselection, listview->onselectiondata);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
139 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
140 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
141
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
142 static XmStringTable create_stringlist(UiList *list, ui_getvaluefunc getvalue, int *count) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
143 int num = list->count(list);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
144 XmStringTable items = (XmStringTable)XtMalloc(num * sizeof(XmString));
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
145 void *data = list->first(list);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
146 for(int i=0;i<num;i++) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
147 char *s = getvalue(data, 0);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
148 items[i] = XmStringCreateLocalized(s ? s : "");
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
149 data = list->next(list);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
150 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
151
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
152 *count = num;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
153 return items;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
154 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
155
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
156 void ui_listview_update(UiList *list, int i) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
157 UiListView *listview = list->obj;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
158
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
159 int count;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
160 XmStringTable items = create_stringlist(
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
161 list,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
162 listview->getvalue,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
163 &count);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
164
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
165 XtVaSetValues(
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
166 listview->widget,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
167 XmNitems, count == 0 ? NULL : items,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
168 XmNitemCount,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
169 count,
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
170 NULL);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
171
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
172 for (int i=0;i<count;i++) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
173 XmStringFree(items[i]);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
174 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
175 XtFree((char *)items);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
176 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
177
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
178 UiListSelection ui_listview_getselection(UiList *list) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
179 UiListView *listview = list->obj;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
180 UiListSelection sel = { listview->current_selection.count, NULL };
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
181 if(sel.count > 0) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
182 sel.rows = calloc(sel.count, sizeof(int));
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
183 memcpy(sel.rows, listview->current_selection.rows, sel.count*sizeof(int));
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
184 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
185 return sel;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
186 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
187
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
188 void ui_listview_setselection(UiList *list, UiListSelection selection) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
189 UiListView *listview = list->obj;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
190 XmListDeselectAllItems(listview->widget);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
191 for(int i=0;i<selection.count;i++) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
192 XmListSelectPos(listview->widget, selection.rows[i]+1, False);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
193 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
194 }
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
195
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
196 void* ui_strmodel_getvalue(void *elm, int column) {
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
197 return column == 0 ? elm : NULL;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 100
diff changeset
198 }

mercurial