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