Fri, 16 May 2014 17:39:45 +0200
added table view events (Motif)
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2014 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <inttypes.h> #include "tree.h" #include "container.h" #include "../common/object.h" #include "../../ucx/utils.h" UIWIDGET ui_table(UiObject *obj, UiList *model, UiModelInfo *modelinfo) { // TODO: check if modelinfo is complete Arg args[16]; int n = 0; // create table headers XmStringTable header = (XmStringTable)XtMalloc( modelinfo->columns * sizeof(XmString)); for(int i=0;i<modelinfo->columns;i++) { header[i] = XmStringCreateLocalized(modelinfo->titles[i]); } XtSetArg(args[n], XmNdetailColumnHeading, header); n++; XtSetArg(args[n], XmNdetailColumnHeadingCount, modelinfo->columns); n++; // set res XtSetArg(args[n], XmNlayoutType, XmDETAIL); n++; XtSetArg(args[n], XmNentryViewType, XmSMALL_ICON); n++; XtSetArg(args[n], XmNselectionPolicy, XmSINGLE_SELECT); n++; // create widget UiContainer *ct = uic_get_current_container(obj); Widget parent = ct->add(ct, args, &n); Widget container = XmCreateContainer(parent, "table", args, n); XtManageChild(container); // add callbacks UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData)); event->obj = obj; event->activate = modelinfo->activate; event->selection = modelinfo->selection; event->userdata = modelinfo->userdata; if(modelinfo->selection) { XtAddCallback( container, XmNselectionCallback, (XtCallbackProc)ui_table_select_callback, event); } if(modelinfo->activate) { XtAddCallback( container, XmNdefaultActionCallback, (XtCallbackProc)ui_table_action_callback, event); } // add initial data void *data = model->first(model); int i = 0; while(data) { ui_add_icon_gadget(container, modelinfo, data, i); data = model->next(model); i++; } // cleanup for(int i=0;i<modelinfo->columns;i++) { XmStringFree(header[i]); } XtFree((char*)header); return container; } void ui_add_icon_gadget( Widget container, UiModelInfo *modelinfo, void *data, int index) { if(modelinfo->columns == 0) { return; } XmString label = NULL; Arg args[8]; Boolean f; // first column if(modelinfo->types[0] != 12345678) { // TODO: icon/label type char *str = ui_type_to_string( modelinfo->types[0], modelinfo->getvalue(data, 0), &f); XmString label = XmStringCreateLocalized(str); XtSetArg(args[0], XmNlabelString, label); if(f) { free(str); } } else { // TODO } // remaining columns are the icon gadget details XmStringTable details = (XmStringTable)XtMalloc( (modelinfo->columns - 1) * sizeof(XmString)); for(int i=1;i<modelinfo->columns;i++) { char *str = ui_type_to_string( modelinfo->types[i], modelinfo->getvalue(data, i), &f); details[i - 1] = XmStringCreateLocalized(str); if(f) { free(str); } } XtSetArg(args[1], XmNdetail, details); XtSetArg(args[2], XmNdetailCount, modelinfo->columns - 1); XtSetArg(args[3], XmNshadowThickness, 0); intptr_t userdata = index; XtSetArg(args[4], XmNuserData, userdata); // create widget Widget item = XmCreateIconGadget(container, "table_item", args, 5); XtManageChild(item); // cleanup XmStringFree(label); for(int i=0;i<modelinfo->columns-1;i++) { XmStringFree(details[i]); } XtFree((char*)details); } char* ui_type_to_string(UiModelType type, void *data, Boolean *free) { switch(type) { case UI_STRING: *free = FALSE; return data; case UI_INTEGER: { *free = TRUE; int *val = data; sstr_t str = ucx_asprintf(ucx_default_allocator(), "%d", *val); return str.ptr; } } *free = FALSE; return NULL; } void ui_table_action_callback( Widget widget, UiTreeEventData *event, XmContainerSelectCallbackStruct *sel) { UiListSelection *selection = malloc(sizeof(UiListSelection)); selection->count = sel->selected_item_count; selection->rows = calloc(selection->count, sizeof(int)); for(int i=0;i<selection->count;i++) { intptr_t index; XtVaGetValues(sel->selected_items[i], XmNuserData, &index, NULL); selection->rows[i] = index; } UiEvent e; e.obj = event->obj; e.window = event->obj->window; e.document = event->obj->document; e.eventdata = selection; e.intval = selection->count > 0 ? selection->rows[0] : -1; event->activate(&e, event->userdata); free(selection); } void ui_table_select_callback( Widget widget, UiTreeEventData *event, XmContainerSelectCallbackStruct *sel) { UiListSelection *selection = malloc(sizeof(UiListSelection)); selection->count = sel->selected_item_count; selection->rows = calloc(selection->count, sizeof(int)); for(int i=0;i<selection->count;i++) { intptr_t index; XtVaGetValues(sel->selected_items[i], XmNuserData, &index, NULL); selection->rows[i] = index; } UiEvent e; e.obj = event->obj; e.window = event->obj->window; e.document = event->obj->document; e.eventdata = selection; e.intval = selection->count > 0 ? selection->rows[0] : -1; event->selection(&e, event->userdata); free(selection); }