Filter:   InfoImg
download bonobo-widget.c
Language: C
Copyright: Copyright 1999, 2001 Ximian, Inc.
LOC: 302
Project Info
libbonoboui
Server: Gnome
Type: cvs
...onoboui\libbonoboui\bonobo\
   .cvsignore
   bonobo-a11y.c
   bonobo-a11y.h
   bonobo-canvas-component.c
   bonobo-canvas-component.h
   bonobo-canvas-item.c
   bonobo-canvas-item.h
   bonobo-control-frame.c
   bonobo-control-frame.h
   bonobo-control-internal.h
   bonobo-control.c
   bonobo-control.h
   bonobo-dock-band.c
   bonobo-dock-band.h
   bonobo-dock-item-grip.c
   bonobo-dock-item-grip.h
   bonobo-dock-item.c
   bonobo-dock-item.h
   bonobo-dock-layout.c
   bonobo-dock-layout.h
   bonobo-dock.c
   bonobo-dock.h
   ...bo-embeddable-factory.h
   ...bo-file-selector-util.c
   ...bo-file-selector-util.h
   ...bo-insert-component.xpm
   bonobo-plug.c
   bonobo-plug.h
   bonobo-property-control.c
   bonobo-property-control.h
   bonobo-selector-widget.c
   bonobo-selector-widget.h
   bonobo-selector.c
   bonobo-selector.h
   bonobo-socket.c
   bonobo-socket.h
   bonobo-ui-component.c
   bonobo-ui-component.h
   bonobo-ui-config-widget.c
   bonobo-ui-config-widget.h
   bonobo-ui-container.c
   bonobo-ui-container.h
   bonobo-ui-engine-config.c
   bonobo-ui-engine-config.h
   bonobo-ui-engine-private.h
   bonobo-ui-engine.c
   bonobo-ui-engine.h
   bonobo-ui-init-gtk.c
   ...o-ui-internal-toolbar.c
   bonobo-ui-main.c
   bonobo-ui-main.h
   bonobo-ui-marshal-main.c
   bonobo-ui-marshal.list
   bonobo-ui-node-private.h
   bonobo-ui-node.c
   bonobo-ui-node.h
   bonobo-ui-preferences.c
   bonobo-ui-preferences.h
   bonobo-ui-private.h
   bonobo-ui-sync-keys.c
   bonobo-ui-sync-keys.h
   bonobo-ui-sync-menu.c
   bonobo-ui-sync-menu.h
   bonobo-ui-sync-status.c
   bonobo-ui-sync-status.h
   bonobo-ui-sync-toolbar.c
   bonobo-ui-sync-toolbar.h
   bonobo-ui-sync.c
   bonobo-ui-sync.h
   ...i-toolbar-button-item.c
   ...i-toolbar-button-item.h
   ...-toolbar-control-item.c
   ...-toolbar-control-item.h
   bonobo-ui-toolbar-item.c
   bonobo-ui-toolbar-item.h
   ...ui-toolbar-popup-item.c
   ...ui-toolbar-popup-item.h
   ...ar-toggle-button-item.c
   ...ar-toggle-button-item.h
   bonobo-ui-toolbar.c
   bonobo-ui-toolbar.h
   bonobo-ui-util.c
   bonobo-ui-util.h
   bonobo-ui-win32.c
   bonobo-ui-xml.c
   bonobo-ui-xml.h
   bonobo-widget.c
   bonobo-widget.h
   bonobo-window.c
   bonobo-window.h
   bonobo-wrapper.h
   bonobo-zoomable-frame.c
   bonobo-zoomable-frame.h
   bonobo-zoomable.c
   bonobo-zoomable.h
   ...i-2.0-uninstalled.pc.in
   libbonoboui-2.0.pc.in
   Makefile.am

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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/**
 * bonobo-widget.c: BonoboWidget object.
 *
 * Authors:
 *   Nat Friedman    (nat@ximian.com)
 *
 * Copyright 1999, 2001 Ximian, Inc.
 * 
 * Bonobo component embedding for hydrocephalic imbeciles.
 *
 * Pure cane sugar.
 *
 * This purpose of BonoboWidget is to make container-side use of
 * Bonobo as easy as pie.  This widget has one function:
 *
 *      Provide a simple wrapper for embedding Controls.  Embedding
 *      controls is already really easy, but BonoboWidget reduces
 *      the work from about 5 lines to 1.  To embed a given control,
 *      just do:
 *
 *        bw = bonobo_widget_new_control ("moniker for control");
 *        gtk_container_add (some_container, bw);
 *
 *      Ta da!
 *
 *      NB. A simple moniker might look like 'file:/tmp/a.jpeg' or
 *      OAFIID:GNOME_Evolution_Calendar_Control
 */

