fixes var binding and motif tableview

Sat, 17 Sep 2016 19:32:44 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 17 Sep 2016 19:32:44 +0200
changeset 128
c284c15509a8
parent 127
ce342364fad5
child 129
5babf09f5f19

fixes var binding and motif tableview

ui/cocoa/menu.m file | annotate | diff | comparison | revisions
ui/cocoa/objs.mk file | annotate | diff | comparison | revisions
ui/cocoa/toolbar.h file | annotate | diff | comparison | revisions
ui/cocoa/toolbar.m file | annotate | diff | comparison | revisions
ui/cocoa/window.m file | annotate | diff | comparison | revisions
ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/context.h file | annotate | diff | comparison | revisions
ui/gtk/model.c file | annotate | diff | comparison | revisions
ui/gtk/tree.c file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
ui/motif/tree.c file | annotate | diff | comparison | revisions
ui/motif/tree.h file | annotate | diff | comparison | revisions
ui/motif/window.c file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
ui/ui/tree.h file | annotate | diff | comparison | revisions
--- a/ui/cocoa/menu.m	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/cocoa/menu.m	Sat Sep 17 19:32:44 2016 +0200
@@ -118,10 +118,7 @@
 @end
 
 
-//static NSMenu *currentMenu = NULL;
-
-static UcxList *current;
-
+static NSMenu *currentMenu = NULL;
 static int currentItemIndex = 0;
 static UiMenuDelegate *delegate;
 
@@ -144,34 +141,8 @@
     [menu setAutoenablesItems:NO];
     
     [[NSApp mainMenu] setSubmenu:menu forItem:menuItem];
-    //currentMenu = menu;
+    currentMenu = menu;
     currentItemIndex = 0;
-    
-    current = ucx_list_prepend(NULL, menu);
-}
-
-void ui_submenu(char *title) {
-    NSString *str = [[NSString alloc] initWithUTF8String:title];
-    NSMenu *currentMenu = current->data;
-    
-    NSMenu *menu = [[NSMenu alloc] initWithTitle: str];
-    NSMenuItem *menuItem = [currentMenu addItemWithTitle:str
-                                                       action:nil keyEquivalent:@""];
-    [menu setDelegate: delegate];
-    [menu setAutoenablesItems:NO];
-    
-    [currentMenu setSubmenu:menu forItem:menuItem];
-    //currentMenu = menu;
-    currentItemIndex = 0;
-    
-    current = ucx_list_prepend(current, menu);
-}
-
-void ui_submenu_end() {
-    if(ucx_list_size(current) < 2) {
-        return;
-    }
-    current = ucx_list_remove(current, current);
 }
 
 void ui_menuitem(char *label, ui_callback f, void *data) {
@@ -198,7 +169,6 @@
     }
     va_end(ap);
     
-    NSMenu *currentMenu = current->data;
     [currentMenu addItem:item];
     
     currentItemIndex++;
@@ -222,7 +192,6 @@
     }
     va_end(ap);
     
-    NSMenu *currentMenu = current->data;
     [currentMenu addItem:item];
     
     currentItemIndex++;
@@ -232,7 +201,6 @@
     EventWrapper *event = [[EventWrapper alloc]initWithData:data callback:f];
     NSString *str = [[NSString alloc] initWithUTF8String:label];
     
-    NSMenu *currentMenu = current->data;
     NSMenuItem *item = [currentMenu addItemWithTitle:str
                                               action:@selector(handleStateEvent:) keyEquivalent:@""];
     [item setTarget:event];
@@ -245,7 +213,6 @@
     EventWrapper *event = [[EventWrapper alloc]initWithData:NULL callback:NULL];
     NSString *str = [[NSString alloc] initWithUTF8String:label];
     
-    NSMenu *currentMenu = current->data;
     NSMenuItem *item = [currentMenu addItemWithTitle:str
                                               action:@selector(handleStateEvent:) keyEquivalent:@""];
     [item setTarget:event];
