~ubuntu-branches/ubuntu/wily/epiphany-browser/wily

« back to all changes in this revision

Viewing changes to lib/widgets/ephy-removable-pixbuf-renderer.c

  • Committer: Package Import Robot
  • Author(s): Gustavo Noronha Silva, Jeremy Bicha, Emilio Pozuelo Monfort, Gustavo Noronha Silva
  • Date: 2012-12-10 11:40:01 UTC
  • mfrom: (1.8.8) (105.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20121210114001-42jjyg3qw3eyz00a
Tags: 3.6.1-1
[ Jeremy Bicha ]
* New upstream unstable release
* debian/control.in:
  - Bump minimum intltool to 0.50
* debian/epiphany-browser-data.install:
  - The developers no longer provide help files to install since they were
    too outdated
* Dropped upstream patches:
  - 01_email-as-user-for-password-remembering.patch
  - 13_toolbar_size_fixes.patch
* debian/watch: Watch for unstable releases.

[ Emilio Pozuelo Monfort ]
* New upstream release.
  + debian/patches/14_pkglibdir.patch:
    - Removed, applied upstream.
  + debian/control.in:
    - Update (build-)dependencies.

[ Gustavo Noronha Silva ]
* debian/control.in:
- Build-Depend on gnome-common >= 3.6, needed for the code
  coverage feature

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2011 Red Hat, Inc.
 
3
 * Copyright (c) 2012 Igalia, S.L.
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 * Based on gd-toggle-pixbuf-renderer by Cosimo Cecchi <cosimoc@redhat.com>
 
20
 *
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include "ephy-removable-pixbuf-renderer.h"
 
26
#include "ephy-widgets-type-builtins.h"
 
27
 
 
28
G_DEFINE_TYPE (EphyRemovablePixbufRenderer, ephy_removable_pixbuf_renderer, GD_TYPE_TOGGLE_PIXBUF_RENDERER);
 
29
 
 
30
enum {
 
31
  DELETE_CLICKED,
 
32
  LAST_SIGNAL
 
33
};
 
34
 
 
35
enum {
 
36
  PROP_0 = 0,
 
37
  PROP_RENDER_POLICY
 
38
};
 
39
 
 
40
static guint signals[LAST_SIGNAL] = { 0 };
 
41
 
 
42
struct _EphyRemovablePixbufRendererPrivate {
 
43
  EphyRemovablePixbufRenderPolicy policy;
 
44
  GdkPixbuf *close_icon;
 
45
};
 
46
 
 
47
static void
 
48
get_icon_rectangle (GtkWidget *widget,
 
49
                    GtkCellRenderer *cell,
 
50
                    const GdkRectangle *cell_area,
 
51
                    GdkPixbuf *icon,
 
52
                    GdkRectangle *rectangle)
 
53
{
 
54
  GtkTextDirection direction;
 
55
  gint x_offset, y_offset, xpad, ypad;
 
56
  gint icon_size;
 
57
  gint w = 0, h = 0;
 
58
  GdkPixbuf *pixbuf;
 
59
 
 
60
  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
 
61
  direction = gtk_widget_get_direction (widget);
 
62
  icon_size = gdk_pixbuf_get_width (icon);
 
63
 
 
64
  g_object_get (cell, "pixbuf", &pixbuf, NULL);
 
65
  if (pixbuf) {
 
66
    w = gdk_pixbuf_get_width (pixbuf);
 
67
    h = gdk_pixbuf_get_height (pixbuf);
 
68
    g_object_unref (pixbuf);
 
69
  }
 
70
 
 
71
  x_offset = (cell_area->width - w)/2 + 10;
 
72
  y_offset = (cell_area->height - h)/2 + 9;
 
73
 
 
74
  if (direction == GTK_TEXT_DIR_RTL)
 
75
    x_offset += xpad;
 
76
  else
 
77
    x_offset = cell_area->width - icon_size - xpad - x_offset;
 
78
 
 
79
  rectangle->x = cell_area->x + x_offset;
 
80
  rectangle->y = cell_area->y + ypad + y_offset;
 
81
  rectangle->width = rectangle->height = icon_size;
 
82
}
 
83
 
 
84
static void
 
85
ephy_removable_pixbuf_renderer_render (GtkCellRenderer      *cell,
 
86
                                       cairo_t              *cr,
 
87
                                       GtkWidget            *widget,
 
88
                                       const GdkRectangle   *background_area,
 
89
                                       const GdkRectangle   *cell_area,
 
90
                                       GtkCellRendererState  flags)
 
91
{
 
92
  GtkStyleContext *context;
 
93
  EphyRemovablePixbufRenderer *self = EPHY_REMOVABLE_PIXBUF_RENDERER (cell);
 
94
  GdkRectangle icon_area;
 
95
 
 
96
  GTK_CELL_RENDERER_CLASS (ephy_removable_pixbuf_renderer_parent_class)->render
 
97
    (cell, cr, widget,
 
98
     background_area, cell_area, flags);
 
99
 
 
100
  if (self->priv->policy ==  EPHY_REMOVABLE_PIXBUF_RENDER_NEVER ||
 
101
      (self->priv->policy == EPHY_REMOVABLE_PIXBUF_RENDER_PRELIT && !(flags & GTK_CELL_RENDERER_PRELIT)))
 
102
    return;
 
103
 
 
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);
 
107
}
 
