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