first mostly-working build supporting GTK rethemeing via the Theme Manager
[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         /* Trigger setting up the GTK color scheme and loading the RC file */
230         ARDOUR_UI::config()->color_theme_changed ();
231 }
232
233 ThemeManager::~ThemeManager()
234 {
235 }
236
237 int
238 ThemeManager::save (string /*path*/)
239 {
240         return 0;
241 }
242
243 bool
244 ThemeManager::button_press_event (GdkEventButton* ev)
245 {
246         TreeIter iter;
247         TreeModel::Path path;
248         TreeViewColumn* column;
249         int cellx;
250         int celly;
251
252         if (!color_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
253                 return false;
254         }
255
256         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
257         case 0:
258                 /* allow normal processing to occur */
259                 return false;
260
261         case 1: /* color */
262                 if ((iter = color_list->get_iter (path))) {
263
264                         ColorVariable<uint32_t>* var = (*iter)[columns.pVar];
265                         if (!var) {
266                                 /* parent row, do nothing */
267                                 return false;
268                         }
269                         
270                         int r,g, b, a;
271                         uint32_t rgba = (*iter)[columns.rgba];
272                         Gdk::Color color;
273
274                         UINT_TO_RGBA (rgba, &r, &g, &b, &a);
275                         color.set_rgb_p (r / 255.0, g / 255.0, b / 255.0);
276                         color_dialog.get_colorsel()->set_previous_color (color);
277                         color_dialog.get_colorsel()->set_current_color (color);
278                         color_dialog.get_colorsel()->set_previous_alpha ((guint16) (a * 256));
279                         color_dialog.get_colorsel()->set_current_alpha ((guint16) (a * 256));
280
281                         color_dialog_connection.disconnect ();
282                         color_dialog_connection = color_dialog.signal_response().connect (sigc::mem_fun (*this, &ThemeManager::foobar_response));
283                         color_dialog.present ();
284                 }
285         }
286
287         return true;
288 }
289
290 void
291 ThemeManager::foobar_response (int result)
292 {
293         // ColorVariable<uint32_t> *ccvar;
294         int r,g, b, a;
295         uint32_t rgba;
296         Gdk::Color color;
297
298         color_dialog_connection.disconnect ();
299         
300         switch (result) {
301         case RESPONSE_CANCEL:
302                 break;
303         case RESPONSE_ACCEPT:
304                 color = color_dialog.get_colorsel()->get_current_color();
305                 a = color_dialog.get_colorsel()->get_current_alpha();
306                 r = (int) floor (color.get_red_p() * 255.0);
307                 g = (int) floor (color.get_green_p() * 255.0);
308                 b = (int) floor (color.get_blue_p() * 255.0);
309                 
310                 rgba = RGBA_TO_UINT(r,g,b,a>>8);
311                 // (*iter)[columns.rgba] = rgba;
312                 // (*iter)[columns.gdkcolor] = color;
313                 
314                 // ccvar = (*iter)[columns.pVar];
315                 // ccvar->set(rgba);
316                 /* mark dirty ... */
317                 ARDOUR_UI::config()->set_dirty ();
318                 /* but save it immediately */
319                 ARDOUR_UI::config()->save_state ();
320                 
321                 ColorsChanged(); //EMIT SIGNAL
322                 break;
323                 
324         default:
325                 break;
326                 
327         }
328         
329         color_dialog.hide ();
330 }
331
332 void
333 ThemeManager::on_flat_buttons_toggled ()
334 {
335         ARDOUR_UI::config()->set_flat_buttons (flat_buttons.get_active());
336         ARDOUR_UI::config()->set_dirty ();
337         ArdourButton::set_flat_buttons (flat_buttons.get_active());
338         /* force a redraw */
339         gtk_rc_reset_styles (gtk_settings_get_default());
340 }
341
342 void
343 ThemeManager::on_blink_rec_arm_toggled ()
344 {
345         ARDOUR_UI::config()->set_blink_rec_arm (blink_rec_button.get_active());
346         ARDOUR_UI::config()->set_dirty ();
347         ARDOUR::Config->ParameterChanged("blink-rec-arm");
348 }
349
350 void
351 ThemeManager::on_region_color_toggled ()
352 {
353         ARDOUR_UI::config()->set_color_regions_using_track_color (region_color_button.get_active());
354         ARDOUR_UI::config()->set_dirty ();
355 }
356
357 void
358 ThemeManager::on_show_clip_toggled ()
359 {
360         ARDOUR_UI::config()->set_show_waveform_clipping (show_clipping_button.get_active());
361         ARDOUR_UI::config()->set_dirty ();
362 }
363
364 void
365 ThemeManager::on_all_dialogs_toggled ()
366 {
367         ARDOUR_UI::config()->set_all_floating_windows_are_dialogs (all_dialogs.get_active());
368         ARDOUR_UI::config()->set_dirty ();
369 }
370
371 void
372 ThemeManager::on_waveform_gradient_depth_change ()
373 {
374         double v = waveform_gradient_depth.get_value();
375
376         ARDOUR_UI::config()->set_waveform_gradient_depth (v);
377         ARDOUR_UI::config()->set_dirty ();
378         ArdourCanvas::WaveView::set_global_gradient_depth (v);
379 }
380
381 void
382 ThemeManager::on_timeline_item_gradient_depth_change ()
383 {
384         double v = timeline_item_gradient_depth.get_value();
385
386         ARDOUR_UI::config()->set_timeline_item_gradient_depth (v);
387         ARDOUR_UI::config()->set_dirty ();
388 }
389
390 void
391 ThemeManager::on_icon_set_changed ()
392 {
393         string new_set = icon_set_dropdown.get_active_text();
394         ARDOUR_UI::config()->set_icon_set (new_set);
395 }
396
397 void
398 ThemeManager::on_dark_theme_button_toggled()
399 {
400         if (!dark_button.get_active()) return;
401
402         UIConfiguration* uic (ARDOUR_UI::config());
403         
404         uic->set_ui_rc_file("ui_dark.rc");
405         uic->set_dirty ();
406         uic->load_rc_file (uic->get_ui_rc_file(), true);
407 }
408
409 void
410 ThemeManager::on_light_theme_button_toggled()
411 {
412         if (!light_button.get_active()) return;
413
414         UIConfiguration* uic (ARDOUR_UI::config());
415         
416         uic->set_ui_rc_file("ui_light.rc");
417         uic->set_dirty ();
418         uic->load_rc_file (uic->get_ui_rc_file(), true);
419 }
420
421 void
422 ThemeManager::setup_theme ()
423 {
424         int r, g, b, a;
425
426         color_list->clear();
427
428         for (std::map<std::string,ColorVariable<uint32_t> *>::iterator i = ARDOUR_UI::config()->configurable_colors.begin(); i != ARDOUR_UI::config()->configurable_colors.end(); i++) {
429
430
431                 ColorVariable<uint32_t>* var = i->second;
432
433                 TreeModel::Children rows = color_list->children();
434                 TreeModel::Row row;
435                 string::size_type colon;
436
437                 if ((colon = var->name().find (':')) != string::npos) {
438
439                         /* this is supposed to be a child node, so find the
440                          * parent 
441                          */
442
443                         string parent = var->name().substr (0, colon);
444                         TreeModel::iterator ri;
445
446                         for (ri = rows.begin(); ri != rows.end(); ++ri) {
447                                 string s = (*ri)[columns.name];
448                                 if (s == parent) {
449                                         break;
450                                 }
451                         }
452
453                         if (ri == rows.end()) {
454                                 /* not found, add the parent as new top level row */
455                                 row = *(color_list->append());
456                                 row[columns.name] = parent;
457                                 row[columns.pVar] = 0;
458                                 
459                                 /* now add the child as a child of this one */
460
461                                 row = *(color_list->insert (row->children().end()));
462                                 row[columns.name] = var->name().substr (colon+1);
463                         } else {
464                                 row = *(color_list->insert ((*ri)->children().end()));
465                                 row[columns.name] = var->name().substr (colon+1);
466                         }
467
468                 } else {
469                         /* add as a child */
470                         row = *(color_list->append());
471                         row[columns.name] = var->name();
472                 }
473
474                 Gdk::Color col;
475                 uint32_t rgba = var->get();
476                 UINT_TO_RGBA (rgba, &r, &g, &b, &a);
477                 //cerr << (*i)->name() << " == " << hex << rgba << ": " << hex << r << " " << hex << g << " " << hex << b << endl;
478                 col.set_rgb_p (r / 255.0, g / 255.0, b / 255.0);
479
480                 row[columns.pVar] = var;
481                 row[columns.rgba] = rgba;
482                 row[columns.gdkcolor] = col;
483         }
484
485         UIConfiguration* uic (ARDOUR_UI::config());
486         
487         flat_buttons.set_active (uic->get_flat_buttons());
488         blink_rec_button.set_active (uic->get_blink_rec_arm());
489         waveform_gradient_depth.set_value (uic->get_waveform_gradient_depth());
490         timeline_item_gradient_depth.set_value (uic->get_timeline_item_gradient_depth());
491         all_dialogs.set_active (uic->get_all_floating_windows_are_dialogs());
492 }
493
494 void
495 ThemeManager::reset_canvas_colors()
496 {
497         ARDOUR_UI::config()->load_defaults();
498         setup_theme ();
499         ARDOUR_UI::config()->set_dirty ();
500         ARDOUR_UI::config()->save_state ();
501 }
502
503 struct NamedColor {
504         string name;
505         ArdourCanvas::HSV    color;
506         NamedColor (string s, ArdourCanvas::HSV c) : name (s), color (c) {}
507 };
508
509 struct SortByHue {
510         bool operator() (NamedColor const & a, NamedColor const & b) {
511                 using namespace ArdourCanvas;
512                 const HSV black (0, 0, 0);
513                 if (a.color.is_gray() || b.color.is_gray()) {
514                         return black.distance (a.color) < black.distance (b.color);
515                 } else {
516                         return a.color.h < b.color.h;
517                         // const HSV red (rgba_to_color (1.0, 0.0, 0.0, 1.0));
518                         // return red.distance (a.color) < red.distance (b.color);
519                 }
520         }
521 };
522
523 ArdourCanvas::Container*
524 ThemeManager::initialize_canvas (ArdourCanvas::Canvas& canvas)
525 {
526         using namespace ArdourCanvas;
527
528         /* hide background */
529         canvas.set_background_color (rgba_to_color (0.0, 0.0, 1.0, 0.0));
530
531         ScrollGroup* base_color_scroll_group = new ScrollGroup (canvas.root(), ScrollGroup::ScrollSensitivity (ScrollGroup::ScrollsVertically|ScrollGroup::ScrollsHorizontally));
532         canvas.add_scroller (*base_color_scroll_group);
533         return new ArdourCanvas::Container (base_color_scroll_group);
534 }
535
536 void
537 ThemeManager::build_base_color_canvas (ArdourCanvas::Container& group, bool (ThemeManager::*event_handler)(GdkEvent*,std::string), double width, double height)
538 {
539         using namespace ArdourCanvas;
540
541         /* we want the colors sorted by hue, with their name */
542
543         UIConfiguration::RelativeColors& relatives (ARDOUR_UI::instance()->config()->relative_colors);
544         vector<NamedColor> nc;
545         for (UIConfiguration::RelativeColors::const_iterator x = relatives.begin(); x != relatives.end(); ++x) {
546                 nc.push_back (NamedColor (x->first, x->second.get()));
547         }
548         SortByHue sorter;
549         sort (nc.begin(), nc.end(), sorter);
550         
551         const uint32_t color_limit = nc.size();
552         const double box_size = 20.0;
553
554         uint32_t color_num = 0;
555
556         /* clear existing rects and delete them */
557         
558         group.clear (true);
559         
560         for (uint32_t y = 0; y < height - box_size && color_num < color_limit; y += box_size) {
561                 for (uint32_t x = 0; x < width - box_size && color_num < color_limit; x += box_size) {
562                         ArdourCanvas::Rectangle* r = new ArdourCanvas::Rectangle (&group, ArdourCanvas::Rect (x, y, x + box_size, y + box_size));
563
564                         string name = nc[color_num++].name;
565
566                         UIConfiguration::RelativeColors::iterator c = relatives.find (name);
567
568                         if (c != relatives.end()) {
569                                 Color color = c->second.get().color ();
570                                 r->set_fill_color (color);
571                                 r->set_outline_color (rgba_to_color (0.0, 0.0, 0.0, 1.0));
572                                 r->set_tooltip (name);
573                                 r->Event.connect (sigc::bind (sigc::mem_fun (*this, event_handler), name));
574                         }
575                 }
576         }
577 }
578         
579 void
580 ThemeManager::base_color_viewport_allocated (Gtk::Allocation&)
581 {
582         if (!base_color_group) {
583                 base_color_group = initialize_canvas (*base_color_viewport.canvas());
584         }
585
586         build_base_color_canvas (*base_color_group, &ThemeManager::base_color_event,
587                                  base_color_viewport.canvas()->width(),
588                                  base_color_viewport.canvas()->height());
589                                  
590 }
591
592 bool
593 ThemeManager::base_color_event (GdkEvent*ev, string name)
594 {
595         switch (ev->type) {
596         case GDK_BUTTON_RELEASE:
597                 edit_named_color (name);
598                 break;
599         default:
600                 break;
601         }
602
603         return true;
604 }
605
606 void
607 ThemeManager::edit_named_color (std::string name)
608 {
609         using namespace ArdourCanvas;
610         double r,g, b, a;
611         UIConfiguration* uic (ARDOUR_UI::instance()->config());
612         ArdourCanvas::Color c = uic->color (name);
613         Gdk::Color gdkcolor;
614
615         color_to_rgba (c, r, g, b, a);
616
617         gdkcolor.set_rgb_p (r, g, b);
618         color_dialog.get_colorsel()->set_previous_color (gdkcolor);
619         color_dialog.get_colorsel()->set_current_color (gdkcolor);
620         color_dialog.get_colorsel()->set_previous_alpha ((guint16) (a * 65535));
621         color_dialog.get_colorsel()->set_current_alpha ((guint16) (a * 65535));
622
623         base_color_edit_name = name;
624         
625         color_dialog_connection.disconnect ();
626         color_dialog_connection = color_dialog.signal_response().connect (sigc::mem_fun (*this, &ThemeManager::base_color_dialog_done));
627         color_dialog.present();
628 }
629
630 void
631 ThemeManager::base_color_dialog_done (int result)
632 {
633         using namespace ArdourCanvas;
634
635         cerr << "Done, using [" << base_color_edit_name << "] res = " << result << endl;
636         
637         if (base_color_edit_name.empty()) {
638                 color_dialog.hide ();
639                 return;
640         }
641
642         UIConfiguration* uic (ARDOUR_UI::instance()->config());
643         UIConfiguration::RelativeHSV rhsv ("", HSV());
644         Gdk::Color gdkcolor;
645         double r,g, b, a;
646
647         switch (result) {
648         case RESPONSE_ACCEPT:
649         case RESPONSE_OK:
650                 cerr << "Accepting\n";
651                 gdkcolor = color_dialog.get_colorsel()->get_current_color();
652                 a = color_dialog.get_colorsel()->get_current_alpha() / 65535.0;
653                 r = gdkcolor.get_red_p();
654                 g = gdkcolor.get_green_p();
655                 b = gdkcolor.get_blue_p();
656
657                 rhsv = uic->color_as_relative_hsv (rgba_to_color (r, g, b, a));
658                 uic->reset_relative (base_color_edit_name, rhsv);
659
660                 /* rebuild */
661                 
662                 build_base_color_canvas (*base_color_group, &ThemeManager::base_color_event,
663                                          base_color_viewport.canvas()->width(),
664                                          base_color_viewport.canvas()->height());
665                 
666                 ColorsChanged(); //EMIT SIGNAL
667                 break;
668                 
669         default:
670                 break;
671         }
672
673         color_dialog.hide ();
674         base_color_edit_name = "";
675 }
676
677 void
678 ThemeManager::palette_canvas_allocated (Gtk::Allocation& alloc, ArdourCanvas::Container* group, bool (ThemeManager::*event_handler)(GdkEvent*,std::string))
679 {
680         build_base_color_canvas (*group, event_handler, alloc.get_width(), alloc.get_height());
681 }
682
683 bool
684 ThemeManager::palette_chosen (GdkEvent* ev, string name)
685 {
686         switch (ev->type) {
687         case GDK_BUTTON_RELEASE:
688                 break;
689         default:
690                 return false;
691                 break;
692         }
693
694         UIConfiguration* uic (ARDOUR_UI::instance()->config());
695         uic->set_alias (palette_edit_name, name);
696
697         (void) palette_done ((GdkEventAny*) 0);
698
699         /* rebuild alias list with new color: inefficient but simple */
700         
701         setup_aliases ();
702         
703         return true;
704 }
705
706 bool
707 ThemeManager::palette_done (GdkEventAny*)
708 {
709         palette_edit_name = "";
710         palette_window->hide ();
711         return true;
712 }
713
714 void
715 ThemeManager::choose_color_from_palette (string const & name)
716 {
717         if (!palette_window) {
718                 palette_window = new Gtk::Window (WINDOW_TOPLEVEL);
719                 ArdourCanvas::GtkCanvas* canvas = new ArdourCanvas::GtkCanvas ();
720                 ArdourCanvas::Container* group = initialize_canvas (*canvas);
721                 
722                 canvas->signal_size_allocate().connect (sigc::bind (sigc::mem_fun (*this, &ThemeManager::palette_canvas_allocated), group, &ThemeManager::palette_chosen));
723                 palette_window->signal_delete_event().connect (sigc::mem_fun (*this, &ThemeManager::palette_done));
724                 
725                 palette_window->add (*canvas);
726                 canvas->show ();
727         }
728
729         palette_edit_name = name;
730
731         palette_window->set_position (WIN_POS_MOUSE);
732         palette_window->present ();
733 }
734
735 void
736 ThemeManager::setup_aliases ()
737 {
738         using namespace ArdourCanvas;
739         
740         UIConfiguration* uic (ARDOUR_UI::instance()->config());
741         UIConfiguration::ColorAliases& aliases (uic->color_aliases);
742
743         alias_list->clear ();
744
745         for (UIConfiguration::ColorAliases::iterator i = aliases.begin(); i != aliases.end(); ++i) {
746                 TreeModel::Row row;
747
748                 row = *(alias_list->append());
749                 row[alias_columns.name] = i->first;
750                 row[alias_columns.alias] = i->second;
751
752                 Color c = uic->color (i->second);
753
754                 /* Gdk colors don't support alpha */
755
756                 double r, g, b, a;
757                 color_to_rgba (c, r, g, b, a);
758                 Gdk::Color gcolor;
759                 gcolor.set_rgb_p (r, g, b);
760
761                 row[alias_columns.color] = gcolor;
762         }
763 }
764
765 bool
766 ThemeManager::alias_button_press_event (GdkEventButton* ev)
767 {
768         TreeIter iter;
769         TreeModel::Path path;
770         TreeViewColumn* column;
771         int cellx;
772         int celly;
773
774         if (!alias_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
775                 return false;
776         }
777
778         guint32 colnum = GPOINTER_TO_UINT (column->get_data (X_("colnum")));
779         
780         switch (colnum) {
781         case 0:
782                 /* allow normal processing to occur */
783                 return false;
784
785         case 1: /* color */
786                 if ((iter = alias_list->get_iter (path))) {
787                         choose_color_from_palette ((*iter)[alias_columns.name]);
788                 }
789                 break;
790         }
791
792         return true;
793 }