File: bonobo-ui-sync.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 (454 lines) | stat: -rw-r--r-- 11,248 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
453
454
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * bonobo-ui-sync.h: An abstract base for Bonobo XML / widget sync sync'ing.
 *
 * Author:
 *	Michael Meeks (michael@helixcode.com)
 *
 * Copyright 2000 Helix Code, Inc.
 */

#include <config.h>
#include <stdlib.h>
#include <bonobo/bonobo-ui-engine.h>
#include <bonobo/bonobo-ui-sync.h>

#define PARENT_TYPE gtk_object_get_type ()
#define CLASS(o) BONOBO_UI_SYNC_CLASS (GTK_OBJECT (o)->klass)

static void
impl_sync_state_placeholder (BonoboUISync     *sync,
			     BonoboUINode     *node,
			     BonoboUINode     *cmd_node,
			     GtkWidget        *widget,
			     GtkWidget        *parent)
{
	gboolean show = FALSE;
	char    *txt;
		
	if ((txt = bonobo_ui_engine_get_attr (
		node, cmd_node, "delimit"))) {

		show = !strcmp (txt, "top");
		bonobo_ui_node_free_string (txt);
	}
		
	if (show)
		gtk_widget_show (widget);	
	else
		gtk_widget_hide (widget);
}

static void
class_init (BonoboUISyncClass *sync_class)
{
	sync_class->sync_state_placeholder = impl_sync_state_placeholder;
}

/**
 * bonobo_ui_sync_get_type:
 * @void: 
 * 
 * Synchronizer type function for derivation.
 * 
 * Return value: the GtkType index.
 **/
GtkType
bonobo_ui_sync_get_type (void)
{
	static GtkType type = 0;

	if (type == 0) {
		static const GtkTypeInfo info = {
			"BonoboUISync",
			sizeof (BonoboUISync),
			sizeof (BonoboUISyncClass),
			(GtkClassInitFunc)  class_init,
			(GtkObjectInitFunc) NULL,
			/* reserved_1 */ NULL,
			/* reserved_2 */ NULL,
			(GtkClassInitFunc) NULL,
		};

		type = gtk_type_unique (PARENT_TYPE, &info);
	}

	return type;
}

/**
 * bonobo_ui_sync_construct:
 * @sync: the synchronizer
 * @engine: the associated engine
 * @is_recursive: whether it deals with its children recursively
 * @has_widgets: whether it has associated widgets.
 * 
 * Used to construct a new synchronizer object
 * 
 * Return value: the new object.
 **/
BonoboUISync *
bonobo_ui_sync_construct (BonoboUISync   *sync,
			  BonoboUIEngine *engine,
			  gboolean        is_recursive,
			  gboolean        has_widgets)
{
	g_return_val_if_fail (BONOBO_IS_UI_SYNC (sync), NULL);

	sync->engine = engine;
	sync->is_recursive = is_recursive;
	sync->has_widgets  = has_widgets;

	return sync;
}

/**
 * bonobo_ui_sync_is_recursive:
 * @sync: the synchronizer
 * 
 * Return value: whether this deals with its children recursively
 **/
gboolean
bonobo_ui_sync_is_recursive (BonoboUISync *sync)
{
	g_return_val_if_fail (BONOBO_IS_UI_SYNC (sync), FALSE);

	return sync->is_recursive;
}

/**
 * bonobo_ui_sync_has_widgets:
 * @sync: the synchronizer 
 * 
 * Return value: whether this deals with widgets
 **/
gboolean
bonobo_ui_sync_has_widgets (BonoboUISync *sync)
{
	g_return_val_if_fail (BONOBO_IS_UI_SYNC (sync), FALSE);

	return sync->has_widgets;
}

/**
 * bonobo_ui_sync_state:
 * @sync: the synchronizer 
 * @node: the node 
 * @cmd_node: the associated command node 
 * @widget: the widget 
 * @parent: the parent of @node
 * 
 * This method is used to synchronize the state of a @node
 * with that of a @widget, by ensuring the pertainant
 * attributes are reflected in the widget view.
 **/
void
bonobo_ui_sync_state (BonoboUISync     *sync,
		      BonoboUINode     *node,
		      BonoboUINode     *cmd_node,
		      GtkWidget        *widget,
		      GtkWidget        *parent)
{
	g_return_if_fail (BONOBO_IS_UI_SYNC (sync));

	CLASS (sync)->sync_state (sync, node, cmd_node, widget, parent);
}

