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