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