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