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  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions, and the following disclaimer in the documentation
13  * provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #include <glib.h>
21 #include <gtk/gtk.h>
22 
23 #include "misc.h"
24 
25 struct Item {
27  const char * name;
28  const char * icon;
29 };
30 
31 static GList * items[AUD_MENU_COUNT];
32 static GtkWidget * menus[AUD_MENU_COUNT];
33 
34 static void add_to_menu (GtkWidget * menu, struct Item * item)
35 {
36  GtkWidget * widget = gtk_image_menu_item_new_with_mnemonic (item->name);
37  g_object_set_data ((GObject *) widget, "func", (void *) item->func);
38  g_signal_connect (widget, "activate", item->func, NULL);
39 
40  if (item->icon)
41  gtk_image_menu_item_set_image ((GtkImageMenuItem *) widget,
42  gtk_image_new_from_stock (item->icon, GTK_ICON_SIZE_MENU));
43 
44  gtk_widget_show (widget);
45  gtk_menu_shell_append ((GtkMenuShell *) menu, widget);
46 }
47 
48 /* GtkWidget * get_plugin_menu (int id) */
49 void * get_plugin_menu (int id)
50 {
51  if (! menus[id])
52  {
53  menus[id] = gtk_menu_new ();
54  g_signal_connect (menus[id], "destroy", (GCallback)
55  gtk_widget_destroyed, & menus[id]);
56 
57  for (GList * node = items[id]; node; node = node->next)
58  add_to_menu (menus[id], node->data);
59  }
60 
61  return menus[id];
62 }
63 
64 void plugin_menu_add (int id, MenuFunc func, const char * name,
65  const char * icon)
66 {
67  struct Item * item = g_slice_new (struct Item);
68  item->name = name;
69  item->icon = icon;
70  item->func = func;
71 
72  items[id] = g_list_append (items[id], item);
73 
74  if (menus[id])
75  add_to_menu (menus[id], item);
76 }
77 
78 static void remove_cb (GtkWidget * widget, MenuFunc func)
79 {
80  if ((MenuFunc) g_object_get_data ((GObject *) widget, "func") == func)
81  gtk_widget_destroy (widget);
82 }
83 
85 {
86  if (menus[id])
87  gtk_container_foreach ((GtkContainer *) menus[id], (GtkCallback)
88  remove_cb, (void *) func);
89 
90  GList * next;
91  for (GList * node = items[id]; node; node = next)
92  {
93  next = node->next;
94 
95  if (((struct Item *) node->data)->func == func)
96  {
97  g_slice_free (struct Item, node->data);
98  items[id] = g_list_delete_link (items[id], node);
99  }
100  }
101 }
const char * icon
static void remove_cb(GtkWidget *widget, MenuFunc func)
void plugin_menu_add(int id, MenuFunc func, const char *name, const char *icon)
void plugin_menu_remove(int id, MenuFunc func)
void * get_plugin_menu(int id)
const char * name
Definition: plugin-init.c:38
#define NULL
Definition: core.h:27
static GList * items[AUD_MENU_COUNT]
func
Definition: plugins-api.h:41
void(* MenuFunc)(void)
Definition: misc.h:60
const char * name
MenuFunc func
static void add_to_menu(GtkWidget *menu, struct Item *item)
static GtkWidget * menus[AUD_MENU_COUNT]