File: rb-source-toolbar.c

package info (click to toggle)
rhythmbox 3.4.9-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 27,376 kB
  • sloc: ansic: 114,861; python: 4,941; xml: 730; javascript: 350; perl: 307; sh: 84; makefile: 43
file content (387 lines) | stat: -rw-r--r-- 11,908 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 *  Copyright (C) 2011 Jonathan Matthew <jonathan@d14n.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The Rhythmbox authors hereby grant permission for non-GPL compatible
 *  GStreamer plugins to be used and distributed together with GStreamer
 *  and Rhythmbox. This permission is above and beyond the permissions granted
 *  by the GPL license by which Rhythmbox is covered. If you modify this code
 *  you may extend this exception to your version of the code, but you are not
 *  obligated to do so. If you do not wish to do so, delete this exception
 *  statement from your version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 *
 */

#include <config.h>

#include <widgets/rb-source-toolbar.h>
#include <widgets/rb-button-bar.h>
#include <lib/rb-util.h>

static void rb_source_toolbar_class_init (RBSourceToolbarClass *klass);
static void rb_source_toolbar_init (RBSourceToolbar *toolbar);

struct _RBSourceToolbarPrivate
{
	GtkAccelGroup *accel_group;
	RBDisplayPage *page;
	RBSearchEntry *search_entry;
	GtkWidget *search_popup;
	GtkWidget *button_bar;
	char *popup_path;

	/* search state */
	int search_value;
	gulong search_change_cb_id;
	RBSourceSearch *active_search;
	char *search_text;

	GAction *search_action;
};

G_DEFINE_TYPE (RBSourceToolbar, rb_source_toolbar, GTK_TYPE_GRID)

/**
 * SECTION:rbsourcetoolbar
 * @short_description: toolbar+search entry for sources
 *
 * This class combines a toolbar for custom source actions with a
 * search entry.  The toolbar content is specified using a UI path.
 * The #RBSourceToolbar takes care of preserving search state when
 * the selected page changes, and performs searches when the user
 * selects a new search type or changes the search text.
 */

enum
{
	PROP_0,
	PROP_PAGE,
	PROP_ACCEL_GROUP,
};

static void
source_selected_cb (GObject *object, GParamSpec *pspec, RBSourceToolbar *toolbar)
{
	gboolean selected;

	g_object_get (object, "selected", &selected, NULL);

	if (selected) {
		if (toolbar->priv->search_entry != NULL) {
			rb_search_entry_set_mnemonic (toolbar->priv->search_entry, TRUE);

			gtk_widget_add_accelerator (GTK_WIDGET (toolbar->priv->search_entry),
						    "grab-focus",
						    toolbar->priv->accel_group,
						    gdk_unicode_to_keyval ('f'),
						    GDK_CONTROL_MASK,
						    0);
		}

		if (toolbar->priv->button_bar != NULL) {
			rb_button_bar_add_accelerators (RB_BUTTON_BAR (toolbar->priv->button_bar),
							toolbar->priv->accel_group);
		}
	} else {
		if (toolbar->priv->search_entry != NULL) {
			rb_search_entry_set_mnemonic (toolbar->priv->search_entry, FALSE);

			gtk_widget_remove_accelerator (GTK_WIDGET (toolbar->priv->search_entry),
						       toolbar->priv->accel_group,
						       gdk_unicode_to_keyval ('f'),
						       GDK_CONTROL_MASK);
		}

		if (toolbar->priv->button_bar != NULL) {
			rb_button_bar_remove_accelerators (RB_BUTTON_BAR (toolbar->priv->button_bar),
							   toolbar->priv->accel_group);
		}
	}
}

static void
search_cb (RBSearchEntry *search_entry, const char *text, RBSourceToolbar *toolbar)
{
	g_return_if_fail (RB_IS_SOURCE (toolbar->priv->page));

	rb_source_search (RB_SOURCE (toolbar->priv->page), toolbar->priv->active_search, toolbar->priv->search_text, text);

	g_free (toolbar->priv->search_text);
	toolbar->priv->search_text = NULL;
	if (text != NULL) {
		toolbar->priv->search_text = g_strdup (text);
	}
}

static void
show_popup_cb (RBSearchEntry *search_entry, RBSourceToolbar *toolbar)
{
	gtk_menu_popup (GTK_MENU (toolbar->priv->search_popup),
			NULL, NULL, NULL, NULL, 3,
			gtk_get_current_event_time ());
}


