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