Initial commit
[fillmore] / src / marina / Ruler.vala
1 /* Copyright 2009 Yorba Foundation
2  *
3  * This software is licensed under the GNU Lesser General Public License
4  * (version 2.1 or later).  See the COPYING file in this distribution. 
5  */
6
7 namespace View {
8 public class Ruler : Gtk.DrawingArea {
9     weak Model.TimeSystem provider;
10     const int BORDER = 4;
11
12     public signal void position_changed(int x);
13
14     public Ruler(Model.TimeSystem provider, int height) {
15         this.provider = provider;
16         set_flags(Gtk.WidgetFlags.NO_WINDOW);
17         set_size_request(0, height);
18     }
19
20     public override bool expose_event(Gdk.EventExpose event) {
21         int x = event.area.x;
22         int frame = provider.get_start_token(x);
23
24         Cairo.Context context = Gdk.cairo_create(window);
25
26         Gdk.cairo_set_source_color(context, parse_color("#777"));
27         context.rectangle(event.area.x, event.area.y, event.area.width, event.area.height);
28         context.fill();
29
30         Cairo.Antialias old_antialias = context.get_antialias();
31
32         context.set_antialias(Cairo.Antialias.NONE);
33         context.set_source_rgb(1.0, 1.0, 1.0);
34         int stop = event.area.x + event.area.width;
35         Pango.FontDescription f = Pango.FontDescription.from_string("Sans 9");
36         while (x <= stop) {
37             x = provider.frame_to_xsize(frame);
38             int y = provider.get_pixel_height(frame);
39
40             context.move_to(x + BORDER, 0);
41             context.line_to(x + BORDER, y);
42
43             string? display_string = provider.get_display_string(frame);
44             if (display_string != null) {
45                 Pango.Layout layout = create_pango_layout(display_string);
46
47                 int w;
48                 int h;
49                 layout.set_font_description(f);
50                 layout.get_pixel_size (out w, out h);
51                 int text_pos = x - (w / 2) + BORDER;
52                 if (text_pos < 0) {
53                     text_pos = 0;
54                 }
55
56                 Gdk.draw_layout(window, style.white_gc, text_pos, 7, layout);
57             }
58
59             frame = provider.get_next_position(frame);
60         }
61         context.set_antialias(old_antialias);
62         context.set_line_width(1.0);
63         context.stroke();
64         return true;
65     }
66
67     public override bool button_press_event(Gdk.EventButton event) {
68         position_changed((int) event.x);
69         return false;
70     }
71
72     public override bool motion_notify_event(Gdk.EventMotion event) {
73         if ((event.state & Gdk.ModifierType.BUTTON1_MASK) != 0) {
74             queue_draw();
75             position_changed((int) event.x);
76         }
77         return false;
78     }
79 }
80 }