13
0
livetrax/libs/surfaces/push2/menu.cc

298 lines
5.9 KiB
C++
Raw Normal View History

/*
Copyright (C) 2016 Paul Davis
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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <cairomm/context.h>
#include <cairomm/surface.h>
#include <cairomm/region.h>
#include <pangomm/layout.h>
#include "pbd/i18n.h"
#include "canvas/text.h"
#include "canvas/types.h"
#include "canvas/rectangle.h"
#include "canvas/colors.h"
#include "canvas.h"
#include "gui.h"
#include "push2.h"
#include "menu.h"
#ifdef __APPLE__
#define Rect ArdourCanvas::Rect
#endif
using namespace ARDOUR;
using namespace std;
using namespace PBD;
using namespace Glib;
using namespace ArdourSurface;
using namespace ArdourCanvas;
2016-09-21 16:25:01 -04:00
Push2Menu::Push2Menu (Item* parent, vector<string> s)
: Container (parent)
, baseline (-1)
2016-09-21 16:25:01 -04:00
, ncols (0)
, nrows (0)
, wrap (true)
, first (0)
, last (0)
2016-09-23 15:34:17 -04:00
, _active (0)
{
2016-09-21 16:25:01 -04:00
Pango::FontDescription fd ("Sans 10");
if (baseline < 0) {
Push2Canvas* p2c = dynamic_cast<Push2Canvas*> (canvas());
Glib::RefPtr<Pango::Layout> throwaway = Pango::Layout::create (p2c->image_context());
2016-09-21 16:25:01 -04:00
throwaway->set_font_description (fd);
throwaway->set_text (X_("Hg")); /* ascender + descender) */
int h, w;
throwaway->get_pixel_size (w, h);
baseline = h;
}
active_bg = new ArdourCanvas::Rectangle (this);
2016-09-21 16:25:01 -04:00
for (vector<string>::iterator si = s.begin(); si != s.end(); ++si) {
Text* t = new Text (this);
2016-09-21 16:25:01 -04:00
t->set_font_description (fd);
t->set (*si);
displays.push_back (t);
}
2016-09-21 16:25:01 -04:00
}
2016-09-21 16:25:01 -04:00
void
Push2Menu::set_layout (int c, int r)
{
ncols = c;
nrows = r;
2016-09-21 16:25:01 -04:00
set_active (_active);
rearrange (_active);
}
void
2016-09-21 16:25:01 -04:00
Push2Menu::rearrange (uint32_t initial_display)
{
2016-09-21 16:25:01 -04:00
if (initial_display >= displays.size()) {
return;
}
2016-09-21 16:25:01 -04:00
vector<Text*>::iterator i = displays.begin();
2016-09-21 16:25:01 -04:00
/* move to first */
for (uint32_t n = 0; n < initial_display; ++n) {
(*i)->hide ();
++i;
}
2016-09-21 16:25:01 -04:00
uint32_t index = initial_display;
uint32_t col = 0;
uint32_t row = 0;
bool active_shown = false;
2016-09-21 16:25:01 -04:00
while (i != displays.end()) {
2016-09-21 16:25:01 -04:00
Coord x = col * Push2Canvas::inter_button_spacing();
Coord y = 2 + (row * baseline);
2016-09-21 16:25:01 -04:00
(*i)->set_position (Duple (x, y));
2016-09-21 16:25:01 -04:00
if (index == _active) {
active_bg->set (Rect (x - 1, y - 1,
x - 1 + Push2Canvas::inter_button_spacing(), y - 1 + baseline));
active_bg->show ();
active_shown = true;
}
2016-09-21 16:25:01 -04:00
(*i)->show ();
last = index;
++i;
++index;
2016-09-21 16:25:01 -04:00
if (++row >= nrows) {
row = 0;
if (++col >= ncols) {
/* no more to display */
break;
}
}
2016-09-21 16:25:01 -04:00
}
2016-09-21 16:25:01 -04:00
while (i != displays.end()) {
(*i)->hide ();
++i;
}
if (!active_shown) {
active_bg->hide ();
}
2016-09-21 16:25:01 -04:00
first = initial_display;
Rearranged (); /* EMIT SIGNAL */
}
void
2016-09-21 16:25:01 -04:00
Push2Menu::scroll (Direction dir, bool page)
{
2016-09-21 16:25:01 -04:00
switch (dir) {
case DirectionUp:
if (_active == 0) {
if (wrap) {
set_active (displays.size() - 1);
}
} else {
set_active (_active - 1);
}
break;
case DirectionDown:
if (_active == displays.size() - 1) {
if (wrap) {
set_active (0);
}
} else {
set_active (_active + 1);
}
break;
case DirectionLeft:
if (page) {
set_active (max (0, (int) (first - (nrows * ncols))));
} else {
if (_active / nrows == 0) {
/* in the first column, go to last column, same row */
if (wrap) {
set_active (displays.size() - 1 - active_row ());
}
} else {
/* move to same row, previous column */
set_active (_active - nrows);
}
}
break;
case DirectionRight:
if (page) {
set_active (min ((uint32_t) displays.size(), first + (nrows * ncols)));
} else {
if (_active / nrows == ncols) {
/* in the last column, go to same row in first column */
if (wrap) {
set_active (active_row());
}
} else {
/* move to same row, next column */
set_active (_active + nrows);
}
}
break;
}
2016-09-21 16:25:01 -04:00
}
2016-09-21 16:25:01 -04:00
void
Push2Menu::render (Rect const& area, Cairo::RefPtr<Cairo::Context> context) const
{
render_children (area, context);
}
void
2016-09-21 16:25:01 -04:00
Push2Menu::set_active (uint32_t index)
{
2016-09-23 15:34:17 -04:00
if (!parent() || (index == _active)) {
return;
}
2016-09-21 16:25:01 -04:00
if (index >= displays.size()) {
active_bg->hide ();
return;
}
2016-09-21 16:25:01 -04:00
/* set text color for old active item, and the new one */
if (_active < displays.size()) {
2016-09-21 16:25:01 -04:00
displays[_active]->set_color (text_color);
}
2016-09-21 16:25:01 -04:00
displays[index]->set_color (contrast_color);
2016-09-21 16:25:01 -04:00
Duple p = displays[index]->position ();
2016-09-21 16:25:01 -04:00
active_bg->set (Rect (p.x - 1, p.y - 1, p.x - 1 + Push2Canvas::inter_button_spacing(), p.y - 1 + baseline ));
active_bg->show ();
_active = index;
2016-09-21 16:25:01 -04:00
if (_active < first) {
2016-09-21 16:25:01 -04:00
/* we jumped before current visible range : try to put its column first
*/
2016-09-21 16:25:01 -04:00
rearrange (active_top());
2016-09-21 16:25:01 -04:00
} else if (_active > last) {
/* we jumped after current visible range : try putting its
* column last
*/
rearrange (active_top() - ((ncols - 1) * nrows));
}
2016-09-21 16:25:01 -04:00
ActiveChanged (); /* EMIT SIGNAL */
}
2016-09-21 16:25:01 -04:00
void
Push2Menu::set_text_color (Color c)
{
text_color = c;
for (vector<Text*>::iterator t = displays.begin(); t != displays.end(); ++t) {
(*t)->set_color (c);
}
}
2016-09-21 16:25:01 -04:00
void
Push2Menu::set_active_color (Color c)
{
2016-09-21 16:25:01 -04:00
active_color = c;
contrast_color = contrasting_text_color (active_color);
if (active_bg) {
active_bg->set_fill_color (c);
}
2016-09-21 16:25:01 -04:00
if (_active < displays.size()) {
displays[_active]->set_color (contrast_color);
}
}
void
2016-09-21 16:25:01 -04:00
Push2Menu::set_font_description (Pango::FontDescription fd)
{
2016-09-21 16:25:01 -04:00
font_description = fd;
for (vector<Text*>::iterator t = displays.begin(); t != displays.end(); ++t) {
(*t)->set_font_description (fd);
}
}