XXX TODO BLAG ETC Allow icons also for submenus
authorMikael Magnusson <mikachu@comhem.se>
Mon, 28 Apr 2008 22:25:20 +0000 (00:25 +0200)
committerMikael Magnusson <mikachu@gmail.com>
Wed, 10 Sep 2008 14:48:28 +0000 (16:48 +0200)
This has some code that should be "refactored" a bit.

openbox/menu.c
openbox/menu.h
openbox/menuframe.c

index 390f9dde68bc2934182585a3788934676589b9c4..5e1d1a47e2d8d10615daa7019ed1b2968221870b 100644 (file)
@@ -266,33 +266,32 @@ static gunichar parse_shortcut(const gchar *label, gboolean allow_shortcut,
 static void parse_menu_item(xmlNodePtr node,  gpointer data)
 {
     ObMenuParseState *state = data;
+    xmlNodePtr n;
     gchar *label;
-    #ifdef USE_IMLIB2
+#ifdef USE_IMLIB2
     gchar *icon;
-    #endif
+#endif
     ObMenuEntry *e;
 
     if (state->parent) {
-        #ifdef USE_IMLIB2
-        /* Don't try to extract "icon" attribute if icons in user-defined
-          menus are not enabled. */
-        if (!(config_menu_user_show_icons &&
-            obt_parse_attr_string(node, "icon", &icon)))
-               icon = NULL;
-        #endif
-
         if (obt_parse_attr_string(node, "label", &label)) {
             GSList *acts = NULL;
 
-            for (node = node->children; node; node = node->next)
-                if (!xmlStrcasecmp(node->name, (const xmlChar*) "action")) {
-                    ObActionsAct *a = actions_parse(node);
+            for (n = node->children; n; n = n->next)
+                if (!xmlStrcasecmp(n->name, (const xmlChar*) "action")) {
+                    ObActionsAct *a = actions_parse(n);
                     if (a)
                         acts = g_slist_append(acts, a);
                 }
             e = menu_add_normal(state->parent, -1, label, acts, TRUE);
             
-            #ifdef USE_IMLIB2
+#ifdef USE_IMLIB2
+            /* Don't try to extract "icon" attribute if icons in user-defined
+               menus are not enabled. */
+            if (!(config_menu_user_show_icons &&
+                obt_parse_attr_string(node, "icon", &icon)))
+                   icon = NULL;
+
             if (icon) { /* Icon will be used. */
                 e->data.normal.icon = RrImageFetchFromFile(ob_rr_icons, icon);
                 if (e->data.normal.icon) {
@@ -300,7 +299,7 @@ static void parse_menu_item(xmlNodePtr node,  gpointer data)
                 }
                 g_free(icon);
             }
-            #endif
+#endif
             g_free(label);
         }
     }
@@ -326,6 +325,8 @@ static void parse_menu(xmlNodePtr node, gpointer data)
     ObMenuParseState *state = data;
     gchar *name = NULL, *title = NULL, *script = NULL;
     ObMenu *menu;
+    ObMenuEntry *e;
+    gchar *icon;
 
     if (!obt_parse_attr_string(node, "id", &name))
         goto parse_menu_fail;
@@ -349,8 +350,20 @@ static void parse_menu(xmlNodePtr node, gpointer data)
         }
     }
 
-    if (state->parent)
-        menu_add_submenu(state->parent, -1, name);
+    if (state->parent) {
+        e = menu_add_submenu(state->parent, -1, name);
+
+        if (!(config_menu_user_show_icons &&
+            obt_parse_attr_string(node, "icon", &icon)))
+               icon = NULL;
+
+        if (icon) {
+            e->data.submenu.icon = RrImageFetchFromFile(ob_rr_icons, icon);
+            if (e->data.submenu.icon)
+                e->data.submenu.icon_alpha = 0xff;
+            g_free(icon);
+        }
+    }
 
 parse_menu_fail:
     g_free(name);
index 43efd41372c727744e68604cf54c9a5cb262a29d..2f1fce12a991e623c2ce15dcda48164e737a406f 100644 (file)
@@ -114,7 +114,7 @@ struct _ObNormalMenuEntry {
 
     /* List of ObActions */
     GSList *actions;
-
+    
     /* Icon stuff.  If you set this, make sure you RrImageRef() it too. */
     RrImage *icon;
     gint     icon_alpha;
@@ -132,6 +132,11 @@ struct _ObNormalMenuEntry {
 struct _ObSubmenuMenuEntry {
     gchar *name;
     ObMenu *submenu;
+    
+    /* Icon stuff.  If you set this, make sure you RrImageRef() it too. */
+    RrImage *icon;
+    gint     icon_alpha;
+
     guint show_from;
 };
 
index e41907cd2b79d448bb90ff83591f8bdd884bf663..e317cd16fe6e6387ad7bf57fe029efee7d570977 100644 (file)
@@ -153,7 +153,8 @@ static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
     self->text = createWindow(self->window, 0, NULL);
     g_hash_table_insert(menu_frame_map, &self->window, self);
     g_hash_table_insert(menu_frame_map, &self->text, self);
-    if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
+    if ((entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
+        (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
         self->icon = createWindow(self->window, 0, NULL);
         g_hash_table_insert(menu_frame_map, &self->icon, self);
     }
@@ -181,7 +182,8 @@ static void menu_entry_frame_free(ObMenuEntryFrame *self)
         XDestroyWindow(obt_display, self->window);
         g_hash_table_remove(menu_frame_map, &self->text);
         g_hash_table_remove(menu_frame_map, &self->window);
-        if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
+        if ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) ||
+            (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)) {
             XDestroyWindow(obt_display, self->icon);
             g_hash_table_remove(menu_frame_map, &self->icon);
         }
@@ -470,8 +472,10 @@ static void menu_entry_frame_render(ObMenuEntryFrame *self)
         break;
     }
 
-    if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
-        self->entry->data.normal.icon)
+    if (((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) &&
+         self->entry->data.normal.icon) ||
+        ((self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) &&
+          self->entry->data.submenu.icon))
     {
         RrAppearance *clear;
 
@@ -486,9 +490,9 @@ static void menu_entry_frame_render(ObMenuEntryFrame *self)
         RrAppearanceClearTextures(clear);
         clear->texture[0].type = RR_TEXTURE_IMAGE;
         clear->texture[0].data.image.image =
-            self->entry->data.normal.icon;
+            self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL ? self->entry->data.normal.icon : self->entry->data.submenu.icon;
         clear->texture[0].data.image.alpha =
-            self->entry->data.normal.icon_alpha;
+            self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL ? self->entry->data.normal.icon_alpha : self->entry->data.submenu.icon_alpha;
         clear->surface.parent = item_a;
         clear->surface.parentx = PADDING;
         clear->surface.parenty = frame->item_margin.top;