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