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