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