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