ui/motif/tree.c

Thu, 28 Aug 2014 16:34:09 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 28 Aug 2014 16:34:09 +0200
changeset 56
87e3a5dc66dd
parent 52
25e5390cce41
child 60
7cd1b8890302
permissions
-rw-r--r--

added text widget (Qt)

/*
 * 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 "../common/context.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 scrolled window
    UiContainer *ct = uic_get_current_container(obj);
    Widget parent = ct->add(ct, args, &n);
    
    XtSetArg(args[n], XmNscrollingPolicy, XmAUTOMATIC);
    n++;
    XtSetArg(args[n], XmNshadowThickness, 0);
    n++;
    Widget scrollw = XmCreateScrolledWindow(parent, "scroll_win", args, n);
    XtManageChild(scrollw);
    
    // 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]);
    }
    n = 0;
    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++;
    XtSetArg(args[n], XmNwidth, 600);
    n++;
    
    // create widget
    //UiContainer *ct = uic_get_current_container(obj);
    //Widget parent = ct->add(ct, args, &n);
    
    Widget container = XmCreateContainer(scrollw, "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;
    event->last_selection = NULL;
    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 width = 0;
    while(data) {
        int w = ui_add_icon_gadget(container, modelinfo, data);
        if(w > width) {
            width = w;
        }
        data = model->next(model);
    }
    
    // set new XmContainer width
    XtVaSetValues(container, XmNwidth, width, NULL);
    
    // cleanup
    for(int i=0;i<modelinfo->columns;i++) {
        XmStringFree(header[i]);
    }
    XtFree((char*)header);
    
    return scrollw;
}

#define UI_COL_CHAR_WIDTH 12

int ui_add_icon_gadget(Widget container, UiModelInfo *modelinfo, void *data) {
    int width = 50;
    
    if(modelinfo->columns == 0) {
        return width;
    }
    
    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);
        
        // column width
        width += strlen(str) * UI_COL_CHAR_WIDTH;
        
        
        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);
        
        // column width
        width += strlen(str) * UI_COL_CHAR_WIDTH;
        
        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); 
    // create widget
    Widget item = XmCreateIconGadget(container, "table_item", args, 4);
    XtManageChild(item);
    
    // cleanup
    XmStringFree(label);
    for(int i=0;i<modelinfo->columns-1;i++) {
        XmStringFree(details[i]);
    }
    XtFree((char*)details);
    
    return width;
}

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 = ui_list_selection(sel);
    
    UiEvent e;
    e.obj = event->obj;
    e.window = event->obj->window;
    e.document = event->obj->ctx->document;
    e.eventdata = selection;
    e.intval = selection->count > 0 ? selection->rows[0] : -1;
    event->activate(&e, event->userdata);
    
    free(event->last_selection->rows);
    free(event->last_selection);
    event->last_selection = selection;
}

void ui_table_select_callback(
        Widget widget,
        UiTreeEventData *event,
        XmContainerSelectCallbackStruct *sel)
{ 
    UiListSelection *selection = ui_list_selection(sel);
    if(!ui_compare_list_selection(selection, event->last_selection)) {
        UiEvent e;
        e.obj = event->obj;
        e.window = event->obj->window;
        e.document = event->obj->ctx->document;
        e.eventdata = selection;
        e.intval = selection->count > 0 ? selection->rows[0] : -1;
        event->selection(&e, event->userdata);
    }
    if(event->last_selection) {
        free(event->last_selection->rows);
        free(event->last_selection);
    }
    event->last_selection = selection;
}

UiListSelection* ui_list_selection(XmContainerSelectCallbackStruct *xs) {
    UiListSelection *selection = malloc(sizeof(UiListSelection));
    selection->count = xs->selected_item_count;
    selection->rows = calloc(selection->count, sizeof(int));
    for(int i=0;i<selection->count;i++) {
        int index;
        XtVaGetValues(xs->selected_items[i], XmNpositionIndex, &index, NULL);
        selection->rows[i] = index;
    }
    return selection;
}

Boolean ui_compare_list_selection(UiListSelection *s1, UiListSelection *s2) {
    if(!s1 || !s2) {
        return FALSE;
    } 
    if(s1->count != s2->count) {
        return FALSE;
    }
    for(int i=0;i<s1->count;i++) {
        if(s1->rows[i] != s2->rows[i]) {
            return FALSE;
        }
    }
    return TRUE;
}

mercurial