File: bonobo-ui-config-widget.c

package info (click to toggle)
bonobo 1.0.22-2.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,412 kB
  • ctags: 5,445
  • sloc: ansic: 51,714; sh: 7,733; makefile: 1,425; yacc: 318; xml: 266; sed: 16
file content (452 lines) | stat: -rw-r--r-- 12,697 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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/**
 * bonobo-ui-config-widget.c: Bonobo Component UIConfig widget
 *
 * Authors:
 *   Michael Meeks (michael@helixcode.com)
 *
 * Copyright 2001 Helix Code, Inc.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <gtk/gtk.h>

#include <libgnome/gnome-defs.h>

#define GNOME_EXPLICIT_TRANSLATION_DOMAIN PACKAGE
#include <libgnome/gnome-i18n.h>

#include <bonobo/bonobo-ui-util.h>
#include <bonobo/bonobo-ui-engine-private.h>
#include <bonobo/bonobo-ui-config-widget.h>
#include <bonobo/bonobo-ui-sync-toolbar.h>

#define PARENT_TYPE gtk_vbox_get_type ()

static GtkObjectClass *parent_class;

struct _BonoboUIConfigWidgetPrivate {
	GtkWidget  *list;

	GtkWidget  *left_attrs;
	GtkWidget  *right_attrs;

	GtkWidget  *show;
	GtkWidget  *hide;

	GtkWidget  *tooltips;

	GtkWidget  *icon;
	GtkWidget  *icon_and_text;
	GtkWidget  *priority_text;

	const char *cur_path;
};

#define WIDGET_ATTR_KEY "BonoboUIConfigWidget_Attr"

static const char *
widget_get_attr (GtkWidget *widget)
{
	return gtk_object_get_data (GTK_OBJECT (widget),
				    WIDGET_ATTR_KEY);
}

static void
set_values (BonoboUIConfigWidget *config)
{
	char    *txt;
	BonoboUINode *node;
	gboolean hidden = FALSE;
	gboolean tooltips = TRUE;
	BonoboUIToolbarStyle style;

	g_return_if_fail (config->priv->cur_path != NULL);

	node = bonobo_ui_engine_get_path (config->engine, config->priv->cur_path);

	if ((txt = bonobo_ui_node_get_attr (node, "hidden"))) {
		hidden = atoi (txt);
		bonobo_ui_node_free_string (txt);
	}

	if (hidden)
		gtk_toggle_button_set_active (
			GTK_TOGGLE_BUTTON (config->priv->hide),
			TRUE);
	else
		gtk_toggle_button_set_active (
			GTK_TOGGLE_BUTTON (config->priv->show),
			TRUE);
	
	/* Set the tooltips */
	if ((txt = bonobo_ui_node_get_attr (node, "tips"))) {
		tooltips = atoi (txt);
		bonobo_ui_node_free_string (txt);
	}
	
	gtk_toggle_button_set_active (
		GTK_TOGGLE_BUTTON (config->priv->tooltips),
		tooltips);

	/* Set the look */
	style = bonobo_ui_sync_toolbar_get_look (config->engine, node);
	switch (style) {
	case BONOBO_UI_TOOLBAR_STYLE_ICONS_ONLY:
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (config->priv->icon), TRUE);
		break;

	case BONOBO_UI_TOOLBAR_STYLE_ICONS_AND_TEXT:
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (config->priv->icon_and_text), TRUE);
		break;

	case BONOBO_UI_TOOLBAR_STYLE_PRIORITY_TEXT:
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (config->priv->priority_text), TRUE);
		break;
		
	default:
		break;
	}
}

static void
select_child_cb (GtkList	      *list,
		 GtkWidget            *child,
		 BonoboUIConfigWidget *config)
{
	BonoboUINode *node;

	config->priv->cur_path = widget_get_attr (child);

	node = bonobo_ui_engine_get_path (
		config->engine, config->priv->cur_path);

	gtk_widget_set_sensitive (config->priv->left_attrs, node != NULL);
	gtk_widget_set_sensitive (config->priv->right_attrs, node != NULL);

	if (node)
		set_values (config);
	else
		g_warning ("Toolbar has been removed");
}

