2 Copyright (C) 2014 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 #include <cairomm/context.h>
23 #include <pangomm/layout.h>
25 #include "canvas/ruler.h"
26 #include "canvas/types.h"
27 #include "canvas/debug.h"
28 #include "canvas/utils.h"
29 #include "canvas/canvas.h"
32 using namespace ArdourCanvas;
34 Ruler::Ruler (Canvas* c, const Metric& m)
39 , _divide_height (-1.0)
40 , _font_description (0)
45 Ruler::Ruler (Canvas* c, const Metric& m, Rect const& r)
50 , _divide_height (-1.0)
51 , _font_description (0)
56 Ruler::Ruler (Item* parent, const Metric& m)
61 , _divide_height (-1.0)
62 , _font_description (0)
67 Ruler::Ruler (Item* parent, const Metric& m, Rect const& r)
68 : Rectangle (parent, r)
72 , _divide_height (-1.0)
73 , _font_description (0)
79 Ruler::set_range (double l, double u)
81 begin_visual_change ();
89 Ruler::set_font_description (Pango::FontDescription fd)
91 begin_visual_change ();
92 delete _font_description;
93 _font_description = new Pango::FontDescription (fd);
98 Ruler::render (Rect const & area, Cairo::RefPtr<Cairo::Context> cr) const
100 if (_lower == _upper) {
101 /* nothing to draw */
105 Rect self (item_to_window (get()));
106 boost::optional<Rect> i = self.intersection (area);
112 Rect intersection (i.get());
114 Distance height = self.height();
118 _metric->get_marks (marks, _lower, _upper, 50);
122 /* draw background */
124 setup_fill_context (cr);
125 cr->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
128 /* switch to outline context */
130 setup_outline_context (cr);
132 /* draw line on lower edge as a separator */
134 if (_outline_width == 1.0) {
135 /* Cairo single pixel line correction */
136 cr->move_to (self.x0, self.y1-0.5);
137 cr->line_to (self.x1, self.y1-0.5);
139 cr->move_to (self.x0, self.y1);
140 cr->line_to (self.x1, self.y1);
144 /* draw ticks + text */
146 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (cr);
147 if (_font_description) {
148 layout->set_font_description (*_font_description);
151 for (vector<Mark>::const_iterator m = marks.begin(); m != marks.end(); ++m) {
154 pos.x = floor ((m->position - _lower) / _metric->units_per_pixel);
155 pos.y = self.y1; /* bottom edge */
157 if (_outline_width == 1.0) {
158 /* Cairo single pixel line correction */
159 cr->move_to (pos.x + 0.5, pos.y);
161 cr->move_to (pos.x, pos.y);
166 if (_divide_height >= 0) {
167 cr->rel_line_to (0, -_divide_height);
169 cr->rel_line_to (0, -height);
173 cr->rel_line_to (0, -height/3.0);
176 cr->rel_line_to (0, -height/5.0);
183 if (!m->label.empty()) {
184 Pango::Rectangle logical;
186 layout->set_text (m->label);
187 logical = layout->get_pixel_logical_extents ();
189 if (_divide_height >= 0) {
190 cr->move_to (pos.x + 2.0, self.y0 + _divide_height + logical.get_y() + 2.0); /* 2 pixel padding below divider */
192 cr->move_to (pos.x + 2.0, self.y0 + logical.get_y());
194 layout->show_in_cairo_context (cr);
198 if (_divide_height >= 0.0) {
200 cr->set_line_width (1.0);
202 set_source_rgba (cr, _divider_color_top);
203 cr->move_to (self.x0, self.y0 + _divide_height-1.0+0.5);
204 cr->line_to (self.x1, self.y0 + _divide_height-1.0+0.5);
207 set_source_rgba (cr, _divider_color_bottom);
208 cr->move_to (self.x0, self.y0 + _divide_height+0.5);
209 cr->line_to (self.x1, self.y0 + _divide_height+0.5);
219 Ruler::set_divide_height (double h)
225 Ruler::set_divide_colors (Color t, Color b)
227 _divider_color_bottom = b;
228 _divider_color_top = t;
232 Ruler::set_metric (const Metric& m)