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.
30 #include <glib/gi18n.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
,
41 static void rb_cell_renderer_rating_set_property (GObject
*object
,
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
,
49 GdkRectangle
*rectangle
,
54 static void rb_cell_renderer_rating_render (GtkCellRenderer
*cell
,
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
,
65 GdkRectangle
*background_area
,
66 GdkRectangle
*cell_area
,
67 GtkCellRendererState flags
);
68 static void rb_cell_renderer_rating_finalize (GObject
*object
);
70 struct RBCellRendererRatingPrivate
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))
97 static guint rb_cell_renderer_rating_signals
[LAST_SIGNAL
] = { 0 };
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 */
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
),
135 G_STRUCT_OFFSET (RBCellRendererRatingClass
, rated
),
137 rb_marshal_VOID__STRING_DOUBLE
,
143 g_type_class_add_private (class, sizeof (RBCellRendererRatingPrivate
));
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
);
157 rb_cell_renderer_rating_get_property (GObject
*object
,
162 RBCellRendererRating
*cellrating
= RB_CELL_RENDERER_RATING (object
);
166 g_value_set_double (value
, cellrating
->priv
->rating
);
169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
175 rb_cell_renderer_rating_set_property (GObject
*object
,
180 RBCellRendererRating
*cellrating
= RB_CELL_RENDERER_RATING (object
);
184 cellrating
->priv
->rating
= g_value_get_double (value
);
185 if (cellrating
->priv
->rating
< 0)
186 cellrating
->priv
->rating
= 0;
189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
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
203 rb_cell_renderer_rating_new ()
205 return GTK_CELL_RENDERER (gtk_type_new (rb_cell_renderer_rating_get_type ()));
209 rb_cell_renderer_rating_get_size (GtkCellRenderer
*cell
,
211 GdkRectangle
*cell_area
,
218 RBCellRendererRating
*cellrating
= (RBCellRendererRating
*) cell
;
220 gtk_icon_size_lookup (GTK_ICON_SIZE_MENU
, &icon_width
, NULL
);
229 *width
= (gint
) GTK_CELL_RENDERER (cellrating
)->xpad
* 2 + icon_width
* RB_RATING_MAX_SCORE
;
232 *height
= (gint
) GTK_CELL_RENDERER (cellrating
)->ypad
* 2 + icon_width
;
236 rb_cell_renderer_rating_render (GtkCellRenderer
*cell
,
239 GdkRectangle
*background_area
,
240 GdkRectangle
*cell_area
,
241 GdkRectangle
*expose_area
,
242 GtkCellRendererState flags
)
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
,
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
)
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
);
276 rb_cell_renderer_rating_activate (GtkCellRenderer
*cell
,
280 GdkRectangle
*background_area
,
281 GdkRectangle
*cell_area
,
282 GtkCellRendererState flags
)
284 int mouse_x
, mouse_y
;
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
),
298 rating
= rb_rating_get_rating_from_widget (widget
,
299 mouse_x
- cell_area
->x
,
301 cellrating
->priv
->rating
);
303 if (rating
!= -1.0) {
304 g_signal_emit (G_OBJECT (cellrating
),
305 rb_cell_renderer_rating_signals
[RATED
],