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)
43 Ruler::Ruler (Canvas* c, const Metric& m, Rect const& r)
52 Ruler::Ruler (Item* parent, const Metric& m)
61 Ruler::Ruler (Item* parent, const Metric& m, Rect const& r)
62 : Rectangle (parent, r)
71 Ruler::set_range (double l, double u)
73 begin_visual_change ();
81 Ruler::set_font_description (Pango::FontDescription fd)
83 begin_visual_change ();
84 _font_description = new Pango::FontDescription (fd);
89 Ruler::render (Rect const & area, Cairo::RefPtr<Cairo::Context> cr) const
91 if (_lower == _upper) {
96 Rect self (item_to_window (get()));
97 boost::optional<Rect> i = self.intersection (area);
103 Rect intersection (i.get());
105 Distance height = self.height();
109 _metric->get_marks (marks, _lower, _upper, 50);
113 /* draw background */
115 setup_fill_context (cr);
116 cr->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
119 /* switch to outline context */
121 setup_outline_context (cr);
123 /* draw line on lower edge as a separator */
125 if (_outline_width == 1.0) {
126 /* Cairo single pixel line correction */
127 cr->move_to (self.x0, self.y1-0.5);
128 cr->line_to (self.x1, self.y1-0.5);
130 cr->move_to (self.x0, self.y1);
131 cr->line_to (self.x1, self.y1);
135 /* draw ticks + text */
137 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (cr);
138 if (_font_description) {
139 layout->set_font_description (*_font_description);
142 for (vector<Mark>::const_iterator m = marks.begin(); m != marks.end(); ++m) {
145 pos.x = floor ((m->position - _lower) / _metric->units_per_pixel);
146 pos.y = self.y1; /* bottom edge */
148 if (_outline_width == 1.0) {
149 /* Cairo single pixel line correction */
150 cr->move_to (pos.x + 0.5, pos.y);
152 cr->move_to (pos.x, pos.y);
157 cr->rel_line_to (0, -height);
160 cr->rel_line_to (0, -height/2.0);
163 cr->rel_line_to (0, -height/4.0);
170 if (!m->label.empty()) {
171 Pango::Rectangle logical;
173 layout->set_text (m->label);
174 logical = layout->get_pixel_logical_extents ();
176 cr->move_to (pos.x + 2.0, self.y0 + logical.get_y());
177 layout->show_in_cairo_context (cr);
185 Ruler::set_metric (const Metric& m)