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