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