1 /* This file is part of Cinaest.
3 * Copyright (C) 2010 Philipp Zabel
5 * Cinaest is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * Cinaest is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Cinaest. If not, see <http://www.gnu.org/licenses/>.
21 public class RatingWidget : DrawingArea {
23 private bool button_pressed;
24 private Gdk.Color active_color;
25 private Gdk.Color disabled_color;
27 public RatingWidget () {
28 var style = Gtk.rc_get_style_by_paths (this.get_settings (), "GtkButton", null, typeof (Gtk.Button));
29 if (!style.lookup_color ("ActiveTextColor", out active_color))
30 Gdk.Color.parse ("white", out active_color);
31 if (!style.lookup_color ("DisabledTextColor", out disabled_color))
32 Gdk.Color.parse ("black", out disabled_color);
34 add_events (Gdk.EventMask.BUTTON_PRESS_MASK
35 | Gdk.EventMask.BUTTON_RELEASE_MASK
36 | Gdk.EventMask.POINTER_MOTION_MASK);
38 set_size_request (640, 70);
41 public override bool expose_event (Gdk.EventExpose event) {
42 var cr = Gdk.cairo_create (this.window);
43 cr.rectangle (event.area.x, event.area.y, event.area.width, event.area.height);
46 for (int i = 0; i < 10; i++) {
47 draw_star (cr, i, (i >= rating) ? disabled_color : active_color);
53 private void draw_star (Cairo.Context cr, int n, Gdk.Color color) {
54 var radius = double.min (this.allocation.width / 20,
55 this.allocation.height / 2);
56 var x = radius * (2 * n + 1);
57 var y = this.allocation.height / 2;
59 cr.set_source_rgb (color.red / 65535.0, color.green / 65535.0, color.blue / 65535.0);
61 for (int i = 0; i <= 10; i++) {
62 double r = radius * (5 + 8 * (i % 2)) / 13.0;
63 double a = x + r * Math.cos (i * 2*Math.PI/10.0 + Math.PI/2.0);
64 double b = y + r * Math.sin (i * 2*Math.PI/10.0 + Math.PI/2.0);
74 public override bool button_press_event (Gdk.EventButton event) {
75 button_pressed = true;
76 rate (10 * event.x / this.allocation.width);
81 public override bool button_release_event (Gdk.EventButton event) {
82 button_pressed = false;
87 public override bool motion_notify_event (Gdk.EventMotion event) {
89 rate (10 * event.x / this.allocation.width);
94 private void rate (double r) {
102 unowned Gdk.Region region = this.window.get_clip_region ();
103 // redraw the cairo canvas completely by exposing it
104 this.window.invalidate_region (region, true);
105 this.window.process_updates (true);
108 public int get_rating () {