10297
|
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
|
|
2 /*
|
|
3 * @file gtkgaim-disclosure.c GTK+ Gaim Disclosure
|
|
4 * @ingroup gtkui
|
|
5 *
|
|
6 * gaim
|
|
7 *
|
|
8 * Gaim is the legal property of its developers, whose names are too numerous
|
|
9 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
10 * source distribution.
|
|
11 *
|
|
12 * This program is free software; you can redistribute it and/or modify
|
|
13 * it under the terms of the GNU General Public License as published by
|
|
14 * the Free Software Foundation; either version 2 of the License, or
|
|
15 * (at your option) any later version.
|
|
16 *
|
|
17 * This program is distributed in the hope that it will be useful,
|
|
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 * GNU General Public License for more details.
|
|
21 *
|
|
22 * You should have received a copy of the GNU General Public License
|
|
23 * along with this program; if not, write to the Free Software
|
|
24 * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
|
|
25 *
|
|
26 */
|
|
27
|
|
28 #ifdef HAVE_CONFIG_H
|
|
29 #include <config.h>
|
|
30 #endif
|
|
31
|
|
32 #include <gtk/gtktogglebutton.h>
|
|
33
|
|
34 #include "gtkgaim-disclosure.h"
|
|
35
|
|
36 #ifdef ENABLE_NLS
|
|
37 # include <libintl.h>
|
|
38 # define _(x) gettext(x)
|
|
39 # ifdef gettext_noop
|
|
40 # define N_(String) gettext_noop (String)
|
|
41 # else
|
|
42 # define N_(String) (String)
|
|
43 # endif
|
|
44 #else
|
|
45 # define N_(String) (String)
|
|
46 # define _(x) (x)
|
|
47 #endif
|
|
48
|
|
49 static GtkCheckButtonClass *parent_class = NULL;
|
|
50
|
|
51 struct _GaimDisclosurePrivate {
|
|
52 GtkWidget *container;
|
|
53 char *shown;
|
|
54 char *hidden;
|
|
55
|
|
56 guint32 expand_id;
|
|
57 GtkExpanderStyle style;
|
|
58
|
|
59 int expander_size;
|
|
60 int direction;
|
|
61 };
|
|
62
|
|
63 static void
|
|
64 finalize (GObject *object)
|
|
65 {
|
|
66 GaimDisclosure *disclosure;
|
|
67
|
|
68 disclosure = GAIM_DISCLOSURE (object);
|
|
69 if (disclosure->priv == NULL) {
|
|
70 return;
|
|
71 }
|
|
72
|
|
73 g_free (disclosure->priv->hidden);
|
|
74 g_free (disclosure->priv->shown);
|
|
75
|
|
76 if (disclosure->priv->container != NULL) {
|
|
77 g_object_unref (G_OBJECT (disclosure->priv->container));
|
|
78 }
|
|
79
|
|
80 g_free (disclosure->priv);
|
|
81 disclosure->priv = NULL;
|
|
82
|
|
83 G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
84 }
|
|
85
|
|
86 static void
|
|
87 get_x_y (GaimDisclosure *disclosure,
|
|
88 int *x,
|
|
89 int *y,
|
|
90 GtkStateType *state_type)
|
|
91 {
|
|
92 GtkCheckButton *check_button;
|
|
93 int indicator_size = 0;
|
|
94 int focus_width;
|
|
95 int focus_pad;
|
|
96 gboolean interior_focus;
|
|
97 GtkWidget *widget = GTK_WIDGET (disclosure);
|
|
98 GtkBin *bin = GTK_BIN (disclosure);
|
|
99 int width;
|
|
100
|
|
101 if (GTK_WIDGET_VISIBLE (disclosure) &&
|
|
102 GTK_WIDGET_MAPPED (disclosure)) {
|
|
103 check_button = GTK_CHECK_BUTTON (disclosure);
|
|
104
|
|
105 gtk_widget_style_get (widget,
|
|
106 "interior_focus", &interior_focus,
|
|
107 "focus-line-width", &focus_width,
|
|
108 "focus-padding", &focus_pad,
|
|
109 NULL);
|
|
110
|
|
111 *state_type = GTK_WIDGET_STATE (widget);
|
|
112 if ((*state_type != GTK_STATE_NORMAL) &&
|
|
113 (*state_type != GTK_STATE_PRELIGHT)) {
|
|
114 *state_type = GTK_STATE_NORMAL;
|
|
115 }
|
|
116
|
|
117 if (bin->child) {
|
|
118 width = bin->child->allocation.x - widget->allocation.x - (2 * GTK_CONTAINER (widget)->border_width);
|
|
119 } else {
|
|
120 width = widget->allocation.width;
|
|
121 }
|
|
122
|
|
123 *x = widget->allocation.x + (width) / 2;
|
|
124 *y = widget->allocation.y + widget->allocation.height / 2;
|
|
125
|
|
126 if (interior_focus == FALSE) {
|
|
127 *x += focus_width + focus_pad;
|
|
128 }
|
|
129
|
|
130 *state_type = GTK_WIDGET_STATE (widget) == GTK_STATE_ACTIVE ? GTK_STATE_NORMAL : GTK_WIDGET_STATE (widget);
|
|
131
|
|
132 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) {
|
|
133 *x = widget->allocation.x + widget->allocation.width - (indicator_size + *x - widget->allocation.x);
|
|
134 }
|
|
135 } else {
|
|
136 *x = 0;
|
|
137 *y = 0;
|
|
138 *state_type = GTK_STATE_NORMAL;
|
|
139 }
|
|
140 }
|
|
141
|
|
142 static gboolean
|
|
143 expand_collapse_timeout (gpointer data)
|
|
144 {
|
|
145 GtkWidget *widget = data;
|
|
146 GaimDisclosure *disclosure = data;
|
|
147 GtkStateType state_type;
|
|
148 int x, y;
|
|
149
|
|
150 gdk_window_invalidate_rect (widget->window, &widget->allocation, TRUE);
|
|
151 get_x_y (disclosure, &x, &y, &state_type);
|
|
152
|
|
153 gtk_paint_expander (widget->style,
|
|
154 widget->window,
|
|
155 state_type,
|
|
156 &widget->allocation,
|
|
157 widget,
|
|
158 "disclosure",
|
|
159 x, y,
|
|
160 disclosure->priv->style);
|
|
161
|
|
162 disclosure->priv->style += disclosure->priv->direction;
|
|
163 if ((int) disclosure->priv->style > (int) GTK_EXPANDER_EXPANDED) {
|
|
164 disclosure->priv->style = GTK_EXPANDER_EXPANDED;
|
|
165
|
|
166 if (disclosure->priv->container != NULL) {
|
|
167 gtk_widget_show (disclosure->priv->container);
|
|
168 }
|
|
169
|
|
170 g_object_set (G_OBJECT (disclosure),
|
|
171 "label", disclosure->priv->hidden,
|
|
172 NULL);
|
|
173 return FALSE;
|
|
174 } else if ((int) disclosure->priv->style < (int) GTK_EXPANDER_COLLAPSED) {
|
|
175 disclosure->priv->style = GTK_EXPANDER_COLLAPSED;
|
|
176
|
|
177 if (disclosure->priv->container != NULL) {
|
|
178 gtk_widget_hide (disclosure->priv->container);
|
|
179 }
|
|
180
|
|
181 g_object_set (G_OBJECT (disclosure),
|
|
182 "label", disclosure->priv->shown,
|
|
183 NULL);
|
|
184
|
|
185 return FALSE;
|
|
186 } else {
|
|
187 return TRUE;
|
|
188 }
|
|
189 }
|
|
190
|
|
191 static void
|
|
192 do_animation (GaimDisclosure *disclosure,
|
|
193 gboolean opening)
|
|
194 {
|
|
195 if (disclosure->priv->expand_id > 0) {
|
|
196 gaim_timeout_remove(disclosure->priv->expand_id);
|
|
197 }
|
|
198
|
|
199 disclosure->priv->direction = opening ? 1 : -1;
|
|
200 disclosure->priv->expand_id = gaim_timeout_add (50, expand_collapse_timeout, disclosure);
|
|
201 }
|
|
202
|
|
203 static void
|
|
204 toggled (GtkToggleButton *tb)
|
|
205 {
|
|
206 GaimDisclosure *disclosure;
|
|
207
|
|
208 disclosure = GAIM_DISCLOSURE (tb);
|
|
209 do_animation (disclosure, gtk_toggle_button_get_active (tb));
|
|
210
|
|
211 if (disclosure->priv->container == NULL) {
|
|
212 return;
|
|
213 }
|
|
214 }
|
|
215
|
|
216 static void
|
|
217 draw_indicator (GtkCheckButton *check,
|
|
218 GdkRectangle *area)
|
|
219 {
|
|
220 GtkWidget *widget = GTK_WIDGET (check);
|
|
221 GaimDisclosure *disclosure = GAIM_DISCLOSURE (check);
|
|
222 GtkStateType state_type;
|
|
223 int x, y;
|
|
224
|
|
225 get_x_y (disclosure, &x, &y, &state_type);
|
|
226 gtk_paint_expander (widget->style,
|
|
227 widget->window,
|
|
228 state_type,
|
|
229 area,
|
|
230 widget,
|
|
231 "treeview",
|
|
232 x, y,
|
|
233 disclosure->priv->style);
|
|
234 }
|
|
235
|
|
236 static void
|
|
237 class_init (GaimDisclosureClass *klass)
|
|
238 {
|
|
239 GObjectClass *object_class;
|
|
240 GtkWidgetClass *widget_class;
|
|
241 GtkCheckButtonClass *button_class;
|
|
242 GtkToggleButtonClass *toggle_class;
|
|
243
|
|
244 object_class = G_OBJECT_CLASS (klass);
|
|
245 widget_class = GTK_WIDGET_CLASS (klass);
|
|
246 button_class = GTK_CHECK_BUTTON_CLASS (klass);
|
|
247 toggle_class = GTK_TOGGLE_BUTTON_CLASS (klass);
|
|
248
|
|
249 toggle_class->toggled = toggled;
|
|
250 button_class->draw_indicator = draw_indicator;
|
|
251
|
|
252 object_class->finalize = finalize;
|
|
253
|
|
254 parent_class = g_type_class_peek_parent (klass);
|
|
255
|
|
256 gtk_widget_class_install_style_property (widget_class,
|
|
257 g_param_spec_int ("expander_size",
|
|
258 _("Expander Size"),
|
|
259 _("Size of the expander arrow"),
|
|
260 0, G_MAXINT,
|
|
261 10, G_PARAM_READABLE));
|
|
262 }
|
|
263
|
|
264 static void
|
|
265 init (GaimDisclosure *disclosure)
|
|
266 {
|
|
267 disclosure->priv = g_new0 (GaimDisclosurePrivate, 1);
|
|
268 disclosure->priv->expander_size = 10;
|
|
269 }
|
|
270
|
|
271 GType
|
|
272 gaim_disclosure_get_type (void)
|
|
273 {
|
|
274 static GType type = 0;
|
|
275
|
|
276 if (type == 0) {
|
|
277 GTypeInfo info = {
|
|
278 sizeof (GaimDisclosureClass),
|
|
279 NULL, NULL, (GClassInitFunc) class_init, NULL, NULL,
|
|
280 sizeof (GaimDisclosure), 0, (GInstanceInitFunc) init
|
|
281 };
|
|
282
|
|
283 type = g_type_register_static (GTK_TYPE_CHECK_BUTTON, "GaimDisclosure", &info, 0);
|
|
284 }
|
|
285
|
|
286 return type;
|
|
287 }
|
|
288
|
|
289 GtkWidget *
|
|
290 gaim_disclosure_new (const char *shown,
|
|
291 const char *hidden)
|
|
292 {
|
|
293 GaimDisclosure *disclosure;
|
|
294
|
|
295 disclosure = g_object_new (gaim_disclosure_get_type (), "label", shown, NULL);
|
|
296
|
|
297 disclosure->priv->shown = g_strdup (shown);
|
|
298 disclosure->priv->hidden = g_strdup (hidden);
|
|
299 return GTK_WIDGET (disclosure);
|
|
300 }
|
|
301
|
|
302 void
|
|
303 gaim_disclosure_set_container (GaimDisclosure *disclosure,
|
|
304 GtkWidget *container)
|
|
305 {
|
|
306 g_object_ref (G_OBJECT (container));
|
|
307 disclosure->priv->container = container;
|
|
308 }
|