#include <config.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkmarshal.h>
#include <bonobo/Bonobo.h>
#include <bonobo/bonobo-main.h>
#include <bonobo/bonobo-object.h>
#include <bonobo/bonobo-widget.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-moniker-util.h>
#include <libgnome/gnome-macros.h>

GNOME_CLASS_BOILERPLATE (BonoboWidget, bonobo_widget,
			 GObject, GTK_TYPE_BIN)

struct _BonoboWidgetPrivate {
	/* Control stuff. */
	BonoboControlFrame *frame;
};

static Bonobo_Unknown
bonobo_widget_launch_component (const char        *moniker,
				const char        *if_name,
				CORBA_Environment *ev)
{
	Bonobo_Unknown component;

	component = bonobo_get_object (moniker, if_name, ev);

	if (BONOBO_EX (ev))
		component = CORBA_OBJECT_NIL;

	if (component == CORBA_OBJECT_NIL)
		return NULL;

	return component;
}


/*
 *
 * Control support for BonoboWidget.
 *
 */
/**
 * bonobo_widget_construct_control_from_objref:
 * @bw: A BonoboWidget to construct
 * @control: A CORBA Object reference to an IDL:Bonobo/Control:1.0
 * @uic: Bonobo_UIContainer for the launched object or CORBA_OBJECT_NIL
 * if there is no need of menu / toolbar merging.
 * @ev: a CORBA exception environment
 *
 * This is a constructor function.  Only usable for wrapping and
 * derivation of new objects.  For normal use, please refer to
 * #bonobo_widget_new_control_from_objref.
 *
 * Returns: A #BonoboWidget (the @bw)
 */
BonoboWidget *
bonobo_widget_construct_control_from_objref (BonoboWidget      *bw,
					     Bonobo_Control     control,
					     Bonobo_UIContainer uic,
					     CORBA_Environment *ev)
{
	GtkWidget *frame_widget;

	/* Create a local ControlFrame for it. */
	bw->priv->frame = bonobo_control_frame_new (uic);

	bonobo_control_frame_bind_to_control (
		bw->priv->frame, control, ev);

	/* Grab the actual widget which visually contains the remote
	 * Control.  This is a GtkSocket, in reality. */
	frame_widget = bonobo_control_frame_get_widget (bw->priv->frame);

	/* Now stick it into this BonoboWidget. */
	gtk_container_add (GTK_CONTAINER (bw), frame_widget);
	gtk_widget_show (frame_widget);

	return bw;
}

/**
 * bonobo_widget_construct_control:
 * @bw: A BonoboWidget to construct
 * @moniker: A Moniker describing the object to be activated 
 * @uic: Bonobo_UIContainer for the launched object or CORBA_OBJECT_NIL
 * if there is no need of menu / toolbar merging.
 * @ev: a CORBA exception environment
 *
 * This is a constructor function.  Only usable for wrapping and
 * derivation of new objects.  For normal use, please refer to
 * #bonobo_widget_new_control.
 *
 * This function will unref the passed in @bw in case it cannot launch
 * the component and return %NULL in such a case.  Otherwise it returns
 * the @bw itself.
 *
 * Returns: A #BonoboWidget or %NULL
 */
BonoboWidget *
bonobo_widget_construct_control (BonoboWidget      *bw,
				 const char        *moniker,
				 Bonobo_UIContainer uic,
				 CORBA_Environment *ev)
{
	BonoboWidget  *widget;
	Bonobo_Control control;

	/* Create the remote Control object. */
	control = bonobo_widget_launch_component (
		moniker, "IDL:Bonobo/Control:1.0", ev);
	if (BONOBO_EX (ev) || control == CORBA_OBJECT_NIL) {
		/* Kill it (it is a floating object) */
		g_object_ref_sink (bw);
		return NULL;
	}

	widget = bonobo_widget_construct_control_from_objref (
		bw, control, uic, ev);

	bonobo_object_release_unref (control, ev);

	return widget;
}

