Use PBD::find_file_in_search_path in ThemeManager to find the rc file.
[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 #include "ardour_ui.h"
37
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace Gtk;
42 using namespace PBD;
43 using namespace ARDOUR;
44
45
46 sigc::signal<void> ColorsChanged;
47 sigc::signal<void,uint32_t> ColorChanged;
48
49 ThemeManager::ThemeManager()
50         : ArdourDialog ("ThemeManager"),
51         dark_button ("Dark Theme"),
52         light_button ("Light Theme")
53 {
54         color_list = ListStore::create (columns);
55         color_display.set_model (color_list);
56         color_display.append_column (_("Object"), columns.name);
57         color_display.append_column (_("Color"), columns.color);
58         color_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));     
59         color_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));     
60         color_display.set_reorderable (false);
61         color_display.get_selection()->set_mode (SELECTION_NONE);
62         color_display.set_headers_visible (true);
63
64         CellRenderer* color_cell = color_display.get_column_cell_renderer (1);
65         TreeViewColumn* color_column = color_display.get_column (1);
66         color_column->add_attribute (color_cell->property_cell_background_gdk(), columns.gdkcolor);
67         
68         scroller.add (color_display);
69         scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
70         
71         RadioButton::Group group = dark_button.get_group();
72         light_button.set_group(group);
73         theme_selection_hbox.set_homogeneous(false);
74         theme_selection_hbox.pack_start (dark_button);
75         theme_selection_hbox.pack_start (light_button);
76
77         get_vbox()->set_homogeneous(false);
78         get_vbox()->pack_start (theme_selection_hbox, PACK_SHRINK);
79         get_vbox()->pack_start (scroller);
80
81         color_display.signal_button_press_event().connect (mem_fun (*this, &ThemeManager::button_press_event), false);
82
83         color_dialog.get_colorsel()->set_has_opacity_control (true);
84         color_dialog.get_colorsel()->set_has_palette (true);
85
86         color_dialog.get_ok_button()->signal_clicked().connect (bind (mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
87         color_dialog.get_cancel_button()->signal_clicked().connect (bind (mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
88         dark_button.signal_clicked().connect (bind (mem_fun (*this, &ThemeManager::load_rc), 1));
89         light_button.signal_clicked().connect (bind (mem_fun (*this, &ThemeManager::load_rc), 2));
90
91         set_size_request (-1, 400);
92 }
93
94 ThemeManager::~ThemeManager()
95 {
96 }
97
98 int
99 ThemeManager::save (string path)
100 {
101         return 0;
102 }
103
104 bool
105 ThemeManager::button_press_event (GdkEventButton* ev)
106 {
107         TreeIter iter;
108         TreeModel::Path path;
109         TreeViewColumn* column;
110         int cellx;
111         int celly;
112
113         ARDOUR::ConfigVariable<uint32_t> *ccvar;
114         
115         if (!color_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
116                 return false;
117         }
118
119         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
120         case 0:
121                 /* allow normal processing to occur */
122                 return false;
123
124         case 1: /* color */
125                 if ((iter = color_list->get_iter (path))) {
126
127                         int r,g, b, a;
128                         uint32_t rgba = (*iter)[columns.rgba];
129                         Gdk::Color color;
130
131                         UINT_TO_RGBA (rgba, &r, &g, &b, &a);
132                         color.set_rgb_p (r / 255.0, g / 255.0, b / 255.0);
133                         color_dialog.get_colorsel()->set_current_color (color);
134                         color_dialog.get_colorsel()->set_previous_color (color);
135                         color_dialog.get_colorsel()->set_current_alpha (a * 256);
136                         color_dialog.get_colorsel()->set_previous_alpha (a * 256);
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);
150                                 //cerr << (*iter)[columns.name] << " == " << hex << rgba << endl;
151                                 (*iter)[columns.rgba] = rgba;
152                                 (*iter)[columns.gdkcolor] = color;
153
154                                 ccvar = (*iter)[columns.pVar];
155                                 ccvar->set(rgba);
156
157                                 //ColorChanged (rgba);
158                                 ColorsChanged();//EMIT SIGNAL
159                                 break;
160
161                         default:
162                                 break;
163
164                         }
165
166                         color_dialog.hide ();
167                 }
168                 return true;
169
170         default:
171                 break;
172         }
173
174         return false;
175 }
176
177
178 void
179 ThemeManager::load_rc(int which)
180 {
181                 
182         if (which == 1) {
183                 Config->set_ui_rc_file("ardour2_ui_dark.rc");
184                 cerr << "dark theme selected" << endl;
185                 
186         } else {
187                 Config->set_ui_rc_file("ardour2_ui_light.rc");
188                 cerr << "light theme selected" << endl;
189         }
190         
191         sys::path rc_file_path;
192
193         find_file_in_search_path (ardour_search_path() + system_config_search_path(),
194                         Config->get_ui_rc_file(), rc_file_path);
195
196         ThemeChanged(rc_file_path.to_string()); //EMIT SIGNAL
197         
198         cerr << "load_rc() called " << rc_file_path.to_string() << endl;
199
200 }
201
202 void
203 ThemeManager::setup_theme ()
204 {
205         int r, g, b, a;
206         for (std::vector<ConfigVariable<uint32_t> *>::iterator i = Config->canvas_colors.begin(); i != Config->canvas_colors.end(); i++) {
207                 
208                 TreeModel::Row row = *(color_list->append());
209
210                 Gdk::Color col;
211                 uint32_t rgba = (*i)->get();
212                 UINT_TO_RGBA (rgba, &r, &g, &b, &a);
213                 //cerr << (*i)->name() << " == " << hex << rgba << ": " << hex << r << " " << hex << g << " " << hex << b << endl;
214                 col.set_rgb_p (r / 255.0, g / 255.0, b / 255.0);
215
216                 row[columns.name] = (*i)->name();
217                 row[columns.color] = "";
218                 row[columns.pVar] = *i;
219                 row[columns.rgba] = rgba;
220                 row[columns.gdkcolor] = col;
221
222                 //cerr << (*i)->name() << " == " << rgba << endl;
223         }
224         cerr << "ThemeManager::setup_theme () called" << endl;
225         ColorsChanged(); //EMIT SIGNAL
226                 
227         if (getenv ("ARDOUR2_UI_RC")) {
228                 return;
229         }
230
231         if (Config->get_ui_rc_file() == "ardour2_ui_dark.rc") {
232                 dark_button.set_active();
233         } else if (Config->get_ui_rc_file() == "ardour2_ui_light.rc") {
234                 light_button.set_active();
235         }
236         
237 }
238