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