/**
 * bonobo_ui_sync_state_placeholder:
 * @sync: the synchronizer 
 * @node: the node 
 * @cmd_node: the associated command node 
 * @widget: the widget 
 * @parent: the parent of @node
 * 
 * This synchronizes the state of a placeholder, there is
 * a default implementation for this method.
 **/
void
bonobo_ui_sync_state_placeholder (BonoboUISync     *sync,
				  BonoboUINode     *node,
				  BonoboUINode     *cmd_node,
				  GtkWidget        *widget,
				  GtkWidget        *parent)
{
	g_return_if_fail (BONOBO_IS_UI_SYNC (sync));

	CLASS (sync)->sync_state_placeholder (sync, node, cmd_node, widget, 
					      parent);
}

/**
 * bonobo_ui_sync_build:
 * @sync: the synchronizer 
 * @node: the node 
 * @cmd_node: the associated command node 
 * @pos: the position in the parent container to insert at
 * @parent: the parent of @node 
 * 
 * This function causes a child widget to be build that matches
 * @node's attributes. This should then be inserted by into
 * @parent's associated widget at position @pos in the container.
 * 
 * Return value: the freshly built widget.
 **/
GtkWidget *
bonobo_ui_sync_build (BonoboUISync     *sync,
		      BonoboUINode     *node,
		      BonoboUINode     *cmd_node,
		      int              *pos,
		      GtkWidget        *parent)
{
	g_return_val_if_fail (BONOBO_IS_UI_SYNC (sync), NULL);

	return CLASS (sync)->build (sync, node, cmd_node, pos, parent);
}

/**
 * bonobo_ui_sync_build_placeholder:
 * @sync: the synchronizer 
 * @node: the node 
 * @cmd_node: the associated command node 
 * @pos: position in the parent to insert the built widget
 * @parent: the parent of @node 
 * 
 * As for #bonobo_ui_sync_build but for placeholders
 * 
 * Return value: the freshly built widget.
 **/
GtkWidget *
bonobo_ui_sync_build_placeholder (BonoboUISync     *sync,
				  BonoboUINode     *node,
				  BonoboUINode     *cmd_node,
				  int              *pos,
				  GtkWidget        *parent)
{
	g_return_val_if_fail (BONOBO_IS_UI_SYNC (sync), NULL);

	return CLASS (sync)->build_placeholder (
		sync, node, cmd_node, pos, parent);
}

/**
 * bonobo_ui_sync_get_widgets:
 * @sync: the synchronizer 
 * @node: the node 
 * 
 * This method is used to obtain a sensibly ordered list
 * of child widgets of the container associated with @node.
 * Essentialy this does something like gtk_container_children
 * but preserving the visible order of the widgets in the list.
 * 
 * Return value: An ordered list of child widgets of @node
 **/
GList *
bonobo_ui_sync_get_widgets (BonoboUISync *sync,
			    BonoboUINode *node)
{
	g_return_val_if_fail (BONOBO_IS_UI_SYNC (sync), NULL);

	if (CLASS (sync)->get_widgets)
		return CLASS (sync)->get_widgets (sync, node);
	else
		return NULL;
}

/**
 * bonobo_ui_sync_state_update:
 * @sync: the synchronizer 
 * @widget: the widget 
 * @new_state: the new state
 * 
 * This is used to synchronize state with a stateful widget,
 * eg. when a "state" attribute is set, this is not reflected
 * in the normal 'state-sync' process, but occurs later with
 * a set of state_updates to avoid re-enterancy problems.
 **/
void
bonobo_ui_sync_state_update (BonoboUISync     *sync,
			     GtkWidget        *widget,
			     const char       *new_state)
{
	g_return_if_fail (BONOBO_IS_UI_SYNC (sync));

	CLASS (sync)->state_update (sync, widget, new_state);
}

/**
 * bonobo_ui_sync_remove_root:
 * @sync: the synchronizer 
 * @root: the toplevel node to be removed.
 * 
 * This is called when a 'root' or toplevel node is
 * removed that this synchronizer deals with. eg. in
 * the toolbar case, this might trigger hiding an
 * associated dock item.
 **/
void
bonobo_ui_sync_remove_root (BonoboUISync *sync,
			    BonoboUINode *root)
{
	g_return_if_fail (BONOBO_IS_UI_SYNC (sync));

	if (CLASS (sync)->remove_root)
		CLASS (sync)->remove_root (sync, root);
}

/**
 * bonobo_ui_sync_update_root:
 * @sync: the synchronizer 
 * @root: the toplevel node
 * 
 * This flags the fact that a toplevel node has changed
 * and is used primarily by non-recursive handlers, such
 * as the keybinding sync method.
 **/
