# HG changeset patch # User Olaf Wintermann # Date 1732729605 -3600 # Node ID b8277deb75b897a59261df8735f0d914c581781b # Parent bedd499b640d7e256e035b13105eec12894b7cb5 remove global menu observer when destroying a window diff -r bedd499b640d -r b8277deb75b8 ui/common/properties.c --- a/ui/common/properties.c Wed Nov 27 13:28:21 2024 +0100 +++ b/ui/common/properties.c Wed Nov 27 18:46:45 2024 +0100 @@ -162,43 +162,52 @@ free(path); } -void uic_store_app_properties() { +int uic_store_app_properties() { char *path = ui_configfile("application.properties"); if(!path) { - return; + return 1; } FILE *file = fopen(path, "w"); if(!file) { fprintf(stderr, "Ui Error: Cannot open properties file: %s\n", path); free(path); - return; + return 1; } + int ret = 0; if(ucx_properties_store(application_properties, file)) { fprintf(stderr, "Ui Error: Cannot store application properties.\n"); + ret = 1; } fclose(file); free(path); + + return ret; } -char* ui_get_property(char *name) { +const char* ui_get_property(const char *name) { return cxMapGet(application_properties, name); } -void ui_set_property(char *name, char *value) { - cxMapPut(application_properties, name, value); +void ui_set_property(const char *name, const char *value) { + cxMapPut(application_properties, name, (char*)value); } -void ui_set_default_property(char *name, char *value) { - char *v = cxMapGet(application_properties, name); +const char* ui_set_default_property(const char *name, const char *value) { + const char *v = cxMapGet(application_properties, name); if(!v) { - cxMapPut(application_properties, name, value); + cxMapPut(application_properties, name, (char*)value); + v = value; } + return v; } +int ui_properties_store(void) { + return uic_store_app_properties(); +} static char* uic_concat_path(const char *base, const char *p, const char *ext) { diff -r bedd499b640d -r b8277deb75b8 ui/common/properties.h --- a/ui/common/properties.h Wed Nov 27 13:28:21 2024 +0100 +++ b/ui/common/properties.h Wed Nov 27 18:46:45 2024 +0100 @@ -44,7 +44,7 @@ #endif void uic_load_app_properties(); -void uic_store_app_properties(); +int uic_store_app_properties(); int uic_load_language_file(const char *path); char* uic_get_image_path(const char *imgfilename); diff -r bedd499b640d -r b8277deb75b8 ui/common/types.c --- a/ui/common/types.c Wed Nov 27 13:28:21 2024 +0100 +++ b/ui/common/types.c Wed Nov 27 18:46:45 2024 +0100 @@ -544,3 +544,36 @@ } free(list.files); } + + +typedef struct UiObserverDestructor { + UiList *list; + UiObserver *observer; +} UiObserverDestructor; + +static void observer_destructor(UiObserverDestructor *destr) { + UiObserver *remove_obs = destr->observer; + UiObserver *obs = destr->list->observers; + UiObserver *prev = NULL; + while(obs) { + if(obs == remove_obs) { + if(prev) { + prev->next = obs->next; + } else { + destr->list->observers = obs->next; + } + break; + } + prev = obs; + obs = obs->next; + } + free(remove_obs); +} + +void uic_list_register_observer_destructor(UiContext *ctx, UiList *list, UiObserver *observer) { + CxMempool *mp = ctx->mp; + UiObserverDestructor *destr = cxMalloc(mp->allocator, sizeof(UiObserverDestructor)); + destr->list = list; + destr->observer = observer; + cxMempoolSetDestructor(destr, (cx_destructor_func)observer_destructor); +} diff -r bedd499b640d -r b8277deb75b8 ui/common/types.h --- a/ui/common/types.h Wed Nov 27 13:28:21 2024 +0100 +++ b/ui/common/types.h Wed Nov 27 18:46:45 2024 +0100 @@ -58,6 +58,8 @@ void uic_range_unbind(UiRange *r); void uic_list_unbind(UiList *l); void uic_generic_unbind(UiGeneric *g); + +void uic_list_register_observer_destructor(UiContext *ctx, UiList *list, UiObserver *observer); #ifdef __cplusplus } diff -r bedd499b640d -r b8277deb75b8 ui/gtk/menu.c --- a/ui/gtk/menu.c Wed Nov 27 13:28:21 2024 +0100 +++ b/ui/gtk/menu.c Wed Nov 27 18:46:45 2024 +0100 @@ -35,6 +35,7 @@ #include "toolkit.h" #include "../common/context.h" #include "../common/menu.h" +#include "../common/types.h" #include "../ui/properties.h" #include "../ui/window.h" #include "container.h" @@ -252,10 +253,9 @@ ls->callback = il->callback; ls->userdata = il->userdata; - ls->list->observers = ui_add_observer( - ls->list->observers, - (ui_callback)ui_update_menuitem_list, - ls); + 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); } @@ -510,10 +510,9 @@ ls->callback = il->callback; ls->userdata = il->userdata; - list->observers = ui_add_observer( - list->observers, - (ui_callback)ui_update_gmenu_item_list, - ls); + UiObserver *observer = ui_observer_new((ui_callback)ui_update_gmenu_item_list, ls); + list->observers = ui_obsvlist_add(list->observers, observer); + uic_list_register_observer_destructor(obj->ctx, list, observer); GSimpleAction *action = g_simple_action_new(item->id, g_variant_type_new("i")); g_action_map_add_action(obj->ctx->action_map, G_ACTION(action)); diff -r bedd499b640d -r b8277deb75b8 ui/ui/properties.h --- a/ui/ui/properties.h Wed Nov 27 13:28:21 2024 +0100 +++ b/ui/ui/properties.h Wed Nov 27 18:46:45 2024 +0100 @@ -35,10 +35,11 @@ extern "C" { #endif -char* ui_get_property(char *name); -void ui_set_property(char *name, char *value); +const char* ui_get_property(const char *name); +void ui_set_property(const char *name, const char *value); +const char* ui_set_default_property(const char *name, const char *value); -void ui_set_default_property(char *name, char *value); +int ui_properties_store(void); void ui_locales_dir(char *path); void ui_pixmaps_dir(char *path);