2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2014 Olaf Wintermann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
36 #include "container.h"
37 #include "../common/object.h"
38 #include "../common/context.h"
39 #include <ucx/utils.h>
41 UIWIDGET ui_table_var(UiObject *obj, UiVar *var, UiModel *model, UiListCallbacks cb) {
42 // TODO: check if modelinfo is complete
47 // create scrolled window
48 UiContainer *ct = uic_get_current_container(obj);
49 Widget parent = ct->prepare(ct, args, &n, TRUE);
51 XtSetArg(args[n], XmNscrollingPolicy, XmAUTOMATIC);
53 XtSetArg(args[n], XmNshadowThickness, 0);
55 Widget scrollw = XmCreateScrolledWindow(parent, "scroll_win", args, n);
57 XtManageChild(scrollw);
59 // create table headers
60 XmStringTable header = (XmStringTable)XtMalloc(
61 model->columns * sizeof(XmString));
62 for(int i=0;i<model->columns;i++) {
63 header[i] = XmStringCreateLocalized(model->titles[i]);
66 XtSetArg(args[n], XmNdetailColumnHeading, header);
68 XtSetArg(args[n], XmNdetailColumnHeadingCount, model->columns);
72 XtSetArg(args[n], XmNlayoutType, XmDETAIL);
74 XtSetArg(args[n], XmNentryViewType, XmSMALL_ICON);
76 XtSetArg(args[n], XmNselectionPolicy, XmSINGLE_SELECT);
78 XtSetArg(args[n], XmNwidth, 600);
82 //UiContainer *ct = uic_get_current_container(obj);
83 //Widget parent = ct->add(ct, args, &n);
85 Widget container = XmCreateContainer(scrollw, "table", args, n);
86 XtManageChild(container);
89 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
91 event->activate = cb.activate;
92 event->selection = cb.selection;
93 event->userdata = cb.userdata;
94 event->last_selection = NULL;
99 (XtCallbackProc)ui_table_select_callback,
105 XmNdefaultActionCallback,
106 (XtCallbackProc)ui_table_action_callback,
111 UiList *list = var->value;
112 void *data = list->first(list);
115 int w = ui_add_icon_gadget(container, model, data);
119 data = list->next(list);
122 UiTableView *tableview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiTableView));
123 tableview->widget = container;
124 tableview->var = var;
125 tableview->model = model;
127 // set new XmContainer width
128 XtVaSetValues(container, XmNwidth, width, NULL);
131 for(int i=0;i<model->columns;i++) {
132 XmStringFree(header[i]);
134 XtFree((char*)header);
139 UIWIDGET ui_table(UiObject *obj, UiList *data, UiModel *model, UiListCallbacks cb) {
140 UiVar *var = malloc(sizeof(UiVar));
142 var->type = UI_VAR_SPECIAL;
143 return ui_table_var(obj, var, model, cb);
146 void ui_table_update(UiEvent *event, UiTableView *view) {
159 for(int i=0;i<nc;i++) {
160 XtDestroyWidget(children[i]);
163 UiList *list = view->var->value;
165 void *data = list->first(list);
168 int w = ui_add_icon_gadget(view->widget, view->model, data);
172 data = list->next(list);
177 #define UI_COL_CHAR_WIDTH 12
179 int ui_add_icon_gadget(Widget container, UiModel *model, void *data) {
182 if(model->columns == 0) {
186 XmString label = NULL;
190 if(model->types[0] != 12345678) { // TODO: icon/label type
191 char *str = ui_type_to_string(
193 model->getvalue(data, 0),
197 width += strlen(str) * UI_COL_CHAR_WIDTH;
200 XmString label = XmStringCreateLocalized(str);
201 XtSetArg(args[0], XmNlabelString, label);
209 // remaining columns are the icon gadget details
210 XmStringTable details = (XmStringTable)XtMalloc(
211 (model->columns - 1) * sizeof(XmString));
212 for(int i=1;i<model->columns;i++) {
213 char *str = ui_type_to_string(
215 model->getvalue(data, i),
219 width += strlen(str) * UI_COL_CHAR_WIDTH;
221 details[i - 1] = XmStringCreateLocalized(str);
226 XtSetArg(args[1], XmNdetail, details);
227 XtSetArg(args[2], XmNdetailCount, model->columns - 1);
228 XtSetArg(args[3], XmNshadowThickness, 0);
230 Widget item = XmCreateIconGadget(container, "table_item", args, 4);
235 for(int i=0;i<model->columns-1;i++) {
236 XmStringFree(details[i]);
238 XtFree((char*)details);
243 char* ui_type_to_string(UiModelType type, void *data, Boolean *free) {
245 case UI_STRING: *free = FALSE; return data;
249 sstr_t str = ucx_asprintf(ucx_default_allocator(), "%d", *val);
258 void ui_table_action_callback(
260 UiTreeEventData *event,
261 XmContainerSelectCallbackStruct *sel)
263 UiListSelection *selection = ui_list_selection(sel);
267 e.window = event->obj->window;
268 e.document = event->obj->ctx->document;
269 e.eventdata = selection;
270 e.intval = selection->count > 0 ? selection->rows[0] : -1;
271 event->activate(&e, event->userdata);
273 free(event->last_selection->rows);
274 free(event->last_selection);
275 event->last_selection = selection;
278 void ui_table_select_callback(
280 UiTreeEventData *event,
281 XmContainerSelectCallbackStruct *sel)
283 UiListSelection *selection = ui_list_selection(sel);
284 if(!ui_compare_list_selection(selection, event->last_selection)) {
287 e.window = event->obj->window;
288 e.document = event->obj->ctx->document;
289 e.eventdata = selection;
290 e.intval = selection->count > 0 ? selection->rows[0] : -1;
291 event->selection(&e, event->userdata);
293 if(event->last_selection) {
294 free(event->last_selection->rows);
295 free(event->last_selection);
297 event->last_selection = selection;
300 UiListSelection* ui_list_selection(XmContainerSelectCallbackStruct *xs) {
301 UiListSelection *selection = malloc(sizeof(UiListSelection));
302 selection->count = xs->selected_item_count;
303 selection->rows = calloc(selection->count, sizeof(int));
304 for(int i=0;i<selection->count;i++) {
306 XtVaGetValues(xs->selected_items[i], XmNpositionIndex, &index, NULL);
307 selection->rows[i] = index;
312 Boolean ui_compare_list_selection(UiListSelection *s1, UiListSelection *s2) {
316 if(s1->count != s2->count) {
319 for(int i=0;i<s1->count;i++) {
320 if(s1->rows[i] != s2->rows[i]) {