diff -r 1a95de56dadc -r e6415fd4af4b ui/motif/menu.c --- a/ui/motif/menu.c Sun Apr 06 13:54:28 2025 +0200 +++ b/ui/motif/menu.c Sun Apr 06 14:10:04 2025 +0200 @@ -202,6 +202,20 @@ ui_bind_radiobutton(obj, button, NULL, it->varname, it->callback, it->userdata, 0); } +static void menuitem_list_remove_binding(void *obj) { + UiActiveMenuItemList *ls = obj; + UiList *list = ls->var->value; + CxList *bindings = list->obj; + if(bindings) { + (void)cxListFindRemove(bindings, obj); + if(cxListSize(bindings) == 0) { + cxListFree(bindings); + list->obj = NULL; + list->update = NULL; + } + } +} + void add_menuitem_list_widget(Widget p, int i, UiMenuItemI *item, UiObject *obj) { UiMenuItemList *il = (UiMenuItemList*)item; const CxAllocator *a = obj->ctx->allocator; @@ -218,17 +232,40 @@ ls->userdata = il->userdata; ls->addseparator = il->addseparator; - ls->var = uic_create_var(ui_global_context(), il->varname, UI_VAR_LIST); //uic_widget_var(obj->ctx, obj->ctx, NULL, il->varname, UI_VAR_LIST); - UiList *list = ls->var->value; - - UiObserver *observer = ui_observer_new((ui_callback)ui_update_menuitem_list, ls); - list->observers = ui_obsvlist_add(list->observers, observer); - uic_list_register_observer_destructor(obj->ctx, list, observer); - - ui_update_menuitem_list(NULL, ls); + //ls->var = uic_create_var(ui_global_context(), il->varname, UI_VAR_LIST); + ls->var = uic_create_var(obj->ctx, il->varname, UI_VAR_LIST); + if(ls->var) { + UiList *list = ls->var->value; + list->update = ui_menulist_update; + list->getselection = NULL; + list->setselection = NULL; + + // It is possible, that the UiVar is from a global shared context, + // used by multiple windows. To support this usecase, the list->obj + // binding object is a list of all connected UiActiveMenuItemList. + CxList *bindings = list->obj; + if(!bindings) { + bindings = cxLinkedListCreate(ls->var->from_ctx->mp->allocator, NULL, CX_STORE_POINTERS); + list->obj = bindings; + } + cxListAdd(bindings, ls); + + // The destruction of the toplevel obj must remove the menulist binding + uic_context_add_destructor(obj->ctx, menuitem_list_remove_binding, ls); + + ui_update_menuitem_list(ls); + } } -void ui_update_menuitem_list(UiEvent *event, UiActiveMenuItemList *list) { +void ui_menulist_update(UiList *list, int ignored) { + CxList *bindings = list->obj; + CxIterator i = cxListIterator(bindings); + cx_foreach(UiActiveMenuItemList *, ls, i) { + ui_update_menuitem_list(ls); + } +} + +void ui_update_menuitem_list(UiActiveMenuItemList *list) { XmString s = NULL; Arg args[4]; int n;