static void
populate_list (GtkWidget            *list,
	       BonoboUIConfigWidget *config)
{
	BonoboUINode *l, *start;

	GList *items = NULL;

	start = bonobo_ui_node_children (
		bonobo_ui_engine_get_xml (config->engine)->root);

	if (!start)
		g_warning ("No tree");

	for (l = start; l;
	     l = bonobo_ui_node_next (l)) {

		if (bonobo_ui_node_has_name (l, "dockitem")) {
			char    *txt, *name;

			if ((txt = bonobo_ui_node_get_attr (l, "tip"))) {
				gboolean err;

				name = bonobo_ui_util_decode_str (txt, &err);
				g_return_if_fail (!err);

				bonobo_ui_node_free_string (txt);
			} else if (!(name = bonobo_ui_node_get_attr (l, "name")))
				name = NULL;

			if (name) {
				GtkWidget *w = gtk_list_item_new_with_label (name);
				char      *path = bonobo_ui_xml_make_path (l);

				gtk_object_set_data_full (GTK_OBJECT (w),
							  WIDGET_ATTR_KEY,
							  path,
							  (GtkDestroyNotify) g_free);

				gtk_widget_show (w);
				items = g_list_prepend (items, w);
			}
		}
	}

	gtk_list_append_items (GTK_LIST (list), items);
	gtk_signal_connect (GTK_OBJECT (list), "select_child",
			    (GtkSignalFunc) select_child_cb, config);

	gtk_list_select_item (GTK_LIST (list), 0);
}

static void
show_hide_cb (GtkWidget            *button,
	      BonoboUIConfigWidget *config)
{
	g_return_if_fail (config->priv->cur_path != NULL);

	if (button == config->priv->show)
		bonobo_ui_engine_config_remove (
			bonobo_ui_engine_get_config (config->engine),
			config->priv->cur_path, "hidden");
	else
		bonobo_ui_engine_config_add (
			bonobo_ui_engine_get_config (config->engine),
			config->priv->cur_path, "hidden", "1");
}

static void
tooltips_cb (GtkWidget            *button,
	     BonoboUIConfigWidget *config)
{
	g_return_if_fail (config->priv->cur_path != NULL);

	if (gtk_toggle_button_get_active (
		GTK_TOGGLE_BUTTON (button)))
		bonobo_ui_engine_config_remove (
			bonobo_ui_engine_get_config (config->engine),
			config->priv->cur_path, "tips");
	else
		bonobo_ui_engine_config_add (
			bonobo_ui_engine_get_config (config->engine),
			config->priv->cur_path, "tips", "0");
}

static void
look_cb (GtkWidget            *button,
	 BonoboUIConfigWidget *config)
{
	const char *value = NULL;

	g_return_if_fail (config->priv->cur_path != NULL);

	if (button == config->priv->icon)
		value = "icon";

	else if (button == config->priv->icon_and_text)
		value = "both";

	else if (button == config->priv->priority_text)
		value = "text";

	else
		g_warning ("Unknown look selection");
	
	bonobo_ui_engine_config_add (
		bonobo_ui_engine_get_config (config->engine),
		config->priv->cur_path, "look", value);
}