@@ -255,13 +222,11 @@
 }
 
 void ui_menuseparator() {
-    NSMenu *currentMenu = current->data;
     [currentMenu addItem: [NSMenuItem separatorItem]];
     currentItemIndex++;
 }
 
 void ui_menuitem_list (UiList *items, ui_callback f, void *data) {
-    NSMenu *currentMenu = current->data;
     [delegate addList:items menu:currentMenu index:currentItemIndex callback:f data:data];
 }
 
--- a/ui/cocoa/objs.mk	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/cocoa/objs.mk	Sat Sep 17 19:32:44 2016 +0200
@@ -38,7 +38,6 @@
 COCOAOBJ += text.o
 COCOAOBJ += resource.o
 COCOAOBJ += tree.o
-COCOAOBJ += graphics.o
 
 
 TOOLKITOBJS += $(COCOAOBJ:%=$(COCOA_OBJPRE)%)
--- a/ui/cocoa/toolbar.h	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/cocoa/toolbar.h	Sat Sep 17 19:32:44 2016 +0200
@@ -67,32 +67,6 @@
 
 @end
 
-/*
- * UiToolbarItem
- *
- * toolbar item with label and icon
- */
-@interface UiToolbarItem : NSObject <UiToolItem> {
-    char           *name;
-    char           *label;
-    // icon
-    ui_callback    callback;
-    void           *userdata;
-    UcxList        *groups;
-    BOOL           isToggleButton;
-}
-
-- (UiToolbarItem*) initWithIdentifier:(char*)identifier
-                                     label:(char*)lbl
-                                  callback:(ui_callback)f
-                                  userdata:(void*)data;
-
-- (void) setIsToggleButton:(BOOL)t;
-
-
-@end
-
-
 
 /*
  * UiToolbarDelegate
--- a/ui/cocoa/toolbar.m	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/cocoa/toolbar.m	Sat Sep 17 19:32:44 2016 +0200
@@ -122,83 +122,6 @@
 @end
 
 
-/* ---------------------      UiToolbarItem     --------------------- */
-
-@implementation UiToolbarItem
-
-- (UiToolbarItem*) initWithIdentifier:(char*)identifier
-                                     label:(char*)lbl
-                                  callback:(ui_callback)f
-                                  userdata:(void*)data
-{
-    name = identifier;
-    label = lbl;
-    callback = f;
-    userdata = data;
-    groups = NULL;
-    isToggleButton = NO;
-    return self;
-}
-
-- (void) setIsToggleButton:(BOOL)t {
-    isToggleButton = t;
-}
-
-- (void) addGroup:(int)group {
-    groups = ucx_list_append(groups, (void*)(intptr_t)group);
-}
-
-
-- (NSToolbarItem *) createItem:(NSToolbar*)toolbar
-                    identifier:(NSString*)identifier
-                        object:(UiObject*)obj
-{
-    NSToolbarItem *item = [[[NSToolbarItem alloc] initWithItemIdentifier:
-                            identifier] autorelease];
-    //[item setLabel:[s label]];
-    //[item setPaletteLabel:[s label]];
-    NSString *l = [[NSString alloc]initWithUTF8String:label];
-    [item setLabel:l];
-    [item setPaletteLabel:@"Operation"];
-    
-    // create button ...
-    NSRect frame = NSMakeRect(0, 0, 40, 22);
-    //NSSearchField *sf = [[NSSearchField alloc]initWithFrame:frame];
-    NSButton *button = [[NSButton alloc]initWithFrame:frame];
-    //[button setImage:[s buttonImage]];
-    //[button setImage:[NSImage imageNamed: NSImageNameAddTemplate]];
-    
-    // TODO: image
-    [button setImage:[NSImage imageNamed: NSImageNameRemoveTemplate]];
-    
-    [button setBezelStyle: NSTexturedRoundedBezelStyle];
-    
-    // event
-    EventWrapper *event = [[EventWrapper alloc]
-                           initWithData:userdata callback:callback];
-    if(isToggleButton) {
-        [button setButtonType: NSPushOnPushOffButton];
-        [button setAction:@selector(handleToggleEvent:)];
-    } else {
-        [button setAction:@selector(handleEvent:)];
-    }
-    [button setTarget:event];
-    
-    if(groups) {
-        uic_add_group_widget(obj->ctx, item, groups);
-    }
-    
-    [item setView:button];
-    return item;
-}
-
-- (UcxList*) groups {
-    return groups;
-}
-
-@end
-
-
 /* ---------------------      UiToolbarDelegate      --------------------- */
 
 @implementation UiToolbarDelegate
