ui/motif/menu.c

branch
newapi
changeset 176
bc63cb601f6d
parent 169
fe49cff3c571
--- a/ui/motif/menu.c	Mon May 22 19:44:27 2023 +0200
+++ b/ui/motif/menu.c	Mon May 22 21:21:20 2023 +0200
@@ -38,169 +38,24 @@
 #include "../common/context.h"
 #include "../ui/window.h"
 
-UcxList *menus;
-UcxList *current;
-
-void ui_menu(char *label) {
-    // free current menu hierarchy
-    ucx_list_free(current);
-    
-    // create menu
-    UiMenu *menu = malloc(sizeof(UiMenu));
-    menu->item.add_to = (ui_menu_add_f)add_menu_widget;
-    
-    menu->label  = label;
-    menu->items  = NULL;
-    menu->parent = NULL;    
-    
-    current = ucx_list_prepend(NULL, menu);
-    menus = ucx_list_append(menus, menu);
-    
-}
-
-void ui_submenu(char *label) {
-    UiMenu *menu = malloc(sizeof(UiMenu));
-    menu->item.add_to = (ui_menu_add_f)add_menu_widget;
-    
-    menu->label  = label;
-    menu->items  = NULL;
-    menu->parent = NULL;
-    
-    // add submenu to current menu
-    UiMenu *cm = current->data;
-    cm->items = ucx_list_append(cm->items, menu);
-    
-    // set the submenu to current menu
-    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 *userdata) {
-    ui_menuitem_gr(label, f, userdata, -1);
-}
-
-void ui_menuitem_st(char *stockid, ui_callback f, void *userdata) {
-    ui_menuitem_stgr(stockid, f, userdata, -1);
-}
-
-void ui_menuitem_gr(char *label, ui_callback f, void *userdata, ...) {
-    if(!current) {
-        return;
-    }
-    
-    UiMenuItem *item = malloc(sizeof(UiMenuItem));
-    item->item.add_to = (ui_menu_add_f)add_menuitem_widget;
-    
-    item->label = label;
-    item->userdata = userdata;
-    item->callback = f;
-    item->groups = NULL;
-    
-    // add groups
-    va_list ap;
-    va_start(ap, userdata);
-    int group;
-    while((group = va_arg(ap, int)) != -1) {
-        item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group);
-    }
-    va_end(ap);
-    
-    UiMenu *cm = current->data;
-    cm->items = ucx_list_append(cm->items, item);
-}
+#include <cx/linked_list.h>
+#include <cx/array_list.h>
 
-void ui_menuitem_stgr(char *stockid, ui_callback f, void *userdata, ...) {
-    if(!current) {
-        return;
-    }
-    
-    UiStMenuItem *item = malloc(sizeof(UiStMenuItem));
-    item->item.add_to = (ui_menu_add_f)add_menuitem_st_widget;
-    
-    item->stockid = stockid;
-    item->userdata = userdata;
-    item->callback = f;
-    item->groups = NULL;
-    
-    // add groups
-    va_list ap;
-    va_start(ap, userdata);
-    int group;
-    while((group = va_arg(ap, int)) != -1) {
-        item->groups = ucx_list_append(item->groups, (void*)(intptr_t)group);
-    }
-    va_end(ap);
-    
-    UiMenu *cm = current->data;
-    cm->items = ucx_list_append(cm->items, item);
-}
-
-void ui_menuseparator() {
-    if(!current) {
-        return;
-    }
-    
-    UiMenuItemI *item = malloc(sizeof(UiMenuItemI));
-    item->add_to = (ui_menu_add_f)add_menuseparator_widget;
-    
-    UiMenu *cm = current->data;
-    cm->items = ucx_list_append(cm->items, item);
-}
-
-
-void ui_checkitem(char *label, ui_callback f, void *userdata) {
-    if(!current) {
-        return;
-    }
-    
-    UiCheckItem *item = malloc(sizeof(UiCheckItem));
-    item->item.add_to = (ui_menu_add_f)add_checkitem_widget;
-    item->label = label;
-    item->callback = f;
-    item->userdata = userdata;
-    
-    UiMenu *cm = current->data;
-    cm->items = ucx_list_append(cm->items, item);
-}
-
-void ui_checkitem_nv(char *label, char *vname) {
-    if(!current) {
-        return;
-    }
-    
-    UiCheckItemNV *item = malloc(sizeof(UiCheckItemNV));
-    item->item.add_to = (ui_menu_add_f)add_checkitemnv_widget;
-    item->varname = vname;
-    item->label = label;
-    
-    UiMenu *cm = current->data;
-    cm->items = ucx_list_append(cm->items, item);
-}
-
-void ui_menuitem_list(UiList *items, ui_callback f, void *userdata) {
-    if(!current) {
-        return;
-    }
-    
-    UiMenuItemList *item = malloc(sizeof(UiMenuItemList));
-    item->item.add_to = (ui_menu_add_f)add_menuitem_list_widget;
-    item->callback = f;
-    item->userdata = userdata;
-    item->list = items;
-    
-    UiMenu *cm = current->data;
-    cm->items = ucx_list_append(cm->items, item);
-}
-
+static ui_menu_add_f createMenuItem[] = {
+    /* UI_MENU                 */ add_menu_widget,
+    /* UI_MENU_SUBMENU         */ add_menu_widget,
+    /* UI_MENU_ITEM            */ add_menuitem_widget,
+    /* UI_MENU_STOCK_ITEM      */ add_menuitem_st_widget,
+    /* UI_MENU_CHECK_ITEM      */ add_checkitem_widget,
+    /* UI_MENU_CHECK_ITEM_NV   */ add_checkitemnv_widget,
+    /* UI_MENU_ITEM_LIST       */ add_menuitem_list_widget,
+    /* UI_MENU_ITEM_LIST_NV    */ NULL, // TODO
+    /* UI_MENU_SEPARATOR       */ add_menuseparator_widget
+};
 
 // private menu functions
 void ui_create_menubar(UiObject *obj) {
+    UiMenu *menus = uic_get_menu_list();
     if(!menus) {
         return;
     }
@@ -208,13 +63,12 @@
     Widget menubar = XmCreateMenuBar(obj->widget, "main_list", NULL, 0);
     XtManageChild(menubar);
     
-    UcxList *ls = menus;
+    UiMenu *menu = menus;
     int menu_index = 0;
-    while(ls) {
-        UiMenu *menu = ls->data;
-        menu_index += menu->item.add_to(menubar, menu_index, &menu->item, obj);
+    while(menu) {
+        menu_index += add_menu_widget(menubar, menu_index, &menu->item, obj);
         
-        ls = ls->next;
+        menu = (UiMenu*)menu->item.next;
     }
 }
 
@@ -228,12 +82,11 @@
             NULL);
     Widget m = XmVaCreateSimplePulldownMenu(parent, menu->label, i, NULL, NULL);
     
