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