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"
47 #include "../general.h"
51 /* === U S I N G =========================================================== */
55 using namespace synfig;
56 using namespace studio;
58 /* === M A C R O S ========================================================= */
60 /* === G L O B A L S ======================================================= */
62 class studio::PaletteSettings : public synfigapp::Settings
64 Dock_PalEdit* dialog_palette;
67 PaletteSettings(Dock_PalEdit* window,const synfig::String& name):
68 dialog_palette(window),
71 dialog_palette->dialog_settings.add_domain(this,name);
74 virtual ~PaletteSettings()
76 dialog_palette->dialog_settings.remove_domain(name);
79 virtual bool get_value(const synfig::String& key, synfig::String& value)const
81 int i(atoi(key.c_str()));
82 if(i<0 || i>=dialog_palette->size())
84 Color c(dialog_palette->get_color(i));
85 value=strprintf("%f %f %f %f",c.get_r(),c.get_g(),c.get_b(),c.get_a());
89 virtual bool set_value(const synfig::String& key,const synfig::String& value)
91 int i(atoi(key.c_str()));
94 if(i>=dialog_palette->size())
95 dialog_palette->palette_.resize(i+1);
97 if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
99 dialog_palette->set_color(Color(r,g,b,a),i);
103 virtual KeyList get_key_list()const
105 synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
108 for(i=0;i<dialog_palette->size();i++)
109 ret.push_back(strprintf("%03d",i));
114 /* === P R O C E D U R E S ================================================= */
116 /* === M E T H O D S ======================================================= */
118 Dock_PalEdit::Dock_PalEdit():
119 Dockable("pal_edit",_("Palette Editor"),Gtk::StockID("gtk-select-color")),
120 //palette_settings(new PaletteSettings(this,"colors")),
123 action_group=Gtk::ActionGroup::create();
124 action_group->add(Gtk::Action::create(
126 Gtk::StockID("gtk-add"),
128 _("Add current foreground color\nto the palette")
132 &Dock_PalEdit::on_add_pressed
136 App::ui_manager()->insert_action_group(action_group);
138 Glib::ustring ui_info =
140 " <toolbar action='toolbar-palette'>"
141 " <toolitem action='palette-add-color' />"
146 App::ui_manager()->add_ui_from_string(ui_info);
148 set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-palette")));
152 Gtk::StockID("gtk-add"),
153 _("Add current foreground color\nto the palette")
154 )->signal_clicked().connect(
157 &Dock_PalEdit::on_add_pressed
163 table.set_homogeneous(true);
165 set_default_palette();
170 Dock_PalEdit::~Dock_PalEdit()
172 //delete palette_settings;
176 Dock_PalEdit::set_palette(const synfig::Palette& x)
183 Dock_PalEdit::on_add_pressed()
185 add_color(synfigapp::Main::get_foreground_color());
189 Dock_PalEdit::show_menu(int i)
191 Gtk::Menu* menu(manage(new Gtk::Menu()));
192 menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
194 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-select-color"),
196 sigc::mem_fun(*this,&studio::Dock_PalEdit::edit_color),
201 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-delete"),
203 sigc::mem_fun(*this,&studio::Dock_PalEdit::erase_color),
208 menu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
210 menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Load Default Palette"),
211 sigc::mem_fun(*this,&studio::Dock_PalEdit::set_default_palette)
214 menu->popup(3,gtk_get_current_event_time());
218 Dock_PalEdit::add_color(const synfig::Color& x)
220 palette_.push_back(x);
227 Dock_PalEdit::set_color(synfig::Color x, int i)
235 Dock_PalEdit::get_color(int i)const
237 return palette_[i].color;
241 Dock_PalEdit::erase_color(int i)
243 palette_.erase(palette_.begin()+i);
249 Dock_PalEdit::refresh()
254 table.foreach(sigc::mem_fun(table,&Gtk::Table::remove));
256 for(int i=0;i<size();i++)
258 Widget_Color* widget_color(manage(new Widget_Color()));
259 widget_color->set_value(get_color(i));
260 widget_color->set_size_request(12,12);
261 widget_color->signal_activate().connect(
263 sigc::mem_fun(*this,&studio::Dock_PalEdit::select_color),
267 widget_color->signal_secondary().connect(
269 sigc::mem_fun(*this,&studio::Dock_PalEdit::show_menu),
273 int c(i%width),r(i/width);
274 table.attach(*widget_color, c, c+1, r, r+1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
282 Dock_PalEdit::edit_color(int i)
284 App::dialog_color->reset();
285 App::dialog_color->set_color(get_color(i));
286 App::dialog_color->signal_edited().connect(
288 sigc::mem_fun(*this,&studio::Dock_PalEdit::set_color),
292 App::dialog_color->present();
296 Dock_PalEdit::select_color(int i)
298 synfigapp::Main::set_foreground_color(get_color(i));
302 Dock_PalEdit::set_default_palette()
309 palette_.push_back(Color::alpha());
310 for(int i=0;i<width-1;i++)
313 float(i)/(float)(width-2),
314 float(i)/(float)(width-2),
315 float(i)/(float)(width-2)
317 palette_.push_back(c);
321 for(int i=0;i<width;i++)
323 float x(float(i)/(float)(width-1));
324 const Color tan1(0.2,0.05,0);
325 const Color tan2(0.85,0.64,0.20);
327 palette_.push_back(Color::blend(tan2,tan1,x));
331 palette_.push_back(Color::red());
332 palette_.push_back(Color(1.0f,0.25f,0.0f)); // Orange
333 palette_.push_back(Color::yellow());
334 palette_.push_back(Color(0.25f,1.00f,0.0f)); // yellow-green
335 palette_.push_back(Color::green());
336 palette_.push_back(Color(0.0f,1.00f,0.25f)); // green-blue
337 palette_.push_back(Color::cyan());
338 palette_.push_back(Color(0.0f,0.25f,1.0f)); // Sea Blue
339 palette_.push_back(Color::blue());
340 palette_.push_back(Color(0.25f,0.0f,1.0f));
341 palette_.push_back(Color::magenta());
342 palette_.push_back(Color(1.0f,0.0f,0.25f));
348 for(int j=0;j<levels;j++)
349 for(int i=0;i<width;i++)
351 Color c(Color::red());
352 c.set_hue(c.get_hue()-Angle::rot(float(i)/(float)(width)));
354 float s(float(levels-j)/float(levels));
356 c.set_r(c.get_r()*s);
357 c.set_g(c.get_g()*s);
358 c.set_b(c.get_b()*s);
359 palette_.push_back(c);
366 for(int i=0;i<levels*levels*levels;i++)
369 float(i%levels)/(float)(levels-1),
370 float(i/levels%levels)/(float)(levels-1),
371 float(i/(levels*levels))/(float)(levels-1)
373 palette_.push_back(c);
380 Dock_PalEdit::size()const
382 return palette_.size();