udapted vi.po
[rhythmbox.git] / widgets / rb-cell-renderer-rating.c
blobdfdab8ecf02141f8fd628b71fd57c00c10ccd09d
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * rb-cell-renderer-rating.c
4 * arch-tag: Implementation of star rating GtkTreeView cell renderer
6 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
7 * Copyright (C) 2002 Olivier Martin <oleevye@wanadoo.fr>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301 USA.
26 #include <config.h>
28 #include <stdlib.h>
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
33 #include "rb-cell-renderer-rating.h"
34 #include "rb-marshal.h"
35 #include "rb-rating-helper.h"
37 static void rb_cell_renderer_rating_get_property (GObject *object,
38 guint param_id,
39 GValue *value,
40 GParamSpec *pspec);
41 static void rb_cell_renderer_rating_set_property (GObject *object,
42 guint param_id,
43 const GValue *value,
44 GParamSpec *pspec);
45 static void rb_cell_renderer_rating_init (RBCellRendererRating *celltext);
46 static void rb_cell_renderer_rating_class_init (RBCellRendererRatingClass *class);
47 static void rb_cell_renderer_rating_get_size (GtkCellRenderer *cell,
48 GtkWidget *widget,
49 GdkRectangle *rectangle,
50 gint *x_offset,
51 gint *y_offset,
52 gint *width,
53 gint *height);
54 static void rb_cell_renderer_rating_render (GtkCellRenderer *cell,
55 GdkWindow *window,
56 GtkWidget *widget,
57 GdkRectangle *background_area,
58 GdkRectangle *cell_area,
59 GdkRectangle *expose_area,
60 GtkCellRendererState flags);
61 static gboolean rb_cell_renderer_rating_activate (GtkCellRenderer *cell,
62 GdkEvent *event,
63 GtkWidget *widget,
64 const gchar *path,
65 GdkRectangle *background_area,
66 GdkRectangle *cell_area,
67 GtkCellRendererState flags);
68 static void rb_cell_renderer_rating_finalize (GObject *object);
70 struct RBCellRendererRatingPrivate
72 double rating;
75 struct RBCellRendererRatingClassPrivate
77 RBRatingPixbufs *pixbufs;
80 G_DEFINE_TYPE (RBCellRendererRating, rb_cell_renderer_rating, GTK_TYPE_CELL_RENDERER)
81 #define RB_CELL_RENDERER_RATING_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
82 RB_TYPE_CELL_RENDERER_RATING, \
83 RBCellRendererRatingPrivate))
85 enum
87 PROP_0,
88 PROP_RATING
91 enum
93 RATED,
94 LAST_SIGNAL
97 static guint rb_cell_renderer_rating_signals[LAST_SIGNAL] = { 0 };
99 static void
100 rb_cell_renderer_rating_init (RBCellRendererRating *cellrating)
103 cellrating->priv = RB_CELL_RENDERER_RATING_GET_PRIVATE (cellrating);
105 /* set the renderer able to be activated */
106 GTK_CELL_RENDERER (cellrating)->mode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
108 /* create the needed icons */
111 static void
112 rb_cell_renderer_rating_class_init (RBCellRendererRatingClass *class)
114 GObjectClass *object_class = G_OBJECT_CLASS (class);
115 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);
117 object_class->finalize = rb_cell_renderer_rating_finalize;
119 object_class->get_property = rb_cell_renderer_rating_get_property;
120 object_class->set_property = rb_cell_renderer_rating_set_property;
122 cell_class->get_size = rb_cell_renderer_rating_get_size;
123 cell_class->render = rb_cell_renderer_rating_render;
124 cell_class->activate = rb_cell_renderer_rating_activate;
126 class->priv = g_new0 (RBCellRendererRatingClassPrivate, 1);
127 class->priv->pixbufs = rb_rating_pixbufs_new ();
129 rb_rating_install_rating_property (object_class, PROP_RATING);
131 rb_cell_renderer_rating_signals[RATED] =
132 g_signal_new ("rated",
133 G_OBJECT_CLASS_TYPE (object_class),
134 G_SIGNAL_RUN_LAST,
135 G_STRUCT_OFFSET (RBCellRendererRatingClass, rated),
136 NULL, NULL,
137 rb_marshal_VOID__STRING_DOUBLE,
138 G_TYPE_NONE,
140 G_TYPE_STRING,
141 G_TYPE_DOUBLE);
143 g_type_class_add_private (class, sizeof (RBCellRendererRatingPrivate));
146 static void
147 rb_cell_renderer_rating_finalize (GObject *object)
149 RBCellRendererRating *cellrating;
151 cellrating = RB_CELL_RENDERER_RATING (object);
153 G_OBJECT_CLASS (rb_cell_renderer_rating_parent_class)->finalize (object);
156 static void
157 rb_cell_renderer_rating_get_property (GObject *object,
158 guint param_id,
159 GValue *value,
160 GParamSpec *pspec)
162 RBCellRendererRating *cellrating = RB_CELL_RENDERER_RATING (object);
164 switch (param_id) {
165 case PROP_RATING:
166 g_value_set_double (value, cellrating->priv->rating);
167 break;
168 default:
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
170 break;
174 static void
175 rb_cell_renderer_rating_set_property (GObject *object,
176 guint param_id,
177 const GValue *value,
178 GParamSpec *pspec)
180 RBCellRendererRating *cellrating= RB_CELL_RENDERER_RATING (object);
182 switch (param_id) {
183 case PROP_RATING:
184 cellrating->priv->rating = g_value_get_double (value);
185 if (cellrating->priv->rating < 0)
186 cellrating->priv->rating = 0;
187 break;
188 default:
189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
190 break;
195 * rb_cell_renderer_rating_new: create a cell renderer that will
196 * display some pixbufs for representing the rating of a song.
197 * It is also able to update the rating.
199 * Return value: the new cell renderer
202 GtkCellRenderer *
203 rb_cell_renderer_rating_new ()
205 return GTK_CELL_RENDERER (gtk_type_new (rb_cell_renderer_rating_get_type ()));
208 static void
209 rb_cell_renderer_rating_get_size (GtkCellRenderer *cell,
210 GtkWidget *widget,
211 GdkRectangle *cell_area,
212 gint *x_offset,
213 gint *y_offset,
214 gint *width,
215 gint *height)
217 int icon_width;
218 RBCellRendererRating *cellrating = (RBCellRendererRating *) cell;
220 gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &icon_width, NULL);
222 if (x_offset)
223 *x_offset = 0;
225 if (y_offset)
226 *y_offset = 0;
228 if (width)
229 *width = (gint) GTK_CELL_RENDERER (cellrating)->xpad * 2 + icon_width * RB_RATING_MAX_SCORE;
231 if (height)
232 *height = (gint) GTK_CELL_RENDERER (cellrating)->ypad * 2 + icon_width;
235 static void
236 rb_cell_renderer_rating_render (GtkCellRenderer *cell,
237 GdkWindow *window,
238 GtkWidget *widget,
239 GdkRectangle *background_area,
240 GdkRectangle *cell_area,
241 GdkRectangle *expose_area,
242 GtkCellRendererState flags)
245 gboolean selected;
246 GdkRectangle pix_rect, draw_rect;
247 RBCellRendererRating *cellrating = (RBCellRendererRating *) cell;
248 RBCellRendererRatingClass *cell_class;
250 cellrating = RB_CELL_RENDERER_RATING (cell);
251 cell_class = RB_CELL_RENDERER_RATING_GET_CLASS (cellrating);
252 rb_cell_renderer_rating_get_size (cell, widget, cell_area,
253 &pix_rect.x,
254 &pix_rect.y,
255 &pix_rect.width,
256 &pix_rect.height);
258 pix_rect.x += cell_area->x;
259 pix_rect.y += cell_area->y;
260 pix_rect.width -= cell->xpad * 2;
261 pix_rect.height -= cell->ypad * 2;
263 if (gdk_rectangle_intersect (cell_area, &pix_rect, &draw_rect) == FALSE)
264 return;
266 selected = (flags & GTK_CELL_RENDERER_SELECTED);
268 rb_rating_render_stars (widget, window, cell_class->priv->pixbufs,
269 draw_rect.x - pix_rect.x,
270 draw_rect.y - pix_rect.y,
271 draw_rect.x, draw_rect.y,
272 cellrating->priv->rating, selected);
275 static gboolean
276 rb_cell_renderer_rating_activate (GtkCellRenderer *cell,
277 GdkEvent *event,
278 GtkWidget *widget,
279 const gchar *path,
280 GdkRectangle *background_area,
281 GdkRectangle *cell_area,
282 GtkCellRendererState flags)
284 int mouse_x, mouse_y;
285 double rating;
287 RBCellRendererRating *cellrating = (RBCellRendererRating *) cell;
289 g_return_val_if_fail (RB_IS_CELL_RENDERER_RATING (cellrating), FALSE);
291 gtk_widget_get_pointer (widget, &mouse_x, &mouse_y);
292 gtk_tree_view_widget_to_tree_coords (GTK_TREE_VIEW (widget),
293 mouse_x,
294 mouse_y,
295 &mouse_x,
296 &mouse_y);
298 rating = rb_rating_get_rating_from_widget (widget,
299 mouse_x - cell_area->x,
300 cell_area->width,
301 cellrating->priv->rating);
303 if (rating != -1.0) {
304 g_signal_emit (G_OBJECT (cellrating),
305 rb_cell_renderer_rating_signals[RATED],
306 0, path, rating);
309 return TRUE;