ui/common/context.c

changeset 21
012418e7dc90
parent 20
2dda1ad6dc7a
child 25
78ae3efe463f
--- a/ui/common/context.c	Wed Apr 02 20:31:47 2014 +0200
+++ b/ui/common/context.c	Thu Apr 03 11:12:22 2014 +0200
@@ -43,6 +43,7 @@
     ctx->toplevel = toplevel;
     ctx->vars = ucx_map_new_a(mp->allocator, 16);
     ctx->groups = NULL;
+    ctx->group_widgets = NULL;
     
 #ifdef UI_GTK
     ctx->accel_group = gtk_accel_group_new();
@@ -158,6 +159,9 @@
     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);
     }
+    
+    // enable/disable group widgets
+    uic_check_group_widgets(ctx);
 }
 
 void ui_unset_group(UiContext *ctx, int group) {
@@ -166,6 +170,9 @@
         UcxList *elm = ucx_list_get(ctx->groups, i);
         ctx->groups = ucx_list_remove_a(ctx->mempool->allocator, ctx->groups, elm);
     }
+    
+    // enable/disable group widgets
+    uic_check_group_widgets(ctx);
 }
 
 int* ui_active_groups(UiContext *ctx, int *ngroups) {
@@ -180,3 +187,51 @@
     *ngroups = nelm;
     return groups;
 }
+
+void uic_check_group_widgets(UiContext *ctx) {
+    int ngroups = 0;
+    int *groups = ui_active_groups(ctx, &ngroups);
+    
+    UCX_FOREACH(elm, ctx->group_widgets) {
+        UiGroupWidget *gw = elm->data;
+        char *check = calloc(1, gw->numgroups);
+        
+        for(int i=0;i<ngroups;i++) {
+            for(int k=0;k<gw->numgroups;k++) {
+                if(groups[i] == gw->groups[k]) {
+                    check[k] = 1;
+                }
+            }
+        }
+        
+        int enable = 1;
+        for(int i=0;i<gw->numgroups;i++) {
+            if(check[i] == 0) {
+                enable = 0;
+                break;
+            }
+        }
+        ui_set_enabled(gw->widget, enable);
+    }
+    
+    free(groups);
+}
+
+void uic_add_group_widget(UiContext *ctx, UIWIDGET widget, UcxList *groups) {
+    UcxMempool *mp = ctx->mempool;
+    UiGroupWidget *gw = ucx_mempool_malloc(mp, sizeof(UiGroupWidget));
+    
+    gw->widget = widget;
+    gw->numgroups = ucx_list_size(groups);
+    gw->groups = ucx_mempool_calloc(mp, gw->numgroups, sizeof(int));
+    int i = 0;
+    UCX_FOREACH(elm, groups) {
+        gw->groups[i++] = (intptr_t)elm->data;
+    }
+    
+    ctx->group_widgets = ucx_list_append_a(
+            mp->allocator,
+            ctx->group_widgets,
+            gw);
+}
+

mercurial