File: gconf-cell-renderer.c

package info (click to toggle)
gconf-editor 3.0.1-6
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,216 kB
  • sloc: sh: 11,060; xml: 7,126; ansic: 5,307; makefile: 209
file content (485 lines) | stat: -rw-r--r-- 13,832 bytes parent folder | download | duplicates (4)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * Copyright (C) 2001, 2002 Anders Carlsson <andersca@gnu.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.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <config.h>
#include "gconf-cell-renderer.h"

#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include "gconf-util.h"
#include "gconf-marshal.h"

#define GCONF_CELL_RENDERER_TEXT_PATH "gconf-cell-renderer-text-path"
#define GCONF_CELL_RENDERER_VALUE "gconf-cell-renderer-value"
#define SCHEMA_TEXT "<schema>"

enum {
	PROP_ZERO,
	PROP_VALUE
};

enum {
	CHANGED,
	CHECK_WRITABLE,
	LAST_SIGNAL
};

static guint gconf_cell_signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE(GConfCellRenderer, gconf_cell_renderer, GTK_TYPE_CELL_RENDERER)


static void
gconf_cell_renderer_get_property (GObject *object, guint param_id, GValue *value, GParamSpec *pspec)
{
	GConfCellRenderer *cellvalue;

	cellvalue = GCONF_CELL_RENDERER (object);
	
	switch (param_id) {
	case PROP_VALUE:
		g_value_set_boxed (value, cellvalue->value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}

}

static void
gconf_cell_renderer_set_property (GObject *object, guint param_id, const GValue *value, GParamSpec *pspec)
{
	GConfCellRenderer *cellvalue;
	GConfValue *gconf_value;
	GtkCellRendererMode new_mode = GTK_CELL_RENDERER_MODE_INERT;
	
	cellvalue = GCONF_CELL_RENDERER (object);

	switch (param_id) {
	case PROP_VALUE:
		if (cellvalue->value) 
			gconf_value_free (cellvalue->value);

		gconf_value = g_value_get_boxed (value);

		if (gconf_value) {
			cellvalue->value = gconf_value_copy (gconf_value);

			switch (gconf_value->type) {
			case GCONF_VALUE_INT:
			case GCONF_VALUE_FLOAT:
			case GCONF_VALUE_STRING:
				new_mode = GTK_CELL_RENDERER_MODE_EDITABLE;
				break;
				
			case GCONF_VALUE_BOOL:
				new_mode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
				break;

			case GCONF_VALUE_LIST:
			case GCONF_VALUE_SCHEMA:
			case GCONF_VALUE_PAIR:
				new_mode = GTK_CELL_RENDERER_MODE_INERT;
				break;
			default:
				g_warning ("unhandled value type %d", gconf_value->type);
				break;
			}
		}
		else {
			cellvalue->value = NULL;
		}

		g_object_set (object, "mode", new_mode, NULL);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;	
	}
}

static void
gconf_cell_renderer_finalize (GObject *object)
{
	GConfCellRenderer *renderer = GCONF_CELL_RENDERER (object);

	if (renderer->value)
		gconf_value_free (renderer->value);

	g_object_unref (renderer->text_renderer);
	g_object_unref (renderer->toggle_renderer);

	(* G_OBJECT_CLASS (gconf_cell_renderer_parent_class)->finalize) (object);
}

static void
gconf_cell_renderer_get_size (GtkCellRenderer *cell,
			      GtkWidget *widget,
			      const GdkRectangle *cell_area,
			      gint *x_offset,
			      gint *y_offset,
			      gint *width,
			      gint *height)
{
	GConfCellRenderer *cellvalue;
	gchar *tmp_str;
	
	cellvalue = GCONF_CELL_RENDERER (cell);

	if (cellvalue->value == NULL) {
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", _("<no value>"),
			      NULL);
		gtk_cell_renderer_get_size (cellvalue->text_renderer, widget, cell_area,
					    x_offset, y_offset, width, height);
		return;
	}
	
	switch (cellvalue->value->type) {
	case GCONF_VALUE_FLOAT:
	case GCONF_VALUE_INT:
		tmp_str = gconf_value_to_string (cellvalue->value);
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", tmp_str,
			      NULL);
		gtk_cell_renderer_get_size (cellvalue->text_renderer, widget, cell_area,
					    x_offset, y_offset, width, height);
		g_free (tmp_str);
		break;
	case GCONF_VALUE_STRING:
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", gconf_value_get_string (cellvalue->value),
			      NULL);
		gtk_cell_renderer_get_size (cellvalue->text_renderer, widget, cell_area,
					    x_offset, y_offset, width, height);
		break;
	case GCONF_VALUE_BOOL:
		g_object_set (G_OBJECT (cellvalue->toggle_renderer),
			      "xalign", 0.0,
			      "active", gconf_value_get_bool (cellvalue->value),
			      NULL);
		gtk_cell_renderer_get_size (cellvalue->toggle_renderer, widget, cell_area,
					    x_offset, y_offset, width, height);
		break;
        case GCONF_VALUE_SCHEMA:
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", SCHEMA_TEXT,
			      NULL);
		gtk_cell_renderer_get_size (cellvalue->text_renderer,
					    widget, cell_area,
					    x_offset, y_offset, width, height);
		break;
	case GCONF_VALUE_LIST:
		tmp_str = gconf_value_to_string (cellvalue->value);
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", tmp_str,
			      NULL);
		gtk_cell_renderer_get_size (cellvalue->text_renderer,
					    widget, cell_area,
					    x_offset, y_offset, width, height);
		g_free (tmp_str);
		break;
	case GCONF_VALUE_PAIR:
		tmp_str = gconf_value_to_string (cellvalue->value);
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", tmp_str,
			      NULL);
		gtk_cell_renderer_get_size (cellvalue->text_renderer,
					    widget, cell_area,
					    x_offset, y_offset, width, height);
		g_free (tmp_str);
		break;
	default:
		g_print ("get_size: Unknown type: %d\n", cellvalue->value->type);
		break;
	}
}

