2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
25 #include <gtkmm/stock.h>
27 #include "color_manager.h"
28 #include "rgb_macros.h"
36 /* the global color map */
40 /* lookup table of color IDs as strings */
43 #define COLORID(s) #s,
44 static const char *color_id_strs[] = {
49 /* global color change signals */
51 sigc::signal<void> ColorsChanged;
52 sigc::signal<void,ColorID,uint32_t> ColorChanged;
54 ColorManager::ColorManager()
55 : ArdourDialog ("ColorManager")
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);
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);
71 scroller.add (color_display);
72 scroller.set_policy (POLICY_NEVER, POLICY_AUTOMATIC);
74 get_vbox()->pack_start (scroller);
76 color_display.signal_button_press_event().connect (mem_fun (*this, &ColorManager::button_press_event), false);
78 color_dialog.get_colorsel()->set_has_opacity_control (true);
79 color_dialog.get_colorsel()->set_has_palette (true);
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));
84 set_size_request (-1, 400);
87 ColorManager::~ColorManager()
92 ColorManager::load (string path)
94 ifstream in (path.c_str());
97 error << string_compose (_("cannot open color definition file %1: %2"), path, strerror(errno)) << endmsg;
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;
111 for (uint32_t i = 0; i < sizeof (color_id_strs)/sizeof(color_id_strs[0]); ++i) {
112 if (name == color_id_strs[i]) {
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);
125 color_map[(ColorID)i] = rgba;
127 /* set up list entry */
130 col.set_rgb_p (r,g,b);
132 TreeModel::Row row = *(color_list->append());
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
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;
151 ColorsChanged(); /* emit signal */
156 ColorManager::save (string path)
162 ColorManager::button_press_event (GdkEventButton* ev)
165 TreeModel::Path path;
166 TreeViewColumn* column;
170 if (!color_display.get_path_at_pos ((int)ev->x, (int)ev->y, path, column, cellx, celly)) {
174 switch (GPOINTER_TO_UINT (column->get_data (X_("colnum")))) {
176 /* allow normal processing to occur */
180 if ((iter = color_list->get_iter (path))) {
182 ColorID edit_color_id = (*iter)[columns.id];
187 ResponseType result = (ResponseType) color_dialog.run();
190 case RESPONSE_CANCEL:
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);
199 rgba = RGBA_TO_UINT(r,g,b,a);
201 (*iter)[columns.rgba] = rgba;
202 (*iter)[columns.gdkcolor] = color;
204 color_map[edit_color_id] = rgba;
206 ColorChanged (edit_color_id, rgba);
214 color_dialog.hide ();