typedef struct {
	BonoboWidget       *bw;
	BonoboWidgetAsyncFn fn;
	gpointer            user_data;
	Bonobo_UIContainer  uic;
} async_closure_t;

static void
control_new_async_cb (Bonobo_Unknown     object,
		      CORBA_Environment *ev,
		      gpointer           user_data)
{
	async_closure_t *c = user_data;

	if (BONOBO_EX (ev) || object == CORBA_OBJECT_NIL)
		c->fn (NULL, ev, c->user_data);
	else {
		bonobo_widget_construct_control_from_objref (
			c->bw, object, c->uic, ev);
		c->fn (c->bw, ev, c->user_data);
	}

	g_object_unref (c->bw);
	bonobo_object_release_unref (c->uic, ev);
	bonobo_object_release_unref (object, ev);
	g_free (c);
}

/**
 * bonobo_widget_new_control_async:
 * @moniker: A Moniker describing the object to be activated 
 * @uic: Bonobo_UIContainer for the launched object or CORBA_OBJECT_NIL
 * if there is no need of menu / toolbar merging.
 * @fn: a callback function called when the activation has happend
 * @user_data: user data to be passed back to the callback.
 * 
 * This method creates a widget, returns it to the user, and
 * asynchronously activates a control to insert into the widget.
 * 
 * Return value: a (temporarily) empty Widget to be filled with the
 * control later
 **/
GtkWidget *
bonobo_widget_new_control_async (const char         *moniker,
				 Bonobo_UIContainer  uic,
				 BonoboWidgetAsyncFn fn,
				 gpointer            user_data)
{
	BonoboWidget     *bw;
	async_closure_t  *c = g_new0 (async_closure_t, 1);
	CORBA_Environment ev;

	g_return_val_if_fail (fn != NULL, NULL);
	g_return_val_if_fail (moniker != NULL, NULL);

	bw = g_object_new (BONOBO_TYPE_WIDGET, NULL);

	CORBA_exception_init (&ev);

	c->bw = g_object_ref (bw);
	c->fn = fn;
	c->user_data = user_data;
	c->uic = bonobo_object_dup_ref (uic, &ev);

	bonobo_get_object_async (
		moniker, "IDL:Bonobo/Control:1.0", &ev,
		control_new_async_cb, c);

	if (BONOBO_EX (&ev)) {
		control_new_async_cb (CORBA_OBJECT_NIL, &ev, c);
		gtk_widget_destroy (GTK_WIDGET (bw));
		bw = NULL;
	}

	CORBA_exception_free (&ev);

	return (GtkWidget *) bw;
}

/**
 * bonobo_widget_new_control_from_objref:
 * @control: A CORBA Object reference to an IDL:Bonobo/Control:1.0
 * @uic: Bonobo_UIContainer for the launched object or CORBA_OBJECT_NIL
 * if there is no need of menu / toolbar merging.
 *
 * This function is a simple wrapper for easily embedding controls
 * into applications.  This function is used when you have already
 * a CORBA object reference to an IDL:Bonobo/Control:1.0 (the
 * @control) argument. 
 *
 * Returns: the @control wrapped as a #GtkWidget.
 */
GtkWidget *
bonobo_widget_new_control_from_objref (Bonobo_Control     control,
				       Bonobo_UIContainer uic)
{
	BonoboWidget     *bw;
	CORBA_Environment ev;

	g_return_val_if_fail (control != CORBA_OBJECT_NIL, NULL);

	CORBA_exception_init (&ev);

	bw = g_object_new (BONOBO_TYPE_WIDGET, NULL);

	bw = bonobo_widget_construct_control_from_objref (bw, control, uic, &ev);

	if (BONOBO_EX (&ev))
		bw = NULL;

	CORBA_exception_free (&ev);

	return (GtkWidget *) bw;
}

/**
 * bonobo_widget_new_control:
 * @moniker: A Moniker describing the object to be activated 
 * @uic: Bonobo_UIContainer for the launched object or CORBA_OBJECT_NIL
 * if there is no need of menu / toolbar merging.
 *
 * This function is a simple wrapper for easily embedding controls
 * into applications.  It will launch the component identified by @id
 * and will return it as a GtkWidget.
 *
 * Returns: A #GtkWidget that is bound to the Bonobo Control. 
 */
