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