Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
plugin-view.c
Go to the documentation of this file.
1 /*
2  * plugin-view.c
3  * Copyright 2010 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 <gtk/gtk.h>
21 
22 #include "plugin.h"
23 #include "plugins.h"
24 #include "ui_preferences.h"
25 
26 enum {
32 };
33 
34 typedef struct {
36  GtkTreeModel * model;
37  GtkTreePath * path;
38 } Node;
39 
40 static PluginHandle * get_selected_plugin (GtkTreeView * tree)
41 {
42  Node * n = NULL;
43 
44  GtkTreeSelection * sel = gtk_tree_view_get_selection (tree);
45 
46  /* the treeview may not have a model yet */
47  if (! sel)
48  return NULL;
49 
50  GtkTreeModel * model;
51  GtkTreeIter iter;
52  if (gtk_tree_selection_get_selected (sel, & model, & iter))
53  gtk_tree_model_get (model, & iter, PVIEW_COL_NODE, & n, -1);
54 
55  return n == NULL ? NULL : n->p;
56 }
57 
58 static void do_enable (GtkCellRendererToggle * cell, const char * path_str,
59  GtkTreeModel * model)
60 {
61  GtkTreePath * path = gtk_tree_path_new_from_string (path_str);
62  GtkTreeIter iter;
63  gtk_tree_model_get_iter (model, & iter, path);
64  gtk_tree_path_free (path);
65 
66  Node * n = NULL;
68  gtk_tree_model_get (model, & iter, PVIEW_COL_NODE, & n,
69  PVIEW_COL_ENABLED, & enabled, -1);
70  g_return_if_fail (n != NULL);
71 
72  plugin_enable (n->p, ! enabled);
73 }
74 
76 {
77  GtkTreeIter iter;
78  gtk_tree_model_get_iter (n->model, & iter, n->path);
79  gtk_list_store_set ((GtkListStore *) n->model, & iter, PVIEW_COL_ENABLED,
80  plugin_get_enabled (n->p), -1);
81  return TRUE;
82 }
83 
84 static bool_t fill_cb (PluginHandle * p, GtkTreeModel * model)
85 {
86  Node * n = g_slice_new (Node);
87 
88  GtkTreeIter iter;
89  gtk_list_store_append ((GtkListStore *) model, & iter);
90  gtk_list_store_set ((GtkListStore *) model, & iter, PVIEW_COL_NODE, n,
92  (p), PVIEW_COL_PATH, plugin_get_filename (p), -1);
93 
94  n->p = p;
95  n->model = model;
96  n->path = gtk_tree_model_get_path (model, & iter);
97 
99 
100  return TRUE;
101 }
102 
103 static void list_fill (GtkTreeView * tree, void * type)
104 {
105  GtkTreeModel * model = (GtkTreeModel *) gtk_list_store_new (PVIEW_COLS,
106  G_TYPE_POINTER, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING);
107  gtk_tree_view_set_model (tree, model);
108 
109  GtkTreeViewColumn * col = gtk_tree_view_column_new ();
110  gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
111  gtk_tree_view_column_set_resizable (col, FALSE);
112  gtk_tree_view_append_column (tree, col);
113 
114  GtkCellRenderer * rend = gtk_cell_renderer_toggle_new ();
115  g_signal_connect (rend, "toggled", (GCallback) do_enable, model);
116  gtk_tree_view_column_pack_start (col, rend, FALSE);
117  gtk_tree_view_column_set_attributes (col, rend, "active", PVIEW_COL_ENABLED,
118  NULL);
119 
120  for (int i = PVIEW_COL_NAME; i <= PVIEW_COL_PATH; i ++)
121  {
122  col = gtk_tree_view_column_new ();
123  gtk_tree_view_column_set_sizing (col, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
124  gtk_tree_view_column_set_resizable (col, FALSE);
125  gtk_tree_view_append_column (tree, col);
126 
127  rend = gtk_cell_renderer_text_new ();
128  gtk_tree_view_column_pack_start (col, rend, FALSE);
129  gtk_tree_view_column_set_attributes (col, rend, "text", i, NULL);
130  }
131 
132  plugin_for_each (GPOINTER_TO_INT (type), (PluginForEachFunc) fill_cb, model);
133 }
134 
135 static void list_destroy (GtkTreeView * tree)
136 {
137  GtkTreeModel * model = gtk_tree_view_get_model (tree);
138  if (model == NULL)
139  return;
140 
141  GtkTreeIter iter;
142  if (gtk_tree_model_get_iter_first (model, & iter))
143  {
144  do
145  {
146  Node * n = NULL;
147  gtk_tree_model_get (model, & iter, PVIEW_COL_NODE, & n, -1);
148  g_return_if_fail (n != NULL);
149 
151  gtk_tree_path_free (n->path);
152  g_slice_free (Node, n);
153  }
154  while (gtk_tree_model_iter_next (model, & iter));
155  }
156 
157  g_object_unref ((GObject *) model);
158 }
159 
160 static bool_t config_watcher (PluginHandle * p, GtkWidget * config)
161 {
162  gtk_widget_set_sensitive (config, plugin_has_configure (p) &&
163  plugin_get_enabled (p));
164  return TRUE;
165 }
166 
167 static bool_t about_watcher (PluginHandle * p, GtkWidget * about)
168 {
169  gtk_widget_set_sensitive (about, plugin_has_about (p) && plugin_get_enabled
170  (p));
171  return TRUE;
172 }
173 
174 static void button_update (GtkTreeView * tree, GtkWidget * b)
175 {
176  PluginForEachFunc watcher = (PluginForEachFunc) g_object_get_data
177  ((GObject *) b, "watcher");
178  g_return_if_fail (watcher != NULL);
179 
180  PluginHandle * p = g_object_steal_data ((GObject *) b, "plugin");
181  if (p != NULL)
182  plugin_remove_watch (p, watcher, b);
183 
184  p = get_selected_plugin (tree);
185  if (p != NULL)
186  {
187  g_object_set_data ((GObject *) b, "plugin", p);
188  watcher (p, b);
189  plugin_add_watch (p, watcher, b);
190  }
191  else
192  gtk_widget_set_sensitive (b, FALSE);
193 }
194 
195 static void do_config (GtkTreeView * tree)
196 {
198  g_return_if_fail (plugin != NULL);
199  plugin_do_configure (plugin);
200 }
201 
202 static void do_about (GtkTreeView * tree)
203 {
205  g_return_if_fail (plugin != NULL);
206  plugin_do_about (plugin);
207 }
208 
209 static void button_destroy (GtkWidget * b)
210 {
211  PluginForEachFunc watcher = (PluginForEachFunc) g_object_get_data
212  ((GObject *) b, "watcher");
213  g_return_if_fail (watcher != NULL);
214 
215  PluginHandle * p = g_object_steal_data ((GObject *) b, "plugin");
216  if (p != NULL)
217  plugin_remove_watch (p, watcher, b);
218 }
219 
220 GtkWidget * plugin_view_new (int type)
221 {
222  GtkWidget * vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
223  gtk_container_set_border_width ((GtkContainer *) vbox, 6);
224 
225  GtkWidget * scrolled = gtk_scrolled_window_new (NULL, NULL);
226  gtk_box_pack_start ((GtkBox *) vbox, scrolled, TRUE, TRUE, 0);
227  gtk_scrolled_window_set_policy ((GtkScrolledWindow *) scrolled,
228  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
229  gtk_scrolled_window_set_shadow_type ((GtkScrolledWindow *) scrolled,
230  GTK_SHADOW_IN);
231 
232  GtkWidget * tree = gtk_tree_view_new ();
233  gtk_container_add ((GtkContainer *) scrolled, tree);
234  gtk_tree_view_set_headers_visible ((GtkTreeView *) tree, FALSE);
235  g_signal_connect (tree, "realize", (GCallback) list_fill, GINT_TO_POINTER
236  (type));
237  g_signal_connect (tree, "destroy", (GCallback) list_destroy, NULL);
238 
239  GtkWidget * hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
240  gtk_box_pack_start ((GtkBox *) vbox, hbox, FALSE, FALSE, 0);
241 
242  GtkWidget * config = gtk_button_new_from_stock (GTK_STOCK_PREFERENCES);
243  gtk_box_pack_start ((GtkBox *) hbox, config, FALSE, FALSE, 0);
244  gtk_widget_set_sensitive (config, FALSE);
245  g_object_set_data ((GObject *) config, "watcher", (void *) config_watcher);
246  g_signal_connect (tree, "cursor-changed", (GCallback) button_update, config);
247  g_signal_connect_swapped (config, "clicked", (GCallback)
248  do_config, tree);
249  g_signal_connect (config, "destroy", (GCallback) button_destroy, NULL);
250 
251  GtkWidget * about = gtk_button_new_from_stock (GTK_STOCK_ABOUT);
252  gtk_box_pack_start ((GtkBox *) hbox, about, FALSE, FALSE, 0);
253  gtk_widget_set_sensitive (about, FALSE);
254  g_object_set_data ((GObject *) about, "watcher", (void *) about_watcher);
255  g_signal_connect (tree, "cursor-changed", (GCallback) button_update, about);
256  g_signal_connect_swapped (about, "clicked", (GCallback) do_about, tree);
257  g_signal_connect (about, "destroy", (GCallback) button_destroy, NULL);
258 
259  return vbox;
260 }
bool_t plugin_get_enabled(PluginHandle *plugin)
GtkTreeModel * model
Definition: plugin-view.c:36
static bool_t about_watcher(PluginHandle *p, GtkWidget *about)
Definition: plugin-view.c:167
void plugin_remove_watch(PluginHandle *plugin, PluginForEachFunc func, void *data)
static float b[EQ_BANDS][2]
Definition: equalizer.c:56
GtkTreePath * path
Definition: plugin-view.c:37
type
Definition: plugins-api.h:41
static void list_fill(GtkTreeView *tree, void *type)
Definition: plugin-view.c:103
#define FALSE
Definition: core.h:37
static bool_t fill_cb(PluginHandle *p, GtkTreeModel *model)
Definition: plugin-view.c:84
Index Index bool_t
Definition: playlist-api.h:122
static bool_t enabled
Definition: vis_runner.c:38
void plugin_do_configure(PluginHandle *plugin)
Definition: plugin-init.c:322
static void button_update(GtkTreeView *tree, GtkWidget *b)
Definition: plugin-view.c:174
static bool_t list_watcher(PluginHandle *p, Node *n)
Definition: plugin-view.c:75
#define NULL
Definition: core.h:29
bool_t plugin_has_about(PluginHandle *plugin)
bool_t plugin_has_configure(PluginHandle *plugin)
static void do_config(GtkTreeView *tree)
Definition: plugin-view.c:195
bool_t(* PluginForEachFunc)(PluginHandle *plugin, void *data)
Definition: plugins.h:27
#define TRUE
Definition: core.h:39
static void do_about(GtkTreeView *tree)
Definition: plugin-view.c:202
bool_t plugin_enable(PluginHandle *plugin, bool_t enable)
Definition: plugin-init.c:267
static bool_t config_watcher(PluginHandle *p, GtkWidget *config)
Definition: plugin-view.c:160
static void button_destroy(GtkWidget *b)
Definition: plugin-view.c:209
void plugin_do_about(PluginHandle *plugin)
Definition: plugin-init.c:310
static PluginHandle * get_selected_plugin(GtkTreeView *tree)
Definition: plugin-view.c:40
const char * plugin_get_filename(PluginHandle *plugin)
void data PluginHandle plugin
Definition: plugins-api.h:54
static void do_enable(GtkCellRendererToggle *cell, const char *path_str, GtkTreeModel *model)
Definition: plugin-view.c:58
static void list_destroy(GtkTreeView *tree)
Definition: plugin-view.c:135
void plugin_for_each(int type, PluginForEachFunc func, void *data)
const char * plugin_get_name(PluginHandle *plugin)
GtkWidget * plugin_view_new(int type)
Definition: plugin-view.c:220
void plugin_add_watch(PluginHandle *plugin, PluginForEachFunc func, void *data)
PluginHandle * p
Definition: plugin-view.c:35