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