No issues found
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* rbcellrendererpixbuf.c
3 *
4 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
5 * Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301 USA.
21 */
22
23 #include <config.h>
24
25 #include <stdlib.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #include "rb-cell-renderer-pixbuf.h"
30 #include "rb-cut-and-paste-code.h"
31
32 static void rb_cell_renderer_pixbuf_init (RBCellRendererPixbuf *celltext);
33 static void rb_cell_renderer_pixbuf_class_init (RBCellRendererPixbufClass *class);
34 static gboolean rb_cell_renderer_pixbuf_activate (GtkCellRenderer *cell,
35 GdkEvent *event,
36 GtkWidget *widget,
37 const gchar *path,
38 const GdkRectangle *background_area,
39 const GdkRectangle *cell_area,
40 GtkCellRendererState flags);
41
42 enum
43 {
44 PIXBUF_CLICKED,
45 LAST_SIGNAL
46 };
47
48 G_DEFINE_TYPE (RBCellRendererPixbuf, rb_cell_renderer_pixbuf, GTK_TYPE_CELL_RENDERER_PIXBUF)
49
50 /**
51 * SECTION:rb-cell-renderer-pixbuf
52 * @short_description: #GtkCellRenderer for displaying pixbufs in tree views
53 *
54 * This is similar to #GtkCellRendererPixbuf, except that it also emits a signal
55 * when the pixbuf is clicked on, and it can only use pixbuf objects.
56 */
57
58 static guint rb_cell_renderer_pixbuf_signals [LAST_SIGNAL] = { 0 };
59
60 static void
61 rb_cell_renderer_pixbuf_init (RBCellRendererPixbuf *cellpixbuf)
62 {
63 /* set the renderer able to be activated */
64 g_object_set (cellpixbuf,
65 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
66 NULL);
67 }
68
69 static void
70 rb_cell_renderer_pixbuf_class_init (RBCellRendererPixbufClass *class)
71 {
72 GObjectClass *object_class = G_OBJECT_CLASS (class);
73 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
74
75 cell_class->activate = rb_cell_renderer_pixbuf_activate;
76
77 /**
78 * RBCellRendererPixbuf::pixbuf-clicked:
79 * @renderer: the #RBCellRendererPixbuf
80 * @path: the #GtkTreePath to the row that was clicked
81 *
82 * Emitted when the user clicks on the pixbuf cell.
83 */
84 rb_cell_renderer_pixbuf_signals[PIXBUF_CLICKED] =
85 g_signal_new ("pixbuf-clicked",
86 G_OBJECT_CLASS_TYPE (object_class),
87 G_SIGNAL_RUN_FIRST,
88 G_STRUCT_OFFSET (RBCellRendererPixbufClass, pixbuf_clicked),
89 NULL, NULL,
90 g_cclosure_marshal_VOID__STRING,
91 G_TYPE_NONE,
92 1,
93 G_TYPE_STRING);
94 }
95
96 /**
97 * rb_cell_renderer_pixbuf_new:
98 *
99 * Creates a new #RBCellRendererPixbuf.
100 *
101 * Return value: the new cell renderer
102 **/
103 GtkCellRenderer *
104 rb_cell_renderer_pixbuf_new (void)
105 {
106 return GTK_CELL_RENDERER (g_object_new (rb_cell_renderer_pixbuf_get_type (), NULL, NULL));
107 }
108
109 static gboolean
110 rb_cell_renderer_pixbuf_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 int mouse_x, mouse_y;
119 RBCellRendererPixbuf *cellpixbuf = (RBCellRendererPixbuf *) cell;
120
121 g_return_val_if_fail (RB_IS_CELL_RENDERER_PIXBUF (cellpixbuf), FALSE);
122
123 if (event == NULL) {
124 return FALSE;
125 }
126 /* only handle mouse events */
127 switch (event->type) {
128 case GDK_BUTTON_PRESS:
129 case GDK_BUTTON_RELEASE:
130 break;
131 default:
132 return FALSE;
133 }
134
135 gdk_window_get_device_position (gtk_widget_get_window (widget),
136 gdk_event_get_source_device (event),
137 &mouse_x,
138 &mouse_y,
139 NULL);
140 gtk_tree_view_convert_widget_to_bin_window_coords (GTK_TREE_VIEW (widget),
141 mouse_x, mouse_y,
142 &mouse_x, &mouse_y);
143
144 /* ensure the user clicks within the good cell */
145 if (mouse_x - cell_area->x >= 0
146 && mouse_x - cell_area->x <= cell_area->width) {
147 g_signal_emit (G_OBJECT (cellpixbuf), rb_cell_renderer_pixbuf_signals [PIXBUF_CLICKED], 0, path);
148 }
149 return TRUE;
150 }