ui/gtk/menu.c

branch
newapi
changeset 178
7c3ff86ee9d4
parent 175
2cb06c231057
child 253
087cc9216f28
--- a/ui/gtk/menu.c	Sun Apr 16 10:20:21 2023 +0200
+++ b/ui/gtk/menu.c	Tue May 23 11:11:28 2023 +0200
@@ -34,184 +34,42 @@
 #include "menu.h"
 #include "toolkit.h"
 #include "../common/context.h"
+#include "../common/menu.h"
 #include "../ui/properties.h"
 #include "../ui/window.h"
 #include "container.h"
 
-static UcxList *menus;
-static 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);
-}
+#include <cx/linked_list.h>
+#include <cx/array_list.h>
 
-void ui_submenu_end() {
-    if(ucx_list_size(current) < 2) {
-        return;
-    }
-    current = ucx_list_remove(current, current);
-    //UcxList *c = 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);
-}
 
-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
 GtkWidget *ui_create_menubar(UiObject *obj) {
-    if(menus == NULL) {
+    UiMenu *menus_begin = uic_get_menu_list();
+    if(menus_begin == NULL) {
         return NULL;
     }
     
     GtkWidget *mb = gtk_menu_bar_new();
     
-    UcxList *ls = menus;
+    UiMenu *ls = menus_begin;
     while(ls) {
-        UiMenu *menu = ls->data;
-        menu->item.add_to(mb, 0, &menu->item, obj);
+        UiMenu *menu = ls;
+        add_menu_widget(mb, 0, &menu->item, obj);
         
-        ls = ls->next;
+        ls = (UiMenu*)ls->item.next;
     }
     
     return mb;
@@ -224,13 +82,12 @@
     GtkWidget *menu_item = gtk_menu_item_new_with_mnemonic(menu->label);
     gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item), menu_widget);
     
-    UcxList *ls = menu->items;
+    UiMenuItemI *it = menu->items_begin;
     int index = 0;
-    while(ls) {
-        UiMenuItemI *i = ls->data;
-        i->add_to(menu_widget, index, i, obj);
+    while(it) {
+        createMenuItem[it->type](menu_widget, index, it, obj);
         
-        ls = ls->next;
+        it = it->next;
         index++;
     }
     
@@ -360,10 +217,10 @@
 
 void add_menuitem_list_widget(GtkWidget *p, int index, UiMenuItemI *item, UiObject *obj) {
     UiMenuItemList *il = (UiMenuItemList*)item;
-    UcxMempool *mp = obj->ctx->mempool;
+    const CxAllocator *a = obj->ctx->allocator;
     
-    UiActiveMenuItemList *ls = ucx_mempool_malloc(
-            mp,
+    UiActiveMenuItemList *ls = cxMalloc(
+            a,
             sizeof(UiActiveMenuItemList));
     
     ls->object = obj;
@@ -519,12 +376,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);
     
@@ -555,6 +415,7 @@
     
     if(groups) {
         uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, groups);
+        cxListDestroy(groups);
     }
 }
 
@@ -569,12 +430,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,5 +469,6 @@
     
     if(groups) {
         uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, groups);
+        cxListDestroy(groups);
     }
 }

mercurial