8 #include "ygtkcellrenderersidebutton.h"
18 static guint toggle_cell_signal = 0;
25 { g_object_set(GTK_CELL_RENDERER(bcell),
"mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL); }
30 g_free (cell->stock_id);
31 cell->stock_id = NULL;
34 g_object_unref (cell->pixbuf);
41 if (!cell->pixbuf && cell->stock_id)
42 cell->pixbuf = gtk_widget_render_icon (widget, cell->stock_id, GTK_ICON_SIZE_BUTTON, NULL);
45 static void ygtk_cell_renderer_side_button_finalize (GObject *
object)
49 G_OBJECT_CLASS (ygtk_cell_renderer_side_button_parent_class)->finalize (
object);
52 static void ygtk_cell_renderer_side_button_get_property (
53 GObject *
object, guint param_id, GValue *value, GParamSpec *pspec)
55 if (pspec->owner_type == YGTK_TYPE_CELL_RENDERER_SIDE_BUTTON) {
59 g_value_set_boolean (value, bcell->active);
62 g_value_set_string (value, bcell->stock_id);
64 case PROP_BUTTON_VISIBLE:
65 g_value_set_boolean (value, bcell->button_visible);
70 G_OBJECT_CLASS (ygtk_cell_renderer_side_button_parent_class)->get_property (
71 object, param_id, value, pspec);
74 static void ygtk_cell_renderer_side_button_set_property (GObject *
object,
75 guint param_id,
const GValue *value, GParamSpec *pspec)
77 if (pspec->owner_type == YGTK_TYPE_CELL_RENDERER_SIDE_BUTTON) {
81 bcell->active = g_value_get_boolean (value);
85 bcell->stock_id = g_value_dup_string (value);
87 case PROP_BUTTON_VISIBLE:
88 bcell->button_visible = g_value_get_boolean (value);
93 G_OBJECT_CLASS (ygtk_cell_renderer_side_button_parent_class)->set_property (
94 object, param_id, value, pspec);
97 static void ygtk_cell_renderer_side_button_get_size (
98 GtkCellRenderer *cell, GtkWidget *widget,
const GdkRectangle *cell_area,
99 gint *x_offset, gint *y_offset, gint *width, gint *height)
101 GTK_CELL_RENDERER_CLASS (ygtk_cell_renderer_side_button_parent_class)->get_size (
102 cell, widget, cell_area, x_offset, y_offset, width, height);
104 if (bcell->button_visible) {
105 gint icon_width, icon_height;
106 gtk_icon_size_lookup (GTK_ICON_SIZE_BUTTON, &icon_width, &icon_height);
107 *width += icon_width + 8 + 4;
111 static void ygtk_cell_renderer_side_button_render (
112 GtkCellRenderer *cell, cairo_t *cr, GtkWidget *widget,
113 const GdkRectangle *background_area,
const GdkRectangle *cell_area,
114 GtkCellRendererState flags)
117 GdkRectangle text_area = *cell_area;
118 gint icon_width, icon_height;
119 if (bcell->button_visible) {
120 gtk_icon_size_lookup (GTK_ICON_SIZE_BUTTON, &icon_width, &icon_height);
121 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
122 text_area.x += icon_width + 8;
123 text_area.width -= icon_width + 8 + 4;
126 GTK_CELL_RENDERER_CLASS (ygtk_cell_renderer_side_button_parent_class)->render (
127 cell, cr, widget, background_area, &text_area, flags);
129 if (bcell->button_visible) {
130 GtkStateType state = GTK_STATE_NORMAL;
131 gboolean sensitive = TRUE;
132 g_object_get(cell,
"sensitive", &sensitive, NULL);
133 if (!sensitive || gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
134 state = GTK_STATE_INSENSITIVE;
138 state = GTK_STATE_ACTIVE;
140 GtkStyleContext *style = gtk_widget_get_style_context(widget);
141 gtk_style_context_save(style);
142 gtk_style_context_set_state(style, state);
143 gtk_style_context_add_class (style, GTK_STYLE_CLASS_BUTTON);
145 GdkRectangle button_area = *cell_area;
146 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
147 button_area.x = cell_area->x + 2;
149 button_area.x = cell_area->x + cell_area->width - icon_width - 8;
150 button_area.width = icon_width + 4;
151 button_area.height = icon_height + 4;
152 button_area.y = cell_area->y + ((cell_area->height - button_area.height) / 2);
154 gtk_render_background (style, cr, button_area.x, button_area.y, button_area.width, button_area.height);
155 gtk_render_frame (style, cr, button_area.x, button_area.y, button_area.width, button_area.height);
157 GdkRectangle icon_area = button_area;
161 icon_area.x += DEPRESS_PAD; icon_area.y += DEPRESS_PAD;
164 ensure_pixbuf (bcell, widget);
165 gdk_cairo_set_source_pixbuf (cr, bcell->pixbuf, icon_area.x, icon_area.y);
166 cairo_rectangle (cr, icon_area.x, icon_area.y, icon_width, icon_height);
169 gtk_style_context_restore(style);
173 static gboolean ygtk_cell_renderer_side_button_activate (
174 GtkCellRenderer *cell, GdkEvent *event, GtkWidget *widget,
const gchar *path,
175 const GdkRectangle *background_area,
const GdkRectangle *cell_area, GtkCellRendererState flags)
178 if (bcell->button_visible) {
179 GdkEventButton *_event = (GdkEventButton *) event;
180 gint icon_width, icon_height;
181 gtk_icon_size_lookup (GTK_ICON_SIZE_BUTTON, &icon_width, &icon_height);
182 int icon_x, x = _event->x - cell_area->x;
183 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
186 icon_x = cell_area->width - (icon_width+12);
188 if (x >= icon_x && x <= icon_x + icon_width) {
189 g_signal_emit (cell, toggle_cell_signal, 0, path);
196 GtkCellRenderer *ygtk_cell_renderer_side_button_new (
void)
197 {
return g_object_new (YGTK_TYPE_CELL_RENDERER_SIDE_BUTTON, NULL); }
200 {
return cell->active; }
204 GObjectClass *object_class = G_OBJECT_CLASS (
class);
205 object_class->get_property = ygtk_cell_renderer_side_button_get_property;
206 object_class->set_property = ygtk_cell_renderer_side_button_set_property;
207 object_class->finalize = ygtk_cell_renderer_side_button_finalize;
209 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (
class);
210 cell_class->get_size = ygtk_cell_renderer_side_button_get_size;
211 cell_class->render = ygtk_cell_renderer_side_button_render;
212 cell_class->activate = ygtk_cell_renderer_side_button_activate;
214 GParamFlags readwrite_flag =
215 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB;
216 g_object_class_install_property (object_class, PROP_ACTIVE,
217 g_param_spec_boolean (
"active",
"Toggle state",
"The toggle state of the button",
218 FALSE, readwrite_flag));
219 g_object_class_install_property (object_class, PROP_STOCK_ID,
220 g_param_spec_string (
"stock-id",
"Stock ID",
"Stock icon to render", NULL, readwrite_flag));
221 g_object_class_install_property (object_class, PROP_BUTTON_VISIBLE,
222 g_param_spec_boolean (
"button-visible",
"Is Button Visible",
"Whether to show side button", TRUE, readwrite_flag));
224 toggle_cell_signal = g_signal_new (
"toggled", G_OBJECT_CLASS_TYPE (object_class),
226 NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);