2 Copyright (C) 2000-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.
23 #include "fix_carbon.h"
25 #include "pbd/gstdio_compat.h"
27 #include "gtkmm2ext/cell_renderer_color_selector.h"
28 #include "gtkmm2ext/utils.h"
30 #include "pbd/compose.h"
31 #include "pbd/file_utils.h"
32 #include "pbd/replace_all.h"
34 #include "ardour/filesystem_paths.h"
35 #include "ardour/profile.h"
37 #include "canvas/container.h"
38 #include "canvas/rectangle.h"
39 #include "canvas/scroll_group.h"
40 #include "canvas/wave_view.h"
42 #include "ardour_button.h"
43 #include "ardour_dialog.h"
44 #include "color_theme_manager.h"
45 #include "rgb_macros.h"
46 #include "ui_config.h"
54 using namespace ARDOUR;
55 using namespace ARDOUR_UI_UTILS;
57 ColorThemeManager::ColorThemeManager ()
58 : reset_button (_("Restore Defaults"))
59 , palette_viewport (*palette_scroller.get_hadjustment(), *palette_scroller.get_vadjustment())
62 , color_theme_label (_("Color Theme"))
64 std::map<string,string> color_themes;
66 get_color_themes (color_themes);
69 if (color_themes.size() > 1) {
70 theme_list = TreeStore::create (color_theme_columns);
72 TreeModel::iterator selected_iter = theme_list->children().end();
74 for (std::map<string,string>::iterator c = color_themes.begin(); c != color_themes.end(); ++c) {
77 row = *(theme_list->append());
78 row[color_theme_columns.name] = c->first;
80 string color_file_name = c->second;
82 row[color_theme_columns.path] = color_file_name;
84 /* match second (path; really basename) since that is
85 what we store/restore.
88 if (UIConfiguration::instance().get_color_file() == color_file_name) {
93 color_theme_dropdown.set_model (theme_list);
94 color_theme_dropdown.pack_start (color_theme_columns.name);
96 if (selected_iter != theme_list->children().end()) {
97 color_theme_dropdown.set_active (selected_iter);
100 Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox());
101 Gtk::Alignment* align = Gtk::manage (new Gtk::Alignment);
103 align->add (color_theme_dropdown);
104 hbox->set_spacing (6);
105 hbox->pack_start (color_theme_label, false, false);
106 hbox->pack_start (*align, true, true);
108 table.attach (*hbox, 0, 3, n, n + 1);
112 reset_button.signal_clicked().connect (sigc::mem_fun (*this, &ColorThemeManager::reset_canvas_colors));
114 /* Now the alias list */
116 alias_list = TreeStore::create (alias_columns);
117 alias_display.set_model (alias_list);
118 alias_display.append_column (_("Object"), alias_columns.name);
120 Gtkmm2ext::CellRendererColorSelector* color_renderer = manage (new Gtkmm2ext::CellRendererColorSelector);
121 TreeViewColumn* color_column = manage (new TreeViewColumn (_("Color"), *color_renderer));
122 color_column->add_attribute (color_renderer->property_color(), alias_columns.color);
123 alias_display.append_column (*color_column);
125 alias_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
126 alias_display.get_column (0)->set_expand (true);
127 alias_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
128 alias_display.get_column (1)->set_expand (false);
129 alias_display.set_reorderable (false);
130 alias_display.set_headers_visible (true);
132 alias_display.signal_button_press_event().connect (sigc::mem_fun (*this, &ColorThemeManager::alias_button_press_event), false);
134 alias_scroller.add (alias_display);
136 palette_group = initialize_palette_canvas (*palette_viewport.canvas());
137 palette_viewport.signal_size_allocate().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::palette_canvas_allocated), palette_group, palette_viewport.canvas(),
138 sigc::mem_fun (*this, &ColorThemeManager::palette_event)));
139 palette_scroller.add (palette_viewport);
141 modifier_scroller.add (modifier_vbox);
143 notebook.append_page (alias_scroller, _("Items"));
144 notebook.append_page (palette_scroller, _("Palette"));
145 notebook.append_page (modifier_scroller, _("Transparency"));
147 notebook.set_size_request (400, 400);
149 table.attach (notebook, 0, 3, n, n + 1);
151 table.attach (reset_button, 0, 3, n, n + 1);
153 color_dialog.get_colorsel()->set_has_opacity_control (true);
154 color_dialog.get_colorsel()->set_has_palette (true);
155 color_dialog.get_ok_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
156 color_dialog.get_cancel_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
158 color_theme_dropdown.signal_changed().connect (sigc::mem_fun (*this, &ColorThemeManager::on_color_theme_changed));
160 /* no need to call setup_palette() here, it will be done when its size is allocated */
164 UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ColorThemeManager::colors_changed));
168 ColorThemeManager::~ColorThemeManager ()
171 palette_group->clear (true);
172 delete palette_group;
178 ColorThemeManager::setup_modifiers ()
180 UIConfiguration* uic (&UIConfiguration::instance());
181 UIConfiguration::Modifiers& modifiers (uic->modifiers);
183 Gtk::Label* mod_label;
184 Gtk::HScale* mod_scale;
186 Gtkmm2ext::container_clear (modifier_vbox);
188 for (UIConfiguration::Modifiers::const_iterator m = modifiers.begin(); m != modifiers.end(); ++m) {
189 mod_hbox = manage (new HBox);
191 mod_scale = manage (new HScale (0.0, 1.0, 0.01));
192 mod_scale->set_draw_value (false);
193 mod_scale->set_value (m->second.a());
194 mod_scale->set_update_policy (Gtk::UPDATE_DISCONTINUOUS);
195 mod_scale->signal_value_changed().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::modifier_edited), mod_scale, m->first));
197 mod_label = manage (new Label (m->first));
198 mod_label->set_alignment (1.0, 0.5);
199 mod_label->set_size_request (150, -1); /* 150 pixels should be enough for anyone */
201 mod_hbox->pack_start (*mod_label, false, true, 12);
202 mod_hbox->pack_start (*mod_scale, true, true);
204 modifier_vbox.pack_start (*mod_hbox, false, false);
207 modifier_vbox.show_all ();
212 ColorThemeManager::modifier_edited (Gtk::Range* range, string name)
214 using namespace ArdourCanvas;
216 double alpha = range->get_value();
217 SVAModifier svam (SVAModifier::Assign, -1.0, -1.0, alpha);
218 UIConfiguration::instance().set_modifier (name, svam);
222 ColorThemeManager::colors_changed ()
230 ColorThemeManager::reset_canvas_colors()
235 /* look for a versioned user-owned color file, and try to rename it */
237 basename = UIConfiguration::instance().color_file_name (true, true);
239 if (find_file (ardour_config_search_path(), basename, cfile)) {
240 string backup = cfile + string (X_(".old"));
241 g_rename (cfile.c_str(), backup.c_str());
242 /* don't really care if it fails */
245 UIConfiguration::instance().load_color_theme (false);
246 UIConfiguration::instance().save_state ();
249 ArdourCanvas::Container*
250 ColorThemeManager::initialize_palette_canvas (ArdourCanvas::Canvas& canvas)
252 using namespace ArdourCanvas;
254 /* hide background */
255 canvas.set_background_color (rgba_to_color (0.0, 0.0, 1.0, 0.0));
257 /* bi-directional scroll group */
259 ScrollGroup* scroll_group = new ScrollGroup (canvas.root(), ScrollGroup::ScrollSensitivity (ScrollGroup::ScrollsVertically|ScrollGroup::ScrollsHorizontally));
260 canvas.add_scroller (*scroll_group);
262 /* new container to hold everything */
264 return new ArdourCanvas::Container (scroll_group);
268 ColorThemeManager::palette_canvas_allocated (Gtk::Allocation& alloc, ArdourCanvas::Container* group, ArdourCanvas::Canvas* canvas, sigc::slot<bool,GdkEvent*,std::string> event_handler)
270 build_palette_canvas (*canvas, *group, event_handler);
275 ArdourCanvas::HSV color;
276 NamedColor (string s, ArdourCanvas::HSV c) : name (s), color (c) {}
280 bool operator() (NamedColor const & a, NamedColor const & b) {
281 using namespace ArdourCanvas;
282 const HSV black (0, 0, 0);
283 if (a.color.is_gray() || b.color.is_gray()) {
284 return black.distance (a.color) < black.distance (b.color);
286 return a.color.h < b.color.h;
287 // const HSV red (rgba_to_color (1.0, 0.0, 0.0, 1.0));
288 // return red.distance (a.color) < red.distance (b.color);
295 ColorThemeManager::build_palette_canvas (ArdourCanvas::Canvas& canvas, ArdourCanvas::Container& group, sigc::slot<bool,GdkEvent*,std::string> event_handler)
297 using namespace ArdourCanvas;
299 /* we want the colors sorted by hue, with their name */
301 UIConfiguration::Colors& colors (UIConfiguration::instance().colors);
302 vector<NamedColor> nc;
303 for (UIConfiguration::Colors::const_iterator x = colors.begin(); x != colors.end(); ++x) {
304 nc.push_back (NamedColor (x->first, HSV (x->second)));
307 sort (nc.begin(), nc.end(), sorter);
309 const uint32_t color_limit = nc.size();
310 const double box_size = 20.0;
311 const double width = canvas.width();
312 const double height = canvas.height();
314 uint32_t color_num = 0;
316 /* clear existing rects and delete them */
320 for (uint32_t y = 0; y < height - box_size && color_num < color_limit; y += box_size) {
321 for (uint32_t x = 0; x < width - box_size && color_num < color_limit; x += box_size) {
322 ArdourCanvas::Rectangle* r = new ArdourCanvas::Rectangle (&group, ArdourCanvas::Rect (x, y, x + box_size, y + box_size));
323 string name = nc[color_num++].name;
325 UIConfiguration::Colors::iterator c = colors.find (name);
327 if (c != colors.end()) {
328 Color color = c->second;
329 r->set_fill_color (color);
330 r->set_outline_color (rgba_to_color (0.0, 0.0, 0.0, 1.0));
331 r->set_tooltip (name);
332 r->Event.connect (sigc::bind (event_handler, name));
339 ColorThemeManager::palette_size_request (Gtk::Requisition* req)
341 uint32_t ncolors = UIConfiguration::instance().colors.size();
342 const int box_size = 20;
344 double c = sqrt ((double)ncolors);
345 req->width = (int) floor (c * box_size);
346 req->height = (int) floor (c * box_size);
348 /* add overflow row if necessary */
350 if (fmod (ncolors, c) != 0.0) {
351 req->height += box_size;
356 ColorThemeManager::setup_palette ()
358 build_palette_canvas (*palette_viewport.canvas(), *palette_group, sigc::mem_fun (*this, &ColorThemeManager::palette_event));
362 ColorThemeManager::palette_event (GdkEvent* ev, string name)
365 case GDK_BUTTON_RELEASE:
366 edit_palette_color (name);
375 ColorThemeManager::edit_palette_color (std::string name)
377 using namespace ArdourCanvas;
379 UIConfiguration* uic (&UIConfiguration::instance());
380 ArdourCanvas::Color c = uic->color (name);
383 color_to_rgba (c, r, g, b, a);
385 gdkcolor.set_rgb_p (r, g, b);
386 color_dialog.get_colorsel()->set_previous_color (gdkcolor);
387 color_dialog.get_colorsel()->set_current_color (gdkcolor);
388 color_dialog.get_colorsel()->set_previous_alpha ((guint16) (a * 65535));
389 color_dialog.get_colorsel()->set_current_alpha ((guint16) (a * 65535));
391 color_dialog_connection.disconnect ();
392 color_dialog_connection = color_dialog.signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::palette_color_response), name));
393 color_dialog.present();
397 ColorThemeManager::palette_color_response (int result, std::string name)
399 using namespace ArdourCanvas;
401 color_dialog_connection.disconnect ();
403 UIConfiguration* uic (&UIConfiguration::instance());
408 case RESPONSE_ACCEPT:
410 gdkcolor = color_dialog.get_colorsel()->get_current_color();
411 a = color_dialog.get_colorsel()->get_current_alpha() / 65535.0;
412 r = gdkcolor.get_red_p();
413 g = gdkcolor.get_green_p();
414 b = gdkcolor.get_blue_p();
416 uic->set_color (name, rgba_to_color (r, g, b, a));
423 color_dialog.hide ();
427 ColorThemeManager::alias_palette_event (GdkEvent* ev, string new_alias, string target_name)
430 case GDK_BUTTON_RELEASE:
431 UIConfiguration::instance().set_alias (target_name, new_alias);
441 ColorThemeManager::alias_palette_response (int response, std::string target_name, std::string old_alias)
444 case GTK_RESPONSE_OK:
445 case GTK_RESPONSE_ACCEPT:
446 /* rebuild alias list with new color: inefficient but simple */
450 case GTK_RESPONSE_REJECT:
452 UIConfiguration::instance().set_alias (target_name, old_alias);
460 palette_window->hide ();
464 ColorThemeManager::choose_color_from_palette (string const & name)
466 UIConfiguration* uic (&UIConfiguration::instance());
467 UIConfiguration::ColorAliases::iterator i = uic->color_aliases.find (name);
469 if (i == uic->color_aliases.end()) {
473 delete palette_window;
475 palette_window = new ArdourDialog (_("Color Palette"));
476 palette_window->add_button (Stock::CANCEL, RESPONSE_REJECT); /* using CANCEL causes confusion if dialog is closed via CloseAllDialogs */
477 palette_window->add_button (Stock::OK, RESPONSE_OK);
479 ArdourCanvas::GtkCanvas* canvas = new ArdourCanvas::GtkCanvas ();
480 ArdourCanvas::Container* group = initialize_palette_canvas (*canvas);
482 canvas->signal_size_request().connect (sigc::mem_fun (*this, &ColorThemeManager::palette_size_request));
483 canvas->signal_size_allocate().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::palette_canvas_allocated), group, canvas,
484 sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::alias_palette_event), name)));
486 palette_window->get_vbox()->pack_start (*canvas);
487 palette_window->show_all ();
489 palette_response_connection = palette_window->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::alias_palette_response), name, i->second));
491 palette_window->set_position (WIN_POS_MOUSE);
492 palette_window->present ();
496 ColorThemeManager::setup_aliases ()
498 using namespace ArdourCanvas;
500 UIConfiguration* uic (&UIConfiguration::instance());
501 UIConfiguration::ColorAliases& aliases (uic->color_aliases);
503 alias_list->clear ();
505 for (UIConfiguration::ColorAliases::iterator i = aliases.begin(); i != aliases.end(); ++i) {
506 TreeModel::Children rows = alias_list->children();
508 string::size_type colon;
510 if ((colon = i->first.find (':')) != string::npos) {
512 /* this is supposed to be a child node, so find the
516 string parent = i->first.substr (0, colon);
517 TreeModel::iterator ri;
519 for (ri = rows.begin(); ri != rows.end(); ++ri) {
520 string s = (*ri)[alias_columns.name];
526 if (ri == rows.end()) {
527 /* not found, add the parent as new top level row */
528 row = *(alias_list->append());
529 row[alias_columns.name] = parent;
530 row[alias_columns.alias] = "";
532 /* now add the child as a child of this one */
534 row = *(alias_list->insert (row->children().end()));
535 row[alias_columns.name] = i->first.substr (colon+1);
537 row = *(alias_list->insert ((*ri)->children().end()));
538 row[alias_columns.name] = i->first.substr (colon+1);
543 row = *(alias_list->append());
544 row[alias_columns.name] = i->first;
545 row[alias_columns.key] = i->first;
548 row[alias_columns.key] = i->first;
549 row[alias_columns.alias] = i->second;
553 Color c (uic->color (i->second));
554 color_to_rgba (c, r, g, b, a);
555 col.set_rgb_p (r, g, b);
557 row[alias_columns.color] = col;
562 ColorThemeManager::alias_button_press_event (GdkEventButton* ev)
565 TreeModel::Path path;
566 TreeViewColumn* column;
570 if (!alias_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
574 guint32 colnum = GPOINTER_TO_UINT (column->get_data (X_("colnum")));
578 /* allow normal processing to occur */
582 if ((iter = alias_list->get_iter (path))) {
583 string target_color_alias = (*iter)[alias_columns.key];
584 if (!target_color_alias.empty()) {
585 choose_color_from_palette (target_color_alias);
595 ColorThemeManager::parameter_changed (string const&)
600 ColorThemeManager::set_state_from_config ()
606 ColorThemeManager::add_to_page (OptionEditorPage* p)
608 int const n = p->table.property_n_rows();
610 if (!_note.empty ()) {
613 p->table.resize (m, 3);
614 p->table.attach (box, 1, 3, n, n + 1, FILL | EXPAND, SHRINK, 0, 0);
615 maybe_add_note (p, n + 1);
619 ColorThemeManager::tip_widget()
621 return reset_button; /* XXX need a better widget for this purpose */
625 ColorThemeManager::on_color_theme_changed ()
627 Gtk::TreeModel::iterator iter = color_theme_dropdown.get_active();
630 Gtk::TreeModel::Row row = *iter;
633 string new_theme = row[color_theme_columns.path];
634 UIConfiguration::instance().set_color_file (new_theme);