GtkWidget *
bonobo_widget_new_control (const char        *moniker,
			   Bonobo_UIContainer uic)
{
	BonoboWidget *bw;
	CORBA_Environment ev;

	g_return_val_if_fail (moniker != NULL, NULL);

	CORBA_exception_init (&ev);

	bw = g_object_new (BONOBO_TYPE_WIDGET, NULL);

	bw = bonobo_widget_construct_control (bw, moniker, uic, &ev);

	if (BONOBO_EX (&ev)) {
		char *txt;
		g_warning ("Activation exception '%s'",
			   (txt = bonobo_exception_get_text (&ev)));
		g_free (txt);
		bw = NULL;
	}

	CORBA_exception_free (&ev);

	return (GtkWidget *) bw;
}

/**
 * bonobo_widget_get_control_frame:
 * @bonobo_widget: a Bonobo Widget returned by one of the bonobo_widget_new() functions.
 *
 * Every IDL:Bonobo/Control:1.0 needs to be placed inside an
 * IDL:Bonobo/ControlFrame:1.0.  This returns the BonoboControlFrame
 * object that wraps the Control in the @bonobo_widget. 
 * 
 * Returns: The BonoboControlFrame associated with the @bonobo_widget
 */
BonoboControlFrame *
bonobo_widget_get_control_frame (BonoboWidget *bonobo_widget)
{
	g_return_val_if_fail (BONOBO_IS_WIDGET (bonobo_widget), NULL);

	return bonobo_widget->priv->frame;
}

/**
 * bonobo_widget_get_ui_container:
 * @bonobo_widget: the #BonoboWidget to query.
 *
 * Returns: the CORBA object reference to the Bonobo_UIContainer
 * associated with the @bonobo_widget.
 */
Bonobo_UIContainer
bonobo_widget_get_ui_container (BonoboWidget *bonobo_widget)
{
	g_return_val_if_fail (BONOBO_IS_WIDGET (bonobo_widget), NULL);

	if (!bonobo_widget->priv->frame)
		return CORBA_OBJECT_NIL;

	return bonobo_control_frame_get_ui_container (
		bonobo_widget->priv->frame);
}

Bonobo_Unknown
bonobo_widget_get_objref (BonoboWidget *bonobo_widget)
{
	g_return_val_if_fail (BONOBO_IS_WIDGET (bonobo_widget), NULL);

	if (!bonobo_widget->priv->frame)
		return CORBA_OBJECT_NIL;
	else
		return bonobo_control_frame_get_control (bonobo_widget->priv->frame);
}

static void
bonobo_widget_dispose (GObject *object)
{
	BonoboWidget *bw = BONOBO_WIDGET (object);
	BonoboWidgetPrivate *priv = bw->priv;
	
	if (priv->frame) {
		bonobo_object_unref (BONOBO_OBJECT (priv->frame));
		priv->frame = NULL;
	}

	parent_class->dispose (object);
}

static void
bonobo_widget_finalize (GObject *object)
{
	BonoboWidget *bw = BONOBO_WIDGET (object);
	
	g_free (bw->priv);

	parent_class->finalize (object);
}

static void
bonobo_widget_size_request (GtkWidget *widget,
			    GtkRequisition *requisition)
{
	GtkBin *bin;

	g_return_if_fail (widget != NULL);
	g_return_if_fail (BONOBO_IS_WIDGET (widget));
	g_return_if_fail (requisition != NULL);

	bin = GTK_BIN (widget);

	if (bin->child && GTK_WIDGET_VISIBLE (bin->child)) {
		GtkRequisition child_requisition;
      
		gtk_widget_size_request (bin->child, &child_requisition);

		requisition->width = child_requisition.width;
		requisition->height = child_requisition.height;
	}
}

static void
bonobo_widget_size_allocate (GtkWidget *widget,
			     GtkAllocation *allocation)
{
	GtkBin *bin;
	GtkAllocation child_allocation;

	g_return_if_fail (widget != NULL);
	g_return_if_fail (BONOBO_IS_WIDGET (widget));
	g_return_if_fail (allocation != NULL);

	widget->allocation = *allocation;
	bin = GTK_BIN (widget);

	child_allocation.x = allocation->x;
	child_allocation.y = allocation->y;
	child_allocation.width = allocation->width;
	child_allocation.height = allocation->height;

	if (bin->child)
		gtk_widget_size_allocate (bin->child, &child_allocation);
}

