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 "../../widgets/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 "../../widgets/widget_color.h"
42 #include <gtkmm/spinbutton.h>
43 #include <gtkmm/menu.h>
44 #include <synfigapp/main.h>
45 #include "../../app.h"
46 #include "../../dialogs/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("action_group_pal_edit");
125 action_group->add(Gtk::Action::create(
127 Gtk::StockID("gtk-add"),
129 _("Add current outline 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 outline 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_outline_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_fill_color),
268 widget_color->signal_middle_click().connect(
270 sigc::mem_fun(*this,&studio::Dock_PalEdit::select_outline_color),
274 widget_color->signal_right_click().connect(
276 sigc::mem_fun(*this,&studio::Dock_PalEdit::show_menu),
280 int c(i%width),r(i/width);
281 table.attach(*widget_color, c, c+1, r, r+1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
289 Dock_PalEdit::edit_color(int i)
291 App::dialog_color->reset();
292 App::dialog_color->set_color(get_color(i));
293 App::dialog_color->signal_edited().connect(
295 sigc::mem_fun(*this,&studio::Dock_PalEdit::set_color),
299 App::dialog_color->present();
303 Dock_PalEdit::select_fill_color(int i)
305 synfigapp::Main::set_fill_color(get_color(i));
309 Dock_PalEdit::select_outline_color(int i)
311 synfigapp::Main::set_outline_color(get_color(i));
315 Dock_PalEdit::set_default_palette()
322 palette_.push_back(Color::alpha());
323 for(int i=0;i<width-1;i++)
326 float(i)/(float)(width-2),
327 float(i)/(float)(width-2),
328 float(i)/(float)(width-2)
330 palette_.push_back(c);
334 for(int i=0;i<width;i++)
336 float x(float(i)/(float)(width-1));
337 const Color tan1(0.2,0.05,0);
338 const Color tan2(0.85,0.64,0.20);
340 palette_.push_back(Color::blend(tan2,tan1,x));
344 palette_.push_back(Color::red());
345 palette_.push_back(Color(1.0f,0.25f,0.0f)); // Orange
346 palette_.push_back(Color::yellow());
347 palette_.push_back(Color(0.25f,1.00f,0.0f)); // yellow-green
348 palette_.push_back(Color::green());
349 palette_.push_back(Color(0.0f,1.00f,0.25f)); // green-blue
350 palette_.push_back(Color::cyan());
351 palette_.push_back(Color(0.0f,0.25f,1.0f)); // Sea Blue
352 palette_.push_back(Color::blue());
353 palette_.push_back(Color(0.25f,0.0f,1.0f));
354 palette_.push_back(Color::magenta());
355 palette_.push_back(Color(1.0f,0.0f,0.25f));
361 for(int j=0;j<levels;j++)
362 for(int i=0;i<width;i++)
364 Color c(Color::red());
365 c.set_hue(c.get_hue()-Angle::rot(float(i)/(float)(width)));
367 float s(float(levels-j)/float(levels));
369 c.set_r(c.get_r()*s);
370 c.set_g(c.get_g()*s);
371 c.set_b(c.get_b()*s);
372 palette_.push_back(c);
379 for(int i=0;i<levels*levels*levels;i++)
382 float(i%levels)/(float)(levels-1),
383 float(i/levels%levels)/(float)(levels-1),
384 float(i/(levels*levels))/(float)(levels-1)
386 palette_.push_back(c);
393 Dock_PalEdit::size()const
395 return palette_.size();