the useless commit: add copyright messages to all(?) files that need it
[ardour.git] / gtk2_ardour / color_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
27 #include "color_manager.h"
28 #include "rgb_macros.h"
29
30 #include "i18n.h"
31
32 using namespace std;
33 using namespace Gtk;
34 using namespace PBD;
35
36 /* the global color map */
37
38 ColorMap color_map;
39
40 /* lookup table of color IDs as strings */
41
42 #undef COLORID
43 #define COLORID(s) #s,
44 static const char *color_id_strs[] = {
45         #include "colors.h"
46 };
47 #undef COLORID
48
49 /* global color change signals */
50
51 sigc::signal<void> ColorsChanged;
52 sigc::signal<void,ColorID,uint32_t> ColorChanged;
53
54 ColorManager::ColorManager()
55         : ArdourDialog ("ColorManager")
56 {
57         color_list = ListStore::create (columns);
58         color_display.set_model (color_list);
59         color_display.append_column (_("Object"), columns.name);
60         color_display.append_column (_("Color"), columns.color);
61         color_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));     
62         color_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));     
63         color_display.set_reorderable (false);
64         color_display.get_selection()->set_mode (SELECTION_NONE);
65         color_display.set_headers_visible (true);
66
67         CellRenderer* color_cell = color_display.get_column_cell_renderer (1);
68         TreeViewColumn* color_column = color_display.get_column (1);
69         color_column->add_attribute (color_cell->property_cell_background_gdk(), columns.gdkcolor);
70         
71         scroller.add (color_display);
72         scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
73
74         get_vbox()->pack_start (scroller);
75
76         color_display.signal_button_press_event().connect (mem_fun (*this, &ColorManager::button_press_event), false);
77
78         color_dialog.get_colorsel()->set_has_opacity_control (true);
79         color_dialog.get_colorsel()->set_has_palette (true);
80
81         color_dialog.get_ok_button()->signal_clicked().connect (bind (mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
82         color_dialog.get_cancel_button()->signal_clicked().connect (bind (mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
83
84         set_size_request (-1, 400);
85 }
86
87 ColorManager::~ColorManager()
88 {
89 }
90
91 int
92 ColorManager::load (string path)
93 {
94         ifstream in (path.c_str());
95
96         if (!in) {
97                 error << string_compose (_("cannot open color definition file %1: %2"), path, strerror(errno)) << endmsg;
98                 return -1;
99         }
100
101         while (in) {
102                 string name;
103                 double r, g, b, a;
104
105                 in >> name; if (!in) break;
106                 in >> r; if (!in) break;
107                 in >> g; if (!in) break;
108                 in >> b; if (!in) break;
109                 in >> a; if (!in) break;
110
111                 for (uint32_t i = 0; i < sizeof (color_id_strs)/sizeof(color_id_strs[0]); ++i) {
112                         if (name == color_id_strs[i]) {
113
114                                 /* set color map */
115
116                                 int ir,ig,ib,ia;
117                                 int rgba;
118
119                                 ir = (int) floor (r * 255.0);
120                                 ig = (int) floor (g * 255.0);
121                                 ib = (int) floor (b * 255.0);
122                                 ia = (int) floor (a * 255.0);
123                                 rgba = RGBA_TO_UINT (ir, ig, ib, ia);
124
125                                 color_map[(ColorID)i] = rgba;
126
127                                 /* set up list entry */
128
129                                 Gdk::Color col;
130                                 col.set_rgb_p (r,g,b);
131
132                                 TreeModel::Row row = *(color_list->append());
133
134                                 /* all the color names are prefixed by 'c' to avoid
135                                    naming collisions when used as enums. trim
136                                    this leading character from the displayed
137                                    value.
138                                 */
139
140                                 row[columns.name] = name.substr (1);
141                                 row[columns.color] = "";
142                                 row[columns.id] = (ColorID) i;
143                                 row[columns.gdkcolor] = col;
144                                 row[columns.rgba] = rgba;
145
146                                 break;
147                         }
148                 }
149         }
150         
151         ColorsChanged(); /* emit signal */
152         return 0;
153 }
154
155 int
156 ColorManager::save (string path)
157 {
158         return 0;
159 }
160
161 bool
162 ColorManager::button_press_event (GdkEventButton* ev)
163 {
164         TreeIter iter;
165         TreeModel::Path path;
166         TreeViewColumn* column;
167         int cellx;
168         int celly;
169         
170         if (!color_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
171                 return false;
172         }
173
174         switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
175         case 0:
176                 /* allow normal processing to occur */
177                 return false;
178
179         case 1: /* color */
180                 if ((iter = color_list->get_iter (path))) {
181
182                         ColorID edit_color_id = (*iter)[columns.id];
183                         int r,g, b, a;
184                         uint32_t rgba;
185                         Gdk::Color color;
186
187                         ResponseType result = (ResponseType) color_dialog.run();
188
189                         switch (result) {
190                         case RESPONSE_CANCEL:
191                                 break;
192                         case RESPONSE_ACCEPT:
193                                 color = color_dialog.get_colorsel()->get_current_color(); 
194                                 a = color_dialog.get_colorsel()->get_current_alpha();
195                                 r = (int) floor (color.get_red_p() * 255.0);
196                                 g = (int) floor (color.get_green_p() * 255.0);
197                                 b = (int) floor (color.get_blue_p() * 255.0);
198
199                                 rgba = RGBA_TO_UINT(r,g,b,a);
200                                 
201                                 (*iter)[columns.rgba] = rgba;
202                                 (*iter)[columns.gdkcolor] = color;
203
204                                 color_map[edit_color_id] = rgba;
205
206                                 ColorChanged (edit_color_id, rgba);
207                                 break;
208
209                         default:
210                                 break;
211
212                         }
213
214                         color_dialog.hide ();
215                 }
216                 return true;
217
218         default:
219                 break;
220         }
221
222         return false;
223 }