static void
bonobo_widget_remove (GtkContainer *container,
		      GtkWidget    *widget)
{
	BonoboWidget *bw = (BonoboWidget *) container;

	bw->priv->frame = NULL;

	GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
}

static void
bonobo_widget_class_init (BonoboWidgetClass *klass)
{
	GObjectClass *object_class = (GObjectClass *) klass;
	GtkWidgetClass *widget_class = (GtkWidgetClass *) klass;
	GtkContainerClass *container_class = (GtkContainerClass *) klass;

	container_class->remove = bonobo_widget_remove;

	widget_class->size_request = bonobo_widget_size_request;
	widget_class->size_allocate = bonobo_widget_size_allocate;

	object_class->finalize = bonobo_widget_finalize;
	object_class->dispose  = bonobo_widget_dispose;
}

static void
bonobo_widget_instance_init (BonoboWidget *bw)
{
	bw->priv = g_new0 (BonoboWidgetPrivate, 1);
}

/**
 * bonobo_widget_set_property:
 * @control: A #BonoboWidget that represents an IDL:Bonobo/Control:1.0
 * @first_prop: first property name to set.
 *
 * This is a utility function used to set a number of properties
 * in the Bonobo Control in @control.
 *
 * This function takes a variable list of arguments that must be NULL
 * terminated.  Arguments come in tuples: a string (for the argument
 * name) and the data type that is to be transfered.  The
 * implementation of the actual setting of the PropertyBag values is
 * done by the bonobo_property_bag_client_setv() function).
 *
 * This only works for BonoboWidgets that represent controls (ie,
 * that were returned by bonobo_widget_new_control_from_objref() or
 * bonobo_widget_new_control().
 */
void
bonobo_widget_set_property (BonoboWidget      *control,
			    const char        *first_prop, ...)
{
	Bonobo_PropertyBag pb;
	CORBA_Environment  ev;

	va_list args;
	va_start (args, first_prop);

	g_return_if_fail (control != NULL);
	g_return_if_fail (first_prop != NULL);
	g_return_if_fail (control->priv != NULL);
	g_return_if_fail (BONOBO_IS_WIDGET (control));

	CORBA_exception_init (&ev);
	
	pb = bonobo_control_frame_get_control_property_bag (
		control->priv->frame, &ev);

	if (BONOBO_EX (&ev))
		g_warning ("Error getting property bag from control");
	else {
		/* FIXME: this should use ev */
		char *err = bonobo_property_bag_client_setv (pb, &ev, first_prop, args);

		if (err)
			g_warning ("Error '%s'", err);
	}

	bonobo_object_release_unref (pb, &ev);

	CORBA_exception_free (&ev);

	va_end (args);
}


/**
 * bonobo_widget_get_property:
 * @control: A #BonoboWidget that represents an IDL:Bonobo/Control:1.0
 * @first_prop: first property name to set.
 *
 * This is a utility function used to get a number of properties
 * in the Bonobo Control in @control.
 *
 * This function takes a variable list of arguments that must be NULL
 * terminated.  Arguments come in tuples: a string (for the argument
 * name) and a pointer where the data will be stored.  The
 * implementation of the actual setting of the PropertyBag values is
 * done by the bonobo_property_bag_client_setv() function).
 *
 * This only works for BonoboWidgets that represent controls (ie,
 * that were returned by bonobo_widget_new_control_from_objref() or
 * bonobo_widget_new_control().
 */
void
bonobo_widget_get_property (BonoboWidget      *control,
			    const char        *first_prop, ...)
{
	Bonobo_PropertyBag pb;
	CORBA_Environment  ev;

	va_list args;
	va_start (args, first_prop);

	g_return_if_fail (control != NULL);
	g_return_if_fail (first_prop != NULL);
	g_return_if_fail (control->priv != NULL);
	g_return_if_fail (BONOBO_IS_WIDGET (control));

	CORBA_exception_init (&ev);
	
	pb = bonobo_control_frame_get_control_property_bag (
		control->priv->frame, &ev);

	if (BONOBO_EX (&ev))
		g_warning ("Error getting property bag from control");
	else {
		/* FIXME: this should use ev */
		char *err = bonobo_property_bag_client_getv (pb, &ev, first_prop, args);

		if (err)
			g_warning ("Error '%s'", err);
	}

	bonobo_object_release_unref (pb, &ev);

	CORBA_exception_free (&ev);

	va_end (args);
}