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