static void
widgets_init (BonoboUIConfigWidget *config,
	      GtkAccelGroup        *accel_group)
{
	BonoboUIConfigWidgetPrivate *priv;
	GtkWidget *table2;
	GtkWidget *vbox6;
	GtkWidget *frame6;
	GtkWidget *vbox7;
	GSList *visible_group = NULL;
	guint key;
	GtkWidget *frame7;
	GtkWidget *toolbar_list;
	GtkWidget *frame5;
	GtkWidget *vbox5;
	GSList *look_group = NULL;

	priv = config->priv;

	table2 = gtk_table_new (2, 2, FALSE);
	gtk_box_pack_start (GTK_BOX (config), table2, TRUE, TRUE, 0);

	priv->left_attrs = vbox6 = gtk_vbox_new (FALSE, 0);
	gtk_table_attach (GTK_TABLE (table2), vbox6, 0, 1, 1, 2,
			  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
			  (GtkAttachOptions) (GTK_FILL), 0, 0);

	frame6 = gtk_frame_new (_("Visible"));
	gtk_box_pack_start (GTK_BOX (vbox6), frame6, TRUE, TRUE, 0);

	vbox7 = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (frame6), vbox7);

	priv->show = gtk_radio_button_new_with_label (visible_group, "");
	key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->show)->child),
						  _("_Show"));
	gtk_widget_add_accelerator (priv->show, "clicked", accel_group,
				    key, GDK_MOD1_MASK, (GtkAccelFlags) 0);
	gtk_signal_connect (GTK_OBJECT (priv->show), "clicked",
			    (GtkSignalFunc) show_hide_cb, config);
	visible_group = gtk_radio_button_group (GTK_RADIO_BUTTON (priv->show));
	gtk_box_pack_start (GTK_BOX (vbox7), priv->show, FALSE, FALSE, 0);

	priv->hide = gtk_radio_button_new_with_label (visible_group, "");
	key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->hide)->child),
						   _("_Hide"));
	gtk_widget_add_accelerator (priv->hide, "clicked", accel_group,
				    key, GDK_MOD1_MASK, (GtkAccelFlags) 0);
	gtk_signal_connect (GTK_OBJECT (priv->hide), "clicked",
			    (GtkSignalFunc) show_hide_cb, config);
	visible_group = gtk_radio_button_group (GTK_RADIO_BUTTON (priv->hide));
	gtk_box_pack_start (GTK_BOX (vbox7), priv->hide, FALSE, FALSE, 0);

	priv->tooltips = gtk_check_button_new_with_label ("");
	key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->tooltips)->child),
						  _("_View tooltips"));
	gtk_widget_add_accelerator (priv->tooltips, "clicked", accel_group,
				    key, GDK_MOD1_MASK, (GtkAccelFlags) 0);
	gtk_box_pack_start (GTK_BOX (vbox6), priv->tooltips, FALSE, FALSE, 0);
	gtk_signal_connect (GTK_OBJECT (priv->tooltips), "clicked",
			    (GtkSignalFunc) tooltips_cb, config);

	frame7 = gtk_frame_new (_("Toolbars"));
	gtk_table_attach (GTK_TABLE (table2), frame7, 0, 2, 0, 1,
			  (GtkAttachOptions) (GTK_FILL),
			  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0);

	priv->list = toolbar_list = gtk_list_new ();

	gtk_container_add (GTK_CONTAINER (frame7), toolbar_list);
	GTK_WIDGET_SET_FLAGS (toolbar_list, GTK_CAN_DEFAULT);

	frame5 = gtk_frame_new (_("Look"));
	gtk_table_attach (GTK_TABLE (table2), frame5, 1, 2, 1, 2,
			  (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
			  (GtkAttachOptions) (GTK_FILL), 0, 0);

	priv->right_attrs = vbox5 = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (frame5), vbox5);

	priv->icon = gtk_radio_button_new_with_label (look_group, "");
	key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->icon)->child),
						  _("_Icon"));
	gtk_widget_add_accelerator (priv->icon, "clicked", accel_group,
				    key, GDK_MOD1_MASK, (GtkAccelFlags) 0);
	gtk_signal_connect (GTK_OBJECT (priv->icon), "clicked",
			    (GtkSignalFunc) look_cb, config);
	look_group = gtk_radio_button_group (GTK_RADIO_BUTTON (priv->icon));
	gtk_box_pack_start (GTK_BOX (vbox5), priv->icon, FALSE, FALSE, 0);

	priv->icon_and_text = gtk_radio_button_new_with_label (look_group, "");
	key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->icon_and_text)->child),
						  _("_Text and Icon"));
	gtk_widget_add_accelerator (priv->icon_and_text, "clicked", accel_group,
				    key, GDK_MOD1_MASK, (GtkAccelFlags) 0);
	gtk_signal_connect (GTK_OBJECT (priv->icon_and_text), "clicked",
			    (GtkSignalFunc) look_cb, config);
	look_group = gtk_radio_button_group (GTK_RADIO_BUTTON (priv->icon_and_text));
	gtk_box_pack_start (GTK_BOX (vbox5), priv->icon_and_text, FALSE, FALSE, 0);

	priv->priority_text = gtk_radio_button_new_with_label (look_group, "");
	key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->priority_text)->child),
						  _("_Priority text only"));
	gtk_widget_add_accelerator (priv->priority_text, "clicked", accel_group,
				    key, GDK_MOD1_MASK, (GtkAccelFlags) 0);
	gtk_signal_connect (GTK_OBJECT (priv->priority_text), "clicked",
			    (GtkSignalFunc) look_cb, config);
	look_group = gtk_radio_button_group (GTK_RADIO_BUTTON (priv->priority_text));
	gtk_box_pack_start (GTK_BOX (vbox5), priv->priority_text, FALSE, FALSE, 0);

	populate_list (toolbar_list, config);
	set_values (config);

	gtk_widget_show_all (GTK_WIDGET (config));
	gtk_widget_hide (GTK_WIDGET (config));
}

