fix/amend pre commit
[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 <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
39 #include "canvas/container.h"
40 #include "canvas/rectangle.h"
41 #include "canvas/scroll_group.h"
42 #include "canvas/wave_view.h"
43
44 #include "ardour_button.h"
45 #include "theme_manager.h"
46 #include "rgb_macros.h"
47 #include "ardour_ui.h"
48 #include "global_signals.h"
49 #include "utils.h"
50
51 #include "i18n.h"
52
53 using namespace std;
54 using namespace Gtk;
55 using namespace PBD;
56 using namespace ARDOUR;
57 using namespace ARDOUR_UI_UTILS;
58
59 namespace ARDOUR_UI_UTILS {
60         sigc::signal<void> ColorsChanged;
61         sigc::signal<void,uint32_t> ColorChanged;
62 }
63
64 ThemeManager::ThemeManager()
65         : ArdourWindow (_("Theme Manager"))
66         , dark_button (_("Dark Theme"))
67         , light_button (_("Light Theme"))
68         , reset_button (_("Restore Defaults"))
69         , flat_buttons (_("Draw \"flat\" buttons"))
70         , blink_rec_button (_("Blink Rec-Arm buttons"))
71         , region_color_button (_("Color regions using their track's color"))
72         , show_clipping_button (_("Show waveform clipping"))
73         , waveform_gradient_depth (0, 1.0, 0.05)
74         , waveform_gradient_depth_label (_("Waveforms color gradient depth"))
75         , timeline_item_gradient_depth (0, 1.0, 0.05)
76         , timeline_item_gradient_depth_label (_("Timeline item gradient depth"))
77         , all_dialogs (_("All floating windows are dialogs"))
78         , icon_set_label (_("Icon Set"))
79         , base_color_viewport (*base_color_scroller.get_hadjustment(), *base_color_scroller.get_vadjustment())
80         , base_color_group (0)
81         , palette_window (0)
82 {
83         set_title (_("Theme Manager"));
84
85         /* Basic color list */
86         
87         color_list = TreeStore::create (columns);
88         color_display.set_model (color_list);
89         color_display.append_column (_("Object"), columns.name);
90         
91         Gtkmm2ext::CellRendererColorSelector* color_renderer = manage (new Gtkmm2ext::CellRendererColorSelector);
92         TreeViewColumn* color_column = manage (new TreeViewColumn (_("Color"), *color_renderer));
93         color_column->add_attribute (color_renderer->property_color(), columns.gdkcolor);
94
95         color_display.append_column (*color_column);
96         
97         color_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
98         color_display.get_column (0)->set_expand (true);
99         color_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
100         color_display.get_column (1)->set_expand (false);
101         color_display.set_reorderable (false);
102         color_display.get_selection()->set_mode (SELECTION_NONE);
103         color_display.set_headers_visible (true);
104
105         color_display.signal_button_press_event().connect (sigc::mem_fun (*this, &ThemeManager::button_press_event), false);
106
107         scroller.add (color_display);
108         scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
109
110         /* Now the alias list */
111         
112         alias_list = TreeStore::create (alias_columns);
113         alias_display.set_model (alias_list);
114         alias_display.append_column (_("Object"), columns.name);
115
116         color_renderer = manage (new Gtkmm2ext::CellRendererColorSelector);
117         color_column = manage (new TreeViewColumn (_("Color"), *color_renderer));
118         color_column->add_attribute (color_renderer->property_color(), alias_columns.color);
119
120         alias_display.append_column (*color_column);
121         
122         alias_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
123         alias_display.get_column (0)->set_expand (true);
124         alias_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
125         alias_display.get_column (1)->set_expand (false);
126         alias_display.set_reorderable (false);
127         alias_display.get_selection()->set_mode (SELECTION_NONE);
128         alias_display.set_headers_visible (true);
129
130         alias_display.signal_button_press_event().connect (sigc::mem_fun (*this, &ThemeManager::alias_button_press_event), false);
131
132         alias_scroller.add (alias_display);
133
134         /* various buttons */
135         
136         RadioButton::Group group = dark_button.get_group();
137         light_button.set_group(group);
138         theme_selection_hbox.set_homogeneous(false);
139         theme_selection_hbox.pack_start (dark_button);
140         theme_selection_hbox.pack_start (light_button);
141
142         Gtk::VBox* vbox = Gtk::manage (new Gtk::VBox ());
143         vbox->set_homogeneous (false);
144         vbox->pack_start (theme_selection_hbox, PACK_SHRINK);
145         vbox->pack_start (reset_button, PACK_SHRINK);
146 #ifndef __APPLE__
147         vbox->pack_start (all_dialogs, PACK_SHRINK);
148 #endif
149         vbox->pack_start (flat_buttons, PACK_SHRINK);
150         vbox->pack_start (blink_rec_button, PACK_SHRINK);
151         vbox->pack_start (region_color_button, PACK_SHRINK);
152         vbox->pack_start (show_clipping_button, PACK_SHRINK);
153
154         Gtk::HBox* hbox;
155
156         vector<string> icon_sets = ::get_icon_sets ();
157
158         if (icon_sets.size() > 1) {
159                 Gtkmm2ext::set_popdown_strings (icon_set_dropdown, icon_sets);
160                 icon_set_dropdown.set_active_text (ARDOUR_UI::config()->get_icon_set());
161
162                 hbox = Gtk::manage (new Gtk::HBox());
163                 hbox->set_spacing (6);
164                 hbox->pack_start (icon_set_label, false, false);
165                 hbox->pack_start (icon_set_dropdown, true, true);
166                 vbox->pack_start (*hbox, PACK_SHRINK);
167         }
168
169         
170         hbox = Gtk::manage (new Gtk::HBox());
171         hbox->set_spacing (6);
172         hbox->pack_start (waveform_gradient_depth, true, true);
173         hbox->pack_start (waveform_gradient_depth_label, false, false);
174         vbox->pack_start (*hbox, PACK_SHRINK);
175
176         hbox = Gtk::manage (new Gtk::HBox());
177         hbox->set_spacing (6);
178         hbox->pack_start (timeline_item_gradient_depth, true, true);
179         hbox->pack_start (timeline_item_gradient_depth_label, false, false);
180         vbox->pack_start (*hbox, PACK_SHRINK);
181
182         base_color_viewport.signal_size_allocate().connect (sigc::mem_fun (*this, &ThemeManager::base_color_viewport_allocated));
183         base_color_scroller.add (base_color_viewport);
184         
185         notebook.append_page (scroller, _("Palette"));
186         notebook.append_page (base_color_scroller, _("Base Colors"));
187         notebook.append_page (alias_scroller, _("Items"));
188         
189         vbox->pack_start (notebook);
190
191         vbox->show_all ();
192
193         add (*vbox);
194
195         waveform_gradient_depth.set_update_policy (Gtk::UPDATE_DELAYED);
196         timeline_item_gradient_depth.set_update_policy (Gtk::UPDATE_DELAYED);
197         
198         color_dialog.get_colorsel()->set_has_opacity_control (true);
199         color_dialog.get_colorsel()->set_has_palette (true);
200         
201         flat_buttons.set_active (ARDOUR_UI::config()->get_flat_buttons());
202         blink_rec_button.set_active (ARDOUR_UI::config()->get_blink_rec_arm());
203         region_color_button.set_active (ARDOUR_UI::config()->get_color_regions_using_track_color());
204         show_clipping_button.set_active (ARDOUR_UI::config()->get_show_waveform_clipping());
205
206         color_dialog.get_ok_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
207         color_dialog.get_cancel_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
208         dark_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_dark_theme_button_toggled));
209         light_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_light_theme_button_toggled));
210         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &ThemeManager::reset_canvas_colors));
211         flat_buttons.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_flat_buttons_toggled));
212         blink_rec_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_blink_rec_arm_toggled));
213         region_color_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_region_color_toggled));
214         show_clipping_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_show_clip_toggled));
215         waveform_gradient_depth.signal_value_changed().connect (sigc::mem_fun (*this, &ThemeManager::on_waveform_gradient_depth_change));
216         timeline_item_gradient_depth.signal_value_changed().connect (sigc::mem_fun (*this, &ThemeManager::on_timeline_item_gradient_depth_change));
217         all_dialogs.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_all_dialogs_toggled));
218         icon_set_dropdown.signal_changed().connect (sigc::mem_fun (*this, &ThemeManager::on_icon_set_changed));
219
220         Gtkmm2ext::UI::instance()->set_tip (all_dialogs, 
221                                             string_compose (_("Mark all floating windows to be type \"Dialog\" rather than using \"Utility\" for some.\n"
222                                                               "This may help with some window managers. This requires a restart of %1 to take effect"),
223                                                             PROGRAM_NAME));
224
225         set_size_request (-1, 400);
226         setup_theme ();
227         setup_aliases ();
228 }
229
230 ThemeManager::~ThemeManager()
231 {
232 }
233
234 int
235 ThemeManager::save (string /*path*/)
236 {
237         return 0;
238 }
239
240 bool
241 ThemeManager::button_press_event (GdkEventButton* ev)
242 {
243         TreeIter iter;
244         TreeModel::Path path;
245         TreeViewColumn* column;
246         int cellx;
247         int celly;
248
249         if (!color_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
250                 return false;
251         }
252
253         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
254         case 0:
255                 /* allow normal processing to occur */
256                 return false;
257
258         case 1: /* color */
259                 if ((iter = color_list->get_iter (path))) {
260
261                         ColorVariable<uint32_t>* var = (*iter)[columns.pVar];
262                         if (!var) {
263                                 /* parent row, do nothing */
264                                 return false;
265                         }
266                         
267                         int r,g, b, a;
268                         uint32_t rgba = (*iter)[columns.rgba];
269                         Gdk::Color color;
270
271                         UINT_TO_RGBA (rgba, &r, &g, &b, &a);
272                         color.set_rgb_p (r / 255.0, g / 255.0, b / 255.0);
273                         color_dialog.get_colorsel()->set_previous_color (color);
274                         color_dialog.get_colorsel()->set_current_color (color);
275                         color_dialog.get_colorsel()->set_previous_alpha ((guint16) (a * 256));
276                         color_dialog.get_colorsel()->set_current_alpha ((guint16) (a * 256));
277
278                         color_dialog_connection.disconnect ();
279                         color_dialog_connection = color_dialog.signal_response().connect (sigc::mem_fun (*this, &ThemeManager::foobar_response));
280                         color_dialog.present ();
281                 }
282         }
283
284         return true;
285 }
286
287 void
288 ThemeManager::foobar_response (int result)
289 {
290         // ColorVariable<uint32_t> *ccvar;
291         int r,g, b, a;
292         uint32_t rgba;
293         Gdk::Color color;
294
295         color_dialog_connection.disconnect ();
296         
297         switch (result) {
298         case RESPONSE_CANCEL:
299                 break;
300         case RESPONSE_ACCEPT:
301                 color = color_dialog.get_colorsel()->get_current_color();
302                 a = color_dialog.get_colorsel()->get_current_alpha();
303                 r = (int) floor (color.get_red_p() * 255.0);
304                 g = (int) floor (color.get_green_p() * 255.0);
305                 b = (int) floor (color.get_blue_p() * 255.0);
306                 
307                 rgba = RGBA_TO_UINT(r,g,b,a>>8);
308                 // (*iter)[columns.rgba] = rgba;
309                 // (*iter)[columns.gdkcolor] = color;
310                 
311                 // ccvar = (*iter)[columns.pVar];
312                 // ccvar->set(rgba);
313                 /* mark dirty ... */
314                 ARDOUR_UI::config()->set_dirty ();
315                 /* but save it immediately */
316                 ARDOUR_UI::config()->save_state ();
317                 
318                 ColorsChanged(); //EMIT SIGNAL
319                 break;
320                 
321         default:
322                 break;
323                 
324         }
325         
326         color_dialog.hide ();
327 }
328
329 void
330 load_rc_file (const string& filename, bool themechange)
331 {
332         std::string rc_file_path;
333
334         if (!find_file (ardour_config_search_path(), filename, rc_file_path)) {
335                 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
336                                            filename, ardour_config_search_path().to_string(), PROGRAM_NAME)
337                                 << endmsg;
338                 return;
339         }
340
341         info << "Loading ui configuration file " << rc_file_path << endmsg;
342
343         Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);
344 }
345
346 void
347 ThemeManager::on_flat_buttons_toggled ()
348 {
349         ARDOUR_UI::config()->set_flat_buttons (flat_buttons.get_active());
350         ARDOUR_UI::config()->set_dirty ();
351         ArdourButton::set_flat_buttons (flat_buttons.get_active());
352         /* force a redraw */
353         gtk_rc_reset_styles (gtk_settings_get_default());
354 }
355
356 void
357 ThemeManager::on_blink_rec_arm_toggled ()
358 {
359         ARDOUR_UI::config()->set_blink_rec_arm (blink_rec_button.get_active());
360         ARDOUR_UI::config()->set_dirty ();
361         ARDOUR::Config->ParameterChanged("blink-rec-arm");
362 }
363
364 void
365 ThemeManager::on_region_color_toggled ()
366 {
367         ARDOUR_UI::config()->set_color_regions_using_track_color (region_color_button.get_active());
368         ARDOUR_UI::config()->set_dirty ();
369 }
370
371 void
372 ThemeManager::on_show_clip_toggled ()
373 {
374         ARDOUR_UI::config()->set_show_waveform_clipping (show_clipping_button.get_active());
375         ARDOUR_UI::config()->set_dirty ();
376 }
377
378 void
379 ThemeManager::on_all_dialogs_toggled ()
380 {
381         ARDOUR_UI::config()->set_all_floating_windows_are_dialogs (all_dialogs.get_active());
382         ARDOUR_UI::config()->set_dirty ();
383 }
384
385 void
386 ThemeManager::on_waveform_gradient_depth_change ()
387 {
388         double v = waveform_gradient_depth.get_value();
389
390         ARDOUR_UI::config()->set_waveform_gradient_depth (v);
391         ARDOUR_UI::config()->set_dirty ();
392         ArdourCanvas::WaveView::set_global_gradient_depth (v);
393 }
394
395 void
396 ThemeManager::on_timeline_item_gradient_depth_change ()
397 {
398         double v = timeline_item_gradient_depth.get_value();
399
400         ARDOUR_UI::config()->set_timeline_item_gradient_depth (v);
401         ARDOUR_UI::config()->set_dirty ();
402 }
403
404 void
405 ThemeManager::on_icon_set_changed ()
406 {
407         string new_set = icon_set_dropdown.get_active_text();
408         ARDOUR_UI::config()->set_icon_set (new_set);
409 }
410
411 void
412 ThemeManager::on_dark_theme_button_toggled()
413 {
414         if (!dark_button.get_active()) return;
415
416         ARDOUR_UI::config()->set_ui_rc_file("ui_dark.rc");
417         ARDOUR_UI::config()->set_dirty ();
418
419         load_rc_file (ARDOUR_UI::config()->get_ui_rc_file(), true);
420 }
421
422 void
423 ThemeManager::on_light_theme_button_toggled()
424 {
425         if (!light_button.get_active()) return;
426
427         ARDOUR_UI::config()->set_ui_rc_file("ui_light.rc");
428         load_rc_file (ARDOUR_UI::config()->get_ui_rc_file(), true);
429 }
430
431 void
432 ThemeManager::setup_theme ()
433 {
434         int r, g, b, a;
435
436         color_list->clear();
437
438         for (std::map<std::string,ColorVariable<uint32_t> *>::iterator i = ARDOUR_UI::config()->configurable_colors.begin(); i != ARDOUR_UI::config()->configurable_colors.end(); i++) {
439
440
441                 ColorVariable<uint32_t>* var = i->second;
442
443                 TreeModel::Children rows = color_list->children();
444                 TreeModel::Row row;
445                 string::size_type colon;
446
447                 if ((colon = var->name().find (':')) != string::npos) {
448
449                         /* this is supposed to be a child node, so find the
450                          * parent 
451                          */
452
453                         string parent = var->name().substr (0, colon);
454                         TreeModel::iterator ri;
455
456                         for (ri = rows.begin(); ri != rows.end(); ++ri) {
457                                 string s = (*ri)[columns.name];
458                                 if (s == parent) {
459                                         break;
460                                 }
461                         }
462
463                         if (ri == rows.end()) {
464                                 /* not found, add the parent as new top level row */
465                                 row = *(color_list->append());
466                                 row[columns.name] = parent;
467                                 row[columns.pVar] = 0;
468                                 
469                                 /* now add the child as a child of this one */
470
471                                 row = *(color_list->insert (row->children().end()));
472                                 row[columns.name] = var->name().substr (colon+1);
473                         } else {
474                                 row = *(color_list->insert ((*ri)->children().end()));
475                                 row[columns.name] = var->name().substr (colon+1);
476                         }
477
478                 } else {
479                         /* add as a child */
480                         row = *(color_list->append());
481                         row[columns.name] = var->name();
482                 }
483
484                 Gdk::Color col;
485                 uint32_t rgba = var->get();
486                 UINT_TO_RGBA (rgba, &r, &g, &b, &a);
487                 //cerr << (*i)->name() << " == " << hex << rgba << ": " << hex << r << " " << hex << g << " " << hex << b << endl;
488                 col.set_rgb_p (r / 255.0, g / 255.0, b / 255.0);
489
490                 row[columns.pVar] = var;
491                 row[columns.rgba] = rgba;
492                 row[columns.gdkcolor] = col;
493         }
494
495         ColorsChanged.emit();
496
497         bool env_defined = false;
498         string rcfile = Glib::getenv("ARDOUR3_UI_RC", env_defined);
499
500         if(!env_defined) {
501                 rcfile = ARDOUR_UI::config()->get_ui_rc_file();
502         }
503
504         if (rcfile == "ui_dark.rc") {
505                 dark_button.set_active();
506         } else if (rcfile == "ui_light.rc") {
507                 light_button.set_active();
508         }
509         
510         flat_buttons.set_active (ARDOUR_UI::config()->get_flat_buttons());
511         blink_rec_button.set_active (ARDOUR_UI::config()->get_blink_rec_arm());
512         waveform_gradient_depth.set_value (ARDOUR_UI::config()->get_waveform_gradient_depth());
513         timeline_item_gradient_depth.set_value (ARDOUR_UI::config()->get_timeline_item_gradient_depth());
514         all_dialogs.set_active (ARDOUR_UI::config()->get_all_floating_windows_are_dialogs());
515         
516         load_rc_file(rcfile, false);
517 }
518
519 void
520 ThemeManager::reset_canvas_colors()
521 {
522         ARDOUR_UI::config()->load_defaults();
523         setup_theme ();
524         /* mark dirty ... */
525         ARDOUR_UI::config()->set_dirty ();
526         /* but save it immediately */
527         ARDOUR_UI::config()->save_state ();
528 }
529
530 struct NamedColor {
531         string name;
532         ArdourCanvas::HSV    color;
533         NamedColor (string s, ArdourCanvas::HSV c) : name (s), color (c) {}
534 };
535
536 struct SortByHue {
537         bool operator() (NamedColor const & a, NamedColor const & b) {
538                 using namespace ArdourCanvas;
539                 const HSV black (0, 0, 0);
540                 if (a.color.is_gray() || b.color.is_gray()) {
541                         return black.distance (a.color) < black.distance (b.color);
542                 } else {
543                         return a.color.h < b.color.h;
544                         // const HSV red (rgba_to_color (1.0, 0.0, 0.0, 1.0));
545                         // return red.distance (a.color) < red.distance (b.color);
546                 }
547         }
548 };
549
550 ArdourCanvas::Container*
551 ThemeManager::initialize_canvas (ArdourCanvas::Canvas& canvas)
552 {
553         using namespace ArdourCanvas;
554
555         /* hide background */
556         canvas.set_background_color (rgba_to_color (0.0, 0.0, 1.0, 0.0));
557
558         ScrollGroup* base_color_scroll_group = new ScrollGroup (canvas.root(), ScrollGroup::ScrollSensitivity (ScrollGroup::ScrollsVertically|ScrollGroup::ScrollsHorizontally));
559         canvas.add_scroller (*base_color_scroll_group);
560         return new ArdourCanvas::Container (base_color_scroll_group);
561 }
562
563 void
564 ThemeManager::build_base_color_canvas (ArdourCanvas::Container& group, bool (ThemeManager::*event_handler)(GdkEvent*,std::string), double width, double height)
565 {
566         using namespace ArdourCanvas;
567
568         /* we want the colors sorted by hue, with their name */
569
570         UIConfiguration::RelativeColors& relatives (ARDOUR_UI::instance()->config()->relative_colors);
571         vector<NamedColor> nc;
572         for (UIConfiguration::RelativeColors::const_iterator x = relatives.begin(); x != relatives.end(); ++x) {
573                 nc.push_back (NamedColor (x->first, x->second.get()));
574         }
575         SortByHue sorter;
576         sort (nc.begin(), nc.end(), sorter);
577         
578         const uint32_t color_limit = nc.size();
579         const double box_size = 20.0;
580
581         uint32_t color_num = 0;
582
583         /* clear existing rects and delete them */
584         
585         group.clear (true);
586         
587         for (uint32_t y = 0; y < height - box_size && color_num < color_limit; y += box_size) {
588                 for (uint32_t x = 0; x < width - box_size && color_num < color_limit; x += box_size) {
589                         ArdourCanvas::Rectangle* r = new ArdourCanvas::Rectangle (&group, ArdourCanvas::Rect (x, y, x + box_size, y + box_size));
590
591                         string name = nc[color_num++].name;
592
593                         UIConfiguration::RelativeColors::iterator c = relatives.find (name);
594
595                         if (c != relatives.end()) {
596                                 Color color = c->second.get().color ();
597                                 r->set_fill_color (color);
598                                 r->set_outline_color (rgba_to_color (0.0, 0.0, 0.0, 1.0));
599                                 r->set_tooltip (name);
600                                 r->Event.connect (sigc::bind (sigc::mem_fun (*this, event_handler), name));
601                         }
602                 }
603         }
604 }
605         
606 void
607 ThemeManager::base_color_viewport_allocated (Gtk::Allocation&)
608 {
609         if (!base_color_group) {
610                 base_color_group = initialize_canvas (*base_color_viewport.canvas());
611         }
612
613         build_base_color_canvas (*base_color_group, &ThemeManager::base_color_event,
614                                  base_color_viewport.canvas()->width(),
615                                  base_color_viewport.canvas()->height());
616                                  
617 }
618
619 bool
620 ThemeManager::base_color_event (GdkEvent*ev, string name)
621 {
622         switch (ev->type) {
623         case GDK_BUTTON_RELEASE:
624                 edit_named_color (name);
625                 break;
626         default:
627                 break;
628         }
629
630         return true;
631 }
632
633 void
634 ThemeManager::edit_named_color (std::string name)
635 {
636         using namespace ArdourCanvas;
637         double r,g, b, a;
638         UIConfiguration* uic (ARDOUR_UI::instance()->config());
639         ArdourCanvas::Color c = uic->color (name);
640         Gdk::Color gdkcolor;
641
642         color_to_rgba (c, r, g, b, a);
643
644         gdkcolor.set_rgb_p (r, g, b);
645         color_dialog.get_colorsel()->set_previous_color (gdkcolor);
646         color_dialog.get_colorsel()->set_current_color (gdkcolor);
647         color_dialog.get_colorsel()->set_previous_alpha ((guint16) (a * 65535));
648         color_dialog.get_colorsel()->set_current_alpha ((guint16) (a * 65535));
649
650         base_color_edit_name = name;
651         
652         color_dialog_connection.disconnect ();
653         color_dialog_connection = color_dialog.signal_response().connect (sigc::mem_fun (*this, &ThemeManager::base_color_dialog_done));
654         color_dialog.present();
655 }
656
657 void
658 ThemeManager::base_color_dialog_done (int result)
659 {
660         using namespace ArdourCanvas;
661
662         cerr << "Done, using [" << base_color_edit_name << "] res = " << result << endl;
663         
664         if (base_color_edit_name.empty()) {
665                 color_dialog.hide ();
666                 return;
667         }
668
669         UIConfiguration* uic (ARDOUR_UI::instance()->config());
670         UIConfiguration::RelativeHSV rhsv ("", HSV());
671         Gdk::Color gdkcolor;
672         double r,g, b, a;
673
674         switch (result) {
675         case RESPONSE_ACCEPT:
676         case RESPONSE_OK:
677                 cerr << "Accepting\n";
678                 gdkcolor = color_dialog.get_colorsel()->get_current_color();
679                 a = color_dialog.get_colorsel()->get_current_alpha() / 65535.0;
680                 r = gdkcolor.get_red_p();
681                 g = gdkcolor.get_green_p();
682                 b = gdkcolor.get_blue_p();
683
684                 rhsv = uic->color_as_relative_hsv (rgba_to_color (r, g, b, a));
685                 uic->reset_relative (base_color_edit_name, rhsv);
686
687                 /* rebuild */
688                 
689                 build_base_color_canvas (*base_color_group, &ThemeManager::base_color_event,
690                                          base_color_viewport.canvas()->width(),
691                                          base_color_viewport.canvas()->height());
692                 
693                 ColorsChanged(); //EMIT SIGNAL
694                 break;
695                 
696         default:
697                 break;
698         }
699
700         color_dialog.hide ();
701         base_color_edit_name = "";
702 }
703
704 void
705 ThemeManager::palette_canvas_allocated (Gtk::Allocation& alloc, ArdourCanvas::Container* group, bool (ThemeManager::*event_handler)(GdkEvent*,std::string))
706 {
707         build_base_color_canvas (*group, event_handler, alloc.get_width(), alloc.get_height());
708 }
709
710 bool
711 ThemeManager::palette_chosen (GdkEvent* ev, string name)
712 {
713         switch (ev->type) {
714         case GDK_BUTTON_RELEASE:
715                 break;
716         default:
717                 return false;
718                 break;
719         }
720
721         UIConfiguration* uic (ARDOUR_UI::instance()->config());
722         uic->set_alias (palette_edit_name, name);
723
724         (void) palette_done ((GdkEventAny*) 0);
725
726         /* rebuild alias list with new color: inefficient but simple */
727         
728         setup_aliases ();
729         
730         return true;
731 }
732
733 bool
734 ThemeManager::palette_done (GdkEventAny*)
735 {
736         palette_edit_name = "";
737         palette_window->hide ();
738         return true;
739 }
740
741 void
742 ThemeManager::choose_color_from_palette (string const & name)
743 {
744         if (!palette_window) {
745                 palette_window = new Gtk::Window (WINDOW_TOPLEVEL);
746                 ArdourCanvas::GtkCanvas* canvas = new ArdourCanvas::GtkCanvas ();
747                 ArdourCanvas::Container* group = initialize_canvas (*canvas);
748                 
749                 canvas->signal_size_allocate().connect (sigc::bind (sigc::mem_fun (*this, &ThemeManager::palette_canvas_allocated), group, &ThemeManager::palette_chosen));
750                 palette_window->signal_delete_event().connect (sigc::mem_fun (*this, &ThemeManager::palette_done));
751                 
752                 palette_window->add (*canvas);
753                 canvas->show ();
754         }
755
756         palette_edit_name = name;
757
758         palette_window->set_position (WIN_POS_MOUSE);
759         palette_window->present ();
760 }
761
762 void
763 ThemeManager::setup_aliases ()
764 {
765         using namespace ArdourCanvas;
766         
767         UIConfiguration* uic (ARDOUR_UI::instance()->config());
768         UIConfiguration::ColorAliases& aliases (uic->color_aliases);
769
770         alias_list->clear ();
771
772         for (UIConfiguration::ColorAliases::iterator i = aliases.begin(); i != aliases.end(); ++i) {
773                 TreeModel::Row row;
774
775                 row = *(alias_list->append());
776                 row[alias_columns.name] = i->first;
777                 row[alias_columns.alias] = i->second;
778
779                 Color c = uic->color (i->second);
780
781                 /* Gdk colors don't support alpha */
782
783                 double r, g, b, a;
784                 color_to_rgba (c, r, g, b, a);
785                 Gdk::Color gcolor;
786                 gcolor.set_rgb_p (r, g, b);
787
788                 row[alias_columns.color] = gcolor;
789         }
790 }
791
792 bool
793 ThemeManager::alias_button_press_event (GdkEventButton* ev)
794 {
795         TreeIter iter;
796         TreeModel::Path path;
797         TreeViewColumn* column;
798         int cellx;
799         int celly;
800
801         if (!alias_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
802                 return false;
803         }
804
805         guint32 colnum = GPOINTER_TO_UINT (column->get_data (X_("colnum")));
806         
807         switch (colnum) {
808         case 0:
809                 /* allow normal processing to occur */
810                 return false;
811
812         case 1: /* color */
813                 if ((iter = alias_list->get_iter (path))) {
814                         choose_color_from_palette ((*iter)[alias_columns.name]);
815                 }
816                 break;
817         }
818
819         return true;
820 }