ptformat: Make PT import more resilient to bad user choices and display messages
[ardour.git] / gtk2_ardour / theme_manager.cc
1 /*
2     Copyright (C) 2000-2007 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 <gtkmm/stock.h>
28 #include <gtkmm/settings.h>
29
30 #include "gtkmm2ext/gtk_ui.h"
31 #include "gtkmm2ext/cell_renderer_color_selector.h"
32 #include "gtkmm2ext/utils.h"
33
34 #include "pbd/file_utils.h"
35 #include "pbd/compose.h"
36
37 #include "ardour/filesystem_paths.h"
38 #include "ardour/profile.h"
39
40 #include "canvas/container.h"
41 #include "canvas/rectangle.h"
42 #include "canvas/scroll_group.h"
43 #include "canvas/wave_view.h"
44
45 #include "ardour_button.h"
46 #include "ardour_dialog.h"
47 #include "theme_manager.h"
48 #include "rgb_macros.h"
49 #include "ui_config.h"
50 #include "utils.h"
51
52 #include "i18n.h"
53
54 using namespace std;
55 using namespace Gtk;
56 using namespace PBD;
57 using namespace ARDOUR;
58 using namespace ARDOUR_UI_UTILS;
59
60 ThemeManager::ThemeManager()
61         : dark_button (_("Dark Theme"))
62         , light_button (_("Light Theme"))
63         , reset_button (_("Restore Defaults"))
64         , flat_buttons (_("Draw \"flat\" buttons"))
65         , blink_rec_button (_("Blink Rec-Arm buttons"))
66         , region_color_button (_("Color regions using their track's color"))
67         , show_clipping_button (_("Show waveform clipping"))
68         , waveform_gradient_depth (0, 1.0, 0.05)
69         , waveform_gradient_depth_label (_("Waveforms color gradient depth"))
70         , timeline_item_gradient_depth (0, 1.0, 0.05)
71         , timeline_item_gradient_depth_label (_("Timeline item gradient depth"))
72         , all_dialogs (_("All floating windows are dialogs"))
73         , transients_follow_front (_("Transient windows follow front window."))
74         , floating_monitor_section (_("Float detached monitor-section window"))
75         , icon_set_label (_("Icon Set"))
76         , palette_viewport (*palette_scroller.get_hadjustment(), *palette_scroller.get_vadjustment())
77         , palette_group (0)
78         , palette_window (0)
79 {
80         /* Now the alias list */
81
82         alias_list = TreeStore::create (alias_columns);
83         alias_display.set_model (alias_list);
84         alias_display.append_column (_("Object"), alias_columns.name);
85
86         Gtkmm2ext::CellRendererColorSelector* color_renderer = manage (new Gtkmm2ext::CellRendererColorSelector);
87         TreeViewColumn* color_column = manage (new TreeViewColumn (_("Color"), *color_renderer));
88         color_column->add_attribute (color_renderer->property_color(), alias_columns.color);
89         alias_display.append_column (*color_column);
90
91         alias_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
92         alias_display.get_column (0)->set_expand (true);
93         alias_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
94         alias_display.get_column (1)->set_expand (false);
95         alias_display.set_reorderable (false);
96         alias_display.set_headers_visible (true);
97
98         alias_display.signal_button_press_event().connect (sigc::mem_fun (*this, &ThemeManager::alias_button_press_event), false);
99
100         alias_scroller.add (alias_display);
101
102         /* various buttons */
103
104         RadioButton::Group group = dark_button.get_group();
105         light_button.set_group(group);
106         theme_selection_hbox.set_homogeneous(false);
107         theme_selection_hbox.pack_start (dark_button);
108         theme_selection_hbox.pack_start (light_button);
109
110         set_homogeneous (false);
111 #if 0 // disable light/dark theme choice. until the 'light theme gets some attention.
112         pack_start (theme_selection_hbox, PACK_SHRINK);
113 #endif
114         pack_start (reset_button, PACK_SHRINK);
115 #ifndef __APPLE__
116         pack_start (all_dialogs, PACK_SHRINK);
117         pack_start (transients_follow_front, PACK_SHRINK);
118 #endif
119         if (!Profile->get_mixbus()) {
120                 pack_start (floating_monitor_section, PACK_SHRINK);
121         }
122         pack_start (flat_buttons, PACK_SHRINK);
123         pack_start (blink_rec_button, PACK_SHRINK);
124         pack_start (region_color_button, PACK_SHRINK);
125         pack_start (show_clipping_button, PACK_SHRINK);
126
127         Gtk::HBox* hbox;
128
129         vector<string> icon_sets = ::get_icon_sets ();
130
131         if (icon_sets.size() > 1) {
132                 Gtkmm2ext::set_popdown_strings (icon_set_dropdown, icon_sets);
133                 icon_set_dropdown.set_active_text (UIConfiguration::instance().get_icon_set());
134
135                 hbox = Gtk::manage (new Gtk::HBox());
136                 hbox->set_spacing (6);
137                 hbox->pack_start (icon_set_label, false, false);
138                 hbox->pack_start (icon_set_dropdown, true, true);
139                 pack_start (*hbox, PACK_SHRINK);
140         }
141
142
143         hbox = Gtk::manage (new Gtk::HBox());
144         hbox->set_spacing (6);
145         hbox->pack_start (waveform_gradient_depth, true, true);
146         hbox->pack_start (waveform_gradient_depth_label, false, false);
147         pack_start (*hbox, PACK_SHRINK);
148
149         hbox = Gtk::manage (new Gtk::HBox());
150         hbox->set_spacing (6);
151         hbox->pack_start (timeline_item_gradient_depth, true, true);
152         hbox->pack_start (timeline_item_gradient_depth_label, false, false);
153         pack_start (*hbox, PACK_SHRINK);
154
155         palette_group = initialize_palette_canvas (*palette_viewport.canvas());
156         palette_viewport.signal_size_allocate().connect (sigc::bind (sigc::mem_fun (*this, &ThemeManager::palette_canvas_allocated), palette_group, palette_viewport.canvas(),
157                                                                      sigc::mem_fun (*this, &ThemeManager::palette_event)));
158         palette_scroller.add (palette_viewport);
159
160         modifier_scroller.add (modifier_vbox);
161
162         notebook.append_page (alias_scroller, _("Items"));
163         notebook.append_page (palette_scroller, _("Palette"));
164         notebook.append_page (modifier_scroller, _("Transparency"));
165
166         pack_start (notebook);
167
168         show_all ();
169
170         waveform_gradient_depth.set_update_policy (Gtk::UPDATE_DELAYED);
171         timeline_item_gradient_depth.set_update_policy (Gtk::UPDATE_DELAYED);
172
173         color_dialog.get_colorsel()->set_has_opacity_control (true);
174         color_dialog.get_colorsel()->set_has_palette (true);
175
176         set_ui_to_state();
177
178         color_dialog.get_ok_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
179         color_dialog.get_cancel_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
180         dark_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_dark_theme_button_toggled));
181         light_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_light_theme_button_toggled));
182         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &ThemeManager::reset_canvas_colors));
183         flat_buttons.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_flat_buttons_toggled));
184         blink_rec_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_blink_rec_arm_toggled));
185         region_color_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_region_color_toggled));
186         show_clipping_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_show_clip_toggled));
187         waveform_gradient_depth.signal_value_changed().connect (sigc::mem_fun (*this, &ThemeManager::on_waveform_gradient_depth_change));
188         timeline_item_gradient_depth.signal_value_changed().connect (sigc::mem_fun (*this, &ThemeManager::on_timeline_item_gradient_depth_change));
189         all_dialogs.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_all_dialogs_toggled));
190         transients_follow_front.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_transients_follow_front_toggled));
191         floating_monitor_section.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_floating_monitor_section_toggled));
192         icon_set_dropdown.signal_changed().connect (sigc::mem_fun (*this, &ThemeManager::on_icon_set_changed));
193
194         Gtkmm2ext::UI::instance()->set_tip (all_dialogs,
195                                             string_compose (_("Mark all floating windows to be type \"Dialog\" rather than using \"Utility\" for some.\n"
196                                                               "This may help with some window managers. This requires a restart of %1 to take effect"),
197                                                             PROGRAM_NAME));
198         Gtkmm2ext::UI::instance()->set_tip (transients_follow_front,
199                                             string_compose (_("Make transient windows follow the front window when toggling between the editor and mixer.\n"
200                                                               "This requires a restart of %1 to take effect"), PROGRAM_NAME));
201         Gtkmm2ext::UI::instance()->set_tip (floating_monitor_section,
202                                             string_compose (_("When detaching the monitoring section, mark it as \"Utility\" window to stay in front.\n"
203                                                               "This requires a restart of %1 to take effect"), PROGRAM_NAME));
204
205         set_size_request (-1, 400);
206         /* no need to call setup_palette() here, it will be done when its size is allocated */
207         setup_aliases ();
208         setup_modifiers ();
209
210         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ThemeManager::colors_changed));
211 }
212
213 ThemeManager::~ThemeManager()
214 {
215 }
216
217 void
218 ThemeManager::setup_modifiers ()
219 {
220         UIConfiguration* uic (&UIConfiguration::instance());
221         UIConfiguration::Modifiers& modifiers (uic->modifiers);
222         Gtk::HBox* mod_hbox;
223         Gtk::Label* mod_label;
224         Gtk::HScale* mod_scale;
225
226         Gtkmm2ext::container_clear (modifier_vbox);
227
228         for (UIConfiguration::Modifiers::const_iterator m = modifiers.begin(); m != modifiers.end(); ++m) {
229                 mod_hbox = manage (new HBox);
230
231                 mod_scale = manage (new HScale (0.0, 1.0, 0.01));
232                 mod_scale->set_draw_value (false);
233                 mod_scale->set_value (m->second.a());
234                 mod_scale->set_update_policy (Gtk::UPDATE_DISCONTINUOUS);
235                 mod_scale->signal_value_changed().connect (sigc::bind (sigc::mem_fun (*this, &ThemeManager::modifier_edited), mod_scale, m->first));
236
237                 mod_label = manage (new Label (m->first));
238                 mod_label->set_alignment (1.0, 0.5);
239                 mod_label->set_size_request (150, -1); /* 150 pixels should be enough for anyone */
240
241                 mod_hbox->pack_start (*mod_label, false, true, 12);
242                 mod_hbox->pack_start (*mod_scale, true, true);
243
244                 modifier_vbox.pack_start (*mod_hbox, false, false);
245         }
246
247         modifier_vbox.show_all ();
248
249 }
250
251 void
252 ThemeManager::modifier_edited (Gtk::Range* range, string name)
253 {
254         using namespace ArdourCanvas;
255
256         double alpha = range->get_value();
257         SVAModifier svam (SVAModifier::Assign, -1.0, -1.0, alpha);
258         UIConfiguration::instance().set_modifier (name, svam);
259 }
260
261 void
262 ThemeManager::colors_changed ()
263 {
264         setup_palette ();
265         setup_aliases ();
266         setup_modifiers ();
267 }
268
269 int
270 ThemeManager::save (string /*path*/)
271 {
272         return 0;
273 }
274
275 void
276 ThemeManager::on_flat_buttons_toggled ()
277 {
278         UIConfiguration::instance().set_flat_buttons (flat_buttons.get_active());
279         ArdourButton::set_flat_buttons (flat_buttons.get_active());
280         /* force a redraw */
281         gtk_rc_reset_styles (gtk_settings_get_default());
282 }
283
284 void
285 ThemeManager::on_blink_rec_arm_toggled ()
286 {
287         UIConfiguration::instance().set_blink_rec_arm (blink_rec_button.get_active());
288         UIConfiguration::instance().ParameterChanged("blink-rec-arm");
289 }
290
291 void
292 ThemeManager::on_region_color_toggled ()
293 {
294         UIConfiguration::instance().set_color_regions_using_track_color (region_color_button.get_active());
295 }
296
297 void
298 ThemeManager::on_show_clip_toggled ()
299 {
300         UIConfiguration::instance().set_show_waveform_clipping (show_clipping_button.get_active());
301         // "show-waveform-clipping" was a session config key
302         ArdourCanvas::WaveView::set_global_show_waveform_clipping (UIConfiguration::instance().get_show_waveform_clipping());
303 }
304
305 void
306 ThemeManager::on_all_dialogs_toggled ()
307 {
308         UIConfiguration::instance().set_all_floating_windows_are_dialogs (all_dialogs.get_active());
309 }
310
311 void
312 ThemeManager::on_transients_follow_front_toggled ()
313 {
314         UIConfiguration::instance().set_transients_follow_front (transients_follow_front.get_active());
315 }
316
317 void
318 ThemeManager::on_floating_monitor_section_toggled ()
319 {
320         UIConfiguration::instance().set_floating_monitor_section (floating_monitor_section.get_active());
321 }
322
323 void
324 ThemeManager::on_waveform_gradient_depth_change ()
325 {
326         double v = waveform_gradient_depth.get_value();
327
328         UIConfiguration::instance().set_waveform_gradient_depth (v);
329         ArdourCanvas::WaveView::set_global_gradient_depth (v);
330 }
331
332 void
333 ThemeManager::on_timeline_item_gradient_depth_change ()
334 {
335         double v = timeline_item_gradient_depth.get_value();
336
337         UIConfiguration::instance().set_timeline_item_gradient_depth (v);
338 }
339
340 void
341 ThemeManager::on_icon_set_changed ()
342 {
343         string new_set = icon_set_dropdown.get_active_text();
344         UIConfiguration::instance().set_icon_set (new_set);
345 }
346
347 void
348 ThemeManager::on_dark_theme_button_toggled()
349 {
350         if (!dark_button.get_active()) return;
351
352         UIConfiguration* uic (&UIConfiguration::instance());
353
354         uic->set_color_file("dark");
355 }
356
357 void
358 ThemeManager::on_light_theme_button_toggled()
359 {
360         if (!light_button.get_active()) return;
361
362         UIConfiguration* uic (&UIConfiguration::instance());
363
364         uic->set_color_file("light");
365 }
366
367 void
368 ThemeManager::set_ui_to_state()
369 {
370         /* there is no way these values can change individually
371          * by themselves (w/o user-interaction)
372          * hence a common combined update function suffices
373          */
374
375         if (UIConfiguration::instance().get_color_file() == "light") {
376                 light_button.set_active(true);
377         } else {
378                 dark_button.set_active(true);
379         }
380
381         /* there is no need to block signal handlers, here,
382          * all elements check if the value has changed and ignore NOOPs
383          */
384         all_dialogs.set_active (UIConfiguration::instance().get_all_floating_windows_are_dialogs());
385         transients_follow_front.set_active (UIConfiguration::instance().get_transients_follow_front());
386         floating_monitor_section.set_active (UIConfiguration::instance().get_floating_monitor_section());
387         flat_buttons.set_active (UIConfiguration::instance().get_flat_buttons());
388         blink_rec_button.set_active (UIConfiguration::instance().get_blink_rec_arm());
389         region_color_button.set_active (UIConfiguration::instance().get_color_regions_using_track_color());
390         show_clipping_button.set_active (UIConfiguration::instance().get_show_waveform_clipping());
391         waveform_gradient_depth.set_value(UIConfiguration::instance().get_waveform_gradient_depth());
392         timeline_item_gradient_depth.set_value(UIConfiguration::instance().get_timeline_item_gradient_depth());
393 }
394
395 void
396 ThemeManager::reset_canvas_colors()
397 {
398         string cfile;
399         string basename;
400
401         basename = "my-";
402         basename += UIConfiguration::instance().get_color_file();
403         basename += ".colors";
404
405         if (find_file (ardour_config_search_path(), basename, cfile)) {
406                 string backup = cfile + string (X_(".old"));
407                 g_rename (cfile.c_str(), backup.c_str());
408                 /* don't really care if it fails */
409         }
410
411         UIConfiguration::instance().load_defaults();
412         UIConfiguration::instance().save_state ();
413         set_ui_to_state();
414 }
415
416 ArdourCanvas::Container*
417 ThemeManager::initialize_palette_canvas (ArdourCanvas::Canvas& canvas)
418 {
419         using namespace ArdourCanvas;
420
421         /* hide background */
422         canvas.set_background_color (rgba_to_color (0.0, 0.0, 1.0, 0.0));
423
424         /* bi-directional scroll group */
425
426         ScrollGroup* scroll_group = new ScrollGroup (canvas.root(), ScrollGroup::ScrollSensitivity (ScrollGroup::ScrollsVertically|ScrollGroup::ScrollsHorizontally));
427         canvas.add_scroller (*scroll_group);
428
429         /* new container to hold everything */
430
431         return new ArdourCanvas::Container (scroll_group);
432 }
433
434 void
435 ThemeManager::palette_canvas_allocated (Gtk::Allocation& alloc, ArdourCanvas::Container* group, ArdourCanvas::Canvas* canvas, sigc::slot<bool,GdkEvent*,std::string> event_handler)
436 {
437         build_palette_canvas (*canvas, *group, event_handler);
438 }
439
440 struct NamedColor {
441         string name;
442         ArdourCanvas::HSV    color;
443         NamedColor (string s, ArdourCanvas::HSV c) : name (s), color (c) {}
444 };
445
446 struct SortByHue {
447         bool operator() (NamedColor const & a, NamedColor const & b) {
448                 using namespace ArdourCanvas;
449                 const HSV black (0, 0, 0);
450                 if (a.color.is_gray() || b.color.is_gray()) {
451                         return black.distance (a.color) < black.distance (b.color);
452                 } else {
453                         return a.color.h < b.color.h;
454                         // const HSV red (rgba_to_color (1.0, 0.0, 0.0, 1.0));
455                         // return red.distance (a.color) < red.distance (b.color);
456                 }
457         }
458 };
459
460
461 void
462 ThemeManager::build_palette_canvas (ArdourCanvas::Canvas& canvas, ArdourCanvas::Container& group, sigc::slot<bool,GdkEvent*,std::string> event_handler)
463 {
464         using namespace ArdourCanvas;
465
466         /* we want the colors sorted by hue, with their name */
467
468         UIConfiguration::Colors& colors (UIConfiguration::instance().colors);
469         vector<NamedColor> nc;
470         for (UIConfiguration::Colors::const_iterator x = colors.begin(); x != colors.end(); ++x) {
471                 nc.push_back (NamedColor (x->first, HSV (x->second)));
472         }
473         SortByHue sorter;
474         sort (nc.begin(), nc.end(), sorter);
475
476         const uint32_t color_limit = nc.size();
477         const double box_size = 20.0;
478         const double width = canvas.width();
479         const double height = canvas.height();
480
481         uint32_t color_num = 0;
482
483         /* clear existing rects and delete them */
484
485         group.clear (true);
486
487         for (uint32_t y = 0; y < height - box_size && color_num < color_limit; y += box_size) {
488                 for (uint32_t x = 0; x < width - box_size && color_num < color_limit; x += box_size) {
489                         ArdourCanvas::Rectangle* r = new ArdourCanvas::Rectangle (&group, ArdourCanvas::Rect (x, y, x + box_size, y + box_size));
490
491                         string name = nc[color_num++].name;
492
493                         UIConfiguration::Colors::iterator c = colors.find (name);
494
495                         if (c != colors.end()) {
496                                 Color color = c->second;
497                                 r->set_fill_color (color);
498                                 r->set_outline_color (rgba_to_color (0.0, 0.0, 0.0, 1.0));
499                                 r->set_tooltip (name);
500                                 r->Event.connect (sigc::bind (event_handler, name));
501                         }
502                 }
503         }
504 }
505
506 void
507 ThemeManager::palette_size_request (Gtk::Requisition* req)
508 {
509         uint32_t ncolors = UIConfiguration::instance().colors.size();
510         const int box_size = 20;
511
512         double c = sqrt ((double)ncolors);
513         req->width = (int) floor (c * box_size);
514         req->height = (int) floor (c * box_size);
515
516         /* add overflow row if necessary */
517
518         if (fmod (ncolors, c) != 0.0) {
519                 req->height += box_size;
520         }
521 }
522
523 void
524 ThemeManager::setup_palette ()
525 {
526         build_palette_canvas (*palette_viewport.canvas(), *palette_group, sigc::mem_fun (*this, &ThemeManager::palette_event));
527 }
528
529 bool
530 ThemeManager::palette_event (GdkEvent* ev, string name)
531 {
532         switch (ev->type) {
533         case GDK_BUTTON_RELEASE:
534                 edit_palette_color (name);
535                 return true;
536         default:
537                 break;
538         }
539         return true;
540 }
541
542 void
543 ThemeManager::edit_palette_color (std::string name)
544 {
545         using namespace ArdourCanvas;
546         double r,g, b, a;
547         UIConfiguration* uic (&UIConfiguration::instance());
548         ArdourCanvas::Color c = uic->color (name);
549         Gdk::Color gdkcolor;
550
551         color_to_rgba (c, r, g, b, a);
552
553         gdkcolor.set_rgb_p (r, g, b);
554         color_dialog.get_colorsel()->set_previous_color (gdkcolor);
555         color_dialog.get_colorsel()->set_current_color (gdkcolor);
556         color_dialog.get_colorsel()->set_previous_alpha ((guint16) (a * 65535));
557         color_dialog.get_colorsel()->set_current_alpha ((guint16) (a * 65535));
558
559         color_dialog_connection.disconnect ();
560         color_dialog_connection = color_dialog.signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ThemeManager::palette_color_response), name));
561         color_dialog.present();
562 }
563
564 void
565 ThemeManager::palette_color_response (int result, std::string name)
566 {
567         using namespace ArdourCanvas;
568
569         color_dialog_connection.disconnect ();
570
571         UIConfiguration* uic (&UIConfiguration::instance());
572         Gdk::Color gdkcolor;
573         double r,g, b, a;
574
575         switch (result) {
576         case RESPONSE_ACCEPT:
577         case RESPONSE_OK:
578                 gdkcolor = color_dialog.get_colorsel()->get_current_color();
579                 a = color_dialog.get_colorsel()->get_current_alpha() / 65535.0;
580                 r = gdkcolor.get_red_p();
581                 g = gdkcolor.get_green_p();
582                 b = gdkcolor.get_blue_p();
583
584                 uic->set_color (name, rgba_to_color (r, g, b, a));
585                 break;
586
587         default:
588                 break;
589         }
590
591         color_dialog.hide ();
592 }
593
594 bool
595 ThemeManager::alias_palette_event (GdkEvent* ev, string new_alias, string target_name)
596 {
597         switch (ev->type) {
598         case GDK_BUTTON_RELEASE:
599                 UIConfiguration::instance().set_alias (target_name, new_alias);
600                 return true;
601                 break;
602         default:
603                 break;
604         }
605         return false;
606 }
607
608 void
609 ThemeManager::alias_palette_response (int response, std::string target_name, std::string old_alias)
610 {
611         switch (response) {
612         case GTK_RESPONSE_OK:
613         case GTK_RESPONSE_ACCEPT:
614                 /* rebuild alias list with new color: inefficient but simple */
615                 setup_aliases ();
616                 break;
617
618         case GTK_RESPONSE_REJECT:
619                 /* revert choice */
620                 UIConfiguration::instance().set_alias (target_name, old_alias);
621                 break;
622
623         default:
624                 /* do nothing */
625                 break;
626         }
627
628         palette_window->hide ();
629 }
630
631 void
632 ThemeManager::choose_color_from_palette (string const & name)
633 {
634         UIConfiguration* uic (&UIConfiguration::instance());
635         UIConfiguration::ColorAliases::iterator i = uic->color_aliases.find (name);
636
637         if (i == uic->color_aliases.end()) {
638                 return;
639         }
640
641         delete palette_window;
642
643         palette_window = new ArdourDialog (_("Color Palette"));
644         palette_window->add_button (Stock::CANCEL, RESPONSE_REJECT); /* using CANCEL causes confusion if dialog is closed via CloseAllDialogs */
645         palette_window->add_button (Stock::OK, RESPONSE_OK);
646
647         ArdourCanvas::GtkCanvas* canvas = new ArdourCanvas::GtkCanvas ();
648         ArdourCanvas::Container* group = initialize_palette_canvas (*canvas);
649
650         canvas->signal_size_request().connect (sigc::mem_fun (*this, &ThemeManager::palette_size_request));
651         canvas->signal_size_allocate().connect (sigc::bind (sigc::mem_fun (*this, &ThemeManager::palette_canvas_allocated), group, canvas,
652                                                             sigc::bind (sigc::mem_fun (*this, &ThemeManager::alias_palette_event), name)));
653
654         palette_window->get_vbox()->pack_start (*canvas);
655         palette_window->show_all ();
656
657         palette_response_connection = palette_window->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ThemeManager::alias_palette_response), name, i->second));
658
659         palette_window->set_position (WIN_POS_MOUSE);
660         palette_window->present ();
661 }
662
663 void
664 ThemeManager::setup_aliases ()
665 {
666         using namespace ArdourCanvas;
667
668         UIConfiguration* uic (&UIConfiguration::instance());
669         UIConfiguration::ColorAliases& aliases (uic->color_aliases);
670
671         alias_list->clear ();
672
673         for (UIConfiguration::ColorAliases::iterator i = aliases.begin(); i != aliases.end(); ++i) {
674                 TreeModel::Children rows = alias_list->children();
675                 TreeModel::Row row;
676                 string::size_type colon;
677
678                 if ((colon = i->first.find (':')) != string::npos) {
679
680                         /* this is supposed to be a child node, so find the
681                          * parent
682                          */
683
684                         string parent = i->first.substr (0, colon);
685                         TreeModel::iterator ri;
686
687                         for (ri = rows.begin(); ri != rows.end(); ++ri) {
688                                 string s = (*ri)[alias_columns.name];
689                                 if (s == parent) {
690                                         break;
691                                 }
692                         }
693
694                         if (ri == rows.end()) {
695                                 /* not found, add the parent as new top level row */
696                                 row = *(alias_list->append());
697                                 row[alias_columns.name] = parent;
698                                 row[alias_columns.alias] = "";
699
700                                 /* now add the child as a child of this one */
701
702                                 row = *(alias_list->insert (row->children().end()));
703                                 row[alias_columns.name] = i->first.substr (colon+1);
704                         } else {
705                                 row = *(alias_list->insert ((*ri)->children().end()));
706                                 row[alias_columns.name] = i->first.substr (colon+1);
707                         }
708
709                 } else {
710                         /* add as a child */
711                         row = *(alias_list->append());
712                         row[alias_columns.name] = i->first;
713                         row[alias_columns.key] = i->first;
714                 }
715
716                 row[alias_columns.key] = i->first;
717                 row[alias_columns.alias] = i->second;
718
719                 Gdk::Color col;
720                 double r, g, b, a;
721                 Color c (uic->color (i->second));
722                 color_to_rgba (c, r, g, b, a);
723                 col.set_rgb_p (r, g, b);
724
725                 row[alias_columns.color] = col;
726         }
727 }
728
729 bool
730 ThemeManager::alias_button_press_event (GdkEventButton* ev)
731 {
732         TreeIter iter;
733         TreeModel::Path path;
734         TreeViewColumn* column;
735         int cellx;
736         int celly;
737
738         if (!alias_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
739                 return false;
740         }
741
742         guint32 colnum = GPOINTER_TO_UINT (column->get_data (X_("colnum")));
743
744         switch (colnum) {
745         case 0:
746                 /* allow normal processing to occur */
747                 return false;
748
749         case 1: /* color */
750                 if ((iter = alias_list->get_iter (path))) {
751                         string target_color_alias = (*iter)[alias_columns.key];
752                         if (!target_color_alias.empty()) {
753                                 choose_color_from_palette (target_color_alias);
754                         }
755                 }
756                 break;
757         }
758
759         return true;
760 }