remove global menu observer when destroying a window newapi

Wed, 27 Nov 2024 18:46:45 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 27 Nov 2024 18:46:45 +0100
branch
newapi
changeset 395
b8277deb75b8
parent 394
bedd499b640d
child 396
4cb216085479

remove global menu observer when destroying a window

ui/common/properties.c file | annotate | diff | comparison | revisions
ui/common/properties.h file | annotate | diff | comparison | revisions
ui/common/types.c file | annotate | diff | comparison | revisions
ui/common/types.h file | annotate | diff | comparison | revisions
ui/gtk/menu.c file | annotate | diff | comparison | revisions
ui/ui/properties.h file | annotate | diff | comparison | revisions
--- 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) {
--- 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);
--- 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);
+}
--- 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
 }
--- 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));
--- 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);

mercurial