25
25
#include <gtk/gtkiconfactory.h>
27
27
#include "rb-rating.h"
28
#include "rb-rating-helper.h"
28
29
#include "rb-stock-icons.h"
29
30
#include "rb-cut-and-paste-code.h"
31
32
/* Offset at the beggining of the widget */
34
35
/* Vertical offset */
125
123
widget_class->expose_event = rb_rating_expose;
126
124
widget_class->size_request = rb_rating_size_request;
128
g_object_class_install_property (object_class,
130
g_param_spec_double ("score",
126
rb_rating_install_rating_property (object_class, PROP_RATING);
136
128
rb_rating_signals[RATED] =
137
129
g_signal_new ("rated",
149
141
rb_rating_init (RBRating *rating)
154
143
rating->priv = g_new0 (RBRatingPrivate, 1);
156
145
/* create the needed icons */
157
dummy = gtk_label_new (NULL);
158
pixbuf = gtk_widget_render_icon (dummy,
162
rating->priv->pix_star = eel_create_colorized_pixbuf
164
dummy->style->text[GTK_STATE_NORMAL].red + COLOR_OFFSET,
165
dummy->style->text[GTK_STATE_NORMAL].green + COLOR_OFFSET,
166
dummy->style->text[GTK_STATE_NORMAL].blue + COLOR_OFFSET);
167
g_object_unref (G_OBJECT (pixbuf));
169
pixbuf = gtk_widget_render_icon (dummy,
173
rating->priv->pix_blank = eel_create_colorized_pixbuf
175
dummy->style->text[GTK_STATE_NORMAL].red,
176
dummy->style->text[GTK_STATE_NORMAL].green,
177
dummy->style->text[GTK_STATE_NORMAL].blue);
178
g_object_unref (G_OBJECT (pixbuf));
180
pixbuf = gtk_widget_render_icon (dummy,
184
rating->priv->pix_dot = eel_create_colorized_pixbuf (pixbuf,
185
dummy->style->text[GTK_STATE_NORMAL].red,
186
dummy->style->text[GTK_STATE_NORMAL].green,
187
dummy->style->text[GTK_STATE_NORMAL].blue);
188
g_object_unref (G_OBJECT (pixbuf));
190
gtk_widget_destroy (dummy);
146
rating->priv->pixbufs = rb_rating_pixbufs_new ();
192
148
/* register some signals */
193
149
g_signal_connect_object (G_OBJECT (rating),
202
158
RBRating *rating;
204
160
rating = RB_RATING (object);
206
g_object_unref (G_OBJECT (rating->priv->pix_star));
207
g_object_unref (G_OBJECT (rating->priv->pix_dot));
208
g_object_unref (G_OBJECT (rating->priv->pix_blank));
161
rb_rating_pixbufs_free (rating->priv->pixbufs);
209
162
g_free (rating->priv);
211
164
G_OBJECT_CLASS (parent_class)->finalize (object);
220
173
RBRating *rating = RB_RATING (object);
222
175
switch (param_id) {
224
g_value_set_double (value, rating->priv->score);
177
g_value_set_double (value, rating->priv->rating);
227
180
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
255
208
RBRating *rating;
257
rating = g_object_new (RB_TYPE_RATING, NULL);
210
rating = g_object_new (RB_TYPE_RATING, NULL, NULL);
259
212
g_return_val_if_fail (rating->priv != NULL, NULL);
272
225
gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &icon_size, NULL);
274
requisition->width = 5 * icon_size + X_OFFSET;
227
requisition->width = RB_RATING_MAX_SCORE * icon_size + X_OFFSET;
275
228
requisition->height = icon_size + Y_OFFSET * 2;
280
233
GdkEventExpose *event)
285
237
g_return_val_if_fail (RB_IS_RATING (widget), FALSE);
287
rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
289
239
gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &icon_size, NULL);
291
241
if (GTK_WIDGET_DRAWABLE (widget) == TRUE) {
293
242
RBRating *rating = RB_RATING (widget);
295
244
/* make the widget prettier */
305
254
widget->allocation.width,
306
255
widget->allocation.height);
308
/* draw a blank area at the beggining, this lets the user click
309
* in this area to unset the rating */
310
gdk_pixbuf_render_to_drawable_alpha (rating->priv->pix_blank,
315
GDK_PIXBUF_ALPHA_FULL, 0,
316
GDK_RGB_DITHER_NORMAL, 0, 0);
319
258
/* draw the stars */
320
for (i = 0; i < MAX_SCORE; i++) {
324
if (i < rating->priv->score)
325
pixbuf = rating->priv->pix_star;
327
pixbuf = rating->priv->pix_dot;
333
icon_x_offset = X_OFFSET + (MAX_SCORE-i-1) * icon_size;
335
icon_x_offset = X_OFFSET + i * icon_size;
337
gdk_pixbuf_render_to_drawable_alpha (pixbuf,
340
icon_x_offset, Y_OFFSET,
341
icon_size, icon_size,
342
GDK_PIXBUF_ALPHA_FULL, 0,
343
GDK_RGB_DITHER_NORMAL, 0, 0);
259
return rb_rating_render_stars (widget, widget->window,
260
rating->priv->pixbufs, 0, 0,
262
rating->priv->rating, FALSE);
355
270
GdkEventButton *event,
356
271
RBRating *rating)
358
int mouse_x, mouse_y, icon_size;
273
int mouse_x, mouse_y;
361
276
g_return_val_if_fail (widget != NULL, FALSE);
362
277
g_return_val_if_fail (RB_IS_RATING (rating), FALSE);
364
gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &icon_size, NULL);
365
279
gtk_widget_get_pointer (widget, &mouse_x, &mouse_y);
367
/* ensure the user clicks within the good area */
368
if (mouse_x >= 0 && mouse_x <= widget->allocation.width) {
371
if (mouse_x <= X_OFFSET)
374
score = ((mouse_x - X_OFFSET) / icon_size) + 1;
376
rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
378
score = MAX_SCORE - score + 1;
381
if (score > 5) score = 5;
382
if (score < 0) score = 0;
281
new_rating = rb_rating_get_rating_from_widget (widget, mouse_x,
282
widget->allocation.width,
283
rating->priv->rating);
285
if (new_rating == -1.0) {
384
288
g_signal_emit (G_OBJECT (rating),
385
289
rb_rating_signals[RATED],