Only show user-presets in favorite sidebar
[ardour.git] / libs / canvas / ruler.cc
1 /*
2     Copyright (C) 2014 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program 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 2 of the License, or
8     (at your option) any later version.
9
10     This program 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.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <algorithm>
21 #include <cairomm/context.h>
22
23 #include <pangomm/layout.h>
24
25 #include "canvas/ruler.h"
26 #include "canvas/types.h"
27 #include "canvas/debug.h"
28 #include "canvas/canvas.h"
29
30 using namespace std;
31 using namespace ArdourCanvas;
32
33 Ruler::Ruler (Canvas* c, const Metric& m)
34         : Rectangle (c)
35         , _metric (&m)
36         , _lower (0)
37         , _upper (0)
38         , _divide_height (-1.0)
39         , _font_description (0)
40         , _need_marks (true)
41 {
42 }
43
44 Ruler::Ruler (Canvas* c, const Metric& m, Rect const& r)
45         : Rectangle (c, r)
46         , _metric (&m)
47         , _lower (0)
48         , _upper (0)
49         , _divide_height (-1.0)
50         , _font_description (0)
51         , _need_marks (true)
52 {
53 }
54
55 Ruler::Ruler (Item* parent, const Metric& m)
56         : Rectangle (parent)
57         , _metric (&m)
58         , _lower (0)
59         , _upper (0)
60         , _divide_height (-1.0)
61         , _font_description (0)
62         , _need_marks (true)
63 {
64 }
65
66 Ruler::Ruler (Item* parent, const Metric& m, Rect const& r)
67         : Rectangle (parent, r)
68         , _metric (&m)
69         , _lower (0)
70         , _upper (0)
71         , _divide_height (-1.0)
72         , _font_description (0)
73         , _need_marks (true)
74 {
75 }
76
77 void
78 Ruler::set_range (double l, double u)
79 {
80         begin_visual_change ();
81         _lower = l;
82         _upper = u;
83         _need_marks = true;
84         end_visual_change ();
85 }
86
87 void
88 Ruler::set_font_description (Pango::FontDescription fd)
89 {
90         begin_visual_change ();
91         delete _font_description;
92         _font_description = new Pango::FontDescription (fd);
93         end_visual_change ();
94 }
95
96 void
97 Ruler::render (Rect const & area, Cairo::RefPtr<Cairo::Context> cr) const
98 {
99         if (_lower == _upper) {
100                 /* nothing to draw */
101                 return;
102         }
103
104         Rect self (item_to_window (get()));
105         Rect i = self.intersection (area);
106
107         if (!i) {
108                 return;
109         }
110
111         Rect intersection (i);
112
113         Distance height = self.height();
114
115         if (_need_marks) {
116                 marks.clear ();
117                 _metric->get_marks (marks, _lower, _upper, 50);
118                 _need_marks = false;
119         }
120
121         /* draw background */
122
123         setup_fill_context (cr);
124         cr->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
125         cr->fill ();
126
127         /* switch to outline context */
128
129         setup_outline_context (cr);
130
131         /* draw line on lower edge as a separator */
132
133         if (_outline_width == 1.0) {
134                 /* Cairo single pixel line correction */
135                 cr->move_to (self.x0, self.y1-0.5);
136                 cr->line_to (self.x1, self.y1-0.5);
137         } else {
138                 cr->move_to (self.x0, self.y1);
139                 cr->line_to (self.x1, self.y1);
140         }
141         cr->stroke ();
142
143         /* draw ticks + text */
144
145         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (cr);
146         if (_font_description) {
147                 layout->set_font_description (*_font_description);
148         }
149
150         for (vector<Mark>::const_iterator m = marks.begin(); m != marks.end(); ++m) {
151                 Duple pos;
152
153                 pos.x = floor ((m->position - _lower) / _metric->units_per_pixel);
154                 pos.y = self.y1; /* bottom edge */
155
156                 if (_outline_width == 1.0) {
157                         /* Cairo single pixel line correction */
158                         cr->move_to (pos.x + 0.5, pos.y);
159                 } else {
160                         cr->move_to (pos.x, pos.y);
161                 }
162
163                 switch (m->style) {
164                         case Mark::Major:
165                                 if (_divide_height >= 0) {
166                                         cr->rel_line_to (0, -_divide_height);
167                                 } else {
168                                         cr->rel_line_to (0, -height);
169                                 }
170                                 break;
171                         case Mark::Minor:
172                                 cr->rel_line_to (0, -height/3.0);
173                                 break;
174                         case Mark::Micro:
175                                 cr->rel_line_to (0, -height/5.0);
176                                 break;
177                 }
178                 cr->stroke ();
179
180                 /* and the text */
181
182                 if (!m->label.empty()) {
183                         Pango::Rectangle logical;
184
185                         layout->set_text (m->label);
186                         logical = layout->get_pixel_logical_extents ();
187
188                         if (_divide_height >= 0) {
189                                 cr->move_to (pos.x + 2.0, self.y0 + _divide_height + logical.get_y() + 2.0); /* 2 pixel padding below divider */
190                         } else {
191                                 cr->move_to (pos.x + 2.0, self.y0 + logical.get_y() + .5 * (height - logical.get_height()));
192                         }
193                         layout->show_in_cairo_context (cr);
194                 }
195         }
196
197         if (_divide_height >= 0.0) {
198
199                 cr->set_line_width (1.0);
200
201                 Gtkmm2ext::set_source_rgba (cr, _divider_color_top);
202                 cr->move_to (self.x0, self.y0 + _divide_height-1.0+0.5);
203                 cr->line_to (self.x1, self.y0 + _divide_height-1.0+0.5);
204                 cr->stroke ();
205
206                 Gtkmm2ext::set_source_rgba (cr, _divider_color_bottom);
207                 cr->move_to (self.x0, self.y0 + _divide_height+0.5);
208                 cr->line_to (self.x1, self.y0 + _divide_height+0.5);
209                 cr->stroke ();
210
211
212         }
213
214         /* done! */
215 }
216
217 void
218 Ruler::set_divide_height (double h)
219 {
220         _divide_height = h;
221 }
222
223 void
224 Ruler::set_divide_colors (Gtkmm2ext::Color t, Gtkmm2ext::Color b)
225 {
226         _divider_color_bottom = b;
227         _divider_color_top = t;
228 }
229
230 void
231 Ruler::set_metric (const Metric& m)
232 {
233         _metric = &m;
234         _need_marks = true;
235         redraw ();
236 }