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