@@ -292,17 +215,6 @@
     toolbar_delegate = [[UiToolbarDelegate alloc]init];
 }
 
-void ui_toolitem(char *name, char *label, ui_callback f, void *udata) {
-    UiToolbarItem *item = [[UiToolbarItem alloc]
-                                initWithIdentifier: name
-                                label: label
-                                callback: f
-                                userdata: udata];
-    
-    NSString *identifier = [[NSString alloc]initWithUTF8String:name];
-    [toolbar_delegate addItem: identifier item: item];
-}
-
 void ui_toolitem_st(char *name, char *stockid, ui_callback f, void *udata) {
     ui_toolitem_stgr(name, stockid, f, udata, -1);
 }
--- a/ui/cocoa/window.m	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/cocoa/window.m	Sat Sep 17 19:32:44 2016 +0200
@@ -186,6 +186,7 @@
     
     obj->widget = (NSView*)window;
     obj->window = window_data;
+    obj->document = NULL;
     obj->container = ui_window_container(obj, window);
     
     
--- a/ui/common/context.c	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/common/context.c	Sat Sep 17 19:32:44 2016 +0200
@@ -56,12 +56,17 @@
     return ctx;
 }
 
+UiContext* uic_root_context(UiContext *ctx) {
+    return ctx->parent ? uic_root_context(ctx->parent) : ctx;
+}
+
 void uic_context_set_document(UiContext *ctx, void *document) {
     UiContext *docctx = ui_document_context(document);
     if(!docctx) {
         return;
     }
     docctx->obj = ctx->obj;
+    docctx->parent = ctx;
     
     if(ctx->document) {
         uic_context_detach_document(ctx, ctx->document);
@@ -72,7 +77,7 @@
     UcxMapIterator i = ucx_map_iterator(docctx->vars);
     UiVar *var;
     UCX_MAP_FOREACH(key, var, i) {
-        UiVar *v = ucx_map_get(ctx->vars, key);
+        UiVar *v = ucx_map_get(uic_root_context(ctx)->vars, key);
         if(v) {
             if(v->isextern) {
                 fprintf(
@@ -87,7 +92,7 @@
             }
             
             // copy value
-            uic_move_var(v, var, 1);
+            uic_move_var(v, var, TRUE);
             var->from = v->from;
             
             // TODO: free var struct
@@ -128,11 +133,12 @@
         }
     }
     
-    if(docctx->obj) {
-        docctx->obj->ctx->document = NULL;
+    if(docctx->parent) {
+        docctx->parent->document = NULL;
     }
     
     docctx->obj = NULL;
+    docctx->parent = NULL;
 }
 
 UiVar* uic_get_var(UiContext *ctx, char *name) {
@@ -188,7 +194,7 @@
     }
 }
 
-void uic_move_var(UiVar *from, UiVar *to, int set) {
+void uic_move_var(UiVar *from, UiVar *to, UiBool set) {
     switch(from->type) {
         case UI_VAR_INTEGER: {
             //memcpy(to->value, from->value, sizeof(UiInteger));
@@ -213,10 +219,10 @@
         case UI_VAR_TEXT: {
             UiText *f = from->value;
             UiText *t = to->value;
-            char *tvalue = t->value;
+            char *tvalue = t->value ? t->value : "";
             int tpos = t->pos;
             memcpy(t, f, sizeof(UiText));
-            if(set && tvalue) {
+            if(set) {
                 t->set(t, tvalue);
                 t->setposition(t, tpos);
             } else {
--- a/ui/common/context.h	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/common/context.h	Sat Sep 17 19:32:44 2016 +0200
@@ -94,13 +94,13 @@
 };
 
 UiContext* uic_context(UiObject *toplevel, UcxMempool *mp);
-
+UiContext* uic_root_context(UiContext *ctx);
 void uic_context_set_document(UiContext *ctx, void *document);
 void uic_context_detach_document(UiContext *ctx, void *document);
 
 UiVar* uic_get_var(UiContext *ctx, char *name);
 UiVar* uic_connect_var(UiContext *ctx, char *name, int type);
-void uic_move_var(UiVar *from, UiVar *to, int set);
+void uic_move_var(UiVar *from, UiVar *to, UiBool set);
 void uic_reg_var(UiContext *ctx, char *name, int type, size_t vs, void *value);
 void uic_add_var(
         UiContext *ctx,
--- a/ui/gtk/model.c	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/gtk/model.c	Sat Sep 17 19:32:44 2016 +0200
@@ -105,14 +105,14 @@
     return G_TYPE_INVALID;
 }
 
-static void ui_model_set_value(GType type, void *data, GValue *value) {
+static void ui_model_set_value(UiModelType type, void *data, GValue *value) {
     switch(type) {
-        case G_TYPE_STRING: {
+        case UI_STRING: {
             value->g_type = G_TYPE_STRING;
             g_value_set_string(value, data);
             return;
         }
-        case G_TYPE_INT: {
+        case UI_INTEGER: {
             value->g_type = G_TYPE_INT;
             int *i = data;
             g_value_set_int(value, *i);
@@ -126,20 +126,11 @@
     UiListModel *model = g_object_new(list_model_type, NULL);
     model->info = info;
     model->list = list;
-    model->columntypes = calloc(sizeof(GType), 2 * info->columns);
-    int ncol = 0;
+    model->columntypes = malloc(sizeof(GType));
+    model->numcolumns = info->columns;
     for(int i=0;i<info->columns;i++) {
-        UiModelType type = info->types[i];
-        if(type == UI_ICON_TEXT) {
-            model->columntypes[ncol] = G_TYPE_STRING;
-            ncol++;
-            model->columntypes[ncol] = G_TYPE_STRING;
-        } else {
-            model->columntypes[ncol] = ui_gtk_type(info->types[i]);
-        }
-        ncol++;
+        model->columntypes[i] = ui_gtk_type(info->types[i]);
     }
-    model->numcolumns = ncol;
     return model;
 }
 
@@ -246,7 +237,7 @@
     //list->current = iter->user_data3;
     if(model->info->getvalue) {
         void *data = model->info->getvalue(iter->user_data3, column);
-        ui_model_set_value(model->columntypes[column], data, value);
+        ui_model_set_value(model->info->types[column], data, value);
     } else {
         value->g_type = G_TYPE_INVALID;
     }
--- a/ui/gtk/tree.c	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/gtk/tree.c	Sat Sep 17 19:32:44 2016 +0200
@@ -134,33 +134,14 @@
 UIWIDGET ui_table_var(UiObject *obj, UiListPtr *list, UiModelInfo *modelinfo) {
     // create treeview
     GtkWidget *view = gtk_tree_view_new();
-    int addi = 0;
     for(int i=0;i<modelinfo->columns;i++) {
-        GtkTreeViewColumn *column = NULL;
-        if(modelinfo->types[i] == UI_ICON_TEXT) {
-            column = gtk_tree_view_column_new();
-            gtk_tree_view_column_set_title(column, modelinfo->titles[i]);
-            
-            GtkCellRenderer *iconrenderer = gtk_cell_renderer_pixbuf_new();
-            GtkCellRenderer *textrenderer = gtk_cell_renderer_text_new();
-            
-            gtk_tree_view_column_pack_end(column, textrenderer, TRUE);
-            gtk_tree_view_column_pack_start(column, iconrenderer, FALSE);
-            
-            
-            gtk_tree_view_column_add_attribute(column, iconrenderer, "icon-name", i);
-            gtk_tree_view_column_add_attribute(column, textrenderer, "text", i+1);
-            
-            addi++;
-        } else {
-            GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
-            column = gtk_tree_view_column_new_with_attributes(
+        GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
+        GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(
                 modelinfo->titles[i],
                 renderer,
                 "text",
-                i + addi,
+                i,
                 NULL);
-        }
         gtk_tree_view_column_set_resizable(column, TRUE);
         gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
     }
@@ -252,7 +233,8 @@
 
 void ui_listview_update(UiEvent *event, UiTableView *view) {
     UiListModel *model = ui_list_model_new(view->list, view->modelinfo);
-    gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model));   
+    gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), NULL);
+    gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(model));
     
     // TODO: free old model
 }
--- a/ui/gtk/window.c	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/gtk/window.c	Sat Sep 17 19:32:44 2016 +0200
@@ -68,6 +68,7 @@
     UiObject *obj = ucx_mempool_calloc(mp, 1, sizeof(UiObject));  
     obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     obj->ctx = uic_context(obj, mp);
+    obj->window = window_data;
     
     if(title != NULL) {
         gtk_window_set_title(GTK_WINDOW(obj->widget), title);
--- a/ui/motif/tree.c	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/motif/tree.c	Sat Sep 17 19:32:44 2016 +0200
@@ -118,6 +118,15 @@
         data = model->next(model);
     }
     
+    UiTableView *tableview = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiTableView));
+    tableview->widget = container;
+    tableview->list = model;
+    tableview->modelinfo = modelinfo;
+    model->observers = ui_add_observer(
+            model->observers,
+            (ui_callback)ui_table_update,
+            tableview);
+    
     // set new XmContainer width
     XtVaSetValues(container, XmNwidth, width, NULL);
     
@@ -130,6 +139,37 @@
     return scrollw;
 }
 
+void ui_table_update(UiEvent *event, UiTableView *view) {
+    // clear container
+    Widget *children;
+    int nc;
+
+    XtVaGetValues(
+            view->widget,
+            XmNchildren,
+            &children,
+            XmNnumChildren,
+            &nc,
+            NULL);
+
+    for(int i=0;i<nc;i++) {
+        XtDestroyWidget(children[i]);
+    }
+    
+    UiList *model = view->list;
+    
+    void *data = model->first(model);
+    int width = 0;
+    while(data) {
+        int w = ui_add_icon_gadget(view->widget, view->modelinfo, data);
+        if(w > width) {
+            width = w;
+        }
+        data = model->next(model);
+    }
+    
+}
+
 #define UI_COL_CHAR_WIDTH 12
 
 int ui_add_icon_gadget(Widget container, UiModelInfo *modelinfo, void *data) {
--- a/ui/motif/tree.h	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/motif/tree.h	Sat Sep 17 19:32:44 2016 +0200
@@ -43,6 +43,13 @@
     UiListSelection *last_selection;
 } UiTreeEventData;    
 
+typedef struct UiTableView {
+    Widget      widget;
+    UiList      *list;
+    UiModelInfo *modelinfo;
+} UiTableView;
+
+void ui_table_update(UiEvent *event, UiTableView *view);
 int ui_add_icon_gadget(Widget container, UiModelInfo *modelinfo, void *data);
 char* ui_type_to_string(UiModelType type, void *data, Boolean *free);
 
--- a/ui/motif/window.c	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/motif/window.c	Sat Sep 17 19:32:44 2016 +0200
@@ -65,6 +65,7 @@
     UcxMempool *mp = ucx_mempool_new(256);
     UiObject *obj = ucx_mempool_calloc(mp, 1, sizeof(UiObject));
     obj->ctx = uic_context(obj, mp);
+    obj->window = window_data;
     
     Arg args[16];
     int n = 0;
--- a/ui/ui/toolkit.h	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/ui/toolkit.h	Sat Sep 17 19:32:44 2016 +0200
@@ -34,10 +34,8 @@
 #ifdef __OBJC__
 #import <Cocoa/Cocoa.h>
 #define UIWIDGET NSView*
-#define UIMENU   NSMenu*
 #else
 typedef void* UIWIDGET;
-typedef void* UIMENU;
 #endif
 
 #elif UI_GTK2 || UI_GTK3
--- a/ui/ui/tree.h	Wed Jul 13 16:40:59 2016 +0200
+++ b/ui/ui/tree.h	Sat Sep 17 19:32:44 2016 +0200
@@ -41,9 +41,7 @@
 
 typedef enum UiModelType {
     UI_STRING = 0,
-    UI_INTEGER,
-    UI_ICON,
-    UI_ICON_TEXT,
+    UI_INTEGER
 } UiModelType;
 
 struct UiModelInfo {

mercurial