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