2014-06-11 12:24:43 -04:00
|
|
|
/*
|
2019-08-02 22:40:09 -04:00
|
|
|
* Copyright (C) 2012 Carl Hetherington <carl@carlh.net>
|
|
|
|
* Copyright (C) 2014-2017 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2014-06-11 12:24:43 -04:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cairomm/context.h>
|
2014-06-11 23:53:06 -04:00
|
|
|
|
|
|
|
#include <pangomm/layout.h>
|
|
|
|
|
2014-06-11 12:24:43 -04:00
|
|
|
#include "canvas/ruler.h"
|
|
|
|
#include "canvas/types.h"
|
|
|
|
#include "canvas/debug.h"
|
|
|
|
#include "canvas/canvas.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
|
2021-12-24 16:06:53 -05:00
|
|
|
Ruler::Ruler (Canvas* c, const Metric* m)
|
2014-06-12 14:53:44 -04:00
|
|
|
: Rectangle (c)
|
2021-12-24 16:06:53 -05:00
|
|
|
, _metric (m)
|
2014-06-12 10:35:16 -04:00
|
|
|
, _lower (0)
|
|
|
|
, _upper (0)
|
2016-07-25 11:16:32 -04:00
|
|
|
, _divide_height (-1.0)
|
|
|
|
, _font_description (0)
|
2021-01-03 18:49:45 -05:00
|
|
|
, _second_font_description (0)
|
2014-06-12 10:35:16 -04:00
|
|
|
, _need_marks (true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-24 16:06:53 -05:00
|
|
|
Ruler::Ruler (Canvas* c, const Metric* m, Rect const& r)
|
2014-06-12 14:53:44 -04:00
|
|
|
: Rectangle (c, r)
|
2021-12-24 16:06:53 -05:00
|
|
|
, _metric (m)
|
2014-06-12 14:53:44 -04:00
|
|
|
, _lower (0)
|
|
|
|
, _upper (0)
|
2016-07-25 11:16:32 -04:00
|
|
|
, _divide_height (-1.0)
|
|
|
|
, _font_description (0)
|
2021-01-03 18:49:45 -05:00
|
|
|
, _second_font_description (0)
|
2014-06-12 14:53:44 -04:00
|
|
|
, _need_marks (true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-24 16:06:53 -05:00
|
|
|
Ruler::Ruler (Item* parent, const Metric* m)
|
2014-06-21 11:43:42 -04:00
|
|
|
: Rectangle (parent)
|
2021-12-24 16:06:53 -05:00
|
|
|
, _metric (m)
|
2014-06-12 14:53:44 -04:00
|
|
|
, _lower (0)
|
|
|
|
, _upper (0)
|
2016-07-25 11:16:32 -04:00
|
|
|
, _divide_height (-1.0)
|
|
|
|
, _font_description (0)
|
2021-01-03 18:49:45 -05:00
|
|
|
, _second_font_description (0)
|
2014-06-12 14:53:44 -04:00
|
|
|
, _need_marks (true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-24 16:06:53 -05:00
|
|
|
Ruler::Ruler (Item* parent, const Metric* m, Rect const& r)
|
2014-06-21 11:43:42 -04:00
|
|
|
: Rectangle (parent, r)
|
2021-12-24 16:06:53 -05:00
|
|
|
, _metric (m)
|
2014-06-11 23:53:06 -04:00
|
|
|
, _lower (0)
|
|
|
|
, _upper (0)
|
2016-07-25 11:16:32 -04:00
|
|
|
, _divide_height (-1.0)
|
|
|
|
, _font_description (0)
|
2021-01-03 18:49:45 -05:00
|
|
|
, _second_font_description (0)
|
2014-06-11 23:53:06 -04:00
|
|
|
, _need_marks (true)
|
2014-06-11 12:24:43 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-11-16 13:19:22 -05:00
|
|
|
Ruler::set_range (int64_t l, int64_t u)
|
2014-06-11 12:24:43 -04:00
|
|
|
{
|
2014-06-11 23:53:06 -04:00
|
|
|
begin_visual_change ();
|
2014-06-11 12:24:43 -04:00
|
|
|
_lower = l;
|
|
|
|
_upper = u;
|
2014-06-11 23:53:06 -04:00
|
|
|
_need_marks = true;
|
|
|
|
end_visual_change ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Ruler::set_font_description (Pango::FontDescription fd)
|
|
|
|
{
|
|
|
|
begin_visual_change ();
|
2016-07-25 11:16:32 -04:00
|
|
|
delete _font_description;
|
2014-06-11 23:53:06 -04:00
|
|
|
_font_description = new Pango::FontDescription (fd);
|
|
|
|
end_visual_change ();
|
2014-06-11 12:24:43 -04:00
|
|
|
}
|
|
|
|
|
2021-01-03 18:49:45 -05:00
|
|
|
void
|
|
|
|
Ruler::set_second_font_description (Pango::FontDescription fd)
|
|
|
|
{
|
|
|
|
begin_visual_change ();
|
|
|
|
delete _second_font_description;
|
|
|
|
_second_font_description = new Pango::FontDescription (fd);
|
|
|
|
end_visual_change ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-11 12:24:43 -04:00
|
|
|
void
|
|
|
|
Ruler::render (Rect const & area, Cairo::RefPtr<Cairo::Context> cr) const
|
|
|
|
{
|
2014-06-11 23:53:06 -04:00
|
|
|
if (_lower == _upper) {
|
|
|
|
/* nothing to draw */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-12 10:35:16 -04:00
|
|
|
Rect self (item_to_window (get()));
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect i = self.intersection (area);
|
2014-06-11 23:53:06 -04:00
|
|
|
|
2014-06-11 12:24:43 -04:00
|
|
|
if (!i) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect intersection (i);
|
2014-06-11 12:24:43 -04:00
|
|
|
|
|
|
|
Distance height = self.height();
|
|
|
|
|
2021-12-24 16:06:53 -05:00
|
|
|
if (_need_marks && _metric) {
|
2014-06-11 23:53:06 -04:00
|
|
|
marks.clear ();
|
2014-09-05 14:42:06 -04:00
|
|
|
_metric->get_marks (marks, _lower, _upper, 50);
|
2014-06-11 23:53:06 -04:00
|
|
|
_need_marks = false;
|
|
|
|
}
|
2014-06-11 12:24:43 -04:00
|
|
|
|
|
|
|
/* draw background */
|
|
|
|
|
|
|
|
setup_fill_context (cr);
|
|
|
|
cr->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
|
|
|
|
cr->fill ();
|
|
|
|
|
2014-06-11 23:53:06 -04:00
|
|
|
/* switch to outline context */
|
|
|
|
|
2014-06-11 12:24:43 -04:00
|
|
|
setup_outline_context (cr);
|
|
|
|
|
2014-06-11 23:53:06 -04:00
|
|
|
/* draw line on lower edge as a separator */
|
2014-06-12 10:35:16 -04:00
|
|
|
|
|
|
|
if (_outline_width == 1.0) {
|
|
|
|
/* Cairo single pixel line correction */
|
2023-07-17 22:54:33 -04:00
|
|
|
cr->move_to (self.x0, self.y1+0.5);
|
|
|
|
cr->line_to (self.x1, self.y1+0.5);
|
2014-06-12 10:35:16 -04:00
|
|
|
} else {
|
|
|
|
cr->move_to (self.x0, self.y1);
|
|
|
|
cr->line_to (self.x1, self.y1);
|
|
|
|
}
|
2014-06-11 23:53:06 -04:00
|
|
|
cr->stroke ();
|
|
|
|
|
2021-12-24 16:06:53 -05:00
|
|
|
if (_metric) {
|
|
|
|
/* draw ticks + text */
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2021-12-24 16:06:53 -05:00
|
|
|
Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (cr);
|
2021-01-03 18:49:45 -05:00
|
|
|
|
2021-12-24 16:06:53 -05:00
|
|
|
Pango::FontDescription* last_font_description = 0;
|
2022-10-31 18:57:04 -04:00
|
|
|
Coord prev = -1;
|
2014-06-11 23:53:06 -04:00
|
|
|
|
2021-12-24 16:06:53 -05:00
|
|
|
for (vector<Mark>::const_iterator m = marks.begin(); m != marks.end(); ++m) {
|
|
|
|
Duple pos;
|
2022-11-29 11:43:26 -05:00
|
|
|
Pango::FontDescription* fd = (m->style == Mark::Major) ? (_second_font_description ? _second_font_description : _font_description) : _font_description;
|
2014-06-11 12:24:43 -04:00
|
|
|
|
2023-08-26 12:07:03 -04:00
|
|
|
pos.x = round ((m->position - _lower) / _metric->units_per_pixel);
|
2021-12-24 16:06:53 -05:00
|
|
|
pos.y = self.y1; /* bottom edge */
|
2014-06-11 23:53:06 -04:00
|
|
|
|
2022-10-31 18:57:04 -04:00
|
|
|
if (fd != last_font_description) {
|
|
|
|
layout->set_font_description (*fd);
|
|
|
|
last_font_description = fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and the text */
|
|
|
|
|
|
|
|
if (!m->label.empty()) {
|
|
|
|
Pango::Rectangle logical;
|
|
|
|
|
|
|
|
layout->set_text (m->label);
|
|
|
|
logical = layout->get_pixel_logical_extents ();
|
|
|
|
|
|
|
|
if ((prev >= 0.) && ((pos.x - prev) < (6. + logical.get_width()))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_divide_height >= 0) {
|
|
|
|
cr->move_to (pos.x + 2.0, self.y0 + _divide_height + logical.get_y() + 2.0); /* 2 pixel padding below divider */
|
|
|
|
} else {
|
|
|
|
cr->move_to (pos.x + 2.0, self.y0 + logical.get_y() + .5 * (height - logical.get_height()));
|
|
|
|
}
|
|
|
|
|
|
|
|
layout->show_in_cairo_context (cr);
|
|
|
|
prev = pos.x;
|
|
|
|
}
|
|
|
|
|
2021-12-24 16:06:53 -05:00
|
|
|
if (_outline_width == 1.0) {
|
|
|
|
/* Cairo single pixel line correction */
|
|
|
|
cr->move_to (pos.x + 0.5, pos.y);
|
|
|
|
} else {
|
|
|
|
cr->move_to (pos.x, pos.y);
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2021-12-24 16:06:53 -05:00
|
|
|
switch (m->style) {
|
2016-12-19 07:23:40 -05:00
|
|
|
case Mark::Major:
|
|
|
|
if (_divide_height >= 0) {
|
|
|
|
cr->rel_line_to (0, -_divide_height);
|
|
|
|
} else {
|
|
|
|
cr->rel_line_to (0, -height);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Mark::Minor:
|
|
|
|
cr->rel_line_to (0, -height/3.0);
|
|
|
|
break;
|
|
|
|
case Mark::Micro:
|
|
|
|
cr->rel_line_to (0, -height/5.0);
|
|
|
|
break;
|
2021-12-24 16:06:53 -05:00
|
|
|
}
|
2021-01-03 18:49:45 -05:00
|
|
|
|
2022-10-31 18:57:04 -04:00
|
|
|
cr->stroke ();
|
2014-06-11 23:53:06 -04:00
|
|
|
}
|
2014-06-11 12:24:43 -04:00
|
|
|
}
|
2022-10-31 18:57:04 -04:00
|
|
|
|
2016-12-19 07:23:40 -05:00
|
|
|
if (_divide_height >= 0.0) {
|
2015-10-04 14:51:05 -04:00
|
|
|
|
2016-12-19 07:23:40 -05:00
|
|
|
cr->set_line_width (1.0);
|
2014-11-24 07:29:37 -05:00
|
|
|
|
2017-07-17 12:34:35 -04:00
|
|
|
Gtkmm2ext::set_source_rgba (cr, _divider_color_top);
|
2016-12-19 07:23:40 -05:00
|
|
|
cr->move_to (self.x0, self.y0 + _divide_height-1.0+0.5);
|
|
|
|
cr->line_to (self.x1, self.y0 + _divide_height-1.0+0.5);
|
|
|
|
cr->stroke ();
|
2014-11-24 07:29:37 -05:00
|
|
|
|
2017-07-17 12:34:35 -04:00
|
|
|
Gtkmm2ext::set_source_rgba (cr, _divider_color_bottom);
|
2016-12-19 07:23:40 -05:00
|
|
|
cr->move_to (self.x0, self.y0 + _divide_height+0.5);
|
|
|
|
cr->line_to (self.x1, self.y0 + _divide_height+0.5);
|
|
|
|
cr->stroke ();
|
2014-11-24 07:29:37 -05:00
|
|
|
|
|
|
|
|
2016-12-19 07:23:40 -05:00
|
|
|
}
|
2014-11-24 07:29:37 -05:00
|
|
|
|
2021-02-11 00:27:27 -05:00
|
|
|
render_children (area, cr);
|
|
|
|
|
2014-06-11 12:24:43 -04:00
|
|
|
/* done! */
|
|
|
|
}
|
2014-09-05 14:45:27 -04:00
|
|
|
|
2014-11-24 07:29:37 -05:00
|
|
|
void
|
|
|
|
Ruler::set_divide_height (double h)
|
|
|
|
{
|
|
|
|
_divide_height = h;
|
|
|
|
}
|
|
|
|
|
2014-11-24 07:40:16 -05:00
|
|
|
void
|
2017-07-17 12:34:35 -04:00
|
|
|
Ruler::set_divide_colors (Gtkmm2ext::Color t, Gtkmm2ext::Color b)
|
2014-11-24 07:40:16 -05:00
|
|
|
{
|
|
|
|
_divider_color_bottom = b;
|
|
|
|
_divider_color_top = t;
|
|
|
|
}
|
|
|
|
|
2014-09-05 14:45:27 -04:00
|
|
|
void
|
2021-12-24 17:34:18 -05:00
|
|
|
Ruler::set_metric (const Metric* m)
|
2014-09-05 14:45:27 -04:00
|
|
|
{
|
2021-12-24 17:34:18 -05:00
|
|
|
_metric = m;
|
2014-09-05 14:45:27 -04:00
|
|
|
_need_marks = true;
|
|
|
|
redraw ();
|
|
|
|
}
|