update DE translation from edgar
[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/filesystem_paths.h"
36
37 #include "ardour_button.h"
38 #include "canvas-waveview.h"
39 #include "theme_manager.h"
40 #include "rgb_macros.h"
41 #include "ardour_ui.h"
42 #include "global_signals.h"
43
44 #include "i18n.h"
45
46 using namespace std;
47 using namespace Gtk;
48 using namespace PBD;
49 using namespace ARDOUR;
50
51 sigc::signal<void> ColorsChanged;
52 sigc::signal<void,uint32_t> ColorChanged;
53
54 ThemeManager::ThemeManager()
55         : ArdourWindow (_("Theme Manager"))
56         , dark_button (_("Dark Theme"))
57         , light_button (_("Light Theme"))
58         , reset_button (_("Restore Defaults"))
59         , flat_buttons (_("Draw \"flat\" buttons"))
60         , gradient_waveforms (_("Draw waveforms with color gradient"))
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 (gradient_waveforms, PACK_SHRINK);
97         vbox->pack_start (scroller);
98         add (*vbox);
99
100         color_display.signal_button_press_event().connect (sigc::mem_fun (*this, &ThemeManager::button_press_event), false);
101
102         color_dialog.get_colorsel()->set_has_opacity_control (true);
103         color_dialog.get_colorsel()->set_has_palette (true);
104
105         color_dialog.get_ok_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
106         color_dialog.get_cancel_button()->signal_clicked().connect (sigc::bind (sigc::mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
107         dark_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_dark_theme_button_toggled));
108         light_button.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_light_theme_button_toggled));
109         reset_button.signal_clicked().connect (sigc::mem_fun (*this, &ThemeManager::reset_canvas_colors));
110         flat_buttons.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_flat_buttons_toggled));
111         gradient_waveforms.signal_toggled().connect (sigc::mem_fun (*this, &ThemeManager::on_gradient_waveforms_toggled));
112
113         set_size_request (-1, 400);
114         setup_theme ();
115 }
116
117 ThemeManager::~ThemeManager()
118 {
119 }
120
121 int
122 ThemeManager::save (string /*path*/)
123 {
124         return 0;
125 }
126
127 bool
128 ThemeManager::button_press_event (GdkEventButton* ev)
129 {
130         TreeIter iter;
131         TreeModel::Path path;
132         TreeViewColumn* column;
133         int cellx;
134         int celly;
135
136         UIConfigVariable<uint32_t> *ccvar;
137
138         if (!color_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
139                 return false;
140         }
141
142         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
143         case 0:
144                 /* allow normal processing to occur */
145                 return false;
146
147         case 1: /* color */
148                 if ((iter = color_list->get_iter (path))) {
149
150                         UIConfigVariable<uint32_t>* var = (*iter)[columns.pVar];
151                         if (!var) {
152                                 /* parent row, do nothing */
153                                 return false;
154                         }
155
156                         int r,g, b, a;
157                         uint32_t rgba = (*iter)[columns.rgba];
158                         Gdk::Color color;
159
160                         UINT_TO_RGBA (rgba, &r, &g, &b, &a);
161                         color.set_rgb_p (r / 255.0, g / 255.0, b / 255.0);
162                         color_dialog.get_colorsel()->set_previous_color (color);
163                         color_dialog.get_colorsel()->set_current_color (color);
164                         color_dialog.get_colorsel()->set_previous_alpha (a * 256);
165                         color_dialog.get_colorsel()->set_current_alpha (a * 256);
166
167                         ResponseType result = (ResponseType) color_dialog.run();
168
169                         switch (result) {
170                         case RESPONSE_CANCEL:
171                                 break;
172                         case RESPONSE_ACCEPT:
173                                 color = color_dialog.get_colorsel()->get_current_color();
174                                 a = color_dialog.get_colorsel()->get_current_alpha();
175                                 r = (int) floor (color.get_red_p() * 255.0);
176                                 g = (int) floor (color.get_green_p() * 255.0);
177                                 b = (int) floor (color.get_blue_p() * 255.0);
178
179                                 rgba = RGBA_TO_UINT(r,g,b,a>>8);
180                                 (*iter)[columns.rgba] = rgba;
181                                 (*iter)[columns.gdkcolor] = color;
182
183                                 ccvar = (*iter)[columns.pVar];
184                                 ccvar->set(rgba);
185                                 /* mark dirty ... */
186                                 ARDOUR_UI::config()->set_dirty ();
187                                 /* but save it immediately */
188                                 ARDOUR_UI::config()->save_state ();
189
190                                 ColorsChanged(); //EMIT SIGNAL
191                                 break;
192
193                         default:
194                                 break;
195
196                         }
197
198                         color_dialog.hide ();
199                 }
200                 return true;
201
202         default:
203                 break;
204         }
205
206         return false;
207 }
208
209 void
210 load_rc_file (const string& filename, bool themechange)
211 {
212         std::string rc_file_path;
213
214         if (!find_file_in_search_path (ardour_config_search_path(), filename, rc_file_path)) {
215                 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
216                                            filename, ardour_config_search_path().to_string(), PROGRAM_NAME)
217                                 << endmsg;
218                 return;
219         }
220
221         info << "Loading ui configuration file " << rc_file_path << endmsg;
222
223         Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);
224 }
225
226 /* hmm, this is a problem. the profile doesn't
227    exist when the theme manager is constructed
228    and toggles buttons during "normal" GTK setup.
229
230    a better solution will be to make all Profile
231    methods static or something.
232
233    XXX FIX ME
234 */
235
236 #define HACK_PROFILE_IS_SAE() (getenv("ARDOUR_SAE")!=0)
237
238 void
239 ThemeManager::on_flat_buttons_toggled ()
240 {
241         ARDOUR_UI::config()->flat_buttons.set (flat_buttons.get_active());
242         ARDOUR_UI::config()->set_dirty ();
243         ArdourButton::set_flat_buttons (flat_buttons.get_active());
244         /* force a redraw */
245         gtk_rc_reset_styles (gtk_settings_get_default());
246 }
247
248 void
249 ThemeManager::on_gradient_waveforms_toggled ()
250 {
251         ARDOUR_UI::config()->gradient_waveforms.set (gradient_waveforms.get_active());
252         ARDOUR_UI::config()->set_dirty ();
253         
254         gnome_canvas_waveview_set_gradient_waveforms (gradient_waveforms.get_active());
255
256         /* force a redraw */
257         gtk_rc_reset_styles (gtk_settings_get_default());
258 }
259
260 void
261 ThemeManager::on_dark_theme_button_toggled()
262 {
263         if (!dark_button.get_active()) return;
264
265         if (HACK_PROFILE_IS_SAE()){
266                 ARDOUR_UI::config()->ui_rc_file.set("ardour3_ui_dark_sae.rc");
267         } else {
268                 ARDOUR_UI::config()->ui_rc_file.set("ardour3_ui_dark.rc");
269         }
270         ARDOUR_UI::config()->set_dirty ();
271
272         load_rc_file (ARDOUR_UI::config()->ui_rc_file.get(), true);
273 }
274
275 void
276 ThemeManager::on_light_theme_button_toggled()
277 {
278         if (!light_button.get_active()) return;
279
280         if (HACK_PROFILE_IS_SAE()){
281                 ARDOUR_UI::config()->ui_rc_file.set("ardour3_ui_light_sae.rc");
282         } else {
283                 ARDOUR_UI::config()->ui_rc_file.set("ardour3_ui_light.rc");
284         }
285
286         load_rc_file (ARDOUR_UI::config()->ui_rc_file.get(), true);
287 }
288
289 void
290 ThemeManager::setup_theme ()
291 {
292         int r, g, b, a;
293
294         color_list->clear();
295
296         for (std::map<std::string,UIConfigVariable<uint32_t> *>::iterator i = ARDOUR_UI::config()->canvas_colors.begin(); i != ARDOUR_UI::config()->canvas_colors.end(); i++) {
297
298
299                 UIConfigVariable<uint32_t>* var = i->second;
300
301                 TreeModel::Children rows = color_list->children();
302                 TreeModel::Row row;
303                 string::size_type colon;
304
305                 if ((colon = var->name().find (':')) != string::npos) {
306
307                         /* this is supposed to be a child node, so find the
308                          * parent 
309                          */
310
311                         string parent = var->name().substr (0, colon);
312                         TreeModel::iterator ri;
313
314                         for (ri = rows.begin(); ri != rows.end(); ++ri) {
315                                 string s = (*ri)[columns.name];
316                                 if (s == parent) {
317                                         break;
318                                 }
319                         }
320
321                         if (ri == rows.end()) {
322                                 /* not found, add the parent as new top level row */
323                                 row = *(color_list->append());
324                                 row[columns.name] = parent;
325                                 row[columns.pVar] = 0;
326                                 
327                                 /* now add the child as a child of this one */
328
329                                 row = *(color_list->insert (row->children().end()));
330                                 row[columns.name] = var->name().substr (colon+1);
331                         } else {
332                                 row = *(color_list->insert ((*ri)->children().end()));
333                                 row[columns.name] = var->name().substr (colon+1);
334                         }
335
336                 } else {
337                         /* add as a child */
338                         row = *(color_list->append());
339                         row[columns.name] = var->name();
340                 }
341
342                 Gdk::Color col;
343                 uint32_t rgba = var->get();
344                 UINT_TO_RGBA (rgba, &r, &g, &b, &a);
345                 //cerr << (*i)->name() << " == " << hex << rgba << ": " << hex << r << " " << hex << g << " " << hex << b << endl;
346                 col.set_rgb_p (r / 255.0, g / 255.0, b / 255.0);
347
348                 row[columns.pVar] = var;
349                 row[columns.rgba] = rgba;
350                 row[columns.gdkcolor] = col;
351         }
352
353         ColorsChanged.emit();
354
355         bool env_defined = false;
356         string rcfile = Glib::getenv("ARDOUR3_UI_RC", env_defined);
357
358         if(!env_defined) {
359                 rcfile = ARDOUR_UI::config()->ui_rc_file.get();
360         }
361
362         if (rcfile == "ardour3_ui_dark.rc" || rcfile == "ardour3_ui_dark_sae.rc") {
363                 dark_button.set_active();
364         } else if (rcfile == "ardour3_ui_light.rc" || rcfile == "ardour3_ui_light_sae.rc") {
365                 light_button.set_active();
366         }
367         
368         flat_buttons.set_active (ARDOUR_UI::config()->flat_buttons.get());
369         gradient_waveforms.set_active (ARDOUR_UI::config()->gradient_waveforms.get());
370         
371         load_rc_file(rcfile, false);
372 }
373
374 void
375 ThemeManager::reset_canvas_colors()
376 {
377         ARDOUR_UI::config()->load_defaults();
378         setup_theme ();
379         /* mark dirty ... */
380         ARDOUR_UI::config()->set_dirty ();
381         /* but save it immediately */
382         ARDOUR_UI::config()->save_state ();
383 }
384