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