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
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "dock_paledit.h"
34 #include "../widget_color.h"
35 #include <gtkmm/frame.h>
36 #include <gtkmm/table.h>
37 #include <gtkmm/label.h>
38 #include <synfig/general.h>
39 #include <synfigapp/canvasinterface.h>
40 #include <synfigapp/value_desc.h>
41 #include "../widget_color.h"
42 #include <gtkmm/spinbutton.h>
43 #include <gtkmm/menu.h>
44 #include <synfigapp/main.h>
46 #include "../dialog_color.h"
48 #include "../general.h"
52 /* === U S I N G =========================================================== */
56 using namespace synfig;
57 using namespace studio;
59 /* === M A C R O S ========================================================= */
61 /* === G L O B A L S ======================================================= */
63 class studio::PaletteSettings : public synfigapp::Settings
65 Dock_PalEdit* dialog_palette;
68 PaletteSettings(Dock_PalEdit* window,const synfig::String& name):
69 dialog_palette(window),
72 dialog_palette->dialog_settings.add_domain(this,name);
75 virtual ~PaletteSettings()
77 dialog_palette->dialog_settings.remove_domain(name);
80 virtual bool get_value(const synfig::String& key, synfig::String& value)const
82 int i(atoi(key.c_str()));
83 if(i<0 || i>=dialog_palette->size())
85 Color c(dialog_palette->get_color(i));
86 value=strprintf("%f %f %f %f",c.get_r(),c.get_g(),c.get_b(),c.get_a());
90 virtual bool set_value(const synfig::String& key,const synfig::String& value)
92 int i(atoi(key.c_str()));
95 if(i>=dialog_palette->size())
96 dialog_palette->palette_.resize(i+1);
98 if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
100 dialog_palette->set_color(Color(r,g,b,a),i);
104 virtual KeyList get_key_list()const
106 synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
109 for(i=0;i<dialog_palette->size();i++)
110 ret.push_back(strprintf("%03d",i));
115 /* === P R O C E D U R E S ================================================= */
117 /* === M E T H O D S ======================================================= */
119 Dock_PalEdit::Dock_PalEdit():
120 Dockable("pal_edit",_("Palette Editor"),Gtk::StockID("gtk-select-color")),
121 //palette_settings(new PaletteSettings(this,"colors")),
124 action_group=Gtk::ActionGroup::create();
125 action_group->add(Gtk::Action::create(
127 Gtk::StockID("gtk-add"),
129 _("Add current foreground color\nto the palette")
133 &Dock_PalEdit::on_add_pressed
137 App::ui_manager()->insert_action_group(action_group);
139 Glib::ustring ui_info =
141 " <toolbar action='toolbar-palette'>"
142 " <toolitem action='palette-add-color' />"
147 App::ui_manager()->add_ui_from_string(ui_info);
149 set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-palette")));
153 Gtk::StockID("gtk-add"),
154 _("Add current foreground color\nto the palette")
155 )->signal_clicked().connect(
158 &Dock_PalEdit::on_add_pressed
164 table.set_homogeneous(true);
166 set_default_palette();
171 Dock_PalEdit::~Dock_PalEdit()
173 //delete palette_settings;
177 Dock_PalEdit::set_palette(const synfig::Palette& x)
184 Dock_PalEdit::on_add_pressed()
186 add_color(synfigapp::Main::get_foreground_color());
190 Dock_PalEdit::show_menu(int i)
192 Gtk::Menu* menu(manage(new Gtk::Menu()));
193 menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
195 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-select-color"),
197 sigc::mem_fun(*this,&studio::Dock_PalEdit::edit_color),
202 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-delete"),
204 sigc::mem_fun(*this,&studio::Dock_PalEdit::erase_color),
209 menu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
211 menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Load Default Palette"),
212 sigc::mem_fun(*this,&studio::Dock_PalEdit::set_default_palette)
215 menu->popup(3,gtk_get_current_event_time());
219 Dock_PalEdit::add_color(const synfig::Color& x)
221 palette_.push_back(x);
228 Dock_PalEdit::set_color(synfig::Color x, int i)
236 Dock_PalEdit::get_color(int i)const
238 return palette_[i].color;
242 Dock_PalEdit::erase_color(int i)
244 palette_.erase(palette_.begin()+i);
250 Dock_PalEdit::refresh()
255 table.foreach(sigc::mem_fun(table,&Gtk::Table::remove));
257 for(int i=0;i<size();i++)
259 Widget_Color* widget_color(manage(new Widget_Color()));
260 widget_color->set_value(get_color(i));
261 widget_color->set_size_request(12,12);
262 widget_color->signal_activate().connect(
264 sigc::mem_fun(*this,&studio::Dock_PalEdit::select_color),
268 widget_color->signal_secondary().connect(
270 sigc::mem_fun(*this,&studio::Dock_PalEdit::show_menu),
274 int c(i%width),r(i/width);
275 table.attach(*widget_color, c, c+1, r, r+1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
283 Dock_PalEdit::edit_color(int i)
285 App::dialog_color->reset();
286 App::dialog_color->set_color(get_color(i));
287 App::dialog_color->signal_edited().connect(
289 sigc::mem_fun(*this,&studio::Dock_PalEdit::set_color),
293 App::dialog_color->present();
297 Dock_PalEdit::select_color(int i)
299 synfigapp::Main::set_foreground_color(get_color(i));
303 Dock_PalEdit::set_default_palette()
310 palette_.push_back(Color::alpha());
311 for(int i=0;i<width-1;i++)
314 float(i)/(float)(width-2),
315 float(i)/(float)(width-2),
316 float(i)/(float)(width-2)
318 palette_.push_back(c);
322 for(int i=0;i<width;i++)
324 float x(float(i)/(float)(width-1));
325 const Color tan1(0.2,0.05,0);
326 const Color tan2(0.85,0.64,0.20);
328 palette_.push_back(Color::blend(tan2,tan1,x));
332 palette_.push_back(Color::red());
333 palette_.push_back(Color(1.0f,0.25f,0.0f)); // Orange
334 palette_.push_back(Color::yellow());
335 palette_.push_back(Color(0.25f,1.00f,0.0f)); // yellow-green
336 palette_.push_back(Color::green());
337 palette_.push_back(Color(0.0f,1.00f,0.25f)); // green-blue
338 palette_.push_back(Color::cyan());
339 palette_.push_back(Color(0.0f,0.25f,1.0f)); // Sea Blue
340 palette_.push_back(Color::blue());
341 palette_.push_back(Color(0.25f,0.0f,1.0f));
342 palette_.push_back(Color::magenta());
343 palette_.push_back(Color(1.0f,0.0f,0.25f));
349 for(int j=0;j<levels;j++)
350 for(int i=0;i<width;i++)
352 Color c(Color::red());
353 c.set_hue(c.get_hue()-Angle::rot(float(i)/(float)(width)));
355 float s(float(levels-j)/float(levels));
357 c.set_r(c.get_r()*s);
358 c.set_g(c.get_g()*s);
359 c.set_b(c.get_b()*s);
360 palette_.push_back(c);
367 for(int i=0;i<levels*levels*levels;i++)
370 float(i%levels)/(float)(levels-1),
371 float(i/levels%levels)/(float)(levels-1),
372 float(i/(levels*levels))/(float)(levels-1)
374 palette_.push_back(c);
381 Dock_PalEdit::size()const
383 return palette_.size();