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