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
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
;
37 static void pidgin_cell_renderer_expander_get_property (GObject
*object
,
41 static void pidgin_cell_renderer_expander_set_property (GObject
*object
,
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
,
49 const GdkRectangle
*cell_area
,
54 static void pidgin_cell_renderer_expander_render (GtkCellRenderer
*cell
,
57 const GdkRectangle
*background_area
,
58 const GdkRectangle
*cell_area
,
59 GtkCellRendererState flags
);
60 static gboolean
pidgin_cell_renderer_expander_activate (GtkCellRenderer
*r
,
64 const GdkRectangle
*bg
,
65 const GdkRectangle
*cell
,
66 GtkCellRendererState flags
);
67 static void pidgin_cell_renderer_expander_finalize (GObject
*gobject
);
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
,
105 g_param_spec_boolean ("expander-visible",
107 "True if the renderer should draw an expander",
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
,
126 PidginCellRendererExpander
*cellexpander
= PIDGIN_CELL_RENDERER_EXPANDER(object
);
130 case PROP_IS_EXPANDER
:
131 g_value_set_boolean(value
, cellexpander
->is_expander
);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, psec
);
140 static void pidgin_cell_renderer_expander_set_property (GObject
*object
,
145 PidginCellRendererExpander
*cellexpander
= PIDGIN_CELL_RENDERER_EXPANDER (object
);
149 case PROP_IS_EXPANDER
:
150 cellexpander
->is_expander
= g_value_get_boolean(value
);
153 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, param_id
, pspec
);
158 GtkCellRenderer
*pidgin_cell_renderer_expander_new(void)
160 return g_object_new(PIDGIN_TYPE_CELL_RENDERER_EXPANDER
, NULL
);
164 pidgin_cell_renderer_expander_get_size (GtkCellRenderer
*cell
,
166 const GdkRectangle
*cell_area
,
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
;
191 *height
= calc_height
;
197 *x_offset
= xalign
* (cell_area
->width
- calc_width
);
198 *x_offset
= MAX (*x_offset
, 0);
202 *y_offset
= yalign
* (cell_area
->height
- calc_height
);
203 *y_offset
= MAX (*y_offset
, 0);
210 pidgin_cell_renderer_expander_render(GtkCellRenderer
*cell
,
213 const GdkRectangle
*background_area
,
214 const GdkRectangle
*cell_area
,
215 GtkCellRendererState flags
)
217 PidginCellRendererExpander
*cellexpander
= (PidginCellRendererExpander
*) cell
;
223 gboolean is_expanded
;
224 GtkAllocation allocation
;
225 GtkStyleContext
*context
;
227 if (!cellexpander
->is_expander
)
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
;
243 state
= GTK_STATE_FLAG_NORMAL
;
249 state
|= GTK_STATE_FLAG_CHECKED
;
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
,
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
);
271 pidgin_cell_renderer_expander_activate(GtkCellRenderer
*r
,
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
);
283 gtk_tree_view_expand_row(GTK_TREE_VIEW(widget
),path
,FALSE
);
284 gtk_tree_path_free(path
);