-    UcxList *ls = menu->items;
+    UiMenuItemI *mi = menu->items_begin;
     int menu_index = 0;
-    while(ls) {
-        UiMenuItemI *mi = ls->data;
-        menu_index += mi->add_to(m, menu_index, mi, obj);
-        ls = ls->next;
+    while(mi) {
+        menu_index += createMenuItem[mi->type](m, menu_index, mi, obj);
+        mi = mi->next;
     }
     
     return 1;
@@ -260,8 +113,8 @@
     XmStringFree(label);
     
     if(mi->callback != NULL) {
-        UiEventData *event = ucx_mempool_malloc(
-                obj->ctx->mempool,
+        UiEventData *event = cxMalloc(
+                obj->ctx->allocator,
                 sizeof(UiEventData));
         event->obj = obj;
         event->userdata = mi->userdata;
@@ -319,8 +172,8 @@
     }
     
     if(mi->callback != NULL) {
-        UiEventData *event = ucx_mempool_malloc(
-                obj->ctx->mempool,
+        UiEventData *event = cxMalloc(
+                obj->ctx->allocator,
                 sizeof(UiEventData));
         event->obj = obj;
         event->userdata = mi->userdata;
@@ -372,8 +225,8 @@
     XmStringFree(label);
     
     if(ci->callback) {
-        UiEventData *event = ucx_mempool_malloc(
-                obj->ctx->mempool,
+        UiEventData *event = cxMalloc(
+                obj->ctx->allocator,
                 sizeof(UiEventData));
         event->obj = obj;
         event->userdata = ci->userdata;
@@ -429,10 +282,9 @@
         UiObject *obj)
 {
     UiMenuItemList *il = (UiMenuItemList*)item;
-    UcxMempool *mp = obj->ctx->mempool;
     
-    UiActiveMenuItemList *ls = ucx_mempool_malloc(
-            mp,
+    UiActiveMenuItemList *ls = cxMalloc(
+            obj->ctx->allocator,
             sizeof(UiActiveMenuItemList));
     
     ls->object = obj;
@@ -575,12 +427,15 @@
     }
     
     // add groups
-    UcxList *groups = NULL;
+    CxList *groups = NULL;
     va_list ap;
     va_start(ap, userdata);
     int group;
     while((group = va_arg(ap, int)) != -1) {
-        ucx_list_append(groups, (void*)(intptr_t)group);
+        if(!groups) {
+            groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
+        }
+        cxListAdd(groups, &group);
     }
     va_end(ap);
     
@@ -605,12 +460,15 @@
     }
     
     // add groups
-    UcxList *groups = NULL;
+    CxList *groups = NULL;
     va_list ap;
     va_start(ap, userdata);
     int group;
     while((group = va_arg(ap, int)) != -1) {
-        ucx_list_append(groups, (void*)(intptr_t)group);
+        if(!groups) {
+            groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
+        }
+        cxListAdd(groups, &group);
     }
     va_end(ap);
     

mercurial