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