static void
impl_finalize (GObject *object)
{
	RBSourceToolbar *toolbar = RB_SOURCE_TOOLBAR (object);

	g_free (toolbar->priv->search_text);
	g_free (toolbar->priv->popup_path);

	G_OBJECT_CLASS (rb_source_toolbar_parent_class)->finalize (object);
}

static void
impl_dispose (GObject *object)
{
	RBSourceToolbar *toolbar = RB_SOURCE_TOOLBAR (object);

	g_clear_object (&toolbar->priv->accel_group);
	g_clear_object (&toolbar->priv->search_popup);

	G_OBJECT_CLASS (rb_source_toolbar_parent_class)->dispose (object);
}

static void
impl_constructed (GObject *object)
{
	RBSourceToolbar *toolbar;
	GtkWidget *blank;
	GMenuModel *toolbar_menu;

	RB_CHAIN_GOBJECT_METHOD (rb_source_toolbar_parent_class, constructed, object);

	toolbar = RB_SOURCE_TOOLBAR (object);

	g_object_get (toolbar->priv->page,
		      "toolbar-menu", &toolbar_menu,
		      NULL);
	if (toolbar_menu != NULL) {
		toolbar->priv->button_bar = rb_button_bar_new (toolbar_menu, G_OBJECT (toolbar->priv->page));
		gtk_widget_show_all (toolbar->priv->button_bar);
		gtk_grid_attach (GTK_GRID (toolbar), toolbar->priv->button_bar, 0, 0, 2 ,1);
		g_object_unref (toolbar_menu);
	} else {
		blank = gtk_toolbar_new ();
		gtk_widget_set_hexpand (blank, TRUE);
		gtk_toolbar_set_style (GTK_TOOLBAR (blank), GTK_TOOLBAR_TEXT);
		gtk_grid_attach (GTK_GRID (toolbar), blank, 0, 0, 2 ,1);
	}

	/* search entry gets created later if required */

	g_signal_connect (toolbar->priv->page, "notify::selected", G_CALLBACK (source_selected_cb), toolbar);
}

