File: bonobo-ui-toolbar-control-item.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 (331 lines) | stat: -rw-r--r-- 9,780 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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/**
 * bonobo-ui-toolbar-control-item.c: A special toolbar item for controls.
 *
 * Author:
 *	Jon K Hellan (hellan@acm.org)
 *
 * Copyright 2000 Jon K Hellan.
 * Copyright (C) 2001 Eazel, Inc.
 */

#include "config.h"
#include <gnome.h>
#include "bonobo-ui-toolbar-control-item.h"

#include "bonobo-exception.h"

static BonoboUIToolbarButtonItemClass *parent_class = NULL;

struct _BonoboUIToolbarControlItemPrivate {
        BonoboWidget *control;	/* The wrapped control */
	GtkWidget *button;	/* Button to display instead of control in
				   vertical mode */
	GtkWidget *box;		/* Container for control and button. Which of
				   its children is visible depends on
				   orientation */
        GtkWidget *eventbox;	/* The eventbox which makes tooltips work */

	BonoboUIToolbarControlDisplay hdisplay;
	BonoboUIToolbarControlDisplay vdisplay;
};

static void
set_control_property_bag_value (BonoboUIToolbarControlItem *item,
				const char *name,
				BonoboArg *value)
{
	BonoboControlFrame *frame;
	Bonobo_PropertyBag bag;
	Bonobo_Property property;
	CORBA_Environment ev;
	
	frame = bonobo_widget_get_control_frame (item->priv->control);
	bag = bonobo_control_frame_get_control_property_bag (frame, NULL);
	if (bag == CORBA_OBJECT_NIL)
		return;
        property = bonobo_property_bag_client_get_property (bag, name, NULL);
	if (property != CORBA_OBJECT_NIL) {
		CORBA_exception_init (&ev);
		Bonobo_Property_setValue (property, value, &ev);
		CORBA_Object_release (property, &ev);
		CORBA_exception_free (&ev);
	}
	bonobo_object_release_unref (bag, NULL);
}

#define MAKE_SET_CONTROL_PROPERTY_BAG_VALUE(gtype, paramtype, capstype)	\
static void								\
set_control_property_bag_##gtype (BonoboUIToolbarControlItem *item,	\
				  const char *name,			\
				  paramtype value)			\
{									\
	BonoboArg *arg;							\
									\
	arg = bonobo_arg_new (BONOBO_ARG_##capstype);			\
	BONOBO_ARG_SET_##capstype (arg, value);				\
	set_control_property_bag_value (item, name, arg);		\
	bonobo_arg_release (arg);					\
}

MAKE_SET_CONTROL_PROPERTY_BAG_VALUE (gboolean, gboolean,     BOOLEAN)
MAKE_SET_CONTROL_PROPERTY_BAG_VALUE (gint,     gint,         INT)
MAKE_SET_CONTROL_PROPERTY_BAG_VALUE (string,   const char *, STRING)

/* BonoboUIToolbarButtonItem virtual methods.  */
static void
impl_set_icon  (BonoboUIToolbarButtonItem *button_item,
		GdkPixbuf                 *icon)
{
	BonoboUIToolbarControlItemPrivate *priv;
	BonoboUIToolbarControlItem *control_item;

	control_item = BONOBO_UI_TOOLBAR_CONTROL_ITEM (button_item);
	priv = control_item->priv;

	bonobo_ui_toolbar_button_item_set_icon (
		BONOBO_UI_TOOLBAR_BUTTON_ITEM (priv->button), icon);
}

static void
impl_set_label (BonoboUIToolbarButtonItem *button_item,
		const char                *label)
{
	BonoboUIToolbarControlItemPrivate *priv;
	BonoboUIToolbarControlItem *control_item;

	control_item = BONOBO_UI_TOOLBAR_CONTROL_ITEM (button_item);
	priv = control_item->priv;

	bonobo_ui_toolbar_button_item_set_label (
		BONOBO_UI_TOOLBAR_BUTTON_ITEM (priv->button), label);
	set_control_property_bag_string (control_item, "bonobo:label", label);
}

/* BonoboUIToolbarItem methods.  */

/*
 * We are assuming that there's room in horizontal orientation, but not
 * vertical. This can be made more sophisticated by looking at the control's
 * requested geometry.
 */
static void
impl_set_orientation (BonoboUIToolbarItem *item,
		      GtkOrientation orientation)
{
	BonoboUIToolbarControlItem        *control_item;
	BonoboUIToolbarControlDisplay      display;
	BonoboUIToolbarControlItemPrivate *priv;

	control_item = BONOBO_UI_TOOLBAR_CONTROL_ITEM (item);
	priv = control_item->priv;

	if (orientation == GTK_ORIENTATION_HORIZONTAL)
		display = priv->hdisplay;
	else
		display = priv->vdisplay;
	
	switch (display) {

	case BONOBO_UI_TOOLBAR_CONTROL_DISPLAY_CONTROL:
		gtk_widget_hide (priv->button);
		gtk_widget_show (GTK_WIDGET (priv->control));
		break;

	case BONOBO_UI_TOOLBAR_CONTROL_DISPLAY_BUTTON:
		gtk_widget_hide (GTK_WIDGET (priv->control));
		gtk_widget_show (priv->button);
		break;

	case BONOBO_UI_TOOLBAR_CONTROL_DISPLAY_NONE:
		gtk_widget_hide (GTK_WIDGET (priv->control));
		gtk_widget_hide (priv->button);
		break;

	default:
		g_assert_not_reached ();
	}

	set_control_property_bag_gint (control_item, "bonobo:orientation", orientation);

	if (BONOBO_UI_TOOLBAR_ITEM_CLASS (parent_class)->set_orientation)
		(* BONOBO_UI_TOOLBAR_ITEM_CLASS (parent_class)
		 ->set_orientation) (item, orientation);	
}

static void
impl_set_style (BonoboUIToolbarItem     *item,
		BonoboUIToolbarItemStyle style)
{
	BonoboUIToolbarControlItem *control_item;

	control_item = BONOBO_UI_TOOLBAR_CONTROL_ITEM (item);
	bonobo_ui_toolbar_item_set_style (
		BONOBO_UI_TOOLBAR_ITEM (control_item->priv->button), style);
	set_control_property_bag_gint (control_item, "bonobo:style", style);
}

static void
impl_set_want_label (BonoboUIToolbarItem     *item,
		     gboolean                 want_label)
{
	BonoboUIToolbarControlItem *control_item;

	control_item = BONOBO_UI_TOOLBAR_CONTROL_ITEM (item);
	bonobo_ui_toolbar_item_set_want_label (
		BONOBO_UI_TOOLBAR_ITEM (control_item->priv->button),
		want_label);
	set_control_property_bag_gboolean (control_item, "bonobo:want_label", want_label);
}

static void
impl_set_tooltip (BonoboUIToolbarItem     *item,
                  GtkTooltips             *tooltips,
                  const char              *tooltip)
{
	BonoboUIToolbarControlItem *control_item;
	GtkWidget *eventbox;

	control_item = BONOBO_UI_TOOLBAR_CONTROL_ITEM (item);
	eventbox = control_item->priv->eventbox;
	
	if (tooltip && eventbox)
		gtk_tooltips_set_tip (tooltips, eventbox, tooltip, NULL);
}

/* GtkObject methods.  */

static void
impl_destroy (GtkObject *object)
{
	g_free (BONOBO_UI_TOOLBAR_CONTROL_ITEM (object)->priv);

	if (GTK_OBJECT_CLASS (parent_class)->destroy)
		 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);	
}

