add option to blink rec-enables
[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/wave_view.h"
40
41 #include "ardour_button.h"
42 #include "theme_manager.h"
43 #include "rgb_macros.h"
44 #include "ardour_ui.h"
45 #include "global_signals.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 namespace ARDOUR_UI_UTILS {
57         sigc::signal<void> ColorsChanged;
58         sigc::signal<void,uint32_t> ColorChanged;
59 }
60
61 ThemeManager::ThemeManager()
62         : ArdourWindow (_("Theme Manager"))
63         , dark_button (_("Dark Theme"))
64         , light_button (_("Light Theme"))
65         , reset_button (_("Restore Defaults"))
66         , flat_buttons (_("Draw \"flat\" buttons"))
67         , blink_rec_button (_("Blink Rec-Arm buttons"))
68         , region_color_button (_("Color regions using their track's color"))
69         , show_clipping_button (_("Show waveform clipping"))
70         , waveform_gradient_depth (0, 1.0, 0.05)
71         , waveform_gradient_depth_label (_("Waveforms color gradient depth"))
72         , timeline_item_gradient_depth (0, 1.0, 0.05)
73         , timeline_item_gradient_depth_label (_("Timeline item gradient depth"))
74         , all_dialogs (_("All floating windows are dialogs"))
75         , icon_set_label (_("Icon Set"))
76 {
77         set_title (_("Theme Manager"));
78
79         color_list = TreeStore::create (columns);
80         color_display.set_model (color_list);
81         color_display.append_column (_("Object"), columns.name);
82
83         Gtkmm2ext::CellRendererColorSelector* color_renderer = manage (new Gtkmm2ext::CellRendererColorSelector);
84         TreeViewColumn* color_column = manage (new TreeViewColumn (_("Color"), *color_renderer));
85         color_column->add_attribute (color_renderer->property_color(), columns.gdkcolor);
86
87         color_display.append_column (*color_column);
88
89         color_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
90         color_display.get_column (0)->set_expand (true);
91         color_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
92         color_display.get_column (1)->set_expand (false);
93         color_display.set_reorderable (false);
94         color_display.get_selection()->set_mode (SELECTION_NONE);
95         color_display.set_headers_visible (true);
96
97         scroller.add (color_display);
98         scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
99
100         RadioButton::Group group = dark_button.get_group();
101         light_button.set_group(group);
102         theme_selection_hbox.set_homogeneous(false);
103         theme_selection_hbox.pack_start (dark_button);
104         theme_selection_hbox.pack_start (light_button);
105
106         Gtk::VBox* vbox = Gtk::manage (new Gtk::VBox ());
107         vbox->set_homogeneous (false);
108         vbox->pack_start (theme_selection_hbox, PACK_SHRINK);
109         vbox->pack_start (reset_button, PACK_SHRINK);
110 #ifndef __APPLE__
111         vbox->pack_start (all_dialogs, PACK_SHRINK);
112 #endif
113         vbox->pack_start (flat_buttons, PACK_SHRINK);
114         vbox->pack_start (blink_rec_button, PACK_SHRINK);
115         vbox->pack_start (region_color_button, PACK_SHRINK);
116         vbox->pack_start (show_clipping_button, PACK_SHRINK);
117
118         Gtk::HBox* hbox;
119
120         vector<string> icon_sets = ::get_icon_sets ();
121
122         if (icon_sets.size() > 1) {
123                 Gtkmm2ext::set_popdown_strings (icon_set_dropdown, icon_sets);
124                 icon_set_dropdown.set_active_text (ARDOUR_UI::config()->get_icon_set());
125
126                 hbox = Gtk::manage (new Gtk::HBox());
127                 hbox->set_spacing (6);
128                 hbox->pack_start (icon_set_label, false, false);
129                 hbox->pack_start (icon_set_dropdown, true, true);
130                 vbox->pack_start (*hbox, PACK_SHRINK);
131         }
132
133         
134         hbox = Gtk::manage (new Gtk::HBox());
135         hbox->set_spacing (6);
136         hbox->pack_start (waveform_gradient_depth, true, true);
137         hbox->pack_start (waveform_gradient_depth_label, false, false);
138         vbox->pack_start (*hbox, PACK_SHRINK);
139
140         hbox = Gtk::manage (new Gtk::HBox());
141         hbox->set_spacing (6);
142         hbox->pack_start (timeline_item_gradient_depth, true, true);
143         hbox->pack_start (timeline_item_gradient_depth_label, false, false);
144         vbox->pack_start (*hbox, PACK_SHRINK);
145
146         vbox->pack_start (scroller);
147
148         vbox->show_all ();
149
150         add (*vbox);
151
152         waveform_gradient_depth.set_update_policy (Gtk::UPDATE_DELAYED);
153         timeline_item_gradient_depth.set_update_policy (Gtk::UPDATE_DELAYED);
154         
155         color_display.signal_button_press_event().connect (sigc::mem_fun (*this, &ThemeManager::button_press_event), false);
156
157         color_dialog.get_colorsel()->set_has_opacity_control (true);
158         color_dialog.get_colorsel()->set_has_palette (true);
159
160         flat_buttons.set_active (ARDOUR_UI::config()->get_flat_buttons());
161         blink_rec_button.set_active (ARDOUR_UI::config()->get_blink_rec_arm());
162         region_color_button.set_active (ARDOUR_UI::config()->get_color_regions_using_track_color());
163         show_clipping_button.set_active (ARDOUR_UI::config()->get_show_waveform_clipping());
164
165         color_dialog.get_ok_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
166         color_dialog.get_cancel_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
167         dark_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_dark_theme_button_toggled));
168         light_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_light_theme_button_toggled));
169         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &ThemeManager::reset_canvas_colors));
170         flat_buttons.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_flat_buttons_toggled));
171         blink_rec_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_blink_rec_arm_toggled));
172         region_color_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_region_color_toggled));
173         show_clipping_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_show_clip_toggled));
174         waveform_gradient_depth.signal_value_changed().connect (sigc::mem_fun (*this, &ThemeManager::on_waveform_gradient_depth_change));
175         timeline_item_gradient_depth.signal_value_changed().connect (sigc::mem_fun (*this, &ThemeManager::on_timeline_item_gradient_depth_change));
176         all_dialogs.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_all_dialogs_toggled));
177         icon_set_dropdown.signal_changed().connect (sigc::mem_fun (*this, &ThemeManager::on_icon_set_changed));
178
179         Gtkmm2ext::UI::instance()->set_tip (all_dialogs, 
180                                             string_compose (_("Mark all floating windows to be type \"Dialog\" rather than using \"Utility\" for some.\n"
181                                                               "This may help with some window managers. This requires a restart of %1 to take effect"),
182                                                             PROGRAM_NAME));
183
184         set_size_request (-1, 400);
185         setup_theme ();
186 }
187
188 ThemeManager::~ThemeManager()
189 {
190 }
191
192 int
193 ThemeManager::save (string /*path*/)
194 {
195         return 0;
196 }
197
198 bool
199 ThemeManager::button_press_event (GdkEventButton* ev)
200 {
201         TreeIter iter;
202         TreeModel::Path path;
203         TreeViewColumn* column;
204         int cellx;
205         int celly;
206
207         ColorVariable<uint32_t> *ccvar;
208
209         if (!color_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
210                 return false;
211         }
212
213         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
214         case 0:
215                 /* allow normal processing to occur */
216                 return false;
217
218         case 1: /* color */
219                 if ((iter = color_list->get_iter (path))) {
220
221                         ColorVariable<uint32_t>* var = (*iter)[columns.pVar];
222                         if (!var) {
223                                 /* parent row, do nothing */
224                                 return false;
225                         }
226
227                         int r,g, b, a;
228                         uint32_t rgba = (*iter)[columns.rgba];
229                         Gdk::Color color;
230
231                         UINT_TO_RGBA (rgba, &r, &g, &b, &a);
232                         color.set_rgb_p (r / 255.0, g / 255.0, b / 255.0);
233                         color_dialog.get_colorsel()->set_previous_color (color);
234                         color_dialog.get_colorsel()->set_current_color (color);
235                         color_dialog.get_colorsel()->set_previous_alpha (a * 256);
236                         color_dialog.get_colorsel()->set_current_alpha (a * 256);
237
238                         ResponseType result = (ResponseType) color_dialog.run();
239
240                         switch (result) {
241                         case RESPONSE_CANCEL:
242                                 break;
243                         case RESPONSE_ACCEPT:
244                                 color = color_dialog.get_colorsel()->get_current_color();
245                                 a = color_dialog.get_colorsel()->get_current_alpha();
246                                 r = (int) floor (color.get_red_p() * 255.0);
247                                 g = (int) floor (color.get_green_p() * 255.0);
248                                 b = (int) floor (color.get_blue_p() * 255.0);
249
250                                 rgba = RGBA_TO_UINT(r,g,b,a>>8);
251                                 (*iter)[columns.rgba] = rgba;
252                                 (*iter)[columns.gdkcolor] = color;
253
254                                 ccvar = (*iter)[columns.pVar];
255                                 ccvar->set(rgba);
256                                 /* mark dirty ... */
257                                 ARDOUR_UI::config()->set_dirty ();
258                                 /* but save it immediately */
259                                 ARDOUR_UI::config()->save_state ();
260
261                                 ColorsChanged(); //EMIT SIGNAL
262                                 break;
263
264                         default:
265                                 break;
266
267                         }
268
269                         color_dialog.hide ();
270                 }
271                 return true;
272
273         default:
274                 break;
275         }
276
277         return false;
278 }
279
280 void
281 load_rc_file (const string& filename, bool themechange)
282 {
283         std::string rc_file_path;
284
285         if (!find_file (ardour_config_search_path(), filename, rc_file_path)) {
286                 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
287                                            filename, ardour_config_search_path().to_string(), PROGRAM_NAME)
288                                 << endmsg;
289                 return;
290         }
291
292         info << "Loading ui configuration file " << rc_file_path << endmsg;
293
294         Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);
295 }
296
297 /* hmm, this is a problem. the profile doesn't
298    exist when the theme manager is constructed
299    and toggles buttons during "normal" GTK setup.
300
301    a better solution will be to make all Profile
302    methods static or something.
303
304    XXX FIX ME
305 */
306
307 #define HACK_PROFILE_IS_SAE() (getenv("ARDOUR_SAE")!=0)
308
309 void
310 ThemeManager::on_flat_buttons_toggled ()
311 {
312         ARDOUR_UI::config()->set_flat_buttons (flat_buttons.get_active());
313         ARDOUR_UI::config()->set_dirty ();
314         ArdourButton::set_flat_buttons (flat_buttons.get_active());
315         /* force a redraw */
316         gtk_rc_reset_styles (gtk_settings_get_default());
317 }
318
319 void
320 ThemeManager::on_blink_rec_arm_toggled ()
321 {
322         ARDOUR_UI::config()->set_blink_rec_arm (blink_rec_button.get_active());
323         ARDOUR_UI::config()->set_dirty ();
324 }
325
326 void
327 ThemeManager::on_region_color_toggled ()
328 {
329         ARDOUR_UI::config()->set_color_regions_using_track_color (region_color_button.get_active());
330         ARDOUR_UI::config()->set_dirty ();
331 }
332
333 void
334 ThemeManager::on_show_clip_toggled ()
335 {
336         ARDOUR_UI::config()->set_show_waveform_clipping (show_clipping_button.get_active());
337         ARDOUR_UI::config()->set_dirty ();
338 }
339
340 void
341 ThemeManager::on_all_dialogs_toggled ()
342 {
343         ARDOUR_UI::config()->set_all_floating_windows_are_dialogs (all_dialogs.get_active());
344         ARDOUR_UI::config()->set_dirty ();
345 }
346
347 void
348 ThemeManager::on_waveform_gradient_depth_change ()
349 {
350         double v = waveform_gradient_depth.get_value();
351
352         ARDOUR_UI::config()->set_waveform_gradient_depth (v);
353         ARDOUR_UI::config()->set_dirty ();
354         ArdourCanvas::WaveView::set_global_gradient_depth (v);
355 }
356
357 void
358 ThemeManager::on_timeline_item_gradient_depth_change ()
359 {
360         double v = timeline_item_gradient_depth.get_value();
361
362         ARDOUR_UI::config()->set_timeline_item_gradient_depth (v);
363         ARDOUR_UI::config()->set_dirty ();
364 }
365
366 void
367 ThemeManager::on_icon_set_changed ()
368 {
369         string new_set = icon_set_dropdown.get_active_text();
370         ARDOUR_UI::config()->set_icon_set (new_set);
371 }
372
373 void
374 ThemeManager::on_dark_theme_button_toggled()
375 {
376         if (!dark_button.get_active()) return;
377
378         if (HACK_PROFILE_IS_SAE()){
379                 ARDOUR_UI::config()->set_ui_rc_file("ardour3_ui_dark_sae.rc");
380         } else {
381                 ARDOUR_UI::config()->set_ui_rc_file("ardour3_ui_dark.rc");
382         }
383         ARDOUR_UI::config()->set_dirty ();
384
385         load_rc_file (ARDOUR_UI::config()->get_ui_rc_file(), true);
386 }
387
388 void
389 ThemeManager::on_light_theme_button_toggled()
390 {
391         if (!light_button.get_active()) return;
392
393         if (HACK_PROFILE_IS_SAE()){
394                 ARDOUR_UI::config()->set_ui_rc_file("ardour3_ui_light_sae.rc");
395         } else {
396                 ARDOUR_UI::config()->set_ui_rc_file("ardour3_ui_light.rc");
397         }
398
399         load_rc_file (ARDOUR_UI::config()->get_ui_rc_file(), true);
400 }
401
402 void
403 ThemeManager::setup_theme ()
404 {
405         int r, g, b, a;
406
407         color_list->clear();
408
409         for (std::map<std::string,ColorVariable<uint32_t> *>::iterator i = ARDOUR_UI::config()->canvas_colors.begin(); i != ARDOUR_UI::config()->canvas_colors.end(); i++) {
410
411
412                 ColorVariable<uint32_t>* var = i->second;
413
414                 TreeModel::Children rows = color_list->children();
415                 TreeModel::Row row;
416                 string::size_type colon;
417
418                 if ((colon = var->name().find (':')) != string::npos) {
419
420                         /* this is supposed to be a child node, so find the
421                          * parent 
422                          */
423
424                         string parent = var->name().substr (0, colon);
425                         TreeModel::iterator ri;
426
427                         for (ri = rows.begin(); ri != rows.end(); ++ri) {
428                                 string s = (*ri)[columns.name];
429                                 if (s == parent) {
430                                         break;
431                                 }
432                         }
433
434                         if (ri == rows.end()) {
435                                 /* not found, add the parent as new top level row */
436                                 row = *(color_list->append());
437                                 row[columns.name] = parent;
438                                 row[columns.pVar] = 0;
439                                 
440                                 /* now add the child as a child of this one */
441
442                                 row = *(color_list->insert (row->children().end()));
443                                 row[columns.name] = var->name().substr (colon+1);
444                         } else {
445                                 row = *(color_list->insert ((*ri)->children().end()));
446                                 row[columns.name] = var->name().substr (colon+1);
447                         }
448
449                 } else {
450                         /* add as a child */
451                         row = *(color_list->append());
452                         row[columns.name] = var->name();
453                 }
454
455                 Gdk::Color col;
456                 uint32_t rgba = var->get();
457                 UINT_TO_RGBA (rgba, &r, &g, &b, &a);
458                 //cerr << (*i)->name() << " == " << hex << rgba << ": " << hex << r << " " << hex << g << " " << hex << b << endl;
459                 col.set_rgb_p (r / 255.0, g / 255.0, b / 255.0);
460
461                 row[columns.pVar] = var;
462                 row[columns.rgba] = rgba;
463                 row[columns.gdkcolor] = col;
464         }
465
466         ColorsChanged.emit();
467
468         bool env_defined = false;
469         string rcfile = Glib::getenv("ARDOUR3_UI_RC", env_defined);
470
471         if(!env_defined) {
472                 rcfile = ARDOUR_UI::config()->get_ui_rc_file();
473         }
474
475         if (rcfile == "ardour3_ui_dark.rc" || rcfile == "ardour3_ui_dark_sae.rc") {
476                 dark_button.set_active();
477         } else if (rcfile == "ardour3_ui_light.rc" || rcfile == "ardour3_ui_light_sae.rc") {
478                 light_button.set_active();
479         }
480         
481         flat_buttons.set_active (ARDOUR_UI::config()->get_flat_buttons());
482         blink_rec_button.set_active (ARDOUR_UI::config()->get_blink_rec_arm());
483         waveform_gradient_depth.set_value (ARDOUR_UI::config()->get_waveform_gradient_depth());
484         timeline_item_gradient_depth.set_value (ARDOUR_UI::config()->get_timeline_item_gradient_depth());
485         all_dialogs.set_active (ARDOUR_UI::config()->get_all_floating_windows_are_dialogs());
486         
487         load_rc_file(rcfile, false);
488 }
489
490 void
491 ThemeManager::reset_canvas_colors()
492 {
493         ARDOUR_UI::config()->load_defaults();
494         setup_theme ();
495         /* mark dirty ... */
496         ARDOUR_UI::config()->set_dirty ();
497         /* but save it immediately */
498         ARDOUR_UI::config()->save_state ();
499 }
500