6 #include <gtkmm/stock.h>
8 #include "color_manager.h"
9 #include "rgb_macros.h"
16 /* the global color map */
20 /* lookup table of color IDs as strings */
23 #define COLORID(s) #s,
24 static const char *color_id_strs[] = {
29 /* global color change signals */
31 sigc::signal<void> ColorsChanged;
32 sigc::signal<void,ColorID,uint32_t> ColorChanged;
34 ColorManager::ColorManager()
35 : ArdourDialog ("ColorManager")
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);
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);
51 scroller.add (color_display);
52 scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
54 get_vbox()->pack_start (scroller);
56 color_display.signal_button_press_event().connect (mem_fun (*this, &ColorManager::button_press_event), false);
58 color_dialog.get_colorsel()->set_has_opacity_control (true);
59 color_dialog.get_colorsel()->set_has_palette (true);
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));
64 set_size_request (-1, 400);
67 ColorManager::~ColorManager()
72 ColorManager::load (string path)
74 ifstream in (path.c_str());
77 error << string_compose (_("cannot open color definition file %1: %2"), path, strerror(errno)) << endmsg;
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;
91 for (uint32_t i = 0; i < sizeof (color_id_strs)/sizeof(color_id_strs[0]); ++i) {
92 if (name == color_id_strs[i]) {
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);
105 color_map[(ColorID)i] = rgba;
107 /* set up list entry */
110 col.set_rgb_p (r,g,b);
112 TreeModel::Row row = *(color_list->append());
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
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;
131 ColorsChanged(); /* emit signal */
136 ColorManager::save (string path)
142 ColorManager::button_press_event (GdkEventButton* ev)
145 TreeModel::Path path;
146 TreeViewColumn* column;
150 if (!color_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
154 switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
156 /* allow normal processing to occur */
160 if ((iter = color_list->get_iter (path))) {
162 ColorID edit_color_id = (*iter)[columns.id];
167 ResponseType result = (ResponseType) color_dialog.run();
170 case RESPONSE_CANCEL:
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);
179 rgba = RGBA_TO_UINT(r,g,b,a);
181 (*iter)[columns.rgba] = rgba;
182 (*iter)[columns.gdkcolor] = color;
184 color_map[edit_color_id] = rgba;
186 ColorChanged (edit_color_id, rgba);
194 color_dialog.hide ();