use new action map API instead of ActionManager::get_action
[ardour.git] / gtk2_ardour / color_theme_manager.cc
1 /*
2     Copyright (C) 2000-2016 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cmath>
21 #include <errno.h>
22
23 #include <gtkmm/stock.h>
24
25 #include "fix_carbon.h"
26
27 #include "pbd/gstdio_compat.h"
28
29 #include "pbd/compose.h"
30 #include "pbd/file_utils.h"
31 #include "pbd/replace_all.h"
32
33 #include "ardour/filesystem_paths.h"
34 #include "ardour/profile.h"
35
36 #include "gtkmm2ext/cell_renderer_color_selector.h"
37 #include "gtkmm2ext/utils.h"
38
39 #include "canvas/container.h"
40 #include "canvas/rectangle.h"
41 #include "canvas/scroll_group.h"
42
43 #include "waveview/wave_view.h"
44
45 #include "ardour_dialog.h"
46 #include "color_theme_manager.h"
47 #include "rgb_macros.h"
48 #include "ui_config.h"
49 #include "utils.h"
50
51 #include "pbd/i18n.h"
52
53 using namespace std;
54 using namespace Gtk;
55 using namespace PBD;
56 using namespace ARDOUR;
57 using namespace Gtkmm2ext;
58 using namespace ARDOUR_UI_UTILS;
59
60 ColorThemeManager::ColorThemeManager ()
61         : reset_button (_("Restore Defaults"))
62         , palette_viewport (*palette_scroller.get_hadjustment(), *palette_scroller.get_vadjustment())
63         , palette_group (0)
64         , palette_window (0)
65         , color_theme_label (_("Color Theme"))
66 {
67         std::map<string,string> color_themes;
68
69         get_color_themes (color_themes);
70         int n = 0;
71
72         if (color_themes.size() > 1) {
73                 theme_list = TreeStore::create (color_theme_columns);
74
75                 TreeModel::iterator selected_iter = theme_list->children().end();
76
77                 for (std::map<string,string>::iterator c = color_themes.begin(); c != color_themes.end(); ++c) {
78                         TreeModel::Row row;
79
80                         row = *(theme_list->append());
81                         row[color_theme_columns.name] = c->first;
82
83                         string color_file_name = c->second;
84
85                         row[color_theme_columns.path] = color_file_name;
86
87                         /* match second (path; really basename) since that is
88                            what we store/restore.
89                         */
90
91                         if (UIConfiguration::instance().get_color_file() == color_file_name) {
92                                 selected_iter = row;
93                         }
94                 }
95
96                 color_theme_dropdown.set_model (theme_list);
97                 color_theme_dropdown.pack_start (color_theme_columns.name);
98
99                 if (selected_iter != theme_list->children().end()) {
100                         color_theme_dropdown.set_active (selected_iter);
101                 }
102
103                 Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox());
104                 Gtk::Alignment* align = Gtk::manage (new Gtk::Alignment);
105                 align->set (0, 0.5);
106                 align->add (color_theme_dropdown);
107                 hbox->set_spacing (6);
108                 hbox->pack_start (color_theme_label, false, false);
109                 hbox->pack_start (*align, true, true);
110                 hbox->show_all ();
111                 table.attach (*hbox, 0, 3, n, n + 1);
112                 ++n;
113         }
114
115         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &ColorThemeManager::reset_canvas_colors));
116
117         /* Now the alias list */
118
119         alias_list = TreeStore::create (alias_columns);
120         alias_display.set_model (alias_list);
121         alias_display.append_column (_("Object"), alias_columns.name);
122
123         Gtkmm2ext::CellRendererColorSelector* color_renderer = manage (new Gtkmm2ext::CellRendererColorSelector);
124         TreeViewColumn* color_column = manage (new TreeViewColumn (_("Color"), *color_renderer));
125         color_column->add_attribute (color_renderer->property_color(), alias_columns.color);
126         alias_display.append_column (*color_column);
127
128         alias_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
129         alias_display.get_column (0)->set_expand (true);
130         alias_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
131         alias_display.get_column (1)->set_expand (false);
132         alias_display.set_reorderable (false);
133         alias_display.set_headers_visible (true);
134
135         alias_display.signal_button_press_event().connect (sigc::mem_fun (*this, &ColorThemeManager::alias_button_press_event), false);
136
137         alias_scroller.add (alias_display);
138
139         palette_group = initialize_palette_canvas (*palette_viewport.canvas());
140         palette_viewport.signal_size_allocate().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::palette_canvas_allocated), palette_group, palette_viewport.canvas(),
141                                                                      sigc::mem_fun (*this, &ColorThemeManager::palette_event)));
142         palette_scroller.add (palette_viewport);
143
144         modifier_scroller.add (modifier_vbox);
145
146         notebook.append_page (alias_scroller, _("Items"));
147         notebook.append_page (palette_scroller, _("Palette"));
148         notebook.append_page (modifier_scroller, _("Transparency"));
149
150         notebook.set_size_request (400, 400);
151
152         table.attach (notebook, 0, 3, n, n + 1);
153         ++n;
154         table.attach (reset_button, 0, 3, n, n + 1);
155
156         color_dialog.get_colorsel()->set_has_opacity_control (true);
157         color_dialog.get_colorsel()->set_has_palette (true);
158         color_dialog.get_ok_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
159         color_dialog.get_cancel_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
160
161         color_theme_dropdown.signal_changed().connect (sigc::mem_fun (*this, &ColorThemeManager::on_color_theme_changed));
162
163         /* no need to call setup_palette() here, it will be done when its size is allocated */
164         setup_aliases ();
165         setup_modifiers ();
166
167         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ColorThemeManager::colors_changed));
168 }
169
170
171 ColorThemeManager::~ColorThemeManager ()
172 {
173         if (palette_group) { 
174                 palette_group->clear (true);  
175                 delete palette_group;
176         }
177 }
178
179
180 void
181 ColorThemeManager::setup_modifiers ()
182 {
183         UIConfiguration* uic (&UIConfiguration::instance());
184         UIConfiguration::Modifiers& modifiers (uic->modifiers);
185         Gtk::HBox* mod_hbox;
186         Gtk::Label* mod_label;
187         Gtk::HScale* mod_scale;
188
189         Gtkmm2ext::container_clear (modifier_vbox);
190
191         for (UIConfiguration::Modifiers::const_iterator m = modifiers.begin(); m != modifiers.end(); ++m) {
192                 mod_hbox = manage (new HBox);
193
194                 mod_scale = manage (new HScale (0.0, 1.0, 0.01));
195                 mod_scale->set_draw_value (false);
196                 mod_scale->set_value (m->second.a());
197                 mod_scale->set_update_policy (Gtk::UPDATE_DISCONTINUOUS);
198                 mod_scale->signal_value_changed().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::modifier_edited), mod_scale, m->first));
199
200                 mod_label = manage (new Label (m->first));
201                 mod_label->set_alignment (1.0, 0.5);
202                 mod_label->set_size_request (150, -1); /* 150 pixels should be enough for anyone */
203
204                 mod_hbox->pack_start (*mod_label, false, true, 12);
205                 mod_hbox->pack_start (*mod_scale, true, true);
206
207                 modifier_vbox.pack_start (*mod_hbox, false, false);
208         }
209
210         modifier_vbox.show_all ();
211
212 }
213
214 void
215 ColorThemeManager::modifier_edited (Gtk::Range* range, string name)
216 {
217         using namespace ArdourCanvas;
218
219         double alpha = range->get_value();
220         SVAModifier svam (SVAModifier::Assign, -1.0, -1.0, alpha);
221         UIConfiguration::instance().set_modifier (name, svam);
222 }
223
224 void
225 ColorThemeManager::colors_changed ()
226 {
227         setup_palette ();
228         setup_aliases ();
229         setup_modifiers ();
230 }
231
232 void
233 ColorThemeManager::reset_canvas_colors()
234 {
235         string cfile;
236         string basename;
237
238         /* look for a versioned user-owned color file, and try to rename it */
239
240         basename = UIConfiguration::instance().color_file_name (true, true);
241
242         if (find_file (ardour_config_search_path(), basename, cfile)) {
243                 string backup = cfile + string (X_(".old"));
244                 g_rename (cfile.c_str(), backup.c_str());
245                 /* don't really care if it fails */
246         }
247
248         UIConfiguration::instance().load_color_theme (false);
249         UIConfiguration::instance().save_state ();
250 }
251
252 ArdourCanvas::Container*
253 ColorThemeManager::initialize_palette_canvas (ArdourCanvas::Canvas& canvas)
254 {
255         using namespace ArdourCanvas;
256
257         /* hide background */
258         canvas.set_background_color (rgba_to_color (0.0, 0.0, 1.0, 0.0));
259
260         /* bi-directional scroll group */
261
262         ScrollGroup* scroll_group = new ScrollGroup (canvas.root(), ScrollGroup::ScrollSensitivity (ScrollGroup::ScrollsVertically|ScrollGroup::ScrollsHorizontally));
263         canvas.add_scroller (*scroll_group);
264
265         /* new container to hold everything */
266
267         return new ArdourCanvas::Container (scroll_group);
268 }
269
270 void
271 ColorThemeManager::palette_canvas_allocated (Gtk::Allocation& alloc, ArdourCanvas::Container* group, ArdourCanvas::Canvas* canvas, sigc::slot<bool,GdkEvent*,std::string> event_handler)
272 {
273         build_palette_canvas (*canvas, *group, event_handler);
274 }
275
276 struct NamedColor {
277         string name;
278         Gtkmm2ext::HSV    color;
279         NamedColor (string s, Gtkmm2ext::HSV c) : name (s), color (c) {}
280 };
281
282 struct SortByHue {
283         bool operator() (NamedColor const & a, NamedColor const & b) {
284                 using namespace ArdourCanvas;
285                 const HSV black (0, 0, 0);
286                 if (a.color.is_gray() || b.color.is_gray()) {
287                         return black.distance (a.color) < black.distance (b.color);
288                 } else {
289                         return a.color.h < b.color.h;
290                         // const HSV red (rgba_to_color (1.0, 0.0, 0.0, 1.0));
291                         // return red.distance (a.color) < red.distance (b.color);
292                 }
293         }
294 };
295
296
297 void
298 ColorThemeManager::build_palette_canvas (ArdourCanvas::Canvas& canvas, ArdourCanvas::Container& group, sigc::slot<bool,GdkEvent*,std::string> event_handler)
299 {
300         using namespace ArdourCanvas;
301
302         /* we want the colors sorted by hue, with their name */
303
304         UIConfiguration::Colors& colors (UIConfiguration::instance().colors);
305         vector<NamedColor> nc;
306         for (UIConfiguration::Colors::const_iterator x = colors.begin(); x != colors.end(); ++x) {
307                 nc.push_back (NamedColor (x->first, HSV (x->second)));
308         }
309         SortByHue sorter;
310         sort (nc.begin(), nc.end(), sorter);
311
312         const uint32_t color_limit = nc.size();
313         const double box_size = 20.0;
314         const double width = canvas.width();
315         const double height = canvas.height();
316
317         uint32_t color_num = 0;
318
319         /* clear existing rects and delete them */
320
321         group.clear (true);
322
323         for (uint32_t y = 0; y < height - box_size && color_num < color_limit; y += box_size) {
324                 for (uint32_t x = 0; x < width - box_size && color_num < color_limit; x += box_size) {
325                         ArdourCanvas::Rectangle* r = new ArdourCanvas::Rectangle (&group, ArdourCanvas::Rect (x, y, x + box_size, y + box_size));
326                         string name = nc[color_num++].name;
327
328                         UIConfiguration::Colors::iterator c = colors.find (name);
329
330                         if (c != colors.end()) {
331                                 Color color = c->second;
332                                 r->set_fill_color (color);
333                                 r->set_outline_color (rgba_to_color (0.0, 0.0, 0.0, 1.0));
334                                 r->set_tooltip (name);
335                                 r->Event.connect (sigc::bind (event_handler, name));
336                         }
337                 }
338         }
339 }
340
341 void
342 ColorThemeManager::palette_size_request (Gtk::Requisition* req)
343 {
344         uint32_t ncolors = UIConfiguration::instance().colors.size();
345         const int box_size = 20;
346
347         double c = sqrt ((double)ncolors);
348         req->width = (int) floor (c * box_size);
349         req->height = (int) floor (c * box_size);
350
351         /* add overflow row if necessary */
352
353         if (fmod (ncolors, c) != 0.0) {
354                 req->height += box_size;
355         }
356 }
357
358 void
359 ColorThemeManager::setup_palette ()
360 {
361         build_palette_canvas (*palette_viewport.canvas(), *palette_group, sigc::mem_fun (*this, &ColorThemeManager::palette_event));
362 }
363
364 bool
365 ColorThemeManager::palette_event (GdkEvent* ev, string name)
366 {
367         switch (ev->type) {
368         case GDK_BUTTON_RELEASE:
369                 edit_palette_color (name);
370                 return true;
371         default:
372                 break;
373         }
374         return true;
375 }
376
377 void
378 ColorThemeManager::edit_palette_color (std::string name)
379 {
380         using namespace ArdourCanvas;
381         double r,g, b, a;
382         UIConfiguration* uic (&UIConfiguration::instance());
383         Gtkmm2ext::Color c = uic->color (name);
384         Gdk::Color gdkcolor;
385
386         color_to_rgba (c, r, g, b, a);
387
388         gdkcolor.set_rgb_p (r, g, b);
389         color_dialog.get_colorsel()->set_previous_color (gdkcolor);
390         color_dialog.get_colorsel()->set_current_color (gdkcolor);
391         color_dialog.get_colorsel()->set_previous_alpha ((guint16) (a * 65535));
392         color_dialog.get_colorsel()->set_current_alpha ((guint16) (a * 65535));
393
394         color_dialog_connection.disconnect ();
395         color_dialog_connection = color_dialog.signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::palette_color_response), name));
396         color_dialog.present();
397 }
398
399 void
400 ColorThemeManager::palette_color_response (int result, std::string name)
401 {
402         using namespace ArdourCanvas;
403
404         color_dialog_connection.disconnect ();
405
406         UIConfiguration* uic (&UIConfiguration::instance());
407         Gdk::Color gdkcolor;
408         double r,g, b, a;
409
410         switch (result) {
411         case RESPONSE_ACCEPT:
412         case RESPONSE_OK:
413                 gdkcolor = color_dialog.get_colorsel()->get_current_color();
414                 a = color_dialog.get_colorsel()->get_current_alpha() / 65535.0;
415                 r = gdkcolor.get_red_p();
416                 g = gdkcolor.get_green_p();
417                 b = gdkcolor.get_blue_p();
418
419                 uic->set_color (name, rgba_to_color (r, g, b, a));
420                 break;
421
422         default:
423                 break;
424         }
425
426         color_dialog.hide ();
427 }
428
429 bool
430 ColorThemeManager::alias_palette_event (GdkEvent* ev, string new_alias, string target_name)
431 {
432         switch (ev->type) {
433         case GDK_BUTTON_RELEASE:
434                 UIConfiguration::instance().set_alias (target_name, new_alias);
435                 return true;
436                 break;
437         default:
438                 break;
439         }
440         return false;
441 }
442
443 void
444 ColorThemeManager::alias_palette_response (int response, std::string target_name, std::string old_alias)
445 {
446         switch (response) {
447         case GTK_RESPONSE_OK:
448         case GTK_RESPONSE_ACCEPT:
449                 /* rebuild alias list with new color: inefficient but simple */
450                 setup_aliases ();
451                 break;
452
453         case GTK_RESPONSE_REJECT:
454                 /* revert choice */
455                 UIConfiguration::instance().set_alias (target_name, old_alias);
456                 break;
457
458         default:
459                 /* do nothing */
460                 break;
461         }
462
463         palette_window->hide ();
464 }
465
466 void
467 ColorThemeManager::choose_color_from_palette (string const & name)
468 {
469         UIConfiguration* uic (&UIConfiguration::instance());
470         UIConfiguration::ColorAliases::iterator i = uic->color_aliases.find (name);
471
472         if (i == uic->color_aliases.end()) {
473                 return;
474         }
475
476         delete palette_window;
477
478         palette_window = new ArdourDialog (_("Color Palette"));
479         palette_window->add_button (Stock::CANCEL, RESPONSE_REJECT); /* using CANCEL causes confusion if dialog is closed via CloseAllDialogs */
480         palette_window->add_button (Stock::OK, RESPONSE_OK);
481
482         ArdourCanvas::GtkCanvas* canvas = new ArdourCanvas::GtkCanvas ();
483         ArdourCanvas::Container* group = initialize_palette_canvas (*canvas);
484
485         canvas->signal_size_request().connect (sigc::mem_fun (*this, &ColorThemeManager::palette_size_request));
486         canvas->signal_size_allocate().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::palette_canvas_allocated), group, canvas,
487                                                             sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::alias_palette_event), name)));
488
489         palette_window->get_vbox()->pack_start (*canvas);
490         palette_window->show_all ();
491
492         palette_response_connection = palette_window->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::alias_palette_response), name, i->second));
493
494         palette_window->set_position (WIN_POS_MOUSE);
495         palette_window->present ();
496 }
497
498 void
499 ColorThemeManager::setup_aliases ()
500 {
501         using namespace ArdourCanvas;
502
503         UIConfiguration* uic (&UIConfiguration::instance());
504         UIConfiguration::ColorAliases& aliases (uic->color_aliases);
505
506         alias_list->clear ();
507
508         for (UIConfiguration::ColorAliases::iterator i = aliases.begin(); i != aliases.end(); ++i) {
509                 TreeModel::Children rows = alias_list->children();
510                 TreeModel::Row row;
511                 string::size_type colon;
512
513                 if ((colon = i->first.find (':')) != string::npos) {
514
515                         /* this is supposed to be a child node, so find the
516                          * parent
517                          */
518
519                         string parent = i->first.substr (0, colon);
520                         TreeModel::iterator ri;
521
522                         for (ri = rows.begin(); ri != rows.end(); ++ri) {
523                                 string s = (*ri)[alias_columns.name];
524                                 if (s == parent) {
525                                         break;
526                                 }
527                         }
528
529                         if (ri == rows.end()) {
530                                 /* not found, add the parent as new top level row */
531                                 row = *(alias_list->append());
532                                 row[alias_columns.name] = parent;
533                                 row[alias_columns.alias] = "";
534
535                                 /* now add the child as a child of this one */
536
537                                 row = *(alias_list->insert (row->children().end()));
538                                 row[alias_columns.name] = i->first.substr (colon+1);
539                         } else {
540                                 row = *(alias_list->insert ((*ri)->children().end()));
541                                 row[alias_columns.name] = i->first.substr (colon+1);
542                         }
543
544                 } else {
545                         /* add as a child */
546                         row = *(alias_list->append());
547                         row[alias_columns.name] = i->first;
548                         row[alias_columns.key] = i->first;
549                 }
550
551                 row[alias_columns.key] = i->first;
552                 row[alias_columns.alias] = i->second;
553
554                 Gdk::Color col;
555                 double r, g, b, a;
556                 Color c (uic->color (i->second));
557                 color_to_rgba (c, r, g, b, a);
558                 col.set_rgb_p (r, g, b);
559
560                 row[alias_columns.color] = col;
561         }
562 }
563
564 bool
565 ColorThemeManager::alias_button_press_event (GdkEventButton* ev)
566 {
567         TreeIter iter;
568         TreeModel::Path path;
569         TreeViewColumn* column;
570         int cellx;
571         int celly;
572
573         if (!alias_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
574                 return false;
575         }
576
577         guint32 colnum = GPOINTER_TO_UINT (column->get_data (X_("colnum")));
578
579         switch (colnum) {
580         case 0:
581                 /* allow normal processing to occur */
582                 return false;
583
584         case 1: /* color */
585                 if ((iter = alias_list->get_iter (path))) {
586                         string target_color_alias = (*iter)[alias_columns.key];
587                         if (!target_color_alias.empty()) {
588                                 choose_color_from_palette (target_color_alias);
589                         }
590                 }
591                 break;
592         }
593
594         return true;
595 }
596
597 void
598 ColorThemeManager::parameter_changed (string const&)
599 {
600 }
601
602 void
603 ColorThemeManager::set_state_from_config ()
604 {
605
606 }
607
608 void
609 ColorThemeManager::add_to_page (OptionEditorPage* p)
610 {
611         int const n = p->table.property_n_rows();
612         int m = n + 1;
613         if (!_note.empty ()) {
614                 ++m;
615         }
616         p->table.resize (m, 3);
617         p->table.attach (box, 1, 3, n, n + 1, FILL | EXPAND, SHRINK, 0, 0);
618         maybe_add_note (p, n + 1);
619 }
620
621 Gtk::Widget&
622 ColorThemeManager::tip_widget()
623 {
624         return reset_button; /* XXX need a better widget for this purpose */
625 }
626
627 void
628 ColorThemeManager::on_color_theme_changed ()
629 {
630         Gtk::TreeModel::iterator iter = color_theme_dropdown.get_active();
631
632         if (iter) {
633                 Gtk::TreeModel::Row row = *iter;
634
635                 if (row) {
636                         string new_theme = row[color_theme_columns.path];
637                         UIConfiguration::instance().set_color_file (new_theme);
638                 }
639         }
640 }