6 #include <gtkmm/stock.h>
8 #include "color_manager.h"
9 #include "rgb_macros.h"
17 /* the global color map */
21 /* lookup table of color IDs as strings */
24 #define COLORID(s) #s,
25 static const char *color_id_strs[] = {
30 /* global color change signals */
32 sigc::signal<void> ColorsChanged;
33 sigc::signal<void,ColorID,uint32_t> ColorChanged;
35 ColorManager::ColorManager()
36 : ArdourDialog ("ColorManager")
38 color_list = ListStore::create (columns);
39 color_display.set_model (color_list);
40 color_display.append_column (_("Object"), columns.name);
41 color_display.append_column (_("Color"), columns.color);
42 color_display.get_column (0)->set_data (X_("colnum"), GUINT_TO_POINTER(0));
43 color_display.get_column (1)->set_data (X_("colnum"), GUINT_TO_POINTER(1));
44 color_display.set_reorderable (false);
45 color_display.get_selection()->set_mode (SELECTION_NONE);
46 color_display.set_headers_visible (true);
48 CellRenderer* color_cell = color_display.get_column_cell_renderer (1);
49 TreeViewColumn* color_column = color_display.get_column (1);
50 color_column->add_attribute (color_cell->property_cell_background_gdk(), columns.gdkcolor);
52 scroller.add (color_display);
53 scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
55 get_vbox()->pack_start (scroller);
57 color_display.signal_button_press_event().connect (mem_fun (*this, &ColorManager::button_press_event), false);
59 color_dialog.get_colorsel()->set_has_opacity_control (true);
60 color_dialog.get_colorsel()->set_has_palette (true);
62 color_dialog.get_ok_button()->signal_clicked().connect (bind (mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_ACCEPT));
63 color_dialog.get_cancel_button()->signal_clicked().connect (bind (mem_fun (color_dialog, &Gtk::Dialog::response), RESPONSE_CANCEL));
65 set_size_request (-1, 400);
68 ColorManager::~ColorManager()
73 ColorManager::load (string path)
75 ifstream in (path.c_str());
78 error << string_compose (_("cannot open color definition file %1: %2"), path, strerror(errno)) << endmsg;
86 in >> name; if (!in) break;
87 in >> r; if (!in) break;
88 in >> g; if (!in) break;
89 in >> b; if (!in) break;
90 in >> a; if (!in) break;
92 for (uint32_t i = 0; i < sizeof (color_id_strs)/sizeof(color_id_strs[0]); ++i) {
93 if (name == color_id_strs[i]) {
100 ir = (int) floor (r * 255.0);
101 ig = (int) floor (g * 255.0);
102 ib = (int) floor (b * 255.0);
103 ia = (int) floor (a * 255.0);
104 rgba = RGBA_TO_UINT (ir, ig, ib, ia);
106 color_map[(ColorID)i] = rgba;
108 /* set up list entry */
111 col.set_rgb_p (r,g,b);
113 TreeModel::Row row = *(color_list->append());
115 /* all the color names are prefixed by 'c' to avoid
116 naming collisions when used as enums. trim
117 this leading character from the displayed
121 row[columns.name] = name.substr (1);
122 row[columns.color] = "";
123 row[columns.id] = (ColorID) i;
124 row[columns.gdkcolor] = col;
125 row[columns.rgba] = rgba;
132 ColorsChanged(); /* emit signal */
137 ColorManager::save (string path)
143 ColorManager::button_press_event (GdkEventButton* ev)
146 TreeModel::Path path;
147 TreeViewColumn* column;
151 if (!color_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
155 switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
157 /* allow normal processing to occur */
161 if ((iter = color_list->get_iter (path))) {
163 ColorID edit_color_id = (*iter)[columns.id];
168 ResponseType result = (ResponseType) color_dialog.run();
171 case RESPONSE_CANCEL:
173 case RESPONSE_ACCEPT:
174 color = color_dialog.get_colorsel()->get_current_color();
175 a = color_dialog.get_colorsel()->get_current_alpha();
176 r = (int) floor (color.get_red_p() * 255.0);
177 g = (int) floor (color.get_green_p() * 255.0);
178 b = (int) floor (color.get_blue_p() * 255.0);
180 rgba = RGBA_TO_UINT(r,g,b,a);
182 (*iter)[columns.rgba] = rgba;
183 (*iter)[columns.gdkcolor] = color;
185 color_map[edit_color_id] = rgba;
187 ColorChanged (edit_color_id, rgba);
195 color_dialog.hide ();