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)
44 Ruler::Ruler (Canvas* c, const Metric& m, Rect const& r)
49 , _divide_height (-1.0)
54 Ruler::Ruler (Item* parent, const Metric& m)
59 , _divide_height (-1.0)
64 Ruler::Ruler (Item* parent, const Metric& m, Rect const& r)
65 : Rectangle (parent, r)
69 , _divide_height (-1.0)
75 Ruler::set_range (double l, double u)
77 begin_visual_change ();
85 Ruler::set_font_description (Pango::FontDescription fd)
87 begin_visual_change ();
88 _font_description = new Pango::FontDescription (fd);
93 Ruler::render (Rect const & area, Cairo::RefPtr<Cairo::Context> cr) const
95 if (_lower == _upper) {
100 Rect self (item_to_window (get()));
101 boost::optional<Rect> i = self.intersection (area);
107 Rect intersection (i.get());
109 Distance height = self.height();
113 _metric->get_marks (marks, _lower, _upper, 50);
117 /* draw background */
119 setup_fill_context (cr);
120 cr->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
123 /* switch to outline context */
125 setup_outline_context (cr);
127 /* draw line on lower edge as a separator */
129 if (_outline_width == 1.0) {
130 /* Cairo single pixel line correction */
131 cr->move_to (self.x0, self.y1-0.5);
132 cr->line_to (self.x1, self.y1-0.5);
134 cr->move_to (self.x0, self.y1);
135 cr->line_to (self.x1, self.y1);
139 /* draw ticks + text */
141 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (cr);
142 if (_font_description) {
143 layout->set_font_description (*_font_description);
146 for (vector<Mark>::const_iterator m = marks.begin(); m != marks.end(); ++m) {
149 pos.x = floor ((m->position - _lower) / _metric->units_per_pixel);
150 pos.y = self.y1; /* bottom edge */
152 if (_outline_width == 1.0) {
153 /* Cairo single pixel line correction */
154 cr->move_to (pos.x + 0.5, pos.y);
156 cr->move_to (pos.x, pos.y);
161 if (_divide_height >= 0) {
162 cr->rel_line_to (0, -_divide_height);
164 cr->rel_line_to (0, -height);
168 cr->rel_line_to (0, -height/3.0);
171 cr->rel_line_to (0, -height/5.0);
178 if (!m->label.empty()) {
179 Pango::Rectangle logical;
181 layout->set_text (m->label);
182 logical = layout->get_pixel_logical_extents ();
184 if (_divide_height >= 0) {
185 cr->move_to (pos.x + 2.0, self.y0 + _divide_height + logical.get_y() + 2.0); /* 2 pixel padding below divider */
187 cr->move_to (pos.x + 2.0, self.y0 + logical.get_y());
189 layout->show_in_cairo_context (cr);
193 if (_divide_height >= 0.0) {
195 cr->set_line_width (1.0);
197 set_source_rgba (cr, _divider_color_top);
198 cr->move_to (self.x0, self.y0 + _divide_height-1.0+0.5);
199 cr->line_to (self.x1, self.y0 + _divide_height-1.0+0.5);
202 set_source_rgba (cr, _divider_color_bottom);
203 cr->move_to (self.x0, self.y0 + _divide_height+0.5);
204 cr->line_to (self.x1, self.y0 + _divide_height+0.5);
214 Ruler::set_divide_height (double h)
220 Ruler::set_divide_colors (Color t, Color b)
222 _divider_color_bottom = b;
223 _divider_color_top = t;
227 Ruler::set_metric (const Metric& m)