b7dc9b303ac8de6c4987ee16e17b95e6176574f4
[ardour.git] / gtk2_ardour / color_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 "gtkmm2ext/cell_renderer_color_selector.h"
28 #include "gtkmm2ext/utils.h"
29
30 #include "pbd/file_utils.h"
31 #include "pbd/compose.h"
32
33 #include "ardour/filesystem_paths.h"
34 #include "ardour/profile.h"
35
36 #include "canvas/container.h"
37 #include "canvas/rectangle.h"
38 #include "canvas/scroll_group.h"
39 #include "canvas/wave_view.h"
40
41 #include "ardour_button.h"
42 #include "ardour_dialog.h"
43 #include "color_theme_manager.h"
44 #include "rgb_macros.h"
45 #include "ui_config.h"
46 #include "utils.h"
47
48 #include "i18n.h"
49
50 using namespace std;
51 using namespace Gtk;
52 using namespace PBD;
53 using namespace ARDOUR;
54 using namespace ARDOUR_UI_UTILS;
55
56 ColorThemeManager::ColorThemeManager ()
57         : reset_button (_("Restore Defaults"))
58         , palette_viewport (*palette_scroller.get_hadjustment(), *palette_scroller.get_vadjustment())
59         , palette_group (0)
60         , palette_window (0)
61         , color_theme_label (_("Color Theme"))
62 {
63         set_spacing (12);
64
65         std::map<string,string> color_themes;
66
67         get_color_themes (color_themes);
68
69         if (color_themes.size() > 1) {
70                 theme_list = TreeStore::create (color_theme_columns);
71
72                 TreeModel::iterator selected_iter = theme_list->children().end();
73
74                 for (std::map<string,string>::iterator c = color_themes.begin(); c != color_themes.end(); ++c) {
75                         TreeModel::Row row;
76
77                         row = *(theme_list->append());
78                         row[color_theme_columns.name] = c->first;
79                         row[color_theme_columns.path] = c->second;
80
81                         /* match second (path; really basename) since that is
82                            what we store/restore.
83                         */
84
85                         if (UIConfiguration::instance().get_color_file() == c->second) {
86                                 selected_iter = row;
87                         }
88                 }
89
90                 color_theme_dropdown.set_model (theme_list);
91                 color_theme_dropdown.pack_start (color_theme_columns.name);
92
93                 if (selected_iter != theme_list->children().end()) {
94                         color_theme_dropdown.set_active (selected_iter);
95                 }
96
97                 Gtk::HBox* hbox = Gtk::manage (new Gtk::HBox());
98                 Gtk::Alignment* align = Gtk::manage (new Gtk::Alignment);
99                 align->set (0, 0.5);
100                 align->add (color_theme_dropdown);
101                 hbox->set_spacing (6);
102                 hbox->pack_start (color_theme_label, false, false);
103                 hbox->pack_start (*align, true, true);
104                 pack_start (*hbox, PACK_SHRINK);
105                 hbox->show_all ();
106         }
107
108         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &ColorThemeManager::reset_canvas_colors));
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"), alias_columns.name);
115
116         Gtkmm2ext::CellRendererColorSelector* color_renderer = manage (new Gtkmm2ext::CellRendererColorSelector);
117         TreeViewColumn* color_column = manage (new TreeViewColumn (_("Color"), *color_renderer));
118         color_column->add_attribute (color_renderer->property_color(), alias_columns.color);
119         alias_display.append_column (*color_column);
120
121         alias_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
122         alias_display.get_column (0)->set_expand (true);
123         alias_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
124         alias_display.get_column (1)->set_expand (false);
125         alias_display.set_reorderable (false);
126         alias_display.set_headers_visible (true);
127
128         alias_display.signal_button_press_event().connect (sigc::mem_fun (*this, &ColorThemeManager::alias_button_press_event), false);
129
130         alias_scroller.add (alias_display);
131
132         palette_group = initialize_palette_canvas (*palette_viewport.canvas());
133         palette_viewport.signal_size_allocate().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::palette_canvas_allocated), palette_group, palette_viewport.canvas(),
134                                                                      sigc::mem_fun (*this, &ColorThemeManager::palette_event)));
135         palette_scroller.add (palette_viewport);
136
137         modifier_scroller.add (modifier_vbox);
138
139         notebook.append_page (alias_scroller, _("Items"));
140         notebook.append_page (palette_scroller, _("Palette"));
141         notebook.append_page (modifier_scroller, _("Transparency"));
142
143         notebook.set_size_request (400, 400);
144
145         pack_start (notebook, true, true);
146         pack_start (reset_button, false, false);
147
148         color_dialog.get_colorsel()->set_has_opacity_control (true);
149         color_dialog.get_colorsel()->set_has_palette (true);
150         color_dialog.get_ok_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
151         color_dialog.get_cancel_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
152
153         color_theme_dropdown.signal_changed().connect (sigc::mem_fun (*this, &ColorThemeManager::on_color_theme_changed));
154
155         /* no need to call setup_palette() here, it will be done when its size is allocated */
156         setup_aliases ();
157         setup_modifiers ();
158
159         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ColorThemeManager::colors_changed));
160
161         show_all ();
162 }
163
164 void
165 ColorThemeManager::setup_modifiers ()
166 {
167         UIConfiguration* uic (&UIConfiguration::instance());
168         UIConfiguration::Modifiers& modifiers (uic->modifiers);
169         Gtk::HBox* mod_hbox;
170         Gtk::Label* mod_label;
171         Gtk::HScale* mod_scale;
172
173         Gtkmm2ext::container_clear (modifier_vbox);
174
175         for (UIConfiguration::Modifiers::const_iterator m = modifiers.begin(); m != modifiers.end(); ++m) {
176                 mod_hbox = manage (new HBox);
177
178                 mod_scale = manage (new HScale (0.0, 1.0, 0.01));
179                 mod_scale->set_draw_value (false);
180                 mod_scale->set_value (m->second.a());
181                 mod_scale->set_update_policy (Gtk::UPDATE_DISCONTINUOUS);
182                 mod_scale->signal_value_changed().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::modifier_edited), mod_scale, m->first));
183
184                 mod_label = manage (new Label (m->first));
185                 mod_label->set_alignment (1.0, 0.5);
186                 mod_label->set_size_request (150, -1); /* 150 pixels should be enough for anyone */
187
188                 mod_hbox->pack_start (*mod_label, false, true, 12);
189                 mod_hbox->pack_start (*mod_scale, true, true);
190
191                 modifier_vbox.pack_start (*mod_hbox, false, false);
192         }
193
194         modifier_vbox.show_all ();
195
196 }
197
198 void
199 ColorThemeManager::modifier_edited (Gtk::Range* range, string name)
200 {
201         using namespace ArdourCanvas;
202
203         double alpha = range->get_value();
204         SVAModifier svam (SVAModifier::Assign, -1.0, -1.0, alpha);
205         UIConfiguration::instance().set_modifier (name, svam);
206 }
207
208 void
209 ColorThemeManager::colors_changed ()
210 {
211         setup_palette ();
212         setup_aliases ();
213         setup_modifiers ();
214 }
215
216 void
217 ColorThemeManager::reset_canvas_colors()
218 {
219         string cfile;
220         string basename;
221
222         basename = UIConfiguration::instance().color_file_name (true, true, true);
223
224         if (find_file (ardour_config_search_path(), basename, cfile)) {
225                 string backup = cfile + string (X_(".old"));
226                 g_rename (cfile.c_str(), backup.c_str());
227                 /* don't really care if it fails */
228         }
229
230         UIConfiguration::instance().load_defaults();
231         UIConfiguration::instance().save_state ();
232 }
233
234 ArdourCanvas::Container*
235 ColorThemeManager::initialize_palette_canvas (ArdourCanvas::Canvas& canvas)
236 {
237         using namespace ArdourCanvas;
238
239         /* hide background */
240         canvas.set_background_color (rgba_to_color (0.0, 0.0, 1.0, 0.0));
241
242         /* bi-directional scroll group */
243
244         ScrollGroup* scroll_group = new ScrollGroup (canvas.root(), ScrollGroup::ScrollSensitivity (ScrollGroup::ScrollsVertically|ScrollGroup::ScrollsHorizontally));
245         canvas.add_scroller (*scroll_group);
246
247         /* new container to hold everything */
248
249         return new ArdourCanvas::Container (scroll_group);
250 }
251
252 void
253 ColorThemeManager::palette_canvas_allocated (Gtk::Allocation& alloc, ArdourCanvas::Container* group, ArdourCanvas::Canvas* canvas, sigc::slot<bool,GdkEvent*,std::string> event_handler)
254 {
255         build_palette_canvas (*canvas, *group, event_handler);
256 }
257
258 struct NamedColor {
259         string name;
260         ArdourCanvas::HSV    color;
261         NamedColor (string s, ArdourCanvas::HSV c) : name (s), color (c) {}
262 };
263
264 struct SortByHue {
265         bool operator() (NamedColor const & a, NamedColor const & b) {
266                 using namespace ArdourCanvas;
267                 const HSV black (0, 0, 0);
268                 if (a.color.is_gray() || b.color.is_gray()) {
269                         return black.distance (a.color) < black.distance (b.color);
270                 } else {
271                         return a.color.h < b.color.h;
272                         // const HSV red (rgba_to_color (1.0, 0.0, 0.0, 1.0));
273                         // return red.distance (a.color) < red.distance (b.color);
274                 }
275         }
276 };
277
278
279 void
280 ColorThemeManager::build_palette_canvas (ArdourCanvas::Canvas& canvas, ArdourCanvas::Container& group, sigc::slot<bool,GdkEvent*,std::string> event_handler)
281 {
282         using namespace ArdourCanvas;
283
284         /* we want the colors sorted by hue, with their name */
285
286         UIConfiguration::Colors& colors (UIConfiguration::instance().colors);
287         vector<NamedColor> nc;
288         for (UIConfiguration::Colors::const_iterator x = colors.begin(); x != colors.end(); ++x) {
289                 nc.push_back (NamedColor (x->first, HSV (x->second)));
290         }
291         SortByHue sorter;
292         sort (nc.begin(), nc.end(), sorter);
293
294         const uint32_t color_limit = nc.size();
295         const double box_size = 20.0;
296         const double width = canvas.width();
297         const double height = canvas.height();
298
299         uint32_t color_num = 0;
300
301         /* clear existing rects and delete them */
302
303         group.clear (true);
304
305         for (uint32_t y = 0; y < height - box_size && color_num < color_limit; y += box_size) {
306                 for (uint32_t x = 0; x < width - box_size && color_num < color_limit; x += box_size) {
307                         ArdourCanvas::Rectangle* r = new ArdourCanvas::Rectangle (&group, ArdourCanvas::Rect (x, y, x + box_size, y + box_size));
308
309                         string name = nc[color_num++].name;
310
311                         UIConfiguration::Colors::iterator c = colors.find (name);
312
313                         if (c != colors.end()) {
314                                 Color color = c->second;
315                                 r->set_fill_color (color);
316                                 r->set_outline_color (rgba_to_color (0.0, 0.0, 0.0, 1.0));
317                                 r->set_tooltip (name);
318                                 r->Event.connect (sigc::bind (event_handler, name));
319                         }
320                 }
321         }
322 }
323
324 void
325 ColorThemeManager::palette_size_request (Gtk::Requisition* req)
326 {
327         uint32_t ncolors = UIConfiguration::instance().colors.size();
328         const int box_size = 20;
329
330         double c = sqrt ((double)ncolors);
331         req->width = (int) floor (c * box_size);
332         req->height = (int) floor (c * box_size);
333
334         /* add overflow row if necessary */
335
336         if (fmod (ncolors, c) != 0.0) {
337                 req->height += box_size;
338         }
339 }
340
341 void
342 ColorThemeManager::setup_palette ()
343 {
344         build_palette_canvas (*palette_viewport.canvas(), *palette_group, sigc::mem_fun (*this, &ColorThemeManager::palette_event));
345 }
346
347 bool
348 ColorThemeManager::palette_event (GdkEvent* ev, string name)
349 {
350         switch (ev->type) {
351         case GDK_BUTTON_RELEASE:
352                 edit_palette_color (name);
353                 return true;
354         default:
355                 break;
356         }
357         return true;
358 }
359
360 void
361 ColorThemeManager::edit_palette_color (std::string name)
362 {
363         using namespace ArdourCanvas;
364         double r,g, b, a;
365         UIConfiguration* uic (&UIConfiguration::instance());
366         ArdourCanvas::Color c = uic->color (name);
367         Gdk::Color gdkcolor;
368
369         color_to_rgba (c, r, g, b, a);
370
371         gdkcolor.set_rgb_p (r, g, b);
372         color_dialog.get_colorsel()->set_previous_color (gdkcolor);
373         color_dialog.get_colorsel()->set_current_color (gdkcolor);
374         color_dialog.get_colorsel()->set_previous_alpha ((guint16) (a * 65535));
375         color_dialog.get_colorsel()->set_current_alpha ((guint16) (a * 65535));
376
377         color_dialog_connection.disconnect ();
378         color_dialog_connection = color_dialog.signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::palette_color_response), name));
379         color_dialog.present();
380 }
381
382 void
383 ColorThemeManager::palette_color_response (int result, std::string name)
384 {
385         using namespace ArdourCanvas;
386
387         color_dialog_connection.disconnect ();
388
389         UIConfiguration* uic (&UIConfiguration::instance());
390         Gdk::Color gdkcolor;
391         double r,g, b, a;
392
393         switch (result) {
394         case RESPONSE_ACCEPT:
395         case RESPONSE_OK:
396                 gdkcolor = color_dialog.get_colorsel()->get_current_color();
397                 a = color_dialog.get_colorsel()->get_current_alpha() / 65535.0;
398                 r = gdkcolor.get_red_p();
399                 g = gdkcolor.get_green_p();
400                 b = gdkcolor.get_blue_p();
401
402                 uic->set_color (name, rgba_to_color (r, g, b, a));
403                 break;
404
405         default:
406                 break;
407         }
408
409         color_dialog.hide ();
410 }
411
412 bool
413 ColorThemeManager::alias_palette_event (GdkEvent* ev, string new_alias, string target_name)
414 {
415         switch (ev->type) {
416         case GDK_BUTTON_RELEASE:
417                 UIConfiguration::instance().set_alias (target_name, new_alias);
418                 return true;
419                 break;
420         default:
421                 break;
422         }
423         return false;
424 }
425
426 void
427 ColorThemeManager::alias_palette_response (int response, std::string target_name, std::string old_alias)
428 {
429         switch (response) {
430         case GTK_RESPONSE_OK:
431         case GTK_RESPONSE_ACCEPT:
432                 /* rebuild alias list with new color: inefficient but simple */
433                 setup_aliases ();
434                 break;
435
436         case GTK_RESPONSE_REJECT:
437                 /* revert choice */
438                 UIConfiguration::instance().set_alias (target_name, old_alias);
439                 break;
440
441         default:
442                 /* do nothing */
443                 break;
444         }
445
446         palette_window->hide ();
447 }
448
449 void
450 ColorThemeManager::choose_color_from_palette (string const & name)
451 {
452         UIConfiguration* uic (&UIConfiguration::instance());
453         UIConfiguration::ColorAliases::iterator i = uic->color_aliases.find (name);
454
455         if (i == uic->color_aliases.end()) {
456                 return;
457         }
458
459         delete palette_window;
460
461         palette_window = new ArdourDialog (_("Color Palette"));
462         palette_window->add_button (Stock::CANCEL, RESPONSE_REJECT); /* using CANCEL causes confusion if dialog is closed via CloseAllDialogs */
463         palette_window->add_button (Stock::OK, RESPONSE_OK);
464
465         ArdourCanvas::GtkCanvas* canvas = new ArdourCanvas::GtkCanvas ();
466         ArdourCanvas::Container* group = initialize_palette_canvas (*canvas);
467
468         canvas->signal_size_request().connect (sigc::mem_fun (*this, &ColorThemeManager::palette_size_request));
469         canvas->signal_size_allocate().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::palette_canvas_allocated), group, canvas,
470                                                             sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::alias_palette_event), name)));
471
472         palette_window->get_vbox()->pack_start (*canvas);
473         palette_window->show_all ();
474
475         palette_response_connection = palette_window->signal_response().connect (sigc::bind (sigc::mem_fun (*this, &ColorThemeManager::alias_palette_response), name, i->second));
476
477         palette_window->set_position (WIN_POS_MOUSE);
478         palette_window->present ();
479 }
480
481 void
482 ColorThemeManager::setup_aliases ()
483 {
484         using namespace ArdourCanvas;
485
486         UIConfiguration* uic (&UIConfiguration::instance());
487         UIConfiguration::ColorAliases& aliases (uic->color_aliases);
488
489         alias_list->clear ();
490
491         for (UIConfiguration::ColorAliases::iterator i = aliases.begin(); i != aliases.end(); ++i) {
492                 TreeModel::Children rows = alias_list->children();
493                 TreeModel::Row row;
494                 string::size_type colon;
495
496                 if ((colon = i->first.find (':')) != string::npos) {
497
498                         /* this is supposed to be a child node, so find the
499                          * parent
500                          */
501
502                         string parent = i->first.substr (0, colon);
503                         TreeModel::iterator ri;
504
505                         for (ri = rows.begin(); ri != rows.end(); ++ri) {
506                                 string s = (*ri)[alias_columns.name];
507                                 if (s == parent) {
508                                         break;
509                                 }
510                         }
511
512                         if (ri == rows.end()) {
513                                 /* not found, add the parent as new top level row */
514                                 row = *(alias_list->append());
515                                 row[alias_columns.name] = parent;
516                                 row[alias_columns.alias] = "";
517
518                                 /* now add the child as a child of this one */
519
520                                 row = *(alias_list->insert (row->children().end()));
521                                 row[alias_columns.name] = i->first.substr (colon+1);
522                         } else {
523                                 row = *(alias_list->insert ((*ri)->children().end()));
524                                 row[alias_columns.name] = i->first.substr (colon+1);
525                         }
526
527                 } else {
528                         /* add as a child */
529                         row = *(alias_list->append());
530                         row[alias_columns.name] = i->first;
531                         row[alias_columns.key] = i->first;
532                 }
533
534                 row[alias_columns.key] = i->first;
535                 row[alias_columns.alias] = i->second;
536
537                 Gdk::Color col;
538                 double r, g, b, a;
539                 Color c (uic->color (i->second));
540                 color_to_rgba (c, r, g, b, a);
541                 col.set_rgb_p (r, g, b);
542
543                 row[alias_columns.color] = col;
544         }
545 }
546
547 bool
548 ColorThemeManager::alias_button_press_event (GdkEventButton* ev)
549 {
550         TreeIter iter;
551         TreeModel::Path path;
552         TreeViewColumn* column;
553         int cellx;
554         int celly;
555
556         if (!alias_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
557                 return false;
558         }
559
560         guint32 colnum = GPOINTER_TO_UINT (column->get_data (X_("colnum")));
561
562         switch (colnum) {
563         case 0:
564                 /* allow normal processing to occur */
565                 return false;
566
567         case 1: /* color */
568                 if ((iter = alias_list->get_iter (path))) {
569                         string target_color_alias = (*iter)[alias_columns.key];
570                         if (!target_color_alias.empty()) {
571                                 choose_color_from_palette (target_color_alias);
572                         }
573                 }
574                 break;
575         }
576
577         return true;
578 }
579
580 void
581 ColorThemeManager::parameter_changed (string const&)
582 {
583 }
584
585 void
586 ColorThemeManager::set_state_from_config ()
587 {
588
589 }
590
591 void
592 ColorThemeManager::add_to_page (OptionEditorPage* page)
593 {
594         add_widget_to_page (page, this);
595 }
596
597 Gtk::Widget&
598 ColorThemeManager::tip_widget()
599 {
600         return reset_button; /* XXX need a better widget for this purpose */
601 }
602
603 void
604 ColorThemeManager::on_color_theme_changed ()
605 {
606         Gtk::TreeModel::iterator iter = color_theme_dropdown.get_active();
607
608         if (iter) {
609                 Gtk::TreeModel::Row row = *iter;
610
611                 if (row) {
612                         string new_theme = row[color_theme_columns.path];
613                         UIConfiguration::instance().set_color_file (new_theme);
614                 }
615         }
616 }
617