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 ======================================================= */
34 #include "dock_paledit.h"
35 #include "../../widgets/widget_color.h"
36 #include <gtkmm/frame.h>
37 #include <gtkmm/table.h>
38 #include <gtkmm/label.h>
39 #include <synfig/general.h>
40 #include <synfigapp/canvasinterface.h>
41 #include <synfigapp/value_desc.h>
42 #include "../../widgets/widget_color.h"
43 #include <gtkmm/spinbutton.h>
44 #include <gtkmm/menu.h>
45 #include <synfigapp/main.h>
46 #include "../../app.h"
47 #include "../../dialogs/dialog_color.h"
50 #include "../../general.h"
54 /* === U S I N G =========================================================== */
58 using namespace synfig;
59 using namespace studio;
61 /* === M A C R O S ========================================================= */
63 /* === G L O B A L S ======================================================= */
65 class studio::PaletteSettings : public synfigapp::Settings
67 Dock_PalEdit* dialog_palette;
70 PaletteSettings(Dock_PalEdit* window,const synfig::String& name):
71 dialog_palette(window),
74 dialog_palette->dialog_settings.add_domain(this,name);
77 virtual ~PaletteSettings()
79 dialog_palette->dialog_settings.remove_domain(name);
82 virtual bool get_value(const synfig::String& key, synfig::String& value)const
84 int i(atoi(key.c_str()));
85 if(i<0 || i>=dialog_palette->size())
87 Color c(dialog_palette->get_color(i));
88 value=strprintf("%f %f %f %f",c.get_r(),c.get_g(),c.get_b(),c.get_a());
92 virtual bool set_value(const synfig::String& key,const synfig::String& value)
94 int i(atoi(key.c_str()));
97 if(i>=dialog_palette->size())
98 dialog_palette->palette_.resize(i+1);
100 if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
102 dialog_palette->set_color(Color(r,g,b,a),i);
106 virtual KeyList get_key_list()const
108 synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
111 for(i=0;i<dialog_palette->size();i++)
112 ret.push_back(strprintf("%03d",i));
117 /* === P R O C E D U R E S ================================================= */
119 /* === M E T H O D S ======================================================= */
121 Dock_PalEdit::Dock_PalEdit():
122 Dockable("pal_edit",_("Palette Editor"),Gtk::StockID("gtk-select-color")),
123 //palette_settings(new PaletteSettings(this,"colors")),
126 action_group=Gtk::ActionGroup::create("action_group_pal_edit");
127 action_group->add(Gtk::Action::create(
129 Gtk::StockID("gtk-add"),
131 _("Add current outline color\nto the palette")
135 &Dock_PalEdit::on_add_pressed
138 action_group->add(Gtk::Action::create(
140 Gtk::StockID("gtk-save"),
142 _("Save the current palette")
146 &Dock_PalEdit::on_save_pressed
149 action_group->add(Gtk::Action::create(
151 Gtk::StockID("gtk-open"),
153 _("Load a saved palette")
157 &Dock_PalEdit::on_load_pressed
160 action_group->add(Gtk::Action::create(
161 "palette-set-default",
162 Gtk::StockID("gtk-clear"),
164 _("Load default palette")
168 &Dock_PalEdit::set_default_palette
173 App::ui_manager()->insert_action_group(action_group);
175 Glib::ustring ui_info =
177 " <toolbar action='toolbar-palette'>"
178 " <toolitem action='palette-add-color' />"
179 " <toolitem action='palette-save' />"
180 " <toolitem action='palette-load' />"
181 " <toolitem action='palette-set-default' />"
186 App::ui_manager()->add_ui_from_string(ui_info);
188 set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-palette")));
192 Gtk::StockID("gtk-add"),
193 _("Add current outline color\nto the palette")
194 )->signal_clicked().connect(
197 &Dock_PalEdit::on_add_pressed
203 table.set_homogeneous(true);
205 set_default_palette();
210 Dock_PalEdit::~Dock_PalEdit()
212 //delete palette_settings;
216 Dock_PalEdit::set_palette(const synfig::Palette& x)
223 Dock_PalEdit::on_add_pressed()
225 add_color(synfigapp::Main::get_outline_color());
229 Dock_PalEdit::on_save_pressed()
231 synfig::String filename = "";
232 while (App::dialog_save_file(_("Choose a Filename to Save As"),
233 filename, ANIMATION_DIR_PREFERENCE))
235 // If the filename still has wildcards, then we should
236 // continue looking for the file we want
237 string base_filename = basename(filename);
238 if (find(base_filename.begin(),base_filename.end(),'*')!=base_filename.end())
241 if (filename_extension(filename) == "")
246 String ext(filename_extension(filename));
247 if(ext!=".spal" && !App::dialog_yes_no(_("Unknown extension"),
248 _("You have given the file name an extension\nwhich I do not recognize. Are you sure this is what you want?")))
258 int stat_return = stat(filename.c_str(), &s);
260 // if stat() fails with something other than 'file doesn't exist', there's been a real
261 // error of some kind. let's give up now and ask for a new path.
262 if (stat_return == -1 && errno != ENOENT)
264 perror(filename.c_str());
265 string msg(strprintf(_("Unable to check whether '%s' exists."), filename.c_str()));
266 App::dialog_error_blocking(_("Save Palette - Error"),msg.c_str());
270 // if the file exists and the user doesn't want to overwrite it, keep prompting for a filename
271 string msg(strprintf(_("A file named '%s' already exists.\n\n"
272 "Do you want to replace it with the file you are saving?"), filename.c_str()));
273 if ((stat_return == 0) &&
274 !App::dialog_yes_no(_("File exists"),msg.c_str()))
277 palette_.save_to_file(filename);
283 Dock_PalEdit::on_load_pressed()
285 synfig::String filename = "*.spal";
286 while(App::dialog_open_file(_("Choose a Palette to load"), filename, ANIMATION_DIR_PREFERENCE))
288 // If the filename still has wildcards, then we should
289 // continue looking for the file we want
290 if(find(filename.begin(),filename.end(),'*')!=filename.end())
295 palette_=synfig::Palette::load_from_file(filename);
299 App::get_ui_interface()->error(_("Unable to open file"));
308 Dock_PalEdit::show_menu(int i)
310 Gtk::Menu* menu(manage(new Gtk::Menu()));
311 menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
313 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-select-color"),
315 sigc::mem_fun(*this,&studio::Dock_PalEdit::edit_color),
320 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-delete"),
322 sigc::mem_fun(*this,&studio::Dock_PalEdit::erase_color),
327 //menu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
329 menu->popup(3,gtk_get_current_event_time());
333 Dock_PalEdit::add_color(const synfig::Color& x)
335 palette_.push_back(x);
342 Dock_PalEdit::set_color(synfig::Color x, int i)
350 Dock_PalEdit::get_color(int i)const
352 return palette_[i].color;
356 Dock_PalEdit::erase_color(int i)
358 palette_.erase(palette_.begin()+i);
364 Dock_PalEdit::refresh()
369 table.foreach(sigc::mem_fun(table,&Gtk::Table::remove));
371 for(int i=0;i<size();i++)
373 Widget_Color* widget_color(manage(new Widget_Color()));
374 widget_color->set_value(get_color(i));
375 widget_color->set_size_request(12,12);
376 widget_color->signal_activate().connect(
378 sigc::mem_fun(*this,&studio::Dock_PalEdit::select_fill_color),
382 widget_color->signal_middle_click().connect(
384 sigc::mem_fun(*this,&studio::Dock_PalEdit::select_outline_color),
388 widget_color->signal_right_click().connect(
390 sigc::mem_fun(*this,&studio::Dock_PalEdit::show_menu),
394 int c(i%width),r(i/width);
395 table.attach(*widget_color, c, c+1, r, r+1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
403 Dock_PalEdit::edit_color(int i)
405 App::dialog_color->reset();
406 App::dialog_color->set_color(get_color(i));
407 App::dialog_color->signal_edited().connect(
409 sigc::mem_fun(*this,&studio::Dock_PalEdit::set_color),
413 App::dialog_color->present();
417 Dock_PalEdit::select_fill_color(int i)
419 synfigapp::Main::set_fill_color(get_color(i));
423 Dock_PalEdit::select_outline_color(int i)
425 synfigapp::Main::set_outline_color(get_color(i));
429 Dock_PalEdit::set_default_palette()
436 palette_.push_back(Color::alpha());
437 for(int i=0;i<width-1;i++)
440 float(i)/(float)(width-2),
441 float(i)/(float)(width-2),
442 float(i)/(float)(width-2)
444 palette_.push_back(c);
448 for(int i=0;i<width;i++)
450 float x(float(i)/(float)(width-1));
451 const Color tan1(0.2,0.05,0);
452 const Color tan2(0.85,0.64,0.20);
454 palette_.push_back(Color::blend(tan2,tan1,x));
458 palette_.push_back(Color::red());
459 palette_.push_back(Color(1.0f,0.25f,0.0f)); // Orange
460 palette_.push_back(Color::yellow());
461 palette_.push_back(Color(0.25f,1.00f,0.0f)); // yellow-green
462 palette_.push_back(Color::green());
463 palette_.push_back(Color(0.0f,1.00f,0.25f)); // green-blue
464 palette_.push_back(Color::cyan());
465 palette_.push_back(Color(0.0f,0.25f,1.0f)); // Sea Blue
466 palette_.push_back(Color::blue());
467 palette_.push_back(Color(0.25f,0.0f,1.0f));
468 palette_.push_back(Color::magenta());
469 palette_.push_back(Color(1.0f,0.0f,0.25f));
475 for(int j=0;j<levels;j++)
476 for(int i=0;i<width;i++)
478 Color c(Color::red());
479 c.set_hue(c.get_hue()-Angle::rot(float(i)/(float)(width)));
481 float s(float(levels-j)/float(levels));
483 c.set_r(c.get_r()*s);
484 c.set_g(c.get_g()*s);
485 c.set_b(c.get_b()*s);
486 palette_.push_back(c);
493 for(int i=0;i<levels*levels*levels;i++)
496 float(i%levels)/(float)(levels-1),
497 float(i/levels%levels)/(float)(levels-1),
498 float(i/(levels*levels))/(float)(levels-1)
500 palette_.push_back(c);
507 Dock_PalEdit::size()const
509 return palette_.size();