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