2
* Copyright (c) 2011 Red Hat, Inc.
3
* Copyright (c) 2012 Igalia, S.L.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU Lesser General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or (at your
8
* option) any later version.
10
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13
* License for more details.
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this program; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
* Based on gd-toggle-pixbuf-renderer by Cosimo Cecchi <cosimoc@redhat.com>
25
#include "ephy-removable-pixbuf-renderer.h"
26
#include "ephy-widgets-type-builtins.h"
28
G_DEFINE_TYPE (EphyRemovablePixbufRenderer, ephy_removable_pixbuf_renderer, GD_TYPE_TOGGLE_PIXBUF_RENDERER);
40
static guint signals[LAST_SIGNAL] = { 0 };
42
struct _EphyRemovablePixbufRendererPrivate {
43
EphyRemovablePixbufRenderPolicy policy;
44
GdkPixbuf *close_icon;
48
get_icon_rectangle (GtkWidget *widget,
49
GtkCellRenderer *cell,
50
const GdkRectangle *cell_area,
52
GdkRectangle *rectangle)
54
GtkTextDirection direction;
55
gint x_offset, y_offset, xpad, ypad;
60
gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
61
direction = gtk_widget_get_direction (widget);
62
icon_size = gdk_pixbuf_get_width (icon);
64
g_object_get (cell, "pixbuf", &pixbuf, NULL);
66
w = gdk_pixbuf_get_width (pixbuf);
67
h = gdk_pixbuf_get_height (pixbuf);
68
g_object_unref (pixbuf);
71
x_offset = (cell_area->width - w)/2 + 10;
72
y_offset = (cell_area->height - h)/2 + 9;
74
if (direction == GTK_TEXT_DIR_RTL)
77
x_offset = cell_area->width - icon_size - xpad - x_offset;
79
rectangle->x = cell_area->x + x_offset;
80
rectangle->y = cell_area->y + ypad + y_offset;
81
rectangle->width = rectangle->height = icon_size;
85
ephy_removable_pixbuf_renderer_render (GtkCellRenderer *cell,
88
const GdkRectangle *background_area,
89
const GdkRectangle *cell_area,
90
GtkCellRendererState flags)
92
GtkStyleContext *context;
93
EphyRemovablePixbufRenderer *self = EPHY_REMOVABLE_PIXBUF_RENDERER (cell);
94
GdkRectangle icon_area;
96
GTK_CELL_RENDERER_CLASS (ephy_removable_pixbuf_renderer_parent_class)->render
98
background_area, cell_area, flags);
100
if (self->priv->policy == EPHY_REMOVABLE_PIXBUF_RENDER_NEVER ||
101
(self->priv->policy == EPHY_REMOVABLE_PIXBUF_RENDER_PRELIT && !(flags & GTK_CELL_RENDERER_PRELIT)))
104
get_icon_rectangle (widget, cell, cell_area, self->priv->close_icon, &icon_area);
105
context = gtk_widget_get_style_context (widget);
106
gtk_render_icon (context, cr, self->priv->close_icon, icon_area.x, icon_area.y);
110
ephy_removable_pixbuf_renderer_activate (GtkCellRenderer *cell,
114
const GdkRectangle *background_area,
115
const GdkRectangle *cell_area,
116
GtkCellRendererState flags)
118
GdkRectangle icon_area;
119
GdkEventButton *ev = (GdkEventButton *) gtk_get_current_event();
120
EphyRemovablePixbufRendererPrivate *priv = EPHY_REMOVABLE_PIXBUF_RENDERER (cell)->priv;
122
if (priv->policy == EPHY_REMOVABLE_PIXBUF_RENDER_NEVER)
125
get_icon_rectangle (widget, cell, cell_area, priv->close_icon, &icon_area);
126
if (icon_area.x <= ev->x && ev->x <= icon_area.x + icon_area.width &&
127
icon_area.y <= ev->y && ev->y <= icon_area.y + icon_area.height) {
128
g_signal_emit (cell, signals [DELETE_CLICKED], 0, path);
136
ephy_removable_pixbuf_renderer_get_property (GObject *object,
141
EphyRemovablePixbufRenderer *self = EPHY_REMOVABLE_PIXBUF_RENDERER (object);
145
case PROP_RENDER_POLICY:
146
g_value_set_enum (value, self->priv->policy);
149
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
155
ephy_removable_pixbuf_renderer_set_property (GObject *object,
160
EphyRemovablePixbufRenderer *self = EPHY_REMOVABLE_PIXBUF_RENDERER (object);
164
case PROP_RENDER_POLICY:
165
self->priv->policy = g_value_get_enum (value);
168
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
174
ephy_removable_pixbuf_renderer_dispose (GObject *object)
176
EphyRemovablePixbufRendererPrivate *priv = EPHY_REMOVABLE_PIXBUF_RENDERER (object)->priv;
178
if (priv->close_icon)
179
g_clear_object (&priv->close_icon);
181
G_OBJECT_CLASS (ephy_removable_pixbuf_renderer_parent_class)->dispose (object);
185
ephy_removable_pixbuf_renderer_class_init (EphyRemovablePixbufRendererClass *klass)
187
GObjectClass *oclass = G_OBJECT_CLASS (klass);
188
GtkCellRendererClass *crclass = GTK_CELL_RENDERER_CLASS (klass);
190
crclass->render = ephy_removable_pixbuf_renderer_render;
191
crclass->activate = ephy_removable_pixbuf_renderer_activate;
192
oclass->get_property = ephy_removable_pixbuf_renderer_get_property;
193
oclass->set_property = ephy_removable_pixbuf_renderer_set_property;
194
oclass->dispose = ephy_removable_pixbuf_renderer_dispose;
196
g_object_class_install_property (oclass,
198
g_param_spec_enum ("render-policy",
200
"The rendering policy for the close icon in the renderer",
201
EPHY_TYPE_REMOVABLE_PIXBUF_RENDER_POLICY,
202
EPHY_REMOVABLE_PIXBUF_RENDER_PRELIT,
205
G_PARAM_STATIC_STRINGS));
207
signals[DELETE_CLICKED] =
208
g_signal_new ("delete-clicked",
209
G_OBJECT_CLASS_TYPE (oclass),
215
g_type_class_add_private (klass, sizeof (EphyRemovablePixbufRendererPrivate));
219
ephy_removable_pixbuf_renderer_init (EphyRemovablePixbufRenderer *self)
221
GtkIconTheme *icon_theme;
223
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EPHY_TYPE_REMOVABLE_PIXBUF_RENDERER,
224
EphyRemovablePixbufRendererPrivate);
225
g_object_set (self, "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
226
icon_theme = gtk_icon_theme_get_default ();
227
self->priv->close_icon = gtk_icon_theme_load_icon (icon_theme,
228
"window-close-symbolic",
233
ephy_removable_pixbuf_renderer_new (void)
235
return g_object_new (EPHY_TYPE_REMOVABLE_PIXBUF_RENDERER, NULL);