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