ui/motif/menu.c

branch
newapi
changeset 406
0ebf9d7b23e8
parent 176
bc63cb601f6d
child 416
89ad8467c39f
--- a/ui/motif/menu.c	Wed Dec 04 08:57:35 2024 +0100
+++ b/ui/motif/menu.c	Wed Dec 04 18:31:22 2024 +0100
@@ -41,444 +41,3 @@
 #include <cx/linked_list.h>
 #include <cx/array_list.h>
 
-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;
-    }
-    
-    Widget menubar = XmCreateMenuBar(obj->widget, "main_list", NULL, 0);
-    XtManageChild(menubar);
-    
-    UiMenu *menu = menus;
-    int menu_index = 0;
-    while(menu) {
-        menu_index += add_menu_widget(menubar, menu_index, &menu->item, obj);
-        
-        menu = (UiMenu*)menu->item.next;
-    }
-}
-
-int add_menu_widget(Widget parent, int i, UiMenuItemI *item, UiObject *obj) {
-    UiMenu *menu = (UiMenu*)item;
-    
-    Widget menuItem = XtVaCreateManagedWidget(
-            menu->label,
-            xmCascadeButtonWidgetClass,
-            parent,
-            NULL);
-    Widget m = XmVaCreateSimplePulldownMenu(parent, menu->label, i, NULL, NULL);
-    
-    UiMenuItemI *mi = menu->items_begin;
-    int menu_index = 0;
-    while(mi) {
-        menu_index += createMenuItem[mi->type](m, menu_index, mi, obj);
-        mi = mi->next;
-    }
-    
-    return 1;
-}
-
-int add_menuitem_widget(
-        Widget parent,
-        int i,
-        UiMenuItemI *item,
-        UiObject *obj)
-{
-    UiMenuItem *mi = (UiMenuItem*)item;
-    
-    Arg args[1];
-    XmString label = XmStringCreateLocalized(mi->label);
-    XtSetArg(args[0], XmNlabelString, label);
-    
-    Widget mitem = XtCreateManagedWidget(
-            "menubutton",
-            xmPushButtonWidgetClass,
-            parent,
-            args,
-            1);
-    XmStringFree(label);
-    
-    if(mi->callback != NULL) {
-        UiEventData *event = cxMalloc(
-                obj->ctx->allocator,
-                sizeof(UiEventData));
-        event->obj = obj;
-        event->userdata = mi->userdata;
-        event->callback = mi->callback;
-        event->value = 0;
-        XtAddCallback(
-                mitem,
-                XmNactivateCallback,
-                (XtCallbackProc)ui_push_button_callback,
-                event);
-    }
-    
-    if(mi->groups) {
-        uic_add_group_widget(obj->ctx, mitem, (ui_enablefunc)XtSetSensitive, mi->groups);
-    }
-    
-    return 1;
-}
-
-int add_menuitem_st_widget(Widget parent, int i, UiMenuItemI *item, UiObject *obj) {
-    UiStMenuItem *mi = (UiStMenuItem*)item;
-    
-    UiStockItem *si = ui_get_stock_item(mi->stockid);
-    if(!si) {
-        fprintf(stderr, "UI Error: unknown stock id: %s\n", mi->stockid);
-        return 0;
-    }
-    
-    int n = 0;
-    Arg args[4];
-    XmString label = XmStringCreateLocalized(si->label);
-    XmString at = NULL;
-    
-    XtSetArg(args[n], XmNlabelString, label);
-    n++;
-    if(si->accelerator) {
-        XtSetArg(args[n], XmNaccelerator, si->accelerator);
-        n++;
-    }
-    if(si->accelerator_label) {
-        at = XmStringCreateLocalized(si->accelerator_label);
-        XtSetArg(args[n], XmNacceleratorText, at);
-        n++;
-    }
-    
-    Widget mitem = XtCreateManagedWidget(
-            "menubutton",
-            xmPushButtonWidgetClass,
-            parent,
-            args,
-            n);
-    XmStringFree(label);
-    if(at) {
-        XmStringFree(at);
-    }
-    
-    if(mi->callback != NULL) {
-        UiEventData *event = cxMalloc(
-                obj->ctx->allocator,
-                sizeof(UiEventData));
-        event->obj = obj;
-        event->userdata = mi->userdata;
-        event->callback = mi->callback;
-        event->value = 0;
-        XtAddCallback(
-                mitem,
-                XmNactivateCallback,
-                (XtCallbackProc)ui_push_button_callback,
-                event);
-    }
-    
-    if(mi->groups) {
-        uic_add_group_widget(obj->ctx, mitem, (ui_enablefunc)XtSetSensitive, mi->groups);
-    }
-    
-    return 1;
-}
-
-int add_menuseparator_widget(
-        Widget parent,
-        int i,
-        UiMenuItemI *item,
-        UiObject *obj)
-{
-    Widget s = XmCreateSeparatorGadget (parent, "menu_separator", NULL, 0);
-    XtManageChild(s);
-    return 1;
-}
-
-int add_checkitem_widget(
-        Widget parent,
-        int i,
-        UiMenuItemI *item,
-        UiObject *obj)
-{
-    UiCheckItem *ci = (UiCheckItem*)item;
-    
-    Arg args[3];
-    XmString label = XmStringCreateLocalized(ci->label);
-    XtSetArg(args[0], XmNlabelString, label);
-    XtSetArg(args[1], XmNvisibleWhenOff, 1);
-    Widget checkbox = XtCreateManagedWidget(
-            "menutogglebutton",
-            xmToggleButtonWidgetClass,
-            parent,
-            args,
-            2);
-    XmStringFree(label);
-    
-    if(ci->callback) {
-        UiEventData *event = cxMalloc(
-                obj->ctx->allocator,
-                sizeof(UiEventData));
-        event->obj = obj;
-        event->userdata = ci->userdata;
-        event->callback = ci->callback;
-        XtAddCallback(
-            checkbox,
-            XmNvalueChangedCallback,
-            (XtCallbackProc)ui_toggle_button_callback,
-            event);
-    }
-    
-    return 1;
-}
-
-int add_checkitemnv_widget(
-        Widget parent,
-        int i,
-        UiMenuItemI *item,
-        UiObject *obj)
-{
-    UiCheckItemNV *ci = (UiCheckItemNV*)item;
-    
-    Arg args[3];
-    XmString label = XmStringCreateLocalized(ci->label);
-    XtSetArg(args[0], XmNlabelString, label);
-    XtSetArg(args[1], XmNvisibleWhenOff, 1);
-    Widget checkbox = XtCreateManagedWidget(
-            "menutogglebutton",
-            xmToggleButtonWidgetClass,
-            parent,
-            args,
-            2);
-    XmStringFree(label);
-    
-    UiVar *var = uic_create_var(obj->ctx, ci->varname, UI_VAR_INTEGER);
-    if(var) {
-        UiInteger *value = var->value;
-        value->obj = checkbox;
-        value->get = ui_toggle_button_get;
-        value->set = ui_toggle_button_set;
-        value = 0;
-    } else {
-        // TODO: error
-    }
-    
-    return 1;
-}
-
-int add_menuitem_list_widget(
-        Widget parent,
-        int i,
-        UiMenuItemI *item,
-        UiObject *obj)
-{
-    UiMenuItemList *il = (UiMenuItemList*)item;
-    
-    UiActiveMenuItemList *ls = cxMalloc(
-            obj->ctx->allocator,
-            sizeof(UiActiveMenuItemList));
-    
-    ls->object = obj;
-    ls->menu = parent;
-    ls->index = i;
-    ls->oldcount = 0;
-    ls->list = il->list;
-    ls->callback = il->callback;
-    ls->userdata = il->userdata;
-    
-    ls->list->observers = ui_add_observer(
-            ls->list->observers,
-            (ui_callback)ui_update_menuitem_list,
-            ls);
-    
-    ui_update_menuitem_list(NULL, ls);
-    
-    return 0;
-}
-
-void ui_update_menuitem_list(UiEvent *event, UiActiveMenuItemList *list) {
-    Arg args[4];
-    
-    // remove old items
-    if(list->oldcount > 0) {
-        Widget *children;
-        int nc;
-        
-        XtVaGetValues(
-                list->menu,
-                XmNchildren,
-                &children,
-                XmNnumChildren,
-                &nc,
-                NULL);
-        
-        for(int i=0;i<list->oldcount;i++) {
-            XtDestroyWidget(children[list->index + i]);
-        }
-    }
-    
-    char *str = ui_list_first(list->list);
-    if(str) {
-        // add separator
-        XtSetArg(args[0], XmNpositionIndex, list->index);
-        Widget s = XmCreateSeparatorGadget (list->menu, "menu_separator", args, 1);
-        XtManageChild(s);
-    }
-    int i = 1;
-    while(str) {
-        XmString label = XmStringCreateLocalized(str);
-        XtSetArg(args[0], XmNlabelString, label);
-        XtSetArg(args[1], XmNpositionIndex, list->index + i);
-
-        Widget mitem = XtCreateManagedWidget(
-                "menubutton",
-                xmPushButtonWidgetClass,
-                list->menu,
-                args,
-                2);
-        XmStringFree(label);
-        
-        if(list->callback) {
-            // TODO: use mempool
-            UiEventData *event = malloc(sizeof(UiEventData));
-            event->obj = list->object;
-            event->userdata = list->userdata;
-            event->callback = list->callback;
-            event->value = i - 1;
-
-            XtAddCallback(
-                mitem,
-                XmNactivateCallback,
-                (XtCallbackProc)ui_push_button_callback,
-                event);
-        }
-        
-        str = ui_list_next(list->list);
-        i++;
-    }
-    
-    list->oldcount = i;
-}
-
-void ui_menu_event_wrapper(Widget widget, XtPointer udata, XtPointer cdata) {
-    UiEventData *event = udata;
-    UiEvent e;
-    e.obj = event->obj;
-    e.window = event->obj->window;
-    e.document = event->obj->ctx->document;
-    e.intval = 0;
-    event->callback(&e, event->userdata);    
-}
-
-
-/*
- * widget menu functions
- */
-
-static void ui_popup_handler(Widget widget, XtPointer data,  XEvent *event, Boolean *c) {
-    Widget menu = data;
-    XmMenuPosition(menu, (XButtonPressedEvent *)event);
-    XtManageChild(menu);
-    
-    *c = FALSE;
-}
-
-UIMENU ui_contextmenu(UiObject *obj) {
-    UiContainer *ct = uic_get_current_container(obj);
-    if(ct->current) {
-        return ui_contextmenu_w(obj, ct->current);
-    } else {
-        return NULL; // TODO: warn
-    }
-}
-
-UIMENU ui_contextmenu_w(UiObject *obj, UIWIDGET widget) {
-    UiContainer *ct = uic_get_current_container(obj);
-    
-    Widget menu = XmCreatePopupMenu(widget, "popup_menu", NULL, 0);
-    ct->menu = menu;
-    
-    XtAddEventHandler(widget, ButtonPressMask, FALSE, ui_popup_handler, menu);
-    
-    return menu;
-}
-
-void ui_contextmenu_popup(UIMENU menu) {
-    
-}
-
-void ui_widget_menuitem(UiObject *obj, char *label, ui_callback f, void *userdata) {
-    ui_widget_menuitem_gr(obj, label, f, userdata, -1);
-}
-
-void ui_widget_menuitem_gr(UiObject *obj, char *label, ui_callback f, void *userdata, ...) {
-    UiContainer *ct = uic_get_current_container(obj);
-    if(!ct->menu) {
-        return;
-    }
-    
-    // add groups
-    CxList *groups = NULL;
-    va_list ap;
-    va_start(ap, userdata);
-    int group;
-    while((group = va_arg(ap, int)) != -1) {
-        if(!groups) {
-            groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
-        }
-        cxListAdd(groups, &group);
-    }
-    va_end(ap);
-    
-    // create menuitem
-    Arg args[4];
-    XmString labelstr = XmStringCreateLocalized(label);
-    XtSetArg(args[0], XmNlabelString, labelstr);
-    
-    Widget item = XmCreatePushButton(ct->menu, "menu_button", args, 1);
-    XtManageChild(item);
-    XmStringFree(labelstr);
-}
-
-void ui_widget_menuitem_st(UiObject *obj, char *stockid, ui_callback f, void *userdata) {
-    ui_widget_menuitem_stgr(obj, stockid, f, userdata, -1);
-}
-
-void ui_widget_menuitem_stgr(UiObject *obj, char *stockid, ui_callback f, void *userdata, ...) {
-    UiContainer *ct = uic_get_current_container(obj);
-    if(!ct->menu) {
-        return;
-    }
-    
-    // add groups
-    CxList *groups = NULL;
-    va_list ap;
-    va_start(ap, userdata);
-    int group;
-    while((group = va_arg(ap, int)) != -1) {
-        if(!groups) {
-            groups = cxArrayListCreate(cxDefaultAllocator, NULL, sizeof(int), 16);
-        }
-        cxListAdd(groups, &group);
-    }
-    va_end(ap);
-    
-    // create menuitem
-    UiStockItem *stockItem = ui_get_stock_item(stockid);
-    Arg args[4];
-    XmString labelstr = XmStringCreateLocalized(stockItem->label);
-    XtSetArg(args[0], XmNlabelString, labelstr);
-    
-    Widget item = XmCreatePushButton(ct->menu, "menu_button", args, 1);
-    XtManageChild(item);
-    XmStringFree(labelstr);
-}

mercurial