fcafe64fbe1eaea65bcf5bd115d328c9f7ce4a7a
[synfig.git] /
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dock_paledit.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "dock_paledit.h"
34 #include "../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 "../widget_color.h"
42 #include <gtkmm/spinbutton.h>
43 #include <gtkmm/menu.h>
44 #include <synfigapp/main.h>
45 #include "../app.h"
46 #include "../dialog_color.h"
47
48 #include "../general.h"
49
50 #endif
51
52 /* === U S I N G =========================================================== */
53
54 using namespace std;
55 using namespace etl;
56 using namespace synfig;
57 using namespace studio;
58
59 /* === M A C R O S ========================================================= */
60
61 /* === G L O B A L S ======================================================= */
62 /*
63 class studio::PaletteSettings : public synfigapp::Settings
64 {
65         Dock_PalEdit* dialog_palette;
66         synfig::String name;
67 public:
68         PaletteSettings(Dock_PalEdit* window,const synfig::String& name):
69                 dialog_palette(window),
70                 name(name)
71         {
72                 dialog_palette->dialog_settings.add_domain(this,name);
73         }
74
75         virtual ~PaletteSettings()
76         {
77                 dialog_palette->dialog_settings.remove_domain(name);
78         }
79
80         virtual bool get_value(const synfig::String& key, synfig::String& value)const
81         {
82                 int i(atoi(key.c_str()));
83                 if(i<0 || i>=dialog_palette->size())
84                         return false;
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());
87                 return true;
88         }
89
90         virtual bool set_value(const synfig::String& key,const synfig::String& value)
91         {
92                 int i(atoi(key.c_str()));
93                 if(i<0)
94                         return false;
95                 if(i>=dialog_palette->size())
96                         dialog_palette->palette_.resize(i+1);
97                 float r,g,b,a;
98                 if(!strscanf(value,"%f %f %f %f",&r,&g,&b,&a))
99                         return false;
100                 dialog_palette->set_color(Color(r,g,b,a),i);
101                 return true;
102         }
103
104         virtual KeyList get_key_list()const
105         {
106                 synfigapp::Settings::KeyList ret(synfigapp::Settings::get_key_list());
107
108                 int i;
109                 for(i=0;i<dialog_palette->size();i++)
110                         ret.push_back(strprintf("%03d",i));
111                 return ret;
112         }
113 };
114 */
115 /* === P R O C E D U R E S ================================================= */
116
117 /* === M E T H O D S ======================================================= */
118
119 Dock_PalEdit::Dock_PalEdit():
120         Dockable("pal_edit",_("Palette Editor"),Gtk::StockID("gtk-select-color")),
121         //palette_settings(new PaletteSettings(this,"colors")),
122         table(2,2,false)
123 {
124         action_group=Gtk::ActionGroup::create();
125         action_group->add(Gtk::Action::create(
126                 "palette-add-color",
127                 Gtk::StockID("gtk-add"),
128                 _("Add Color"),
129                 _("Add current foreground color\nto the palette")
130         ),
131                 sigc::mem_fun(
132                         *this,
133                         &Dock_PalEdit::on_add_pressed
134                 )
135         );
136
137         App::ui_manager()->insert_action_group(action_group);
138
139     Glib::ustring ui_info =
140         "<ui>"
141         "       <toolbar action='toolbar-palette'>"
142         "       <toolitem action='palette-add-color' />"
143         "       </toolbar>"
144         "</ui>"
145         ;
146
147         App::ui_manager()->add_ui_from_string(ui_info);
148
149         set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-palette")));
150
151         /*
152         add_button(
153                 Gtk::StockID("gtk-add"),
154                 _("Add current foreground color\nto the palette")
155         )->signal_clicked().connect(
156                 sigc::mem_fun(
157                         *this,
158                         &Dock_PalEdit::on_add_pressed
159                 )
160         );
161         */
162
163         add(table);
164         table.set_homogeneous(true);
165
166         set_default_palette();
167
168         show_all_children();
169 }
170
171 Dock_PalEdit::~Dock_PalEdit()
172 {
173         //delete palette_settings;
174 }
175
176 void
177 Dock_PalEdit::set_palette(const synfig::Palette& x)
178 {
179         palette_=x;
180         refresh();
181 }
182
183 void
184 Dock_PalEdit::on_add_pressed()
185 {
186         add_color(synfigapp::Main::get_foreground_color());
187 }
188
189 void
190 Dock_PalEdit::show_menu(int i)
191 {
192         Gtk::Menu* menu(manage(new Gtk::Menu()));
193         menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
194
195         menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-select-color"),
196                 sigc::bind(
197                         sigc::mem_fun(*this,&studio::Dock_PalEdit::edit_color),
198                         i
199                 )
200         ));
201
202         menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-delete"),
203                 sigc::bind(
204                         sigc::mem_fun(*this,&studio::Dock_PalEdit::erase_color),
205                         i
206                 )
207         ));
208
209         menu->items().push_back(Gtk::Menu_Helpers::SeparatorElem());
210
211         menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Load Default Palette"),
212                 sigc::mem_fun(*this,&studio::Dock_PalEdit::set_default_palette)
213         ));
214
215         menu->popup(3,gtk_get_current_event_time());
216 }
217
218 int
219 Dock_PalEdit::add_color(const synfig::Color& x)
220 {
221         palette_.push_back(x);
222         signal_changed()();
223         refresh();
224         return size()-1;
225 }
226
227 void
228 Dock_PalEdit::set_color(synfig::Color x, int i)
229 {
230         palette_[i].color=x;
231         signal_changed()();
232         refresh();
233 }
234
235 Color
236 Dock_PalEdit::get_color(int i)const
237 {
238         return palette_[i].color;
239 }
240
241 void
242 Dock_PalEdit::erase_color(int i)
243 {
244         palette_.erase(palette_.begin()+i);
245         signal_changed()();
246         refresh();
247 }
248
249 void
250 Dock_PalEdit::refresh()
251 {
252         const int width(12);
253
254         // Clear the table
255         table.foreach(sigc::mem_fun(table,&Gtk::Table::remove));
256
257         for(int i=0;i<size();i++)
258         {
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(
263                         sigc::bind(
264                                 sigc::mem_fun(*this,&studio::Dock_PalEdit::select_color),
265                                 i
266                         )
267                 );
268                 widget_color->signal_secondary().connect(
269                         sigc::bind(
270                                 sigc::mem_fun(*this,&studio::Dock_PalEdit::show_menu),
271                                 i
272                         )
273                 );
274                 int c(i%width),r(i/width);
275                 table.attach(*widget_color, c, c+1, r, r+1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
276         }
277         table.show_all();
278         queue_draw();
279 }
280
281
282 void
283 Dock_PalEdit::edit_color(int i)
284 {
285         App::dialog_color->reset();
286         App::dialog_color->set_color(get_color(i));
287         App::dialog_color->signal_edited().connect(
288                 sigc::bind(
289                         sigc::mem_fun(*this,&studio::Dock_PalEdit::set_color),
290                         i
291                 )
292         );
293         App::dialog_color->present();
294 }
295
296 void
297 Dock_PalEdit::select_color(int i)
298 {
299         synfigapp::Main::set_foreground_color(get_color(i));
300 }
301
302 void
303 Dock_PalEdit::set_default_palette()
304 {
305         int width=12;
306
307         palette_.clear();
308
309         // Greys
310         palette_.push_back(Color::alpha());
311         for(int i=0;i<width-1;i++)
312         {
313                 Color c(
314                         float(i)/(float)(width-2),
315                         float(i)/(float)(width-2),
316                         float(i)/(float)(width-2)
317                 );
318                 palette_.push_back(c);
319         }
320
321         // Tans
322         for(int i=0;i<width;i++)
323         {
324                 float x(float(i)/(float)(width-1));
325                 const Color tan1(0.2,0.05,0);
326                 const Color tan2(0.85,0.64,0.20);
327
328                 palette_.push_back(Color::blend(tan2,tan1,x));
329         }
330
331         // Solids
332         palette_.push_back(Color::red());
333         palette_.push_back(Color(1.0f,0.25f,0.0f));     // Orange
334         palette_.push_back(Color::yellow());
335         palette_.push_back(Color(0.25f,1.00f,0.0f));    // yellow-green
336         palette_.push_back(Color::green());
337         palette_.push_back(Color(0.0f,1.00f,0.25f));    // green-blue
338         palette_.push_back(Color::cyan());
339         palette_.push_back(Color(0.0f,0.25f,1.0f));     // Sea Blue
340         palette_.push_back(Color::blue());
341         palette_.push_back(Color(0.25f,0.0f,1.0f));
342         palette_.push_back(Color::magenta());
343         palette_.push_back(Color(1.0f,0.0f,0.25f));
344
345
346         const int levels(3);
347
348         // Colors
349         for(int j=0;j<levels;j++)
350         for(int i=0;i<width;i++)
351         {
352                 Color c(Color::red());
353                 c.set_hue(c.get_hue()-Angle::rot(float(i)/(float)(width)));
354                 c=c.clamped();
355                 float s(float(levels-j)/float(levels));
356                 s*=s;
357                 c.set_r(c.get_r()*s);
358                 c.set_g(c.get_g()*s);
359                 c.set_b(c.get_b()*s);
360                 palette_.push_back(c);
361         }
362
363
364         /*
365         const int levels(3);
366
367         for(int i=0;i<levels*levels*levels;i++)
368         {
369                 Color c(
370                         float(i%levels)/(float)(levels-1),
371                         float(i/levels%levels)/(float)(levels-1),
372                         float(i/(levels*levels))/(float)(levels-1)
373                 );
374                 palette_.push_back(c);
375         }
376         */
377         refresh();
378 }
379
380 int
381 Dock_PalEdit::size()const
382 {
383         return palette_.size();
384 }