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