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