File: rb-object-property-editor.c

package info (click to toggle)
rhythmbox 3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 43,424 kB
  • ctags: 15,462
  • sloc: ansic: 115,908; sh: 11,821; xml: 5,144; python: 3,777; makefile: 2,438
file content (416 lines) | stat: -rw-r--r-- 11,865 bytes parent folder | download | duplicates (2)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 *  Copyright (C) 2012 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-object-property-editor.h>
#include <lib/rb-util.h>
#include <lib/rb-debug.h>

static void rb_object_property_editor_class_init (RBObjectPropertyEditorClass *klass);
static void rb_object_property_editor_init (RBObjectPropertyEditor *editor);

struct _RBObjectPropertyEditorPrivate
{
	GObject *object;
	char **properties;

	gboolean changed;
	gulong notify_id;
};

G_DEFINE_TYPE (RBObjectPropertyEditor, rb_object_property_editor, GTK_TYPE_GRID);

/**
 * SECTION:rb-object-property-editor
 * @short_description: builds widgetry for editing simple GObject properties
 *
 * RBObjectPropertyEditor can be used to provide an interface to edit
 * simple (boolean, integer, enum, float) properties of a GObject.
 */

enum
{
	PROP_0,
	PROP_OBJECT,
	PROP_PROPERTIES
};

enum
{
	CHANGED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

static void
impl_finalize (GObject *object)
{
	RBObjectPropertyEditor *editor = RB_OBJECT_PROPERTY_EDITOR (object);

	g_strfreev (editor->priv->properties);

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

static void
impl_dispose (GObject *object)
{
	RBObjectPropertyEditor *editor = RB_OBJECT_PROPERTY_EDITOR (object);

	if (editor->priv->object != NULL) {
		if (editor->priv->notify_id) {
			g_signal_handler_disconnect (editor->priv->object,
						     editor->priv->notify_id);
			editor->priv->notify_id = 0;
		}
		g_object_unref (editor->priv->object);
		editor->priv->object = NULL;
	}

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

static void
notify_cb (GObject *object, GParamSpec *pspec, RBObjectPropertyEditor *editor)
{
	editor->priv->changed = TRUE;
}

static void
focus_out_cb (GtkWidget *widget, GdkEvent *event, RBObjectPropertyEditor *editor)
{
	if (editor->priv->changed) {
		rb_debug ("emitting changed");
		g_signal_emit (editor, signals[CHANGED], 0);
		editor->priv->changed = FALSE;
	} else {
		rb_debug ("not emitting changed");
	}
}

static GtkWidget *
create_boolean_editor (RBObjectPropertyEditor *editor, const char *property, GParamSpec *pspec)
{
	GtkWidget *control;

	control = gtk_check_button_new ();

	g_object_bind_property (editor->priv->object, property,
				control, "active",
				G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);

	return control;
}

static GtkWidget *
create_enum_editor (RBObjectPropertyEditor *editor, const char *property, GParamSpec *pspec)
{
	GParamSpecEnum *penum;
	GtkListStore *model;
	GtkCellRenderer *renderer;
	GtkWidget *control;
	int p;

	control = gtk_combo_box_new ();
	penum = G_PARAM_SPEC_ENUM (pspec);
	renderer = gtk_cell_renderer_text_new ();

	model = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
	gtk_combo_box_set_model (GTK_COMBO_BOX (control), GTK_TREE_MODEL (model));
	for (p = 0; p < penum->enum_class->n_values; p++) {
		gtk_list_store_insert_with_values (model, NULL, p,
						   0, penum->enum_class->values[p].value_name,
						   1, p,
						   -1);
	}
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (control), renderer, TRUE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (control), renderer, "text", 0, NULL);

	g_object_bind_property (editor->priv->object, property,
				control, "active",
				G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);

	return control;
}

static GtkWidget *
create_int_editor (RBObjectPropertyEditor *editor, const char *property, GParamSpec *pspec)
{
	GParamSpecInt *pint;
	GtkWidget *control;
	GtkAdjustment *adjustment;

	pint = G_PARAM_SPEC_INT (pspec);

	adjustment = gtk_adjustment_new (pint->default_value,
					 pint->minimum,
					 pint->maximum + 1,
					 1.0,
					 1.0, 1.0);

	control = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adjustment);
	gtk_scale_set_digits (GTK_SCALE (control), 0);

	g_object_bind_property (editor->priv->object, property,
				adjustment, "value",
				G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);

	return control;
}

static GtkWidget *
create_float_editor (RBObjectPropertyEditor *editor, const char *property, GParamSpec *pspec)
{
	GParamSpecFloat *pfloat;
	GtkWidget *control;
	GtkAdjustment *adjustment;

	pfloat = G_PARAM_SPEC_FLOAT (pspec);

	adjustment = gtk_adjustment_new (pfloat->default_value,
					 pfloat->minimum,
					 pfloat->maximum + pfloat->epsilon*2,
					 pfloat->epsilon*10,
					 0.1, 0.1);

	control = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adjustment);

	g_object_bind_property (editor->priv->object, property,
				adjustment, "value",
				G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);

	return control;
}

static GtkWidget *
create_double_editor (RBObjectPropertyEditor *editor, const char *property, GParamSpec *pspec)
{
	GParamSpecDouble *pdouble;
	GtkWidget *control;
	GtkAdjustment *adjustment;

	pdouble = G_PARAM_SPEC_DOUBLE (pspec);

	adjustment = gtk_adjustment_new (pdouble->default_value,
					 pdouble->minimum,
					 pdouble->maximum + pdouble->epsilon*2,
					 pdouble->epsilon*10,
					 0.1, 0.1);

	control = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adjustment);

	g_object_bind_property (editor->priv->object, property,
				adjustment, "value",
				G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);

	return control;
}

