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