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