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