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