refactor widget groups

Fri, 11 Dec 2020 11:47:30 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 11 Dec 2020 11:47:30 +0100
changeset 168
1b99acacc5bb
parent 167
161511838ea6
child 169
fe49cff3c571

refactor widget groups

ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/context.h file | annotate | diff | comparison | revisions
ui/gtk/menu.c file | annotate | diff | comparison | revisions
ui/gtk/text.c file | annotate | diff | comparison | revisions
ui/gtk/toolbar.c file | annotate | diff | comparison | revisions
ui/ui/text.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/ui/common/context.c	Thu Dec 10 13:42:25 2020 +0100
+++ b/ui/common/context.c	Fri Dec 11 11:47:30 2020 +0100
@@ -395,7 +395,6 @@
 }
 
 
-
 void ui_set_group(UiContext *ctx, int group) {
     if(ucx_list_find(ctx->groups, (void*)(intptr_t)group, NULL, NULL) == -1) {
         ctx->groups = ucx_list_append_a(ctx->mempool->allocator, ctx->groups, (void*)(intptr_t)group);
@@ -456,7 +455,7 @@
                 break;
             }
         }
-        ui_set_enabled(gw->widget, enable);
+        gw->enable(gw->widget, enable);
     }
     
     if(groups) {
@@ -464,11 +463,28 @@
     }
 }
 
-void uic_add_group_widget(UiContext *ctx, void *widget, UcxList *groups) {
+void ui_widget_set_groups(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...) {
+    // get groups
+    UcxList *groups = NULL;
+    va_list ap;
+    va_start(ap, enable);
+    int group;
+    while((group = va_arg(ap, int)) != -1) {
+        groups = ucx_list_append(groups, (void*)(intptr_t)group);
+    }
+    va_end(ap);
+    
+    uic_add_group_widget(ctx, widget, enable, groups);
+    
+    ucx_list_free(groups);
+}
+
+void uic_add_group_widget(UiContext *ctx, void *widget, ui_enablefunc enable, UcxList *groups) {
     UcxMempool *mp = ctx->mempool;
     UiGroupWidget *gw = ucx_mempool_malloc(mp, sizeof(UiGroupWidget));
     
     gw->widget = widget;
+    gw->enable = enable;
     gw->numgroups = ucx_list_size(groups);
     gw->groups = ucx_mempool_calloc(mp, gw->numgroups, sizeof(int));
     int i = 0;
--- a/ui/common/context.h	Thu Dec 10 13:42:25 2020 +0100
+++ b/ui/common/context.h	Fri Dec 11 11:47:30 2020 +0100
@@ -91,9 +91,10 @@
 };
 
 struct UiGroupWidget {
-    UIWIDGET widget;
-    int      *groups;
-    int      numgroups;
+    void          *widget;
+    ui_enablefunc enable;
+    int           *groups;
+    int           numgroups;
 };
 
 
@@ -121,7 +122,7 @@
 void uic_remove_bound_var(UiContext *ctx, UiVar *var);
 
 void uic_check_group_widgets(UiContext *ctx);
-void uic_add_group_widget(UiContext *ctx, void *widget, UcxList *groups);
+void uic_add_group_widget(UiContext *ctx, void *widget, ui_enablefunc enable, UcxList *groups);
 
 
 #ifdef	__cplusplus
--- a/ui/gtk/menu.c	Thu Dec 10 13:42:25 2020 +0100
+++ b/ui/gtk/menu.c	Fri Dec 11 11:47:30 2020 +0100
@@ -265,7 +265,7 @@
     gtk_menu_shell_append(GTK_MENU_SHELL(parent), widget);
     
     if(i->groups) {
-        uic_add_group_widget(obj->ctx, widget, i->groups);
+        uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, i->groups);
     }
 }
 
@@ -301,7 +301,7 @@
     gtk_menu_shell_append(GTK_MENU_SHELL(parent), widget);
     
     if(i->groups) {
-        uic_add_group_widget(obj->ctx, widget, i->groups);
+        uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, i->groups);
     }
 }
 
