Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
ui_plugin_menu.c
Go to the documentation of this file.
1 /*
2  * ui_plugin_menu.c
3  * Copyright 2009-2011 John Lindgren
4  *
5  * This file is part of Audacious.
6  *
7  * Audacious is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free Software
9  * Foundation, version 2 or version 3 of the License.
10  *
11  * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * Audacious. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The Audacious team does not consider modular code linking to Audacious or
19  * using our public API to be a derived work.
20  */
21 
22 #include <glib.h>
23 #include <gtk/gtk.h>
24 
25 #include "misc.h"
26 
27 struct Item {
29  const char * name;
30  const char * icon;
31 };
32 
33 static GList * items[AUD_MENU_COUNT];
34 static GtkWidget * menus[AUD_MENU_COUNT];
35 
36 static void add_to_menu (GtkWidget * menu, struct Item * item)
37 {
38  GtkWidget * widget = gtk_image_menu_item_new_with_mnemonic (item->name);
39  g_object_set_data ((GObject *) widget, "func", (void *) item->func);
40  g_signal_connect (widget, "activate", item->func, NULL);
41 
42  if (item->icon)
43  gtk_image_menu_item_set_image ((GtkImageMenuItem *) widget,
44  gtk_image_new_from_stock (item->icon, GTK_ICON_SIZE_MENU));
45 
46  gtk_widget_show (widget);
47  gtk_menu_shell_append ((GtkMenuShell *) menu, widget);
48 }
49 
50 /* GtkWidget * get_plugin_menu (int id) */
51 void * get_plugin_menu (int id)
52 {
53  if (! menus[id])
54  {
55  menus[id] = gtk_menu_new ();
56  g_signal_connect (menus[id], "destroy", (GCallback)
57  gtk_widget_destroyed, & menus[id]);
58 
59  for (GList * node = items[id]; node; node = node->next)
60  add_to_menu (menus[id], node->data);
61  }
62 
63  return menus[id];
64 }
65 
66 void plugin_menu_add (int id, MenuFunc func, const char * name,
67  const char * icon)
68 {
69  struct Item * item = g_slice_new (struct Item);
70  item->name = name;
71  item->icon = icon;
72  item->func = func;
73 
74  items[id] = g_list_append (items[id], item);
75 
76  if (menus[id])
77  add_to_menu (menus[id], item);
78 }
79 
80 static void remove_cb (GtkWidget * widget, MenuFunc func)
81 {
82  if ((MenuFunc) g_object_get_data ((GObject *) widget, "func") == func)
83  gtk_widget_destroy (widget);
84 }
85 
87 {
88  if (menus[id])
89  gtk_container_foreach ((GtkContainer *) menus[id], (GtkCallback)
90  remove_cb, (void *) func);
91 
92  GList * next;
93  for (GList * node = items[id]; node; node = next)
94  {
95  next = node->next;
96 
97  if (((struct Item *) node->data)->func == func)
98  {
99  g_slice_free (struct Item, node->data);
100  items[id] = g_list_delete_link (items[id], node);
101  }
102  }
103 }