Use signal_toggled instead of signal_clicked in ThemeManager so a theme is only ...
[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 <pbd/file_utils.h>
30
31 #include <ardour/configuration.h>
32 #include <ardour/filesystem_paths.h>
33
34 #include "theme_manager.h"
35 #include "rgb_macros.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace Gtk;
41 using namespace PBD;
42 using namespace ARDOUR;
43
44
45 sigc::signal<void> ColorsChanged;
46 sigc::signal<void,uint32_t> ColorChanged;
47
48 ThemeManager::ThemeManager()
49         : ArdourDialog ("ThemeManager"),
50         dark_button ("Dark Theme"),
51         light_button ("Light Theme")
52 {
53         color_list = ListStore::create (columns);
54         color_display.set_model (color_list);
55         color_display.append_column (_("Object"), columns.name);
56         color_display.append_column (_("Color"), columns.color);
57         color_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));     
58         color_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));     
59         color_display.set_reorderable (false);
60         color_display.get_selection()->set_mode (SELECTION_NONE);
61         color_display.set_headers_visible (true);
62
63         CellRenderer* color_cell = color_display.get_column_cell_renderer (1);
64         TreeViewColumn* color_column = color_display.get_column (1);
65         color_column->add_attribute (color_cell->property_cell_background_gdk(), columns.gdkcolor);
66         
67         scroller.add (color_display);
68         scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
69         
70         RadioButton::Group group = dark_button.get_group();
71         light_button.set_group(group);
72         theme_selection_hbox.set_homogeneous(false);
73         theme_selection_hbox.pack_start (dark_button);
74         theme_selection_hbox.pack_start (light_button);
75
76         get_vbox()->set_homogeneous(false);
77         get_vbox()->pack_start (theme_selection_hbox, PACK_SHRINK);
78         get_vbox()->pack_start (scroller);
79
80         color_display.signal_button_press_event().connect (mem_fun (*this, &ThemeManager::button_press_event), false);
81
82         color_dialog.get_colorsel()->set_has_opacity_control (true);
83         color_dialog.get_colorsel()->set_has_palette (true);
84
85         color_dialog.get_ok_button()->signal_clicked().connect (bind (mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
86         color_dialog.get_cancel_button()->signal_clicked().connect (bind (mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
87         dark_button.signal_toggled().connect (mem_fun (*this, &ThemeManager::on_dark_theme_button_toggled));
88         light_button.signal_toggled().connect (mem_fun (*this, &ThemeManager::on_light_theme_button_toggled));
89
90         set_size_request (-1, 400);
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         ARDOUR::ConfigVariable<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_current_color (color);
133                         color_dialog.get_colorsel()->set_previous_color (color);
134                         color_dialog.get_colorsel()->set_current_alpha (a * 256);
135                         color_dialog.get_colorsel()->set_previous_alpha (a * 256);
136                         ResponseType result = (ResponseType) color_dialog.run();
137
138                         switch (result) {
139                         case RESPONSE_CANCEL:
140                                 break;
141                         case RESPONSE_ACCEPT:
142                                 color = color_dialog.get_colorsel()->get_current_color(); 
143                                 a = color_dialog.get_colorsel()->get_current_alpha();
144                                 r = (int) floor (color.get_red_p() * 255.0);
145                                 g = (int) floor (color.get_green_p() * 255.0);
146                                 b = (int) floor (color.get_blue_p() * 255.0);
147
148                                 rgba = RGBA_TO_UINT(r,g,b,a);
149                                 //cerr << (*iter)[columns.name] << " == " << hex << rgba << endl;
150                                 (*iter)[columns.rgba] = rgba;
151                                 (*iter)[columns.gdkcolor] = color;
152
153                                 ccvar = (*iter)[columns.pVar];
154                                 ccvar->set(rgba);
155
156                                 //ColorChanged (rgba);
157                                 ColorsChanged();//EMIT SIGNAL
158                                 break;
159
160                         default:
161                                 break;
162
163                         }
164
165                         color_dialog.hide ();
166                 }
167                 return true;
168
169         default:
170                 break;
171         }
172
173         return false;
174 }
175
176 void
177 load_rc_file (const string& filename)
178 {
179         sys::path rc_file_path;
180
181         SearchPath spath (ardour_search_path());
182         spath += user_config_directory();
183         spath += system_config_search_path();
184
185         if(!find_file_in_search_path (spath, filename, rc_file_path))
186         {
187                 warning << string_compose(_("Unable to find UI style file %1 in search path %2. Ardour will look strange"),
188                                 filename, spath.get_string()) 
189                         << endmsg;
190                 return;
191         }
192
193         info << "Loading ui configuration file " << rc_file_path.to_string() << endmsg;
194
195         Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path.to_string());
196 }
197
198 void
199 ThemeManager::on_dark_theme_button_toggled()
200 {
201         if (!dark_button.get_active()) return;
202
203         Config->set_ui_rc_file("ardour2_ui_dark.rc");
204         load_rc_file (Config->get_ui_rc_file());
205 }
206
207 void
208 ThemeManager::on_light_theme_button_toggled()
209 {
210         if (!light_button.get_active()) return;
211
212         Config->set_ui_rc_file("ardour2_ui_light.rc");
213         load_rc_file (Config->get_ui_rc_file());
214 }
215
216 void
217 ThemeManager::setup_theme ()
218 {
219         int r, g, b, a;
220         for (std::vector<ConfigVariable<uint32_t> *>::iterator i = Config->canvas_colors.begin(); i != Config->canvas_colors.end(); i++) {
221                 
222                 TreeModel::Row row = *(color_list->append());
223
224                 Gdk::Color col;
225                 uint32_t rgba = (*i)->get();
226                 UINT_TO_RGBA (rgba, &r, &g, &b, &a);
227                 //cerr << (*i)->name() << " == " << hex << rgba << ": " << hex << r << " " << hex << g << " " << hex << b << endl;
228                 col.set_rgb_p (r / 255.0, g / 255.0, b / 255.0);
229
230                 row[columns.name] = (*i)->name();
231                 row[columns.color] = "";
232                 row[columns.pVar] = *i;
233                 row[columns.rgba] = rgba;
234                 row[columns.gdkcolor] = col;
235
236                 //cerr << (*i)->name() << " == " << rgba << endl;
237         }
238
239         ColorsChanged.emit();
240
241         bool env_defined = false;
242         string rcfile = Glib::getenv("ARDOUR2_UI_RC", env_defined);
243
244         if(!env_defined) {
245                 rcfile = Config->get_ui_rc_file();
246         }
247
248         if (rcfile == "ardour2_ui_dark.rc") {
249                 dark_button.set_active();
250         } else if (rcfile == "ardour2_ui_light.rc") {
251                 light_button.set_active();
252         }
253
254         load_rc_file(rcfile);
255 }
256