@@ -554,7 +554,7 @@
     gtk_menu_shell_append(GTK_MENU_SHELL(ct->menu), widget);
     
     if(groups) {
-        uic_add_group_widget(obj->ctx, widget, groups);
+        uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, groups);
     }
 }
 
@@ -604,6 +604,6 @@
     gtk_menu_shell_append(GTK_MENU_SHELL(ct->menu), widget);
     
     if(groups) {
-        uic_add_group_widget(obj->ctx, widget, groups);
+        uic_add_group_widget(obj->ctx, widget, (ui_enablefunc)ui_set_enabled, groups);
     }
 }
--- a/ui/gtk/text.c	Thu Dec 10 13:42:25 2020 +0100
+++ b/ui/gtk/text.c	Fri Dec 11 11:47:30 2020 +0100
@@ -173,6 +173,10 @@
     return NULL;
 }
 
+UIWIDGET ui_textarea_gettextwidget(UIWIDGET textarea) {
+    return gtk_bin_get_child(GTK_BIN(textarea));
+}
+
 char* ui_textarea_get(UiText *text) {
     if(text->value.ptr) {
         text->value.free(text->value.ptr);
--- a/ui/gtk/toolbar.c	Thu Dec 10 13:42:25 2020 +0100
+++ b/ui/gtk/toolbar.c	Fri Dec 11 11:47:30 2020 +0100
@@ -275,7 +275,7 @@
     gtk_toolbar_insert(tb, button, -1);
     
     if(item->groups) {
-        uic_add_group_widget(obj->ctx, button, item->groups);
+        uic_add_group_widget(obj->ctx, button, (ui_enablefunc)ui_set_enabled, item->groups);
     }
 }
 
@@ -304,7 +304,7 @@
     gtk_toolbar_insert(tb, button, -1);
     
     if(item->groups) {
-        uic_add_group_widget(obj->ctx, button, item->groups);
+        uic_add_group_widget(obj->ctx, button, (ui_enablefunc)ui_set_enabled, item->groups);
     }
 }
 
@@ -366,7 +366,7 @@
     gtk_toolbar_insert(tb, button, -1);
     
     if(item->groups) {
-        uic_add_group_widget(obj->ctx, button, item->groups);
+        uic_add_group_widget(obj->ctx, button, (ui_enablefunc)ui_set_enabled, item->groups);
     }
 }
 
@@ -390,7 +390,7 @@
 }
 
 void ui_tool_toggle_button_set(UiInteger *integer, int64_t value) {
-    gboolean s = integer->value != 0 ? TRUE : FALSE;
+    gboolean s = value != 0 ? TRUE : FALSE;
     gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(integer->obj), s);
     integer->value = s;
 }
--- a/ui/ui/text.h	Thu Dec 10 13:42:25 2020 +0100
+++ b/ui/ui/text.h	Fri Dec 11 11:47:30 2020 +0100
@@ -38,6 +38,8 @@
 UIWIDGET ui_textarea(UiObject *obj, UiText *value);
 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname);
 
+UIWIDGET ui_textarea_gettextwidget(UIWIDGET textarea);
+
 void ui_text_undo(UiText *value);
 void ui_text_redo(UiText *value);
 
--- a/ui/ui/toolkit.h	Thu Dec 10 13:42:25 2020 +0100
+++ b/ui/ui/toolkit.h	Fri Dec 11 11:47:30 2020 +0100
@@ -126,6 +126,8 @@
 
 typedef void(*ui_freefunc)(void*);
 
+typedef void(*ui_enablefunc)(void*, UiBool);
+
 struct UiObject {
     /*
      * native widget
@@ -314,6 +316,8 @@
 void ui_attach_document(UiContext *ctx, void *document);
 void ui_detach_document2(UiContext *ctx, void *document);
 
+void ui_widget_set_groups(UiContext *ctx, UIWIDGET widget, ui_enablefunc enable, ...);
+
 void ui_set_group(UiContext *ctx, int group);
 void ui_unset_group(UiContext *ctx, int group);
 int* ui_active_groups(UiContext *ctx, int *ngroups);

mercurial