static void
gconf_cell_renderer_text_editing_done (GtkCellEditable *entry, GConfCellRenderer *cell)
{
	const gchar *path;
	const gchar *new_text;
	gboolean editing_canceled = FALSE;
	GConfValue *value;

	g_object_get (entry, "editing-canceled", &editing_canceled, NULL);
	if (editing_canceled != FALSE)
		return;

	path = g_object_get_data (G_OBJECT (entry), GCONF_CELL_RENDERER_TEXT_PATH);
	value = g_object_get_data (G_OBJECT (entry), GCONF_CELL_RENDERER_VALUE);
	new_text = gtk_entry_get_text (GTK_ENTRY (entry));

	switch (value->type) {
	case GCONF_VALUE_STRING:
		gconf_value_set_string (value, new_text);
		break;
	case GCONF_VALUE_FLOAT:
		gconf_value_set_float (value, (gdouble)(g_ascii_strtod (new_text, NULL)));
		break;
	case GCONF_VALUE_INT:
		gconf_value_set_int (value, (gint)(g_ascii_strtod (new_text, NULL)));
		break;
	default:
		g_error ("editing done, unknown value %d", value->type);
		break;
	}
	
	g_signal_emit (cell, gconf_cell_signals[CHANGED], 0, path, value);
}

static gboolean
gconf_cell_renderer_check_writability (GConfCellRenderer *cell, const gchar *path)
{
	gboolean ret = TRUE;
	g_signal_emit (cell, gconf_cell_signals[CHECK_WRITABLE], 0, path, &ret);
	return ret;
}

static GtkCellEditable *
gconf_cell_renderer_start_editing (GtkCellRenderer      *cell,
				   GdkEvent             *event,
				   GtkWidget            *widget,
				   const gchar          *path,
				   const GdkRectangle   *background_area,
				   const GdkRectangle   *cell_area,
				   GtkCellRendererState  flags)
{
	GtkWidget *entry;
	GConfCellRenderer *cellvalue;
	gchar *tmp_str;

	cellvalue = GCONF_CELL_RENDERER (cell);

	/* If not writable then we definately can't edit */
	if ( ! gconf_cell_renderer_check_writability (cellvalue, path))
		return NULL;
	
	switch (cellvalue->value->type) {
	case GCONF_VALUE_INT:
	case GCONF_VALUE_FLOAT:
	case GCONF_VALUE_STRING:
		tmp_str = gconf_value_to_string (cellvalue->value);
		entry = g_object_new (GTK_TYPE_ENTRY,
				      "has_frame", FALSE,
				      "text", tmp_str,
				      NULL);
		g_free (tmp_str);
		g_signal_connect (entry, "editing_done",
				  G_CALLBACK (gconf_cell_renderer_text_editing_done), cellvalue);

		g_object_set_data_full (G_OBJECT (entry), GCONF_CELL_RENDERER_TEXT_PATH, g_strdup (path), g_free);
		g_object_set_data_full (G_OBJECT (entry), GCONF_CELL_RENDERER_VALUE, gconf_value_copy (cellvalue->value), (GDestroyNotify)gconf_value_free);
		
		gtk_widget_show (entry);

		return GTK_CELL_EDITABLE (entry);
		break;
	default:
		g_error ("%d shouldn't be handled here", cellvalue->value->type);
		break;
	}

	
	return NULL;
}

static gint
gconf_cell_renderer_activate (GtkCellRenderer      *cell,
			      GdkEvent             *event,
			      GtkWidget            *widget,
			      const gchar          *path,
			      const GdkRectangle   *background_area,
			      const GdkRectangle   *cell_area,
			      GtkCellRendererState  flags)
{
	GConfCellRenderer *cellvalue;
	
	cellvalue = GCONF_CELL_RENDERER (cell);

	if (cellvalue->value == NULL)
		return TRUE;

	switch (cellvalue->value->type) {
	case GCONF_VALUE_BOOL:
		gconf_value_set_bool (cellvalue->value, !gconf_value_get_bool (cellvalue->value));
		g_signal_emit (cell, gconf_cell_signals[CHANGED], 0, path, cellvalue->value);
		
		break;
	default:
		g_error ("%d shouldn't be handled here", cellvalue->value->type);
		break;
	}

	return TRUE;
}

