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