1 /* === S Y N F I G ========================================================= */
2 /*! \file dock_paledit.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "dock_paledit.h"
33 #include "../widget_color.h"
34 #include <gtkmm/frame.h>
35 #include <gtkmm/table.h>
36 #include <gtkmm/label.h>
37 #include <synfig/general.h>
38 #include <synfigapp/canvasinterface.h>
39 #include <synfigapp/value_desc.h>
40 #include "../widget_color.h"
41 #include <gtkmm/spinbutton.h>
42 #include <gtkmm/menu.h>
43 #include <synfigapp/main.h>
45 #include "../dialog_color.h"
49 /* === U S I N G =========================================================== */
53 using namespace synfig;
54 using namespace studio;
56 /* === M A C R O S ========================================================= */
58 /* === G L O B A L S ======================================================= */
60 class studio::PaletteSettings : public synfigapp::Settings
62 Dock_PalEdit* dialog_palette;
65 PaletteSettings(Dock_PalEdit* window,const synfig::String& name):
66 dialog_palette(window),
69 dialog_palette->dialog_settings.add_domain(this,name);
72 virtual ~PaletteSettings()
74 dialog_palette->dialog_settings.remove_domain(name);
77 virtual bool get_value(const synfig::String& key, synfig::String& value)const
79 int i(atoi(key.c_str()));
80 if(i<0 || i>=dialog_palette->size())
82 Color c(dialog_palette->get_color(i));
83 value=strprintf("%f %f %f %f",c.get_r(),c.get_g(),c.get_b(),c.get_a());
87 virtual bool set_value(const synfig::String& key,const synfig::String& value)
89 int i(atoi(key.c_str()));
92 if(i>=dialog_palette->size())
93 dialog_palette->palette_.resize(i+1);
95 if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
97 dialog_palette->set_color(Color(r,g,b,a),i);
101 virtual KeyList get_key_list()const
103 synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
106 for(i=0;i<dialog_palette->size();i++)
107 ret.push_back(strprintf("%03d",i));
112 /* === P R O C E D U R E S ================================================= */
114 /* === M E T H O D S ======================================================= */
116 Dock_PalEdit::Dock_PalEdit():
117 Dockable("pal_edit",_("Palette Editor"),Gtk::StockID("gtk-select-color")),
118 //palette_settings(new PaletteSettings(this,"colors")),
121 action_group=Gtk::ActionGroup::create();
122 action_group->add(Gtk::Action::create(
124 Gtk::StockID("gtk-add"),
126 _("Add current foreground color\nto the palette")
130 &Dock_PalEdit::on_add_pressed
134 App::ui_manager()->insert_action_group(action_group);
136 Glib::ustring ui_info =
138 " <toolbar action='toolbar-palette'>"
139 " <toolitem action='palette-add-color' />"
144 App::ui_manager()->add_ui_from_string(ui_info);
146 set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-palette")));
150 Gtk::StockID("gtk-add"),
151 _("Add current foreground color\nto the palette")
152 )->signal_clicked().connect(
155 &Dock_PalEdit::on_add_pressed
161 table.set_homogeneous(true);
163 set_default_palette();
168 Dock_PalEdit::~Dock_PalEdit()
170 //delete palette_settings;
174 Dock_PalEdit::set_palette(const synfig::Palette& x)
181 Dock_PalEdit::on_add_pressed()
183 add_color(synfigapp::Main::get_foreground_color());
187 Dock_PalEdit::show_menu(int i)
189 Gtk::Menu* menu(manage(new Gtk::Menu()));
191 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-select-color"),
193 sigc::mem_fun(*this,&studio::Dock_PalEdit::edit_color),
198 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-delete"),
200 sigc::mem_fun(*this,&studio::Dock_PalEdit::erase_color),
205 menu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
207 menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Load Default Palette"),
208 sigc::mem_fun(*this,&studio::Dock_PalEdit::set_default_palette)
211 menu->popup(3,gtk_get_current_event_time());
215 Dock_PalEdit::add_color(const synfig::Color& x)
217 palette_.push_back(x);
224 Dock_PalEdit::set_color(synfig::Color x, int i)
232 Dock_PalEdit::get_color(int i)const
234 return palette_[i].color;
238 Dock_PalEdit::erase_color(int i)
240 palette_.erase(palette_.begin()+i);
246 Dock_PalEdit::refresh()
251 table.foreach(sigc::mem_fun(table,&Gtk::Table::remove));
253 for(int i=0;i<size();i++)
255 Widget_Color* widget_color(manage(new Widget_Color()));
256 widget_color->set_value(get_color(i));
257 widget_color->set_size_request(12,12);
258 widget_color->signal_activate().connect(
260 sigc::mem_fun(*this,&studio::Dock_PalEdit::select_color),
264 widget_color->signal_secondary().connect(
266 sigc::mem_fun(*this,&studio::Dock_PalEdit::show_menu),
270 int c(i%width),r(i/width);
271 table.attach(*widget_color, c, c+1, r, r+1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
279 Dock_PalEdit::edit_color(int i)
281 App::dialog_color->reset();
282 App::dialog_color->set_color(get_color(i));
283 App::dialog_color->signal_edited().connect(
285 sigc::mem_fun(*this,&studio::Dock_PalEdit::set_color),
289 App::dialog_color->present();
293 Dock_PalEdit::select_color(int i)
295 synfigapp::Main::set_foreground_color(get_color(i));
299 Dock_PalEdit::set_default_palette()
306 palette_.push_back(Color::alpha());
307 for(int i=0;i<width-1;i++)
310 float(i)/(float)(width-2),
311 float(i)/(float)(width-2),
312 float(i)/(float)(width-2)
314 palette_.push_back(c);
318 for(int i=0;i<width;i++)
320 float x(float(i)/(float)(width-1));
321 const Color tan1(0.2,0.05,0);
322 const Color tan2(0.85,0.64,0.20);
324 palette_.push_back(Color::blend(tan2,tan1,x));
328 palette_.push_back(Color::red());
329 palette_.push_back(Color(1.0f,0.25f,0.0f)); // Orange
330 palette_.push_back(Color::yellow());
331 palette_.push_back(Color(0.25f,1.00f,0.0f)); // yellow-green
332 palette_.push_back(Color::green());
333 palette_.push_back(Color(0.0f,1.00f,0.25f)); // green-blue
334 palette_.push_back(Color::cyan());
335 palette_.push_back(Color(0.0f,0.25f,1.0f)); // Sea Blue
336 palette_.push_back(Color::blue());
337 palette_.push_back(Color(0.25f,0.0f,1.0f));
338 palette_.push_back(Color::magenta());
339 palette_.push_back(Color(1.0f,0.0f,0.25f));
345 for(int j=0;j<levels;j++)
346 for(int i=0;i<width;i++)
348 Color c(Color::red());
349 c.set_hue(c.get_hue()-Angle::rot(float(i)/(float)(width)));
351 float s(float(levels-j)/float(levels));
353 c.set_r(c.get_r()*s);
354 c.set_g(c.get_g()*s);
355 c.set_b(c.get_b()*s);
356 palette_.push_back(c);
363 for(int i=0;i<levels*levels*levels;i++)
366 float(i%levels)/(float)(levels-1),
367 float(i/levels%levels)/(float)(levels-1),
368 float(i/(levels*levels))/(float)(levels-1)
370 palette_.push_back(c);
377 Dock_PalEdit::size()const
379 return palette_.size();