static void
bonobo_ui_config_widget_init (GtkWidget *widget)
{
	BonoboUIConfigWidget *config = BONOBO_UI_CONFIG_WIDGET (widget);

	config->priv = g_new0 (BonoboUIConfigWidgetPrivate, 1);
}

static void
bonobo_ui_config_widget_finalize (GtkObject *object)
{
	BonoboUIConfigWidget *config = BONOBO_UI_CONFIG_WIDGET (object);

	g_free (config->priv);

	parent_class->finalize (object);
}

GtkWidget *
bonobo_ui_config_widget_construct (BonoboUIConfigWidget *config,
				   BonoboUIEngine       *engine,
				   GtkAccelGroup        *accel_group)
{
	config->engine = engine;
	widgets_init (config, accel_group);

	return GTK_WIDGET (config);
}

/**
 * bonobo_ui_config_widget_new:
 *
 * Creates a new BonoboUIConfigWidget widget, this contains
 * a List of toolbars and allows configuration of the widgets.
 *
 * Returns: A pointer to the newly-created BonoboUIConfigWidget widget.
 */
GtkWidget *
bonobo_ui_config_widget_new (BonoboUIEngine *engine,
			     GtkAccelGroup  *accel_group)
{
	BonoboUIConfigWidget *config = gtk_type_new (
		bonobo_ui_config_widget_get_type ());

	return bonobo_ui_config_widget_construct (
		config, engine, accel_group);
}

static void
bonobo_ui_config_widget_class_init (BonoboUIConfigWidgetClass *klass)
{
	GtkObjectClass *object_class;
	
	g_return_if_fail (klass != NULL);
	parent_class = gtk_type_class (PARENT_TYPE);
	
	object_class = (GtkObjectClass *) klass;
	
	object_class->finalize = bonobo_ui_config_widget_finalize;
}

/**
 * bonobo_ui_config_widget_get_type:
 *
 * Returns: The GtkType for the BonoboUIConfigWidget object class.
 */
GtkType
bonobo_ui_config_widget_get_type (void)
{
	static guint bonobo_ui_config_widget_type = 0;

	if (!bonobo_ui_config_widget_type) {
		GtkTypeInfo bonobo_ui_config_widget_info = {
			"BonoboUIConfigWidget",
			sizeof (BonoboUIConfigWidget),
			sizeof (BonoboUIConfigWidgetClass),
			(GtkClassInitFunc)  bonobo_ui_config_widget_class_init,
			(GtkObjectInitFunc) bonobo_ui_config_widget_init,
			(GtkArgSetFunc) NULL,
			(GtkArgGetFunc) NULL
		};

		bonobo_ui_config_widget_type = gtk_type_unique (
			PARENT_TYPE,
			&bonobo_ui_config_widget_info);
	}

	return bonobo_ui_config_widget_type;
}