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