File: bonobo-ui-toolbar-toggle-button-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 (195 lines) | stat: -rw-r--r-- 4,802 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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/**
 * bonobo-ui-toolbar-toggle-button-item.h
 *
 * Author:
 *     Ettore Perazzoli
 *
 * Copyright (C) 2000 Helix Code, Inc.
 */

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

#include <gnome.h>

#include "bonobo-ui-toolbar-toggle-button-item.h"


#define PARENT_TYPE bonobo_ui_toolbar_button_item_get_type ()
static BonoboUIToolbarButtonItemClass *parent_class = NULL;

enum {
	TOGGLED,
	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };


/* GtkToggleButton callback.  */

static void
button_widget_toggled_cb (GtkToggleButton *toggle_button,
			  void *data)
{
	BonoboUIToolbarToggleButtonItem *button_item;

	button_item = BONOBO_UI_TOOLBAR_TOGGLE_BUTTON_ITEM (data);

	gtk_signal_emit (GTK_OBJECT (button_item), signals[TOGGLED]);
}

static void
impl_set_state (BonoboUIToolbarItem *item,
		const char          *state)
{
	GtkButton *button;
	gboolean   active = atoi (state);

	button = bonobo_ui_toolbar_button_item_get_button_widget (
		BONOBO_UI_TOOLBAR_BUTTON_ITEM (item));

	if (GTK_WIDGET_STATE (GTK_WIDGET (button)) != active)
		gtk_toggle_button_set_active (
			GTK_TOGGLE_BUTTON (button), active);
}		


/* Gtk+ object initialization.  */

static void
class_init (BonoboUIToolbarToggleButtonItemClass *klass)
{
	GtkObjectClass *object_class;
	BonoboUIToolbarItemClass *item_class = (BonoboUIToolbarItemClass *) klass;

	object_class = GTK_OBJECT_CLASS (klass);

	parent_class = gtk_type_class (bonobo_ui_toolbar_button_item_get_type ());

	item_class->set_state = impl_set_state;

	signals[TOGGLED] = gtk_signal_new ("toggled",
					   GTK_RUN_FIRST,
					   object_class->type,
					   GTK_SIGNAL_OFFSET (BonoboUIToolbarToggleButtonItemClass, toggled),
					   gtk_marshal_NONE__NONE,
					   GTK_TYPE_NONE, 0);

	gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);
}


static void
init (BonoboUIToolbarToggleButtonItem *toolbar_toggle_button_item)
{
	/* Nothing to do here.  */
}

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

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

		type = gtk_type_unique (PARENT_TYPE, &info);
	}

	return type;
}

static void
proxy_toggle_click_cb (GtkWidget *button, GtkObject *item)
{
	gboolean active;
	char    *new_state;

	active = gtk_toggle_button_get_active (
		GTK_TOGGLE_BUTTON (button));

	new_state = g_strdup_printf ("%d", active);

	gtk_signal_emit_by_name (
		item, "state_altered", new_state);

	g_free (new_state);
}

void
bonobo_ui_toolbar_toggle_button_item_construct (BonoboUIToolbarToggleButtonItem *toggle_button_item,
					     GdkPixbuf *icon,
					     const char *label)
{
	GtkWidget *button_widget;

	button_widget = gtk_toggle_button_new ();

	gtk_signal_connect_while_alive (
		GTK_OBJECT (button_widget), "toggled",
		GTK_SIGNAL_FUNC (button_widget_toggled_cb), toggle_button_item,
		GTK_OBJECT (toggle_button_item));

	gtk_signal_connect_while_alive (
		GTK_OBJECT (button_widget), "clicked",
		GTK_SIGNAL_FUNC (proxy_toggle_click_cb), toggle_button_item,
		GTK_OBJECT (toggle_button_item));

	bonobo_ui_toolbar_button_item_construct (
		BONOBO_UI_TOOLBAR_BUTTON_ITEM (toggle_button_item),
		GTK_BUTTON (button_widget), icon, label);
}

GtkWidget *
bonobo_ui_toolbar_toggle_button_item_new (GdkPixbuf *icon,
				       const char *label)
{
	BonoboUIToolbarToggleButtonItem *toggle_button_item;

	toggle_button_item = gtk_type_new (bonobo_ui_toolbar_toggle_button_item_get_type ());

	bonobo_ui_toolbar_toggle_button_item_construct (toggle_button_item, icon, label);

	return GTK_WIDGET (toggle_button_item);
}


void
bonobo_ui_toolbar_toggle_button_item_set_active (BonoboUIToolbarToggleButtonItem *item,
					      gboolean active)
{
	GtkButton *button_widget;

	g_return_if_fail (item != NULL);
	g_return_if_fail (BONOBO_IS_UI_TOOLBAR_TOGGLE_BUTTON_ITEM (item));

	button_widget = bonobo_ui_toolbar_button_item_get_button_widget (BONOBO_UI_TOOLBAR_BUTTON_ITEM (item));

	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_widget), active);
}

gboolean
bonobo_ui_toolbar_toggle_button_item_get_active (BonoboUIToolbarToggleButtonItem *item)
{
	GtkButton *button_widget;

	g_return_val_if_fail (item != NULL, FALSE);
	g_return_val_if_fail (BONOBO_IS_UI_TOOLBAR_TOGGLE_BUTTON_ITEM (item), FALSE);

	button_widget = bonobo_ui_toolbar_button_item_get_button_widget (BONOBO_UI_TOOLBAR_BUTTON_ITEM (item));

	return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button_widget));
}