2 Copyright (C) 2016 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <cairomm/context.h>
20 #include <cairomm/surface.h>
21 #include <cairomm/region.h>
22 #include <pangomm/layout.h>
24 #include "canvas/text.h"
25 #include "canvas/rectangle.h"
26 #include "canvas/colors.h"
32 using namespace ARDOUR;
36 using namespace ArdourSurface;
37 using namespace ArdourCanvas;
42 Push2Menu::Push2Menu (Item* parent, vector<string> s)
52 Pango::FontDescription fd ("Sans 10");
55 Push2Canvas* p2c = dynamic_cast<Push2Canvas*> (canvas());
56 Glib::RefPtr<Pango::Layout> throwaway = Pango::Layout::create (p2c->image_context());
57 throwaway->set_font_description (fd);
58 throwaway->set_text (X_("Hg")); /* ascender + descender) */
60 throwaway->get_pixel_size (w, h);
64 active_bg = new Rectangle (this);
66 for (vector<string>::iterator si = s.begin(); si != s.end(); ++si) {
67 Text* t = new Text (this);
68 t->set_font_description (fd);
70 displays.push_back (t);
76 Push2Menu::set_layout (int c, int r)
86 Push2Menu::rearrange (uint32_t initial_display)
88 if (initial_display >= displays.size()) {
92 vector<Text*>::iterator i = displays.begin();
96 for (uint32_t n = 0; n < initial_display; ++n) {
101 uint32_t index = initial_display;
104 bool active_shown = false;
106 while (i != displays.end()) {
108 Coord x = col * Push2Canvas::inter_button_spacing();
109 Coord y = 2 + (row * baseline);
111 (*i)->set_position (Duple (x, y));
113 if (index == _active) {
114 active_bg->set (Rect (x - 1, y - 1,
115 x - 1 + Push2Canvas::inter_button_spacing(), y - 1 + baseline));
125 if (++row >= nrows) {
127 if (++col >= ncols) {
128 /* no more to display */
135 while (i != displays.end()) {
144 first = initial_display;
146 Rearranged (); /* EMIT SIGNAL */
150 Push2Menu::scroll (Direction dir, bool page)
156 set_active (displays.size() - 1);
159 set_active (_active - 1);
164 if (_active == displays.size() - 1) {
169 set_active (_active + 1);
175 set_active (max (0, (int) (first - (nrows * ncols))));
177 if (_active / nrows == 0) {
178 /* in the first column, go to last column, same row */
180 set_active (displays.size() - 1 - active_row ());
183 /* move to same row, previous column */
184 set_active (_active - nrows);
191 set_active (min ((uint32_t) displays.size(), first + (nrows * ncols)));
193 if (_active / nrows == ncols) {
194 /* in the last column, go to same row in first column */
196 set_active (active_row());
199 /* move to same row, next column */
200 set_active (_active + nrows);
208 Push2Menu::render (Rect const& area, Cairo::RefPtr<Cairo::Context> context) const
210 render_children (area, context);
214 Push2Menu::set_active (uint32_t index)
216 if (!parent() || (index == _active)) {
220 if (index >= displays.size()) {
225 /* set text color for old active item, and the new one */
227 if (_active <= displays.size()) {
228 displays[_active]->set_color (text_color);
231 displays[index]->set_color (contrast_color);
233 Duple p = displays[index]->position ();
235 active_bg->set (Rect (p.x - 1, p.y - 1, p.x - 1 + Push2Canvas::inter_button_spacing(), p.y - 1 + baseline ));
239 if (_active < first) {
241 /* we jumped before current visible range : try to put its column first
244 rearrange (active_top());
246 } else if (_active > last) {
248 /* we jumped after current visible range : try putting its
252 rearrange (active_top() - ((ncols - 1) * nrows));
255 ActiveChanged (); /* EMIT SIGNAL */
259 Push2Menu::set_text_color (Color c)
263 for (vector<Text*>::iterator t = displays.begin(); t != displays.end(); ++t) {
270 Push2Menu::set_active_color (Color c)
273 contrast_color = contrasting_text_color (active_color);
275 active_bg->set_fill_color (c);
278 if (_active < displays.size()) {
279 displays[_active]->set_color (contrast_color);
284 Push2Menu::set_font_description (Pango::FontDescription fd)
286 font_description = fd;
288 for (vector<Text*>::iterator t = displays.begin(); t != displays.end(); ++t) {
289 (*t)->set_font_description (fd);