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