Simplify handling of GG xfer auth queue.
[pidgin-git.git] / pidgin / gtkcellrendererexpander.c
blob9e2e023e5c2201151ea3f86ffc02494a8bb0b3e3
1 /* pidgin
3 * Pidgin is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 /* This is taken largely from GtkCellRenderer[Text|Pixbuf|Toggle] by
24 * Jonathon Blandford <jrb@redhat.com> for RedHat, Inc.
27 #include "gtkcellrendererexpander.h"
29 #include "gtk3compat.h"
31 struct _PidginCellRendererExpander {
32 GtkCellRenderer parent;
34 gboolean is_expander;
37 static void pidgin_cell_renderer_expander_get_property (GObject *object,
38 guint param_id,
39 GValue *value,
40 GParamSpec *pspec);
41 static void pidgin_cell_renderer_expander_set_property (GObject *object,
42 guint param_id,
43 const GValue *value,
44 GParamSpec *pspec);
45 static void pidgin_cell_renderer_expander_init (PidginCellRendererExpander *cellexpander);
46 static void pidgin_cell_renderer_expander_class_init (PidginCellRendererExpanderClass *class);
47 static void pidgin_cell_renderer_expander_get_size (GtkCellRenderer *cell,
48 GtkWidget *widget,
49 const GdkRectangle *cell_area,
50 gint *x_offset,
51 gint *y_offset,
52 gint *width,
53 gint *height);
54 static void pidgin_cell_renderer_expander_render (GtkCellRenderer *cell,
55 cairo_t *cr,
56 GtkWidget *widget,
57 const GdkRectangle *background_area,
58 const GdkRectangle *cell_area,
59 GtkCellRendererState flags);
60 static gboolean pidgin_cell_renderer_expander_activate (GtkCellRenderer *r,
61 GdkEvent *event,
62 GtkWidget *widget,
63 const gchar *p,
64 const GdkRectangle *bg,
65 const GdkRectangle *cell,
66 GtkCellRendererState flags);
67 static void pidgin_cell_renderer_expander_finalize (GObject *gobject);
69 enum {
70 LAST_SIGNAL
73 enum {
74 PROP_0,
75 PROP_IS_EXPANDER
78 /* static guint expander_cell_renderer_signals [LAST_SIGNAL]; */
80 G_DEFINE_TYPE(PidginCellRendererExpander, pidgin_cell_renderer_expander,
81 GTK_TYPE_CELL_RENDERER)
83 static void pidgin_cell_renderer_expander_init (PidginCellRendererExpander *cellexpander)
85 g_object_set(G_OBJECT(cellexpander), "mode",
86 GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
87 gtk_cell_renderer_set_padding(GTK_CELL_RENDERER(cellexpander), 0, 2);
90 static void pidgin_cell_renderer_expander_class_init (PidginCellRendererExpanderClass *class)
92 GObjectClass *object_class = G_OBJECT_CLASS(class);
93 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(class);
95 object_class->finalize = pidgin_cell_renderer_expander_finalize;
96 object_class->get_property = pidgin_cell_renderer_expander_get_property;
97 object_class->set_property = pidgin_cell_renderer_expander_set_property;
99 cell_class->get_size = pidgin_cell_renderer_expander_get_size;
100 cell_class->render = pidgin_cell_renderer_expander_render;
101 cell_class->activate = pidgin_cell_renderer_expander_activate;
103 g_object_class_install_property (object_class,
104 PROP_IS_EXPANDER,
105 g_param_spec_boolean ("expander-visible",
106 "Is Expander",
107 "True if the renderer should draw an expander",
108 FALSE,
109 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
112 static void pidgin_cell_renderer_expander_finalize (GObject *object)
115 PidginCellRendererExpander *cellexpander = PIDGIN_CELL_RENDERER_EXPANDER(object);
118 G_OBJECT_CLASS(pidgin_cell_renderer_expander_parent_class)->finalize(object);
121 static void pidgin_cell_renderer_expander_get_property (GObject *object,
122 guint param_id,
123 GValue *value,
124 GParamSpec *psec)
126 PidginCellRendererExpander *cellexpander = PIDGIN_CELL_RENDERER_EXPANDER(object);
128 switch (param_id)
130 case PROP_IS_EXPANDER:
131 g_value_set_boolean(value, cellexpander->is_expander);
132 break;
133 default:
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, psec);
135 break;
140 static void pidgin_cell_renderer_expander_set_property (GObject *object,
141 guint param_id,
142 const GValue *value,
143 GParamSpec *pspec)
145 PidginCellRendererExpander *cellexpander = PIDGIN_CELL_RENDERER_EXPANDER (object);
147 switch (param_id)
149 case PROP_IS_EXPANDER:
150 cellexpander->is_expander = g_value_get_boolean(value);
151 break;
152 default:
153 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
154 break;
158 GtkCellRenderer *pidgin_cell_renderer_expander_new(void)
160 return g_object_new(PIDGIN_TYPE_CELL_RENDERER_EXPANDER, NULL);
163 static void
164 pidgin_cell_renderer_expander_get_size (GtkCellRenderer *cell,
165 GtkWidget *widget,
166 const GdkRectangle *cell_area,
167 gint *x_offset,
168 gint *y_offset,
169 gint *width,
170 gint *height)
172 gint calc_width;
173 gint calc_height;
174 gint expander_size;
175 gint xpad;
176 gint ypad;
177 gfloat xalign;
178 gfloat yalign;
180 gtk_widget_style_get(widget, "expander-size", &expander_size, NULL);
182 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
183 gtk_cell_renderer_get_alignment(cell, &xalign, &yalign);
184 calc_width = (gint) xpad * 2 + expander_size;
185 calc_height = (gint) ypad * 2 + expander_size;
187 if (width)
188 *width = calc_width;
190 if (height)
191 *height = calc_height;
193 if (cell_area)
195 if (x_offset)
197 *x_offset = xalign * (cell_area->width - calc_width);
198 *x_offset = MAX (*x_offset, 0);
200 if (y_offset)
202 *y_offset = yalign * (cell_area->height - calc_height);
203 *y_offset = MAX (*y_offset, 0);
209 static void
210 pidgin_cell_renderer_expander_render(GtkCellRenderer *cell,
211 cairo_t *cr,
212 GtkWidget *widget,
213 const GdkRectangle *background_area,
214 const GdkRectangle *cell_area,
215 GtkCellRendererState flags)
217 PidginCellRendererExpander *cellexpander = (PidginCellRendererExpander *) cell;
218 gboolean set;
219 gint width, height;
220 GtkStateFlags state;
221 gint xpad;
222 gint ypad;
223 gboolean is_expanded;
224 GtkAllocation allocation;
225 GtkStyleContext *context;
227 if (!cellexpander->is_expander)
228 return;
230 gtk_cell_renderer_get_padding(cell, &xpad, &ypad);
231 g_object_get(G_OBJECT(cell), "is-expanded", &is_expanded, NULL);
233 width = cell_area->width;
234 height = cell_area->height;
236 if (!gtk_widget_get_sensitive(widget))
237 state = GTK_STATE_FLAG_INSENSITIVE;
238 else if (flags & GTK_CELL_RENDERER_PRELIT)
239 state = GTK_STATE_FLAG_PRELIGHT;
240 else if (gtk_widget_has_focus(widget) && flags & GTK_CELL_RENDERER_SELECTED)
241 state = GTK_STATE_FLAG_ACTIVE;
242 else
243 state = GTK_STATE_FLAG_NORMAL;
245 width -= xpad*2;
246 height -= ypad*2;
248 if (is_expanded)
249 state |= GTK_STATE_FLAG_CHECKED;
250 else
251 state &= ~GTK_STATE_FLAG_CHECKED;
253 context = gtk_widget_get_style_context(widget);
254 gtk_style_context_add_class(context, GTK_STYLE_CLASS_VIEW);
255 gtk_style_context_add_class(context, GTK_STYLE_CLASS_EXPANDER);
256 gtk_style_context_set_state(context, state);
257 gtk_render_expander(context, cr,
258 cell_area->x + xpad, cell_area->y + ypad,
259 width, height);
261 /* only draw the line if the color isn't set - this prevents a bug where the hline appears only under the expander */
262 g_object_get(cellexpander, "cell-background-set", &set, NULL);
263 gtk_widget_get_allocation(widget, &allocation);
265 if (is_expanded && !set)
266 gtk_render_line(context, cr, 0, cell_area->y + cell_area->height,
267 allocation.width, cell_area->y + cell_area->height);
270 static gboolean
271 pidgin_cell_renderer_expander_activate(GtkCellRenderer *r,
272 GdkEvent *event,
273 GtkWidget *widget,
274 const gchar *p,
275 const GdkRectangle *bg,
276 const GdkRectangle *cell,
277 GtkCellRendererState flags)
279 GtkTreePath *path = gtk_tree_path_new_from_string(p);
280 if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(widget), path))
281 gtk_tree_view_collapse_row(GTK_TREE_VIEW(widget), path);
282 else
283 gtk_tree_view_expand_row(GTK_TREE_VIEW(widget),path,FALSE);
284 gtk_tree_path_free(path);
285 return FALSE;