Fix 1820750. Delete popup menus once we're finished with them, so they don't keep...
[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 #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         menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
216
217         menu->items().clear();
218
219         menu->items().push_back(
220                 Gtk::Menu_Helpers::MenuElem(
221                         _("Insert CPoint"),
222                         sigc::bind(
223                                 sigc::mem_fun(*this,&studio::Widget_Gradient::insert_cpoint),
224                                 x
225                         )
226                 )
227         );
228
229         if(!gradient_.empty())
230         {
231                 menu->items().push_back(
232                         Gtk::Menu_Helpers::MenuElem(
233                                 _("Remove CPoint"),
234                                 sigc::bind(
235                                         sigc::mem_fun(*this,&studio::Widget_Gradient::remove_cpoint),
236                                         x
237                                 )
238                         )
239                 );
240         }
241
242         menu->popup(0,0);
243 }
244
245 void
246 Widget_Gradient::set_value(const synfig::Gradient& x)
247 {
248         gradient_=x;
249         if(gradient_.size())
250                 set_selected_cpoint(*gradient_.proximity(0.0f));
251         queue_draw();
252 }
253
254 void
255 Widget_Gradient::set_selected_cpoint(const synfig::Gradient::CPoint &x)
256 {
257         selected_cpoint=x;
258         signal_cpoint_selected_(selected_cpoint);
259         queue_draw();
260 }
261
262 void
263 Widget_Gradient::update_cpoint(const synfig::Gradient::CPoint &x)
264 {
265         try
266         {
267                 Gradient::iterator iter(gradient_.find(x));
268                 iter->pos=x.pos;
269                 iter->color=x.color;
270                 gradient_.sort();
271                 queue_draw();
272         }
273         catch(synfig::Exception::NotFound)
274         {
275                 // Yotta...
276         }
277 }
278
279 bool
280 Widget_Gradient::on_event(GdkEvent *event)
281 {
282         //if(editable_)
283         {
284                 const int x(static_cast<int>(event->button.x));
285                 const int y(static_cast<int>(event->button.y));
286
287                 float pos((float)x/(float)get_width());
288                 if(pos<0.0f)pos=0.0f;
289                 if(pos>1.0f)pos=1.0f;
290
291                 switch(event->type)
292                 {
293                 case GDK_MOTION_NOTIFY:
294                         if(editable_ && y>get_height()-CONTROL_HEIGHT)
295                         {
296                                 Gradient::iterator iter(gradient_.find(selected_cpoint));
297
298                                 if(event->button.state&GDK_SHIFT_MASK)
299                                 {
300                                         float begin(-100000000),end(100000000);
301                                         Gradient::iterator before(iter),after(iter);
302                                         after++;
303                                         if(iter!=gradient_.begin())
304                                         {
305                                                 before--;
306                                                 begin=before->pos;
307                                         }
308                                         if(after!=gradient_.end())
309                                         {
310                                                 end=after->pos;
311                                         }
312
313                                         if(pos>end)
314                                                 pos=end;
315                                         if(pos<begin)
316                                                 pos=begin;
317
318                                         iter->pos=pos;
319                                 }
320                                 else
321                                 {
322                                         iter->pos=pos;
323                                         gradient_.sort();
324                                 }
325
326 //                              signal_value_changed_();
327                                 changed_=true;
328                                 queue_draw();
329                                 return true;
330                         }
331                         break;
332                 case GDK_BUTTON_PRESS:
333                         changed_=false;
334                         if(event->button.button==1)
335                         {
336                                 if(editable_ && y>get_height()-CONTROL_HEIGHT)
337                                 {
338                                         set_selected_cpoint(*gradient_.proximity(pos));
339                                         queue_draw();
340                                         return true;
341                                 }
342                                 else
343                                 {
344                                         signal_clicked_();
345                                         return true;
346                                 }
347                         }
348                         else if(editable_ && event->button.button==3)
349                         {
350                                 popup_menu(pos);
351                                 return true;
352                         }
353                         break;
354                 case GDK_BUTTON_RELEASE:
355                         if(editable_ && event->button.button==1 && y>get_height()-CONTROL_HEIGHT)
356                         {
357                                 set_selected_cpoint(*gradient_.proximity(pos));
358                                 if(changed_)signal_value_changed_();
359                                 return true;
360                         }
361                 default:
362                         break;
363                 }
364         }
365
366         return false;
367 }