static void
gconf_cell_renderer_render (GtkCellRenderer *cell,
			    cairo_t *cr,
			    GtkWidget *widget,
			    const GdkRectangle *background_area,
			    const GdkRectangle *cell_area,
			    GtkCellRendererState flags)
{
	GConfCellRenderer *cellvalue;
	char *tmp_str;
	
	cellvalue = GCONF_CELL_RENDERER (cell);

	if (cellvalue->value == NULL) {
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", _("<no value>"),
			      NULL);

		gtk_cell_renderer_render (cellvalue->text_renderer, cr, widget,
					  background_area, cell_area, flags);
		return;
	}

	switch (cellvalue->value->type) {
	case GCONF_VALUE_FLOAT:
	case GCONF_VALUE_INT:
		tmp_str = gconf_value_to_string (cellvalue->value);
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", tmp_str,
			      NULL);
		gtk_cell_renderer_render (cellvalue->text_renderer, cr, widget,
					  background_area, cell_area, flags);
		g_free (tmp_str);
		break;
	case GCONF_VALUE_STRING:
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", gconf_value_get_string (cellvalue->value),
			      NULL);
		gtk_cell_renderer_render (cellvalue->text_renderer, cr, widget,
					  background_area, cell_area, flags);
		break;
	case GCONF_VALUE_BOOL:
		g_object_set (G_OBJECT (cellvalue->toggle_renderer),
			      "xalign", 0.0,
			      "active", gconf_value_get_bool (cellvalue->value),
			      NULL);
		
		gtk_cell_renderer_render (cellvalue->toggle_renderer, cr, widget,
					  background_area, cell_area, flags);
		break;

	case GCONF_VALUE_SCHEMA:
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", SCHEMA_TEXT,
			      NULL);
		
		gtk_cell_renderer_render (cellvalue->text_renderer, cr, widget,
					  background_area, cell_area, flags);
		break;
		
	case GCONF_VALUE_LIST:
		tmp_str = gconf_value_to_string (cellvalue->value);
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", tmp_str,
			      NULL);
		
		gtk_cell_renderer_render (cellvalue->text_renderer, cr, widget,
					  background_area, cell_area, flags);
		g_free (tmp_str);
		break;
	case GCONF_VALUE_PAIR:
		tmp_str = gconf_value_to_string (cellvalue->value);
		g_object_set (G_OBJECT (cellvalue->text_renderer),
			      "text", tmp_str,
			      NULL);
		gtk_cell_renderer_render (cellvalue->text_renderer, cr, widget,
					  background_area, cell_area, flags);
		g_free (tmp_str);
		break;

	default:
		g_print ("render: Unknown type: %d\n", cellvalue->value->type);
		break;
	}
}

static void
gconf_cell_renderer_class_init (GConfCellRendererClass *klass)
{
	GObjectClass *object_class = (GObjectClass *)klass;
	GtkCellRendererClass *cell_renderer_class = (GtkCellRendererClass *)klass;

	object_class->get_property = gconf_cell_renderer_get_property;
	object_class->set_property = gconf_cell_renderer_set_property;
	object_class->finalize = gconf_cell_renderer_finalize;

	cell_renderer_class->get_size = gconf_cell_renderer_get_size;
	cell_renderer_class->render = gconf_cell_renderer_render;
	cell_renderer_class->activate = gconf_cell_renderer_activate;
	cell_renderer_class->start_editing = gconf_cell_renderer_start_editing;

	g_object_class_install_property (object_class, PROP_VALUE,
					 g_param_spec_boxed ("value",
							     NULL, NULL,
							     GCONF_TYPE_VALUE,
							     G_PARAM_READWRITE));

	gconf_cell_signals[CHANGED] =
		g_signal_new ("changed",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (GConfCellRendererClass, changed),
			      (GSignalAccumulator) NULL, NULL,
			      gconf_marshal_VOID__STRING_BOXED,
			      G_TYPE_NONE, 2,
			      G_TYPE_STRING,
			      GCONF_TYPE_VALUE);
	gconf_cell_signals[CHECK_WRITABLE] =
		g_signal_new ("check_writable",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (GConfCellRendererClass, changed),
			      (GSignalAccumulator) NULL, NULL,
			      gconf_marshal_BOOLEAN__STRING,
			      G_TYPE_BOOLEAN, 1,
			      G_TYPE_STRING);
}

static void
gconf_cell_renderer_init (GConfCellRenderer *renderer)
{
	g_object_set (renderer,
	              "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
	              NULL);
	
	renderer->text_renderer = gtk_cell_renderer_text_new ();
	g_object_ref_sink (renderer->text_renderer);

	renderer->toggle_renderer = gtk_cell_renderer_toggle_new ();
	g_object_ref_sink (renderer->toggle_renderer);
}



GtkCellRenderer *
gconf_cell_renderer_new (void)
{
  return GTK_CELL_RENDERER (g_object_new (GCONF_TYPE_CELL_RENDERER, NULL));
}