1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /** |
---|
3 | * bonobo-ui-config-widget.c: Bonobo Component UIConfig widget |
---|
4 | * |
---|
5 | * Authors: |
---|
6 | * Michael Meeks (michael@helixcode.com) |
---|
7 | * |
---|
8 | * Copyright 2001 Helix Code, Inc. |
---|
9 | */ |
---|
10 | |
---|
11 | #ifdef HAVE_CONFIG_H |
---|
12 | #include <config.h> |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <stdlib.h> |
---|
16 | #include <gtk/gtk.h> |
---|
17 | |
---|
18 | #include <libgnome/gnome-defs.h> |
---|
19 | |
---|
20 | #define GNOME_EXPLICIT_TRANSLATION_DOMAIN PACKAGE |
---|
21 | #include <libgnome/gnome-i18n.h> |
---|
22 | |
---|
23 | #include <bonobo/bonobo-ui-util.h> |
---|
24 | #include <bonobo/bonobo-ui-engine-private.h> |
---|
25 | #include <bonobo/bonobo-ui-config-widget.h> |
---|
26 | #include <bonobo/bonobo-ui-sync-toolbar.h> |
---|
27 | |
---|
28 | #define PARENT_TYPE gtk_vbox_get_type () |
---|
29 | |
---|
30 | static GtkObjectClass *parent_class; |
---|
31 | |
---|
32 | struct _BonoboUIConfigWidgetPrivate { |
---|
33 | GtkWidget *list; |
---|
34 | |
---|
35 | GtkWidget *left_attrs; |
---|
36 | GtkWidget *right_attrs; |
---|
37 | |
---|
38 | GtkWidget *show; |
---|
39 | GtkWidget *hide; |
---|
40 | |
---|
41 | GtkWidget *tooltips; |
---|
42 | |
---|
43 | GtkWidget *icon; |
---|
44 | GtkWidget *icon_and_text; |
---|
45 | GtkWidget *priority_text; |
---|
46 | |
---|
47 | const char *cur_path; |
---|
48 | }; |
---|
49 | |
---|
50 | #define WIDGET_ATTR_KEY "BonoboUIConfigWidget_Attr" |
---|
51 | |
---|
52 | static const char * |
---|
53 | widget_get_attr (GtkWidget *widget) |
---|
54 | { |
---|
55 | return gtk_object_get_data (GTK_OBJECT (widget), |
---|
56 | WIDGET_ATTR_KEY); |
---|
57 | } |
---|
58 | |
---|
59 | static void |
---|
60 | select_child_cb (GtkList *list, |
---|
61 | GtkWidget *child, |
---|
62 | BonoboUIConfigWidget *config) |
---|
63 | { |
---|
64 | BonoboUINode *node; |
---|
65 | |
---|
66 | config->priv->cur_path = widget_get_attr (child); |
---|
67 | |
---|
68 | node = bonobo_ui_engine_get_path ( |
---|
69 | config->engine, config->priv->cur_path); |
---|
70 | |
---|
71 | gtk_widget_set_sensitive (config->priv->left_attrs, node != NULL); |
---|
72 | gtk_widget_set_sensitive (config->priv->right_attrs, node != NULL); |
---|
73 | |
---|
74 | if (node) { |
---|
75 | char *txt; |
---|
76 | gboolean hidden = FALSE; |
---|
77 | gboolean tooltips = TRUE; |
---|
78 | |
---|
79 | if ((txt = bonobo_ui_node_get_attr (node, "hidden"))) { |
---|
80 | hidden = atoi (txt); |
---|
81 | bonobo_ui_node_free_string (txt); |
---|
82 | } |
---|
83 | |
---|
84 | if (hidden) |
---|
85 | gtk_toggle_button_set_active ( |
---|
86 | GTK_TOGGLE_BUTTON (config->priv->hide), |
---|
87 | TRUE); |
---|
88 | else |
---|
89 | gtk_toggle_button_set_active ( |
---|
90 | GTK_TOGGLE_BUTTON (config->priv->show), |
---|
91 | TRUE); |
---|
92 | |
---|
93 | if ((txt = bonobo_ui_node_get_attr (node, "tips"))) { |
---|
94 | tooltips = atoi (txt); |
---|
95 | bonobo_ui_node_free_string (txt); |
---|
96 | } |
---|
97 | |
---|
98 | gtk_toggle_button_set_active ( |
---|
99 | GTK_TOGGLE_BUTTON (config->priv->tooltips), |
---|
100 | tooltips); |
---|
101 | } else |
---|
102 | g_warning ("Toolbar has been removed"); |
---|
103 | } |
---|
104 | |
---|
105 | static void |
---|
106 | populate_list (GtkWidget *list, |
---|
107 | BonoboUIConfigWidget *config) |
---|
108 | { |
---|
109 | BonoboUINode *l, *start; |
---|
110 | |
---|
111 | GList *items = NULL; |
---|
112 | |
---|
113 | start = bonobo_ui_node_children ( |
---|
114 | bonobo_ui_engine_get_xml (config->engine)->root); |
---|
115 | |
---|
116 | if (!start) |
---|
117 | g_warning ("No tree"); |
---|
118 | |
---|
119 | for (l = start; l; |
---|
120 | l = bonobo_ui_node_next (l)) { |
---|
121 | |
---|
122 | if (bonobo_ui_node_has_name (l, "dockitem")) { |
---|
123 | char *txt, *name; |
---|
124 | |
---|
125 | if ((txt = bonobo_ui_node_get_attr (l, "tip"))) { |
---|
126 | gboolean err; |
---|
127 | |
---|
128 | name = bonobo_ui_util_decode_str (txt, &err); |
---|
129 | g_return_if_fail (!err); |
---|
130 | |
---|
131 | bonobo_ui_node_free_string (txt); |
---|
132 | } else if (!(name = bonobo_ui_node_get_attr (l, "name"))) |
---|
133 | name = NULL; |
---|
134 | |
---|
135 | if (name) { |
---|
136 | GtkWidget *w = gtk_list_item_new_with_label (name); |
---|
137 | char *path = bonobo_ui_xml_make_path (l); |
---|
138 | |
---|
139 | gtk_object_set_data_full (GTK_OBJECT (w), |
---|
140 | WIDGET_ATTR_KEY, |
---|
141 | path, |
---|
142 | (GtkDestroyNotify) g_free); |
---|
143 | |
---|
144 | gtk_widget_show (w); |
---|
145 | items = g_list_prepend (items, w); |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | gtk_list_append_items (GTK_LIST (list), items); |
---|
151 | gtk_signal_connect (GTK_OBJECT (list), "select_child", |
---|
152 | (GtkSignalFunc) select_child_cb, config); |
---|
153 | |
---|
154 | gtk_list_select_item (GTK_LIST (list), 0); |
---|
155 | } |
---|
156 | |
---|
157 | static void |
---|
158 | show_hide_cb (GtkWidget *button, |
---|
159 | BonoboUIConfigWidget *config) |
---|
160 | { |
---|
161 | g_return_if_fail (config->priv->cur_path != NULL); |
---|
162 | |
---|
163 | if (button == config->priv->show) |
---|
164 | bonobo_ui_engine_config_remove ( |
---|
165 | bonobo_ui_engine_get_config (config->engine), |
---|
166 | config->priv->cur_path, "hidden"); |
---|
167 | else |
---|
168 | bonobo_ui_engine_config_add ( |
---|
169 | bonobo_ui_engine_get_config (config->engine), |
---|
170 | config->priv->cur_path, "hidden", "1"); |
---|
171 | } |
---|
172 | |
---|
173 | static void |
---|
174 | tooltips_cb (GtkWidget *button, |
---|
175 | BonoboUIConfigWidget *config) |
---|
176 | { |
---|
177 | g_return_if_fail (config->priv->cur_path != NULL); |
---|
178 | |
---|
179 | if (gtk_toggle_button_get_active ( |
---|
180 | GTK_TOGGLE_BUTTON (button))) |
---|
181 | bonobo_ui_engine_config_remove ( |
---|
182 | bonobo_ui_engine_get_config (config->engine), |
---|
183 | config->priv->cur_path, "tips"); |
---|
184 | else |
---|
185 | bonobo_ui_engine_config_add ( |
---|
186 | bonobo_ui_engine_get_config (config->engine), |
---|
187 | config->priv->cur_path, "tips", "0"); |
---|
188 | } |
---|
189 | |
---|
190 | static void |
---|
191 | look_cb (GtkWidget *button, |
---|
192 | BonoboUIConfigWidget *config) |
---|
193 | { |
---|
194 | const char *value = NULL; |
---|
195 | |
---|
196 | g_return_if_fail (config->priv->cur_path != NULL); |
---|
197 | |
---|
198 | if (button == config->priv->icon) |
---|
199 | value = "icon"; |
---|
200 | |
---|
201 | else if (button == config->priv->icon_and_text) |
---|
202 | value = "both"; |
---|
203 | |
---|
204 | else if (button == config->priv->priority_text) |
---|
205 | value = "text"; |
---|
206 | |
---|
207 | else |
---|
208 | g_warning ("Unknown look selection"); |
---|
209 | |
---|
210 | bonobo_ui_engine_config_add ( |
---|
211 | bonobo_ui_engine_get_config (config->engine), |
---|
212 | config->priv->cur_path, "look", value); |
---|
213 | } |
---|
214 | |
---|
215 | static void |
---|
216 | set_values (BonoboUIConfigWidget *config) |
---|
217 | { |
---|
218 | BonoboUIToolbarStyle style; |
---|
219 | BonoboUINode *node; |
---|
220 | char *value; |
---|
221 | |
---|
222 | g_return_if_fail (config->priv->cur_path != NULL); |
---|
223 | |
---|
224 | node = bonobo_ui_engine_get_path (config->engine, config->priv->cur_path); |
---|
225 | |
---|
226 | /* Set the look */ |
---|
227 | style = bonobo_ui_sync_toolbar_get_look (config->engine, node); |
---|
228 | switch (style) { |
---|
229 | case BONOBO_UI_TOOLBAR_STYLE_ICONS_ONLY: |
---|
230 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (config->priv->icon), TRUE); |
---|
231 | break; |
---|
232 | |
---|
233 | case BONOBO_UI_TOOLBAR_STYLE_ICONS_AND_TEXT: |
---|
234 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (config->priv->icon_and_text), TRUE); |
---|
235 | break; |
---|
236 | |
---|
237 | case BONOBO_UI_TOOLBAR_STYLE_PRIORITY_TEXT: |
---|
238 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (config->priv->priority_text), TRUE); |
---|
239 | break; |
---|
240 | |
---|
241 | default: |
---|
242 | break; |
---|
243 | } |
---|
244 | |
---|
245 | /* Set the tooltips */ |
---|
246 | value = bonobo_ui_node_get_attr (node, "tips"); |
---|
247 | if (value != NULL) { |
---|
248 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (config->priv->tooltips), atoi (value)); |
---|
249 | bonobo_ui_node_free_string (value); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | static void |
---|
254 | widgets_init (BonoboUIConfigWidget *config, |
---|
255 | GtkAccelGroup *accel_group) |
---|
256 | { |
---|
257 | BonoboUIConfigWidgetPrivate *priv; |
---|
258 | GtkWidget *table2; |
---|
259 | GtkWidget *vbox6; |
---|
260 | GtkWidget *frame6; |
---|
261 | GtkWidget *vbox7; |
---|
262 | GSList *visible_group = NULL; |
---|
263 | guint key; |
---|
264 | GtkWidget *frame7; |
---|
265 | GtkWidget *toolbar_list; |
---|
266 | GtkWidget *frame5; |
---|
267 | GtkWidget *vbox5; |
---|
268 | GSList *look_group = NULL; |
---|
269 | |
---|
270 | priv = config->priv; |
---|
271 | |
---|
272 | table2 = gtk_table_new (2, 2, FALSE); |
---|
273 | gtk_box_pack_start (GTK_BOX (config), table2, TRUE, TRUE, 0); |
---|
274 | |
---|
275 | priv->left_attrs = vbox6 = gtk_vbox_new (FALSE, 0); |
---|
276 | gtk_table_attach (GTK_TABLE (table2), vbox6, 0, 1, 1, 2, |
---|
277 | (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), |
---|
278 | (GtkAttachOptions) (GTK_FILL), 0, 0); |
---|
279 | |
---|
280 | frame6 = gtk_frame_new (_("Visible")); |
---|
281 | gtk_box_pack_start (GTK_BOX (vbox6), frame6, TRUE, TRUE, 0); |
---|
282 | |
---|
283 | vbox7 = gtk_vbox_new (FALSE, 0); |
---|
284 | gtk_container_add (GTK_CONTAINER (frame6), vbox7); |
---|
285 | |
---|
286 | priv->show = gtk_radio_button_new_with_label (visible_group, ""); |
---|
287 | key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->show)->child), |
---|
288 | _("_Show")); |
---|
289 | gtk_widget_add_accelerator (priv->show, "clicked", accel_group, |
---|
290 | key, GDK_MOD1_MASK, (GtkAccelFlags) 0); |
---|
291 | gtk_signal_connect (GTK_OBJECT (priv->show), "clicked", |
---|
292 | (GtkSignalFunc) show_hide_cb, config); |
---|
293 | visible_group = gtk_radio_button_group (GTK_RADIO_BUTTON (priv->show)); |
---|
294 | gtk_box_pack_start (GTK_BOX (vbox7), priv->show, FALSE, FALSE, 0); |
---|
295 | |
---|
296 | priv->hide = gtk_radio_button_new_with_label (visible_group, ""); |
---|
297 | key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->hide)->child), |
---|
298 | _("_Hide")); |
---|
299 | gtk_widget_add_accelerator (priv->hide, "clicked", accel_group, |
---|
300 | key, GDK_MOD1_MASK, (GtkAccelFlags) 0); |
---|
301 | gtk_signal_connect (GTK_OBJECT (priv->hide), "clicked", |
---|
302 | (GtkSignalFunc) show_hide_cb, config); |
---|
303 | visible_group = gtk_radio_button_group (GTK_RADIO_BUTTON (priv->hide)); |
---|
304 | gtk_box_pack_start (GTK_BOX (vbox7), priv->hide, FALSE, FALSE, 0); |
---|
305 | |
---|
306 | priv->tooltips = gtk_check_button_new_with_label (""); |
---|
307 | key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->tooltips)->child), |
---|
308 | _("_View tooltips")); |
---|
309 | gtk_widget_add_accelerator (priv->tooltips, "clicked", accel_group, |
---|
310 | key, GDK_MOD1_MASK, (GtkAccelFlags) 0); |
---|
311 | gtk_box_pack_start (GTK_BOX (vbox6), priv->tooltips, FALSE, FALSE, 0); |
---|
312 | gtk_signal_connect (GTK_OBJECT (priv->tooltips), "clicked", |
---|
313 | (GtkSignalFunc) tooltips_cb, config); |
---|
314 | |
---|
315 | frame7 = gtk_frame_new (_("Toolbars")); |
---|
316 | gtk_table_attach (GTK_TABLE (table2), frame7, 0, 2, 0, 1, |
---|
317 | (GtkAttachOptions) (GTK_FILL), |
---|
318 | (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0); |
---|
319 | |
---|
320 | priv->list = toolbar_list = gtk_list_new (); |
---|
321 | |
---|
322 | gtk_container_add (GTK_CONTAINER (frame7), toolbar_list); |
---|
323 | GTK_WIDGET_SET_FLAGS (toolbar_list, GTK_CAN_DEFAULT); |
---|
324 | |
---|
325 | frame5 = gtk_frame_new (_("Look")); |
---|
326 | gtk_table_attach (GTK_TABLE (table2), frame5, 1, 2, 1, 2, |
---|
327 | (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), |
---|
328 | (GtkAttachOptions) (GTK_FILL), 0, 0); |
---|
329 | |
---|
330 | priv->right_attrs = vbox5 = gtk_vbox_new (FALSE, 0); |
---|
331 | gtk_container_add (GTK_CONTAINER (frame5), vbox5); |
---|
332 | |
---|
333 | priv->icon = gtk_radio_button_new_with_label (look_group, ""); |
---|
334 | key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->icon)->child), |
---|
335 | _("_Icon")); |
---|
336 | gtk_widget_add_accelerator (priv->icon, "clicked", accel_group, |
---|
337 | key, GDK_MOD1_MASK, (GtkAccelFlags) 0); |
---|
338 | gtk_signal_connect (GTK_OBJECT (priv->icon), "clicked", |
---|
339 | (GtkSignalFunc) look_cb, config); |
---|
340 | look_group = gtk_radio_button_group (GTK_RADIO_BUTTON (priv->icon)); |
---|
341 | gtk_box_pack_start (GTK_BOX (vbox5), priv->icon, FALSE, FALSE, 0); |
---|
342 | |
---|
343 | priv->icon_and_text = gtk_radio_button_new_with_label (look_group, ""); |
---|
344 | key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->icon_and_text)->child), |
---|
345 | _("_Text and Icon")); |
---|
346 | gtk_widget_add_accelerator (priv->icon_and_text, "clicked", accel_group, |
---|
347 | key, GDK_MOD1_MASK, (GtkAccelFlags) 0); |
---|
348 | gtk_signal_connect (GTK_OBJECT (priv->icon_and_text), "clicked", |
---|
349 | (GtkSignalFunc) look_cb, config); |
---|
350 | look_group = gtk_radio_button_group (GTK_RADIO_BUTTON (priv->icon_and_text)); |
---|
351 | gtk_box_pack_start (GTK_BOX (vbox5), priv->icon_and_text, FALSE, FALSE, 0); |
---|
352 | |
---|
353 | priv->priority_text = gtk_radio_button_new_with_label (look_group, ""); |
---|
354 | key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (priv->priority_text)->child), |
---|
355 | _("_Priority text only")); |
---|
356 | gtk_widget_add_accelerator (priv->priority_text, "clicked", accel_group, |
---|
357 | key, GDK_MOD1_MASK, (GtkAccelFlags) 0); |
---|
358 | gtk_signal_connect (GTK_OBJECT (priv->priority_text), "clicked", |
---|
359 | (GtkSignalFunc) look_cb, config); |
---|
360 | look_group = gtk_radio_button_group (GTK_RADIO_BUTTON (priv->priority_text)); |
---|
361 | gtk_box_pack_start (GTK_BOX (vbox5), priv->priority_text, FALSE, FALSE, 0); |
---|
362 | |
---|
363 | populate_list (toolbar_list, config); |
---|
364 | set_values (config); |
---|
365 | |
---|
366 | gtk_widget_show_all (GTK_WIDGET (config)); |
---|
367 | gtk_widget_hide (GTK_WIDGET (config)); |
---|
368 | } |
---|
369 | |
---|
370 | static void |
---|
371 | bonobo_ui_config_widget_init (GtkWidget *widget) |
---|
372 | { |
---|
373 | BonoboUIConfigWidget *config = BONOBO_UI_CONFIG_WIDGET (widget); |
---|
374 | |
---|
375 | config->priv = g_new0 (BonoboUIConfigWidgetPrivate, 1); |
---|
376 | } |
---|
377 | |
---|
378 | static void |
---|
379 | bonobo_ui_config_widget_finalize (GtkObject *object) |
---|
380 | { |
---|
381 | BonoboUIConfigWidget *config = BONOBO_UI_CONFIG_WIDGET (object); |
---|
382 | |
---|
383 | g_free (config->priv); |
---|
384 | |
---|
385 | parent_class->finalize (object); |
---|
386 | } |
---|
387 | |
---|
388 | GtkWidget * |
---|
389 | bonobo_ui_config_widget_construct (BonoboUIConfigWidget *config, |
---|
390 | BonoboUIEngine *engine, |
---|
391 | GtkAccelGroup *accel_group) |
---|
392 | { |
---|
393 | config->engine = engine; |
---|
394 | widgets_init (config, accel_group); |
---|
395 | |
---|
396 | return GTK_WIDGET (config); |
---|
397 | } |
---|
398 | |
---|
399 | /** |
---|
400 | * bonobo_ui_config_widget_new: |
---|
401 | * |
---|
402 | * Creates a new BonoboUIConfigWidget widget, this contains |
---|
403 | * a List of toolbars and allows configuration of the widgets. |
---|
404 | * |
---|
405 | * Returns: A pointer to the newly-created BonoboUIConfigWidget widget. |
---|
406 | */ |
---|
407 | GtkWidget * |
---|
408 | bonobo_ui_config_widget_new (BonoboUIEngine *engine, |
---|
409 | GtkAccelGroup *accel_group) |
---|
410 | { |
---|
411 | BonoboUIConfigWidget *config = gtk_type_new ( |
---|
412 | bonobo_ui_config_widget_get_type ()); |
---|
413 | |
---|
414 | return bonobo_ui_config_widget_construct ( |
---|
415 | config, engine, accel_group); |
---|
416 | } |
---|
417 | |
---|
418 | static void |
---|
419 | bonobo_ui_config_widget_class_init (BonoboUIConfigWidgetClass *klass) |
---|
420 | { |
---|
421 | GtkObjectClass *object_class; |
---|
422 | |
---|
423 | g_return_if_fail (klass != NULL); |
---|
424 | parent_class = gtk_type_class (PARENT_TYPE); |
---|
425 | |
---|
426 | object_class = (GtkObjectClass *) klass; |
---|
427 | |
---|
428 | object_class->finalize = bonobo_ui_config_widget_finalize; |
---|
429 | } |
---|
430 | |
---|
431 | /** |
---|
432 | * bonobo_ui_config_widget_get_type: |
---|
433 | * |
---|
434 | * Returns: The GtkType for the BonoboUIConfigWidget object class. |
---|
435 | */ |
---|
436 | GtkType |
---|
437 | bonobo_ui_config_widget_get_type (void) |
---|
438 | { |
---|
439 | static guint bonobo_ui_config_widget_type = 0; |
---|
440 | |
---|
441 | if (!bonobo_ui_config_widget_type) { |
---|
442 | GtkTypeInfo bonobo_ui_config_widget_info = { |
---|
443 | "BonoboUIConfigWidget", |
---|
444 | sizeof (BonoboUIConfigWidget), |
---|
445 | sizeof (BonoboUIConfigWidgetClass), |
---|
446 | (GtkClassInitFunc) bonobo_ui_config_widget_class_init, |
---|
447 | (GtkObjectInitFunc) bonobo_ui_config_widget_init, |
---|
448 | (GtkArgSetFunc) NULL, |
---|
449 | (GtkArgGetFunc) NULL |
---|
450 | }; |
---|
451 | |
---|
452 | bonobo_ui_config_widget_type = gtk_type_unique ( |
---|
453 | PARENT_TYPE, |
---|
454 | &bonobo_ui_config_widget_info); |
---|
455 | } |
---|
456 | |
---|
457 | return bonobo_ui_config_widget_type; |
---|
458 | } |
---|