Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_07_rc1 / src / gtkmm / widget_gradient.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_gradient.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 "widget_gradient.h"
34 #include "app.h"
35 #include <gtkmm/menu.h>
36 #include <synfig/exception.h>
37 #include <ETL/misc>
38
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46 using namespace studio;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 void
55 studio::render_gradient_to_window(const Glib::RefPtr<Gdk::Drawable>& window,const Gdk::Rectangle& ca,const synfig::Gradient &gradient)
56 {
57         int     height = ca.get_height();
58         int     width = ca.get_width()-4;
59
60         float sample_width(1.0f/(float)width);
61         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(window));
62         const Color bg1(0.25, 0.25, 0.25);
63         const Color bg2(0.5, 0.5, 0.5);
64         Gdk::Color gdk_c;
65         int i;
66         for(i=0;i<width;i++)
67         {
68                 const Color c(gradient(float(i)/float(width),sample_width));
69                 const Color c1(Color::blend(c,bg1,1.0).clamped());
70                 const Color c2(Color::blend(c,bg2,1.0).clamped());
71                 gushort r1(256*App::gamma.r_F32_to_U8(c1.get_r()));
72                 gushort g1(256*App::gamma.g_F32_to_U8(c1.get_g()));
73                 gushort b1(256*App::gamma.b_F32_to_U8(c1.get_b()));
74                 gushort r2(256*App::gamma.r_F32_to_U8(c2.get_r()));
75                 gushort g2(256*App::gamma.g_F32_to_U8(c2.get_g()));
76                 gushort b2(256*App::gamma.b_F32_to_U8(c2.get_b()));
77
78                 if((i*2/height)&1)
79                 {
80                         gdk_c.set_rgb(r1,g1,b1);
81                         gc->set_rgb_fg_color(gdk_c);
82                         window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y(), 1, height/2);
83
84                         gdk_c.set_rgb(r2,g2,b2);
85                         gc->set_rgb_fg_color(gdk_c);
86                         window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y()+height/2, 1, height/2);
87                 }
88                 else
89                 {
90                         gdk_c.set_rgb(r2,g2,b2);
91                         gc->set_rgb_fg_color(gdk_c);
92                         window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y(), 1, height/2);
93
94                         gdk_c.set_rgb(r1,g1,b1);
95                         gc->set_rgb_fg_color(gdk_c);
96                         window->draw_rectangle(gc, true, ca.get_x()+i+2, ca.get_y()+height/2, 1, height/2);
97                 }
98         }
99         gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
100         window->draw_rectangle(gc, false, ca.get_x()+1, ca.get_y()+1, ca.get_width()-3, height-3);
101         gc->set_rgb_fg_color(Gdk::Color("#000000"));
102         window->draw_rectangle(gc, false, ca.get_x(), ca.get_y(), ca.get_width()-1, height-1);
103 }
104
105 /* === M E T H O D S ======================================================= */
106
107 Widget_Gradient::Widget_Gradient():
108         editable_(false)
109 {
110         set_size_request(-1,64);
111         signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Gradient::redraw));
112         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
113         add_events(Gdk::BUTTON1_MOTION_MASK);
114
115 }
116
117 Widget_Gradient::~Widget_Gradient()
118 {
119 }
120
121 #define CONTROL_HEIGHT          16
122 bool
123 Widget_Gradient::redraw(GdkEventExpose */*bleh*/)
124 {
125         const int h(get_height());
126         const int w(get_width());
127
128         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
129         Gdk::Rectangle area(0,0,w,h);
130         if(!editable_)
131         {
132                 render_gradient_to_window(get_window(),area,gradient_);
133                 return true;
134         }
135
136         render_gradient_to_window(get_window(),Gdk::Rectangle(0,0,w,h-CONTROL_HEIGHT),gradient_);
137
138         gc->set_rgb_fg_color(Gdk::Color("#7f7f7f"));
139         get_window()->draw_rectangle(gc, false, 0, h-CONTROL_HEIGHT, w, CONTROL_HEIGHT);
140
141         Gradient::iterator iter,selected_iter;
142         bool show_selected(false);
143         for(iter=gradient_.begin();iter!=gradient_.end();iter++)
144         {
145                 if(*iter!=selected_cpoint)
146                 get_style()->paint_arrow(
147                         get_window(),
148                         (*iter==selected_cpoint)?Gtk::STATE_SELECTED:Gtk::STATE_ACTIVE,
149                         Gtk::SHADOW_OUT,
150                         area,
151                         *this,
152                         " ",
153                         Gtk::ARROW_UP,
154                         1,
155                         int(iter->pos*w)-CONTROL_HEIGHT/2+1,
156                         h-CONTROL_HEIGHT,
157                         CONTROL_HEIGHT,
158                         CONTROL_HEIGHT
159                 );
160                 else
161                 {
162                         selected_iter=iter;
163                         show_selected=true;
164                 }
165         }
166
167         // we do this so that we can be sure that
168         // the selected marker is shown on top
169         if(show_selected)
170         {
171                 get_style()->paint_arrow(
172                         get_window(),
173                         Gtk::STATE_SELECTED,
174                         Gtk::SHADOW_OUT,
175                         area,
176                         *this,
177                         " ",
178                         Gtk::ARROW_UP,
179                         1,
180                         round_to_int(selected_iter->pos*w)-CONTROL_HEIGHT/2+1,
181                         h-CONTROL_HEIGHT,
182                         CONTROL_HEIGHT,
183                         CONTROL_HEIGHT
184                 );
185         }
186
187         return true;
188 }
189
190 void
191 Widget_Gradient::insert_cpoint(float x)
192 {
193         Gradient::CPoint new_cpoint;
194         new_cpoint.pos=x;
195         new_cpoint.color=gradient_(x);
196         gradient_.push_back(new_cpoint);
197         gradient_.sort();
198         gradient_.sort();
199         set_selected_cpoint(new_cpoint);
200         queue_draw();
201 }
202
203 void
204 Widget_Gradient::remove_cpoint(float x)
205 {
206         gradient_.erase(gradient_.proximity(x));
207         signal_value_changed_();
208         queue_draw();
209 }
210
211 void
212 Widget_Gradient::popup_menu(float x)
213 {
214         Gtk::Menu* menu(manage(new Gtk::Menu()));
215
216         menu->items().clear();
217
218         menu->items().push_back(
219                 Gtk::Menu_Helpers::MenuElem(
220                         _("Insert CPoint"),
221                         sigc::bind(
222                                 sigc::mem_fun(*this,&studio::Widget_Gradient::insert_cpoint),
223                                 x
224                         )
225                 )
226         );
227
228         if(!gradient_.empty())
229         {
230                 menu->items().push_back(
231                         Gtk::Menu_Helpers::MenuElem(
232                                 _("Remove CPoint"),
233                                 sigc::bind(
234                                         sigc::mem_fun(*this,&studio::Widget_Gradient::remove_cpoint),
235                                         x
236                                 )
237                         )
238                 );
239         }
240
241         menu->popup(0,0);
242 }
243
244 void
245 Widget_Gradient::set_value(const synfig::Gradient& x)
246 {
247         gradient_=x;
248         if(gradient_.size())
249                 set_selected_cpoint(*gradient_.proximity(0.0f));
250         queue_draw();
251 }
252
253 void
254 Widget_Gradient::set_selected_cpoint(const synfig::Gradient::CPoint &x)
255 {
256         selected_cpoint=x;
257         signal_cpoint_selected_(selected_cpoint);
258         queue_draw();
259 }
260
261 void
262 Widget_Gradient::update_cpoint(const synfig::Gradient::CPoint &x)
263 {
264         try
265         {
266                 Gradient::iterator iter(gradient_.find(x));
267                 iter->pos=x.pos;
268                 iter->color=x.color;
269                 gradient_.sort();
270                 queue_draw();
271         }
272         catch(synfig::Exception::NotFound)
273         {
274                 // Yotta...
275         }
276 }
277
278 bool
279 Widget_Gradient::on_event(GdkEvent *event)
280 {
281         //if(editable_)
282         {
283                 const int x(static_cast<int>(event->button.x));
284                 const int y(static_cast<int>(event->button.y));
285
286                 float pos((float)x/(float)get_width());
287                 if(pos<0.0f)pos=0.0f;
288                 if(pos>1.0f)pos=1.0f;
289
290                 switch(event->type)
291                 {
292                 case GDK_MOTION_NOTIFY:
293                         if(editable_ && y>get_height()-CONTROL_HEIGHT)
294                         {
295                                 Gradient::iterator iter(gradient_.find(selected_cpoint));
296
297                                 if(event->button.state&GDK_SHIFT_MASK)
298                                 {
299                                         float begin(-100000000),end(100000000);
300                                         Gradient::iterator before(iter),after(iter);
301                                         after++;
302                                         if(iter!=gradient_.begin())
303                                         {
304                                                 before--;
305                                                 begin=before->pos;
306                                         }
307                                         if(after!=gradient_.end())
308                                         {
309                                                 end=after->pos;
310                                         }
311
312                                         if(pos>end)
313                                                 pos=end;
314                                         if(pos<begin)
315                                                 pos=begin;
316
317                                         iter->pos=pos;
318                                 }
319                                 else
320                                 {
321                                         iter->pos=pos;
322                                         gradient_.sort();
323                                 }
324
325 //                              signal_value_changed_();
326                                 changed_=true;
327                                 queue_draw();
328                                 return true;
329                         }
330                         break;
331                 case GDK_BUTTON_PRESS:
332                         changed_=false;
333                         if(event->button.button==1)
334                         {
335                                 if(editable_ && y>get_height()-CONTROL_HEIGHT)
336                                 {
337                                         set_selected_cpoint(*gradient_.proximity(pos));
338                                         queue_draw();
339                                         return true;
340                                 }
341                                 else
342                                 {
343                                         signal_clicked_();
344                                         return true;
345                                 }
346                         }
347                         else if(editable_ && event->button.button==3)
348                         {
349                                 popup_menu(pos);
350                                 return true;
351                         }
352                         break;
353                 case GDK_BUTTON_RELEASE:
354                         if(editable_ && event->button.button==1 && y>get_height()-CONTROL_HEIGHT)
355                         {
356                                 set_selected_cpoint(*gradient_.proximity(pos));
357                                 if(changed_)signal_value_changed_();
358                                 return true;
359                         }
360                 default:
361                         break;
362                 }
363         }
364
365         return false;
366 }