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