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