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/canvas.h"
31 using namespace ArdourCanvas;
33 Ruler::Ruler (Canvas* c, const Metric& m)
38 , _divide_height (-1.0)
39 , _font_description (0)
44 Ruler::Ruler (Canvas* c, const Metric& m, Rect const& r)
49 , _divide_height (-1.0)
50 , _font_description (0)
55 Ruler::Ruler (Item* parent, const Metric& m)
60 , _divide_height (-1.0)
61 , _font_description (0)
66 Ruler::Ruler (Item* parent, const Metric& m, Rect const& r)
67 : Rectangle (parent, r)
71 , _divide_height (-1.0)
72 , _font_description (0)
78 Ruler::set_range (double l, double u)
80 begin_visual_change ();
88 Ruler::set_font_description (Pango::FontDescription fd)
90 begin_visual_change ();
91 delete _font_description;
92 _font_description = new Pango::FontDescription (fd);
97 Ruler::render (Rect const & area, Cairo::RefPtr<Cairo::Context> cr) const
99 if (_lower == _upper) {
100 /* nothing to draw */
104 Rect self (item_to_window (get()));
105 Rect i = self.intersection (area);
111 Rect intersection (i);
113 Distance height = self.height();
117 _metric->get_marks (marks, _lower, _upper, 50);
121 /* draw background */
123 setup_fill_context (cr);
124 cr->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
127 /* switch to outline context */
129 setup_outline_context (cr);
131 /* draw line on lower edge as a separator */
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);
138 cr->move_to (self.x0, self.y1);
139 cr->line_to (self.x1, self.y1);
143 /* draw ticks + text */
145 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (cr);
146 if (_font_description) {
147 layout->set_font_description (*_font_description);
150 for (vector<Mark>::const_iterator m = marks.begin(); m != marks.end(); ++m) {
153 pos.x = floor ((m->position - _lower) / _metric->units_per_pixel);
154 pos.y = self.y1; /* bottom edge */
156 if (_outline_width == 1.0) {
157 /* Cairo single pixel line correction */
158 cr->move_to (pos.x + 0.5, pos.y);
160 cr->move_to (pos.x, pos.y);
165 if (_divide_height >= 0) {
166 cr->rel_line_to (0, -_divide_height);
168 cr->rel_line_to (0, -height);
172 cr->rel_line_to (0, -height/3.0);
175 cr->rel_line_to (0, -height/5.0);
182 if (!m->label.empty()) {
183 Pango::Rectangle logical;
185 layout->set_text (m->label);
186 logical = layout->get_pixel_logical_extents ();
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 */
191 cr->move_to (pos.x + 2.0, self.y0 + logical.get_y() + .5 * (height - logical.get_height()));
193 layout->show_in_cairo_context (cr);
197 if (_divide_height >= 0.0) {
199 cr->set_line_width (1.0);
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);
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);
218 Ruler::set_divide_height (double h)
224 Ruler::set_divide_colors (Gtkmm2ext::Color t, Gtkmm2ext::Color b)
226 _divider_color_bottom = b;
227 _divider_color_top = t;
231 Ruler::set_metric (const Metric& m)