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
10 ** Copyright (c) 2010 Nikita Kitaev
12 ** This package is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU General Public License as
14 ** published by the Free Software Foundation; either version 2 of
15 ** the License, or (at your option) any later version.
17 ** This package is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
23 /* ========================================================================= */
25 /* === H E A D E R S ======================================================= */
35 #include "dock_paledit.h"
36 #include "../../widgets/widget_color.h"
37 #include <gtkmm/frame.h>
38 #include <gtkmm/table.h>
39 #include <gtkmm/label.h>
40 #include <synfig/general.h>
41 #include <synfigapp/canvasinterface.h>
42 #include <synfigapp/value_desc.h>
43 #include "../../widgets/widget_color.h"
44 #include <gtkmm/spinbutton.h>
45 #include <gtkmm/menu.h>
46 #include <synfigapp/main.h>
47 #include "../../app.h"
48 #include "../../dialogs/dialog_color.h"
51 #include "../../general.h"
55 /* === U S I N G =========================================================== */
59 using namespace synfig;
60 using namespace studio;
62 /* === M A C R O S ========================================================= */
64 /* === G L O B A L S ======================================================= */
66 class studio::PaletteSettings : public synfigapp::Settings
68 Dock_PalEdit* dialog_palette;
71 PaletteSettings(Dock_PalEdit* window,const synfig::String& name):
72 dialog_palette(window),
75 dialog_palette->dialog_settings.add_domain(this,name);
78 virtual ~PaletteSettings()
80 dialog_palette->dialog_settings.remove_domain(name);
83 virtual bool get_value(const synfig::String& key, synfig::String& value)const
85 int i(atoi(key.c_str()));
86 if(i<0 || i>=dialog_palette->size())
88 Color c(dialog_palette->get_color(i));
89 value=strprintf("%f %f %f %f",c.get_r(),c.get_g(),c.get_b(),c.get_a());
93 virtual bool set_value(const synfig::String& key,const synfig::String& value)
95 int i(atoi(key.c_str()));
98 if(i>=dialog_palette->size())
99 dialog_palette->palette_.resize(i+1);
101 if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
103 dialog_palette->set_color(Color(r,g,b,a),i);
107 virtual KeyList get_key_list()const
109 synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
112 for(i=0;i<dialog_palette->size();i++)
113 ret.push_back(strprintf("%03d",i));
118 /* === P R O C E D U R E S ================================================= */
120 /* === M E T H O D S ======================================================= */
122 Dock_PalEdit::Dock_PalEdit():
123 Dockable("pal_edit",_("Palette Editor"),Gtk::StockID("synfig-palette")),
124 //palette_settings(new PaletteSettings(this,"colors")),
127 action_group=Gtk::ActionGroup::create("action_group_pal_edit");
128 action_group->add(Gtk::Action::create(
130 Gtk::StockID("gtk-add"),
132 _("Add current outline color\nto the palette")
136 &Dock_PalEdit::on_add_pressed
139 action_group->add(Gtk::Action::create(
141 Gtk::StockID("gtk-save"),
143 _("Save the current palette")
147 &Dock_PalEdit::on_save_pressed
150 action_group->add(Gtk::Action::create(
152 Gtk::StockID("gtk-open"),
154 _("Load a saved palette")
158 &Dock_PalEdit::on_load_pressed
161 action_group->add(Gtk::Action::create(
162 "palette-set-default",
163 Gtk::StockID("gtk-refresh"),
165 _("Load default palette")
169 &Dock_PalEdit::set_default_palette
174 App::ui_manager()->insert_action_group(action_group);
176 Glib::ustring ui_info =
178 " <toolbar action='toolbar-palette'>"
179 " <toolitem action='palette-add-color' />"
180 " <toolitem action='palette-save' />"
181 " <toolitem action='palette-load' />"
182 " <toolitem action='palette-set-default' />"
187 App::ui_manager()->add_ui_from_string(ui_info);
189 set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-palette")));
193 Gtk::StockID("gtk-add"),
194 _("Add current outline color\nto the palette")
195 )->signal_clicked().connect(
198 &Dock_PalEdit::on_add_pressed
204 table.set_homogeneous(true);
206 set_default_palette();
211 Dock_PalEdit::~Dock_PalEdit()
213 //delete palette_settings;
217 Dock_PalEdit::set_palette(const synfig::Palette& x)
224 Dock_PalEdit::on_add_pressed()
226 add_color(synfigapp::Main::get_outline_color());
230 Dock_PalEdit::on_save_pressed()
232 synfig::String filename = "";
233 while (App::dialog_save_file(_("Choose a Filename to Save As"),
234 filename, ANIMATION_DIR_PREFERENCE))
236 // If the filename still has wildcards, then we should
237 // continue looking for the file we want
238 string base_filename = basename(filename);
239 if (find(base_filename.begin(),base_filename.end(),'*')!=base_filename.end())
242 if (filename_extension(filename) == "")
247 String ext(filename_extension(filename));
248 if(ext!=".spal" && !App::dialog_yes_no(_("Unknown extension"),
249 _("You have given the file name an extension\nwhich I do not recognize. Are you sure this is what you want?")))
259 int stat_return = stat(filename.c_str(), &s);
261 // if stat() fails with something other than 'file doesn't exist', there's been a real
262 // error of some kind. let's give up now and ask for a new path.
263 if (stat_return == -1 && errno != ENOENT)
265 perror(filename.c_str());
266 string msg(strprintf(_("Unable to check whether '%s' exists."), filename.c_str()));
267 App::dialog_error_blocking(_("Save Palette - Error"),msg.c_str());
271 // if the file exists and the user doesn't want to overwrite it, keep prompting for a filename
272 string msg(strprintf(_("A file named '%s' already exists.\n\n"
273 "Do you want to replace it with the file you are saving?"), filename.c_str()));
274 if ((stat_return == 0) &&
275 !App::dialog_yes_no(_("File exists"),msg.c_str()))
278 palette_.save_to_file(filename);
284 Dock_PalEdit::on_load_pressed()
286 synfig::String filename = "*.spal";
287 while(App::dialog_open_file(_("Choose a Palette to load"), filename, ANIMATION_DIR_PREFERENCE))
289 // If the filename still has wildcards, then we should
290 // continue looking for the file we want
291 if(find(filename.begin(),filename.end(),'*')!=filename.end())
296 palette_=synfig::Palette::load_from_file(filename);
300 App::get_ui_interface()->error(_("Unable to open file"));
309 Dock_PalEdit::show_menu(int i)
311 Gtk::Menu* menu(manage(new Gtk::Menu()));
312 menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
314 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-select-color"),
316 sigc::mem_fun(*this,&studio::Dock_PalEdit::edit_color),
321 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-delete"),
323 sigc::mem_fun(*this,&studio::Dock_PalEdit::erase_color),
328 //menu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
330 menu->popup(3,gtk_get_current_event_time());
334 Dock_PalEdit::add_color(const synfig::Color& x)
336 palette_.push_back(x);
343 Dock_PalEdit::set_color(synfig::Color x, int i)
351 Dock_PalEdit::get_color(int i)const
353 return palette_[i].color;
357 Dock_PalEdit::erase_color(int i)
359 palette_.erase(palette_.begin()+i);
365 Dock_PalEdit::refresh()
370 table.foreach(sigc::mem_fun(table,&Gtk::Table::remove));
372 for(int i=0;i<size();i++)
374 Widget_Color* widget_color(manage(new Widget_Color()));
375 widget_color->set_value(get_color(i));
376 widget_color->set_size_request(12,12);
377 widget_color->signal_activate().connect(
379 sigc::mem_fun(*this,&studio::Dock_PalEdit::select_fill_color),
383 widget_color->signal_middle_click().connect(
385 sigc::mem_fun(*this,&studio::Dock_PalEdit::select_outline_color),
389 widget_color->signal_right_click().connect(
391 sigc::mem_fun(*this,&studio::Dock_PalEdit::show_menu),
395 int c(i%width),r(i/width);
396 table.attach(*widget_color, c, c+1, r, r+1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
404 Dock_PalEdit::edit_color(int i)
406 App::dialog_color->reset();
407 App::dialog_color->set_color(get_color(i));
408 App::dialog_color->signal_edited().connect(
410 sigc::mem_fun(*this,&studio::Dock_PalEdit::set_color),
414 App::dialog_color->present();
418 Dock_PalEdit::select_fill_color(int i)
420 synfigapp::Main::set_fill_color(get_color(i));
424 Dock_PalEdit::select_outline_color(int i)
426 synfigapp::Main::set_outline_color(get_color(i));
430 Dock_PalEdit::set_default_palette()
437 palette_.push_back(Color::alpha());
438 for(int i=0;i<width-1;i++)
441 float(i)/(float)(width-2),
442 float(i)/(float)(width-2),
443 float(i)/(float)(width-2)
445 palette_.push_back(c);
449 for(int i=0;i<width;i++)
451 float x(float(i)/(float)(width-1));
452 const Color tan1(0.2,0.05,0);
453 const Color tan2(0.85,0.64,0.20);
455 palette_.push_back(Color::blend(tan2,tan1,x));
459 palette_.push_back(Color::red());
460 palette_.push_back(Color(1.0f,0.25f,0.0f)); // Orange
461 palette_.push_back(Color::yellow());
462 palette_.push_back(Color(0.25f,1.00f,0.0f)); // yellow-green
463 palette_.push_back(Color::green());
464 palette_.push_back(Color(0.0f,1.00f,0.25f)); // green-blue
465 palette_.push_back(Color::cyan());
466 palette_.push_back(Color(0.0f,0.25f,1.0f)); // Sea Blue
467 palette_.push_back(Color::blue());
468 palette_.push_back(Color(0.25f,0.0f,1.0f));
469 palette_.push_back(Color::magenta());
470 palette_.push_back(Color(1.0f,0.0f,0.25f));
476 for(int j=0;j<levels;j++)
477 for(int i=0;i<width;i++)
479 Color c(Color::red());
480 c.set_hue(c.get_hue()-Angle::rot(float(i)/(float)(width)));
482 float s(float(levels-j)/float(levels));
484 c.set_r(c.get_r()*s);
485 c.set_g(c.get_g()*s);
486 c.set_b(c.get_b()*s);
487 palette_.push_back(c);
494 for(int i=0;i<levels*levels*levels;i++)
497 float(i%levels)/(float)(levels-1),
498 float(i/levels%levels)/(float)(levels-1),
499 float(i/(levels*levels))/(float)(levels-1)
501 palette_.push_back(c);
508 Dock_PalEdit::size()const
510 return palette_.size();