void
bonobo_ui_sync_update_root (BonoboUISync *sync,
			    BonoboUINode *root)
{
	g_return_if_fail (BONOBO_IS_UI_SYNC (sync));

	if (CLASS (sync)->update_root)
		CLASS (sync)->update_root (sync, root);
}

/**
 * bonobo_ui_sync_ignore_widget:
 * @sync: the synchronizer 
 * @widget: the widget 
 * 
 * Return value: TRUE if this widget should be ignored in a container
 * this is the case for eg. menu tearoffs items, and toolbar popout items.
 **/
gboolean
bonobo_ui_sync_ignore_widget (BonoboUISync *sync,
			      GtkWidget    *widget)
{
	g_return_val_if_fail (BONOBO_IS_UI_SYNC (sync), FALSE);

	if (CLASS (sync)->ignore_widget)
		return CLASS (sync)->ignore_widget (sync, widget);
	else
		return FALSE;
}

/**
 * bonobo_ui_sync_stamp_root:
 * @sync: the synchronizer 
 * 
 * This asks the synchronizer to stamp all its associated
 * root widget containers into the XML tree.
 **/
void
bonobo_ui_sync_stamp_root (BonoboUISync *sync)
{
	g_return_if_fail (BONOBO_IS_UI_SYNC (sync));

	if (CLASS (sync)->stamp_root)
		CLASS (sync)->stamp_root (sync);
}

/**
 * bonobo_ui_sync_can_handle:
 * @sync: the synchronizer 
 * @node: the node 
 * 
 * This is used to determine which, of multiple synchronizers
 * can be used to deal with a specific node type. Each synchronizer
 * deals with different types of node.
 * 
 * Return value: TRUE if the synchronizer can deal with this node type
 **/
gboolean
bonobo_ui_sync_can_handle (BonoboUISync *sync,
			   BonoboUINode *node)
{
	if (CLASS (sync)->can_handle)
		return CLASS (sync)->can_handle (sync, node);
	else
		return FALSE;
}

/**
 * bonobo_ui_sync_get_attached:
 * @sync: the synchronizer 
 * @widget: the widget 
 * @node: the node 
 * 
 * This is used to get an 'attached' widget - some
 * widgets have associated widgets that are coupled
 * in strange ways - eg. GtkMenuItem <-> GtkMenuShell
 * It is neccessary to store the GtkContainer item of
 * these couples in the XML tree, since then we can
 * do things more genericaly and cleanly.
 * 
 * Return value: an assoicated widget or NULL if none exists.
 **/
GtkWidget *
bonobo_ui_sync_get_attached (BonoboUISync *sync,
			     GtkWidget    *widget,
			     BonoboUINode *node)
{
	/*
	 *   For some widgets such as menus, the submenu widget
	 * is attached to the actual container in a strange way
	 * this works around only having single inheritance.
	 */
	g_return_val_if_fail (BONOBO_IS_UI_SYNC (sync), NULL);

	if (CLASS (sync)->get_attached)
		return CLASS (sync)->get_attached (sync, widget, node);
	else
		return NULL;
}

/**
 * bonobo_ui_sync_do_show_hide:
 * @sync: the synchronizer 
 * @node: the node 
 * @cmd_node: the associated command node 
 * @widget: the widget 
 * 
 * This is a helper function that applies the hidden attribute
 * from either the @node or fallback to the @cmd_node to the
 * @widget.
 * 
 * Return value: TRUE if the widget's hidden / shown state changed,
 * this is needed to work around some nasty dock sizing bugs.
 **/
gboolean
bonobo_ui_sync_do_show_hide (BonoboUISync *sync,
			     BonoboUINode *node,
			     BonoboUINode *cmd_node,
			     GtkWidget    *widget)
{
	char      *txt;
	gboolean   changed;
	GtkWidget *attached;

	if (sync && 
	    (attached = bonobo_ui_sync_get_attached (
		    sync, widget, node)))

		widget = attached;

	if (!widget)
		return FALSE;

	if ((txt = bonobo_ui_engine_get_attr (node, cmd_node, "hidden"))) {
		if (atoi (txt)) {
			changed = GTK_WIDGET_VISIBLE (widget);
			gtk_widget_hide (widget);
		} else {
			changed = !GTK_WIDGET_VISIBLE (widget);
			gtk_widget_show (widget);
		}
		bonobo_ui_node_free_string (txt);
	} else {
		changed = !GTK_WIDGET_VISIBLE (widget);
		gtk_widget_show (widget);
	}

	return changed;
}