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