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