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