108
 
 
109
static gboolean
 
110
ephy_removable_pixbuf_renderer_activate (GtkCellRenderer      *cell,
 
111
                                         GdkEvent             *event,
 
112
                                         GtkWidget            *widget,
 
113
                                         const gchar          *path,
 
114
                                         const GdkRectangle   *background_area,
 
115
                                         const GdkRectangle   *cell_area,
 
116
                                         GtkCellRendererState  flags)
 
117
{
 
118
  GdkRectangle icon_area;
 
119
  GdkEventButton *ev = (GdkEventButton *) gtk_get_current_event();
 
120
  EphyRemovablePixbufRendererPrivate *priv = EPHY_REMOVABLE_PIXBUF_RENDERER (cell)->priv;
 
121
 
 
122
  if (priv->policy ==  EPHY_REMOVABLE_PIXBUF_RENDER_NEVER)
 
123
    return FALSE;
 
124
 
 
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);
 
129
    return TRUE;
 
130
  }
 
131
 
 
132
  return FALSE;
 
133
}
 
134
 
 
135
static void
 
136
ephy_removable_pixbuf_renderer_get_property (GObject    *object,
 
137
                                             guint       property_id,
 
138
                                             GValue     *value,
 
139
                                             GParamSpec *pspec)
 
140
{
 
141
  EphyRemovablePixbufRenderer *self = EPHY_REMOVABLE_PIXBUF_RENDERER (object);
 
142
 
 
143
  switch (property_id)
 
144
    {
 
145
    case PROP_RENDER_POLICY:
 
146
      g_value_set_enum (value, self->priv->policy);
 
147
      break;
 
148
    default:
 
149
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
150
      break;
 
151
    }
 
152
}
 
153
 
 
154
static void
 
155
ephy_removable_pixbuf_renderer_set_property (GObject    *object,
 
156
                                             guint       property_id,
 
157
                                             const GValue *value,
 
158
                                             GParamSpec *pspec)
 
159
{
 
160
  EphyRemovablePixbufRenderer *self = EPHY_REMOVABLE_PIXBUF_RENDERER (object);
 
161
 
 
162
  switch (property_id)
 
163
    {
 
164
    case PROP_RENDER_POLICY:
 
165
      self->priv->policy = g_value_get_enum (value);
 
166
      break;
 
167
    default:
 
168
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 
169
      break;
 
170
    }
 
171
}
 
172
 
 
173
static void
 
174
ephy_removable_pixbuf_renderer_dispose (GObject     *object)
 
175
{
 
176
  EphyRemovablePixbufRendererPrivate *priv = EPHY_REMOVABLE_PIXBUF_RENDERER (object)->priv;
 
177
 
 
178
  if (priv->close_icon)
 
179
    g_clear_object (&priv->close_icon);
 
180
 
 
181
  G_OBJECT_CLASS (ephy_removable_pixbuf_renderer_parent_class)->dispose (object);
 
182
}
 
183
 
 
184
static void
 
185
ephy_removable_pixbuf_renderer_class_init (EphyRemovablePixbufRendererClass *klass)
 
186
{
 
187
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
 
188
  GtkCellRendererClass *crclass = GTK_CELL_RENDERER_CLASS (klass);
 
189
 
 
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;
 
195
 
 
196
  g_object_class_install_property (oclass,
 
197
                                   PROP_RENDER_POLICY,
 
198
                                   g_param_spec_enum ("render-policy",
 
199
                                                      "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,
 
203
                                                      G_PARAM_CONSTRUCT |
 
204
                                                      G_PARAM_READWRITE |
 
205
                                                      G_PARAM_STATIC_STRINGS));
 
206
 
 
207
  signals[DELETE_CLICKED] =
 
208
    g_signal_new ("delete-clicked",
 
209
                  G_OBJECT_CLASS_TYPE (oclass),
 
210
                  G_SIGNAL_RUN_LAST,
 
211
                  0, NULL, NULL, NULL,
 
212
                  G_TYPE_NONE, 1,
 
213
                  G_TYPE_STRING);
 
214
 
 
215
  g_type_class_add_private (klass, sizeof (EphyRemovablePixbufRendererPrivate));
 
216
}
 
217
 
 
218
static void
 
219
ephy_removable_pixbuf_renderer_init (EphyRemovablePixbufRenderer *self)
 
220
{
 
221
  GtkIconTheme *icon_theme;
 
222
 
 
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",
 
229
                                                     24, 0, NULL);
 
230
}
 
231
 
 
232
GtkCellRenderer *
 
233
ephy_removable_pixbuf_renderer_new (void)
 
234
{
 
235
  return g_object_new (EPHY_TYPE_REMOVABLE_PIXBUF_RENDERER, NULL);
 
236
}