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