/* Gtk+ object initialization.  */

static void
class_init (BonoboUIToolbarControlItemClass *klass)
{
        BonoboUIToolbarButtonItemClass *button_item_class;
        BonoboUIToolbarItemClass *item_class;
	GtkObjectClass *object_class;
	
	button_item_class = BONOBO_UI_TOOLBAR_BUTTON_ITEM_CLASS (klass);
	item_class = BONOBO_UI_TOOLBAR_ITEM_CLASS (klass);
	object_class = GTK_OBJECT_CLASS (klass);

        button_item_class->set_icon = impl_set_icon;
        button_item_class->set_label = impl_set_label;
        item_class->set_tooltip     = impl_set_tooltip;
        item_class->set_orientation = impl_set_orientation;
	item_class->set_style       = impl_set_style;
	item_class->set_want_label  = impl_set_want_label;
	object_class->destroy = impl_destroy;

        parent_class = gtk_type_class (
		bonobo_ui_toolbar_button_item_get_type ());
}


static void
init (BonoboUIToolbarControlItem *control_item)
{
        control_item->priv = g_new0 (BonoboUIToolbarControlItemPrivate, 1);
}

GtkType
bonobo_ui_toolbar_control_item_get_type (void)
{
        static GtkType type = 0;

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

                type = gtk_type_unique (
			bonobo_ui_toolbar_button_item_get_type (),
			&info);
        }

        return type;
}

static void
proxy_activate_cb (GtkWidget *button, GtkObject *item)
{
	gtk_signal_emit_by_name (item, "activate");
}

GtkWidget *
bonobo_ui_toolbar_control_item_construct (
        BonoboUIToolbarControlItem *control_item,
        Bonobo_Control              control_ref)
{
        BonoboUIToolbarControlItemPrivate *priv = control_item->priv;
	GtkWidget *w  = bonobo_widget_new_control_from_objref (
                bonobo_object_dup_ref (control_ref, NULL), CORBA_OBJECT_NIL);

        if (!w)
                return NULL;

	priv->control  = BONOBO_WIDGET (w); 
	priv->button   = bonobo_ui_toolbar_button_item_new (NULL, NULL);
        priv->eventbox = gtk_event_box_new ();
        priv->box      = gtk_vbox_new (FALSE, 0);
	
	gtk_signal_connect (GTK_OBJECT (priv->button), "activate",
			    (GtkSignalFunc) proxy_activate_cb, control_item);
	
	gtk_container_add (GTK_CONTAINER (priv->box),
			   GTK_WIDGET (priv->control));
        gtk_container_add (GTK_CONTAINER (priv->box), priv->button);

        gtk_container_add (GTK_CONTAINER (priv->eventbox), priv->box);

	gtk_widget_show (GTK_WIDGET (priv->control));
	gtk_widget_show (priv->box);
	gtk_widget_show   (priv->eventbox);
        gtk_container_add (GTK_CONTAINER (control_item), priv->eventbox);

        return GTK_WIDGET (control_item);
}

GtkWidget *
bonobo_ui_toolbar_control_item_new (Bonobo_Control control_ref)
{
        BonoboUIToolbarControlItem *control_item;
	GtkWidget *ret;

        control_item = gtk_type_new (
                bonobo_ui_toolbar_control_item_get_type ());

        ret = bonobo_ui_toolbar_control_item_construct (
		control_item, control_ref);

	if (!ret)
		impl_destroy (GTK_OBJECT (control_item));

	return ret;
}

void
bonobo_ui_toolbar_control_item_set_display (BonoboUIToolbarControlItem    *item,
					    BonoboUIToolbarControlDisplay  hdisplay,
					    BonoboUIToolbarControlDisplay  vdisplay)
{
	g_return_if_fail (BONOBO_IS_UI_TOOLBAR_CONTROL_ITEM (item));

	item->priv->hdisplay = hdisplay;
	item->priv->vdisplay = vdisplay;
}