static void
impl_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
	RBSourceToolbar *toolbar = RB_SOURCE_TOOLBAR (object);

	switch (prop_id) {
	case PROP_PAGE:
		g_value_set_object (value, toolbar->priv->page);
		break;
	case PROP_ACCEL_GROUP:
		g_value_set_object (value, toolbar->priv->accel_group);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
impl_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
	RBSourceToolbar *toolbar = RB_SOURCE_TOOLBAR (object);

	switch (prop_id) {
	case PROP_PAGE:
		toolbar->priv->page = g_value_get_object (value);	/* don't take a ref */
		break;
	case PROP_ACCEL_GROUP:
		toolbar->priv->accel_group = g_value_dup_object (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
rb_source_toolbar_init (RBSourceToolbar *toolbar)
{
	toolbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (toolbar, RB_TYPE_SOURCE_TOOLBAR, RBSourceToolbarPrivate);

	toolbar->priv->search_value = -1;
}

static void
rb_source_toolbar_class_init (RBSourceToolbarClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->constructed = impl_constructed;
	object_class->dispose = impl_dispose;
	object_class->finalize = impl_finalize;
	object_class->set_property = impl_set_property;
	object_class->get_property = impl_get_property;

	/**
	 * RBSourceToolbar:page:
	 *
	 * The #RBDisplayPage the toolbar is associated with
	 */
	g_object_class_install_property (object_class,
					 PROP_PAGE,
					 g_param_spec_object ("page",
							      "page",
							      "RBDisplayPage instance",
							      RB_TYPE_DISPLAY_PAGE,
							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
	/**
	 * RBSourceToolbar:accel-group:
	 *
	 * The #GtkAccelGroup to add accelerators to
	 */
	g_object_class_install_property (object_class,
					 PROP_ACCEL_GROUP,
					 g_param_spec_object ("accel-group",
							      "accel group",
							      "GtkAccelGroup instance",
							      GTK_TYPE_ACCEL_GROUP,
							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
	g_type_class_add_private (klass, sizeof (RBSourceToolbarPrivate));
}

/**
 * rb_source_toolbar_new:
 * @page: a #RBDisplayPage
 * @accel_group: a #GtkAccelGroup to add accelerators to
 *
 * Creates a new source toolbar for @page.  The toolbar does not
 * initially include a search entry.  Call #rb_source_toolbar_add_search_entry
 * to add one.  The toolbar content comes from the @RBSource:toolbar-menu property.
 *
 * Return value: the #RBSourceToolbar
 */
RBSourceToolbar *
rb_source_toolbar_new (RBDisplayPage *page, GtkAccelGroup *accel_group)
{
	GObject *object;
	object = g_object_new (RB_TYPE_SOURCE_TOOLBAR,
			       "page", page,
			       "accel-group", accel_group,
			       "column-spacing", 6,
			       "column-homogeneous", TRUE,
			       "row-spacing", 6,
			       "row-homogeneous", TRUE,
			       "margin-start", 6,
			       "margin-end", 6,
			       NULL);
	return RB_SOURCE_TOOLBAR (object);
}

static void
search_state_notify_cb (GObject *action, GParamSpec *pspec, RBSourceToolbar *toolbar)
{
	GVariant *state;

	/* get search for current state */
	state = g_action_get_state (G_ACTION (action));
	toolbar->priv->active_search = rb_source_search_get_by_name (g_variant_get_string (state, NULL));
	g_variant_unref (state);

	if (toolbar->priv->search_text != NULL) {
		rb_source_search (RB_SOURCE (toolbar->priv->page), toolbar->priv->active_search, NULL, toolbar->priv->search_text);
	}

	if (toolbar->priv->active_search != NULL) {
		rb_search_entry_set_placeholder (toolbar->priv->search_entry, rb_source_search_get_description (toolbar->priv->active_search));
	} else {
		rb_search_entry_set_placeholder (toolbar->priv->search_entry, NULL);	/* ? */
	}
}

static void
add_search_entry (RBSourceToolbar *toolbar, gboolean menu)
{
	g_assert (toolbar->priv->search_entry == NULL);

	toolbar->priv->search_entry = rb_search_entry_new (menu);
	gtk_grid_attach (GTK_GRID (toolbar), GTK_WIDGET (toolbar->priv->search_entry), 2, 0, 1, 1);

	g_signal_connect (toolbar->priv->search_entry, "search", G_CALLBACK (search_cb), toolbar);
}

/**
 * rb_source_toolbar_add_search_entry_menu:
 * @toolbar: a #RBSourceToolbar
 * @search_menu: a #GMenu containing search items
 * @search_action: the #GAction for search state
 *
 * Adds a search entry to the toolbar.
 */
void
rb_source_toolbar_add_search_entry_menu (RBSourceToolbar *toolbar, GMenuModel *search_menu, GAction *search_action)
{
	g_return_if_fail (search_menu != NULL);
	g_return_if_fail (search_action != NULL);

	add_search_entry (toolbar, TRUE);

	toolbar->priv->search_popup = gtk_menu_new_from_model (search_menu);
	gtk_menu_attach_to_widget (GTK_MENU (toolbar->priv->search_popup), GTK_WIDGET (toolbar), NULL);
	g_object_ref_sink (toolbar->priv->search_popup);
	toolbar->priv->search_action = g_object_ref (search_action);

	g_signal_connect (toolbar->priv->search_entry, "show-popup", G_CALLBACK (show_popup_cb), toolbar);
	g_signal_connect (toolbar->priv->search_action, "notify::state", G_CALLBACK (search_state_notify_cb), toolbar);
	search_state_notify_cb (G_OBJECT (toolbar->priv->search_action), NULL, toolbar);
}

/**
 * rb_source_toolbar_add_search_entry:
 * @toolbar: a #RBSourceToolbar
 * @placeholder: the placeholder text for the search entry (or NULL)
 *
 * Adds a search entry with no search type menu.
 */
void
rb_source_toolbar_add_search_entry (RBSourceToolbar *toolbar, const char *placeholder)
{
	add_search_entry (toolbar, FALSE);
	rb_search_entry_set_placeholder (toolbar->priv->search_entry, placeholder);
}

/**
 * rb_source_toolbar_clear_search_entry:
 * @toolbar: a #RBSourceToolbar
 *
 * Clears the search entry text.  Call this from RBSource:reset_filters.
 */
void
rb_source_toolbar_clear_search_entry (RBSourceToolbar *toolbar)
{
	g_assert (toolbar->priv->search_entry != NULL);
	rb_search_entry_clear (toolbar->priv->search_entry);
	g_free (toolbar->priv->search_text);
	toolbar->priv->search_text = NULL;
}