static void
impl_constructed (GObject *object)
{
	RBObjectPropertyEditor *editor;
	GObjectClass *klass;
	int i;
	int row;

	RB_CHAIN_GOBJECT_METHOD (rb_object_property_editor_parent_class, constructed, object);

	editor = RB_OBJECT_PROPERTY_EDITOR (object);
	klass = G_OBJECT_GET_CLASS (editor->priv->object);

	editor->priv->notify_id = g_signal_connect (editor->priv->object, "notify", G_CALLBACK (notify_cb), editor);

	row = 0;
	for (i = 0; editor->priv->properties[i] != NULL; i++) {
		GParamSpec *pspec;
		GtkWidget *label;
		GtkWidget *control;
		GType prop_type;

		pspec = g_object_class_find_property (klass, editor->priv->properties[i]);
		if (pspec == NULL) {
			g_warning ("couldn't find property %s on object %s",
				   editor->priv->properties[i],
				   G_OBJECT_CLASS_NAME (klass));
			continue;
		}

		prop_type = G_PARAM_SPEC_TYPE (pspec);
		if (prop_type == G_TYPE_PARAM_BOOLEAN) {
			control = create_boolean_editor (editor, editor->priv->properties[i], pspec);
		} else if (prop_type == G_TYPE_PARAM_ENUM) {
			control = create_enum_editor (editor, editor->priv->properties[i], pspec);
		} else if (prop_type == G_TYPE_PARAM_INT) {
			control = create_int_editor (editor, editor->priv->properties[i], pspec);
		} else if (prop_type == G_TYPE_PARAM_FLOAT) {
			control = create_float_editor (editor, editor->priv->properties[i], pspec);
		} else if (prop_type == G_TYPE_PARAM_DOUBLE) {
			control = create_double_editor (editor, editor->priv->properties[i], pspec);
		} else {
			/* can't do this */
			g_warning ("don't know how to edit %s", g_type_name (prop_type));
			continue;
		}
		g_signal_connect (control, "focus-out-event", G_CALLBACK (focus_out_cb), editor);
		gtk_widget_set_hexpand (control, TRUE);

		label = gtk_label_new (g_param_spec_get_nick (pspec));
		gtk_widget_set_tooltip_text (label, g_param_spec_get_blurb (pspec));

		gtk_grid_attach (GTK_GRID (editor),
				 label,
				 0, row, 1, 1);
		gtk_grid_attach (GTK_GRID (editor),
				 control,
				 1, row, 1, 1);

		row++;
	}
}

static void
impl_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
	RBObjectPropertyEditor *editor = RB_OBJECT_PROPERTY_EDITOR (object);
	switch (prop_id) {
	case PROP_OBJECT:
		editor->priv->object = g_value_dup_object (value);
		break;
	case PROP_PROPERTIES:
		editor->priv->properties = g_value_dup_boxed (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
	}
}

static void
impl_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
	RBObjectPropertyEditor *editor = RB_OBJECT_PROPERTY_EDITOR (object);
	switch (prop_id) {
	case PROP_OBJECT:
		g_value_set_object (value, editor->priv->object);
		break;
	case PROP_PROPERTIES:
		g_value_set_boxed (value, editor->priv->properties);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
	}
}

static void
rb_object_property_editor_init (RBObjectPropertyEditor *editor)
{
	editor->priv = G_TYPE_INSTANCE_GET_PRIVATE (editor,
						    RB_TYPE_OBJECT_PROPERTY_EDITOR,
						    RBObjectPropertyEditorPrivate);
}


static void
rb_object_property_editor_class_init (RBObjectPropertyEditorClass *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;

	/**
	 * RBObjectPropertyEditor::changed:
	 * @editor: the #RBObjectPropertyEditor
	 *
	 * Emitted when a property has been changed.
	 * This won't be emitted on every single change (use 'notify' on the
	 * object being edited for that), but rather when the edit widget
	 * for a property loses focus and the value was changed.
	 */
	signals[CHANGED] =
		g_signal_new ("changed",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (RBObjectPropertyEditorClass, changed),
			      NULL, NULL,
			      NULL,
			      G_TYPE_NONE,
			      0);

	/**
	 * RBObjectPropertyEditor:object
	 *
	 * The object to edit.
	 */
	g_object_class_install_property (object_class,
					 PROP_OBJECT,
					 g_param_spec_object ("object",
							      "object",
							      "object to edit",
							      G_TYPE_OBJECT,
							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	/**
	 * RBObjectPropertyEditor:properties
	 * 
	 * Array of names of properties to edit.
	 */
	g_object_class_install_property (object_class,
					 PROP_PROPERTIES,
					 g_param_spec_boxed ("properties",
							     "properties",
							     "properties to edit",
							     G_TYPE_STRV,
							     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

	g_type_class_add_private (klass, sizeof (RBObjectPropertyEditorPrivate));
}

/**
 * rb_object_property_editor_new:
 * @object: the object to edit
 * @properties: array of names of properties to edit
 *
 * Creates a property editor for the specified properties of an object.
 *
 * Return value: property editor widget.
 */
GtkWidget *
rb_object_property_editor_new (GObject *object, char **properties)
{
	return GTK_WIDGET (g_object_new (RB_TYPE_OBJECT_PROPERTY_EDITOR,
					 "object", object,
					 "properties", properties,
					 "column-spacing", 6,
					 NULL));
}