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"
49 #include "../../general.h"
53 /* === U S I N G =========================================================== */
57 using namespace synfig;
58 using namespace studio;
60 /* === M A C R O S ========================================================= */
62 /* === G L O B A L S ======================================================= */
64 class studio::PaletteSettings : public synfigapp::Settings
66 Dock_PalEdit* dialog_palette;
69 PaletteSettings(Dock_PalEdit* window,const synfig::String& name):
70 dialog_palette(window),
73 dialog_palette->dialog_settings.add_domain(this,name);
76 virtual ~PaletteSettings()
78 dialog_palette->dialog_settings.remove_domain(name);
81 virtual bool get_value(const synfig::String& key, synfig::String& value)const
83 int i(atoi(key.c_str()));
84 if(i<0 || i>=dialog_palette->size())
86 Color c(dialog_palette->get_color(i));
87 value=strprintf("%f %f %f %f",c.get_r(),c.get_g(),c.get_b(),c.get_a());
91 virtual bool set_value(const synfig::String& key,const synfig::String& value)
93 int i(atoi(key.c_str()));
96 if(i>=dialog_palette->size())
97 dialog_palette->palette_.resize(i+1);
99 if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
101 dialog_palette->set_color(Color(r,g,b,a),i);
105 virtual KeyList get_key_list()const
107 synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
110 for(i=0;i<dialog_palette->size();i++)
111 ret.push_back(strprintf("%03d",i));
116 /* === P R O C E D U R E S ================================================= */
118 /* === M E T H O D S ======================================================= */
120 Dock_PalEdit::Dock_PalEdit():
121 Dockable("pal_edit",_("Palette Editor"),Gtk::StockID("gtk-select-color")),
122 //palette_settings(new PaletteSettings(this,"colors")),
125 action_group=Gtk::ActionGroup::create("action_group_pal_edit");
126 action_group->add(Gtk::Action::create(
128 Gtk::StockID("gtk-add"),
130 _("Add current outline color\nto the palette")
134 &Dock_PalEdit::on_add_pressed
137 action_group->add(Gtk::Action::create(
139 Gtk::StockID("gtk-save"),
141 _("Save the current palette")
145 &Dock_PalEdit::on_save_pressed
148 action_group->add(Gtk::Action::create(
150 Gtk::StockID("gtk-open"),
152 _("Load a saved palette")
156 &Dock_PalEdit::on_load_pressed
159 action_group->add(Gtk::Action::create(
160 "palette-set-default",
161 Gtk::StockID("gtk-clear"),
163 _("Load default palette")
167 &Dock_PalEdit::set_default_palette
172 App::ui_manager()->insert_action_group(action_group);
174 Glib::ustring ui_info =
176 " <toolbar action='toolbar-palette'>"
177 " <toolitem action='palette-add-color' />"
178 " <toolitem action='palette-save' />"
179 " <toolitem action='palette-load' />"
180 " <toolitem action='palette-set-default' />"
185 App::ui_manager()->add_ui_from_string(ui_info);
187 set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-palette")));
191 Gtk::StockID("gtk-add"),
192 _("Add current outline color\nto the palette")
193 )->signal_clicked().connect(
196 &Dock_PalEdit::on_add_pressed
202 table.set_homogeneous(true);
204 set_default_palette();
209 Dock_PalEdit::~Dock_PalEdit()
211 //delete palette_settings;
215 Dock_PalEdit::set_palette(const synfig::Palette& x)
222 Dock_PalEdit::on_add_pressed()
224 add_color(synfigapp::Main::get_outline_color());
228 Dock_PalEdit::on_save_pressed()
230 synfig::String filename = "";
231 while (App::dialog_save_file(_("Choose a Filename to Save As"),
232 filename, ANIMATION_DIR_PREFERENCE))
234 // If the filename still has wildcards, then we should
235 // continue looking for the file we want
236 string base_filename = basename(filename);
237 if (find(base_filename.begin(),base_filename.end(),'*')!=base_filename.end())
240 if (filename_extension(filename) == "")
245 String ext(filename_extension(filename));
246 if(ext!=".spal" && !App::dialog_yes_no(_("Unknown extension"),
247 _("You have given the file name an extension\nwhich I do not recognize. Are you sure this is what you want?")))
257 int stat_return = stat(filename.c_str(), &s);
259 // if stat() fails with something other than 'file doesn't exist', there's been a real
260 // error of some kind. let's give up now and ask for a new path.
261 if (stat_return == -1 && errno != ENOENT)
263 perror(filename.c_str());
264 string msg(strprintf(_("Unable to check whether '%s' exists."), filename.c_str()));
265 App::dialog_error_blocking(_("Save Palette - Error"),msg.c_str());
269 // if the file exists and the user doesn't want to overwrite it, keep prompting for a filename
270 string msg(strprintf(_("A file named '%s' already exists.\n\n"
271 "Do you want to replace it with the file you are saving?"), filename.c_str()));
272 if ((stat_return == 0) &&
273 !App::dialog_yes_no(_("File exists"),msg.c_str()))
276 palette_.save_to_file(filename);
282 Dock_PalEdit::on_load_pressed()
284 synfig::String filename = "*.spal";
285 while(App::dialog_open_file(_("Choose a Palette to load"), filename, ANIMATION_DIR_PREFERENCE))
287 // If the filename still has wildcards, then we should
288 // continue looking for the file we want
289 if(find(filename.begin(),filename.end(),'*')!=filename.end())
294 palette_=synfig::Palette::load_from_file(filename);
298 App::get_ui_interface()->error(_("Unable to open file"));
307 Dock_PalEdit::show_menu(int i)
309 Gtk::Menu* menu(manage(new Gtk::Menu()));
310 menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
312 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-select-color"),
314 sigc::mem_fun(*this,&studio::Dock_PalEdit::edit_color),
319 menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-delete"),
321 sigc::mem_fun(*this,&studio::Dock_PalEdit::erase_color),
326 //menu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
328 menu->popup(3,gtk_get_current_event_time());
332 Dock_PalEdit::add_color(const synfig::Color& x)
334 palette_.push_back(x);
341 Dock_PalEdit::set_color(synfig::Color x, int i)
349 Dock_PalEdit::get_color(int i)const
351 return palette_[i].color;
355 Dock_PalEdit::erase_color(int i)
357 palette_.erase(palette_.begin()+i);
363 Dock_PalEdit::refresh()
368 table.foreach(sigc::mem_fun(table,&Gtk::Table::remove));
370 for(int i=0;i<size();i++)
372 Widget_Color* widget_color(manage(new Widget_Color()));
373 widget_color->set_value(get_color(i));
374 widget_color->set_size_request(12,12);
375 widget_color->signal_activate().connect(
377 sigc::mem_fun(*this,&studio::Dock_PalEdit::select_fill_color),
381 widget_color->signal_middle_click().connect(
383 sigc::mem_fun(*this,&studio::Dock_PalEdit::select_outline_color),
387 widget_color->signal_right_click().connect(
389 sigc::mem_fun(*this,&studio::Dock_PalEdit::show_menu),
393 int c(i%width),r(i/width);
394 table.attach(*widget_color, c, c+1, r, r+1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
402 Dock_PalEdit::edit_color(int i)
404 App::dialog_color->reset();
405 App::dialog_color->set_color(get_color(i));
406 App::dialog_color->signal_edited().connect(
408 sigc::mem_fun(*this,&studio::Dock_PalEdit::set_color),
412 App::dialog_color->present();
416 Dock_PalEdit::select_fill_color(int i)
418 synfigapp::Main::set_fill_color(get_color(i));
422 Dock_PalEdit::select_outline_color(int i)
424 synfigapp::Main::set_outline_color(get_color(i));
428 Dock_PalEdit::set_default_palette()
435 palette_.push_back(Color::alpha());
436 for(int i=0;i<width-1;i++)
439 float(i)/(float)(width-2),
440 float(i)/(float)(width-2),
441 float(i)/(float)(width-2)
443 palette_.push_back(c);
447 for(int i=0;i<width;i++)
449 float x(float(i)/(float)(width-1));
450 const Color tan1(0.2,0.05,0);
451 const Color tan2(0.85,0.64,0.20);
453 palette_.push_back(Color::blend(tan2,tan1,x));
457 palette_.push_back(Color::red());
458 palette_.push_back(Color(1.0f,0.25f,0.0f)); // Orange
459 palette_.push_back(Color::yellow());
460 palette_.push_back(Color(0.25f,1.00f,0.0f)); // yellow-green
461 palette_.push_back(Color::green());
462 palette_.push_back(Color(0.0f,1.00f,0.25f)); // green-blue
463 palette_.push_back(Color::cyan());
464 palette_.push_back(Color(0.0f,0.25f,1.0f)); // Sea Blue
465 palette_.push_back(Color::blue());
466 palette_.push_back(Color(0.25f,0.0f,1.0f));
467 palette_.push_back(Color::magenta());
468 palette_.push_back(Color(1.0f,0.0f,0.25f));
474 for(int j=0;j<levels;j++)
475 for(int i=0;i<width;i++)
477 Color c(Color::red());
478 c.set_hue(c.get_hue()-Angle::rot(float(i)/(float)(width)));
480 float s(float(levels-j)/float(levels));
482 c.set_r(c.get_r()*s);
483 c.set_g(c.get_g()*s);
484 c.set_b(c.get_b()*s);
485 palette_.push_back(c);
492 for(int i=0;i<levels*levels*levels;i++)
495 float(i%levels)/(float)(levels-1),
496 float(i/levels%levels)/(float)(levels-1),
497 float(i/(levels*levels))/(float)(levels-1)
499 palette_.push_back(c);
506 Dock_PalEdit::size()const
508 return palette_.size();