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