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