9ab98bacac6728bbff7800abd5e8252d474a2a90
[synfig.git] / synfig-studio / trunk / src / gtkmm / widget_keyframe_list.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file widget_keyframe_list.cpp
3 **      \brief A custom widget to manage keyframes in the timeline.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **      Copyright (c) 2009 Carlos López
11 **
12 **      This package is free software; you can redistribute it and/or
13 **      modify it under the terms of the GNU General Public License as
14 **      published by the Free Software Foundation; either version 2 of
15 **      the License, or (at your option) any later version.
16 **
17 **      This package is distributed in the hope that it will be useful,
18 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **      General Public License for more details.
21 **      \endlegal
22 */
23 /* ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "widget_keyframe_list.h"
35 #include "app.h"
36 #include <gtkmm/menu.h>
37 #include <synfig/exception.h>
38 #include <ETL/misc>
39
40 #include "general.h"
41
42 #endif
43
44 /* === U S I N G =========================================================== */
45
46 using namespace std;
47 using namespace etl;
48 using namespace synfig;
49 using namespace studio;
50
51
52 /* === M A C R O S ========================================================= */
53
54 /* === G L O B A L S ======================================================= */
55
56 /* === P R O C E D U R E S ================================================= */
57
58 /* === M E T H O D S ======================================================= */
59
60 Widget_Keyframe_List::Widget_Keyframe_List():
61         editable_(true),
62         adj_default(0,0,2,1/24,10/24),
63         adj_timescale(0),
64         fps(24),
65         kf_list_(&default_kf_list_),
66         time_ratio("4f", fps)
67 {
68         set_size_request(-1,64);
69         //!This signal is called when the widget need to be redrawn
70         signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Keyframe_List::redraw));
71         //! The widget respond to mouse button press and release and to
72         //! left button motion
73         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
74         add_events(Gdk::BUTTON1_MOTION_MASK);
75         set_time_adjustment(&adj_default);
76
77 }
78
79 Widget_Keyframe_List::~Widget_Keyframe_List()
80 {
81 }
82
83 bool
84 Widget_Keyframe_List::redraw(GdkEventExpose */*bleh*/)
85 {
86         if (kf_list_->empty()) return false;
87         const int h(get_height());
88         const int w(get_width());
89
90         //!Boundaries of the drawing area in time units.
91         synfig::Time top(adj_timescale->get_upper());
92         synfig::Time bottom(adj_timescale->get_lower());
93
94         //! The graphic context
95         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
96         //! A rectangle that defines the drawing area.
97         Gdk::Rectangle area(0,0,w,h);
98
99         if(!editable_)
100         {
101                 return true;
102         }
103
104         //! draw a background
105         gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
106         get_window()->draw_rectangle(gc, true, 0, 0, w, h);
107
108         //!Loop all the keyframes
109         synfig::KeyframeList::iterator iter,selected_iter;
110         bool show_selected(false);
111         for(iter=kf_list_->begin();iter!=kf_list_->end();iter++)
112         {
113                 //!do not draw keyframes out of the widget boundaries
114                 if (iter->get_time()>top || iter->get_time()<bottom)
115                         continue;
116                 //! If the keyframe is not the selected one
117                 if(*iter!=selected_kf)
118                 {
119                         const int x((int)((float)(iter->get_time()-bottom) * (w/(top-bottom)) ) );
120                         get_style()->paint_arrow(get_window(), Gtk::STATE_NORMAL,
121                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
122                         x-h/2+1, 0, h, h );
123                 }
124                 else
125                 {
126                         selected_iter=iter;
127                         show_selected=true;
128                 }
129         }
130
131         // we do this so that we can be sure that
132         // the selected keyframe is shown on top
133         if(show_selected)
134         {
135                 // If not dragging just show the selected keyframe
136                 if (!dragging_)
137                 {
138                         int x((int)((float)(selected_iter->get_time()-bottom) * (w/(top-bottom)) ) );
139                         get_style()->paint_arrow(get_window(), Gtk::STATE_SELECTED,
140                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
141                         x-h/2+1, 0, h, h );
142                 }
143                 // If dragging then show the selected as insensitive and the
144                 // dragged as selected
145                 else
146                 {
147                         int x((int)((float)(selected_iter->get_time()-bottom) * (w/(top-bottom)) ) );
148                         get_style()->paint_arrow(get_window(), Gtk::STATE_INSENSITIVE,
149                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
150                         x-h/2, 0, h, h );
151                         x=(int)((float)(dragging_kf_time-bottom) * (w/(top-bottom)) ) ;
152                         get_style()->paint_arrow(get_window(), Gtk::STATE_SELECTED,
153                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
154                         x-h/2+1, 0, h, h );
155                 }
156
157
158
159         }
160
161         return true;
162 }
163
164
165 void
166 Widget_Keyframe_List::set_kf_list(synfig::KeyframeList* x)
167 {
168         kf_list_=x;
169         set_selected_keyframe(selected_none);
170         selected_=false;
171         dragging_=false;
172 }
173
174 void
175 Widget_Keyframe_List::set_selected_keyframe(const synfig::Keyframe &x)
176 {
177         selected_kf=x;
178         selected_=true;
179         dragging_kf_time=selected_kf.get_time();
180         //signal_keyframe_selected_(selected_kf);
181         dragging_=false;
182         queue_draw();
183 }
184
185 bool
186 Widget_Keyframe_List::perform_move_kf()
187 {
188         if(!selected_)
189                 return false;
190         if(dragging_kf_time == selected_kf.get_time())
191                 return false;
192         synfigapp::Action::Handle action(synfigapp::Action::create("KeyframeSet"));
193         if(!action)
194                 return false;
195         selected_kf.set_time(dragging_kf_time);
196         action->set_param("canvas",canvas_interface_->get_canvas());
197         action->set_param("canvas_interface",canvas_interface_);
198         action->set_param("keyframe",selected_kf);
199         //synfig::info("DELTA: %s", (dragging_kf_time-selected_kf.get_time()).get_string().c_str());
200         try
201         {
202                 canvas_interface_->get_instance()->perform_action(action);
203         }
204         catch(...)
205         {
206                         return false;
207         }
208         queue_draw();
209         return true;
210 }
211
212 bool
213 Widget_Keyframe_List::on_event(GdkEvent *event)
214 {
215         const int x(static_cast<int>(event->button.x));
216         const int y(static_cast<int>(event->button.y));
217         //!Boundaries of the drawing area in time units.
218         synfig::Time top(adj_timescale->get_upper());
219         synfig::Time bottom(adj_timescale->get_lower());
220         //!pos is the [0,1] relative horizontal place on the widget
221         float pos((float)x/(get_width()));
222         if(pos<0.0f)pos=0.0f;
223         if(pos>1.0f)pos=1.0f;
224         //! The time where the event x is
225         synfig::Time t((float)(bottom+pos*(top-bottom)));
226
227         //Do not respond mouse events if the list is empty
228         if(!kf_list_->size())
229         {
230                 synfig::info("Keyframe list empty");
231                 return true;
232         }
233
234         //! here the guts of the event
235         switch(event->type)
236         {
237         case GDK_MOTION_NOTIFY:
238                 if(editable_)
239                 {
240                         // stick to integer frames.
241                         if(fps)
242                                 {
243                                         t = floor(t*fps + 0.5)/fps;
244                                 }
245                         dragging_kf_time=t;
246                         dragging_=true;
247                         queue_draw();
248                         return true;
249                 }
250                 break;
251         case GDK_BUTTON_PRESS:
252                 changed_=false;
253                 dragging_=false;
254                 if(event->button.button==1)
255                 {
256                         synfig::info("Looking keyframe at  %s", t.get_string().c_str());
257                         synfig::info("Total amount of keyframes %i", kf_list_->size());
258                         synfig::info("Time ratio %s",time_ratio.get_string().c_str());
259                         synfig::info("Bottom %s",bottom.get_string().c_str());
260                         synfig::info("Top %s",top.get_string().c_str());
261                         if(editable_)
262                         {
263                                 synfig::Time prev_t,next_t;
264                                 kf_list_->find_prev_next(t, prev_t, next_t);
265                                 if( (prev_t==Time::begin()      &&      next_t==Time::end())
266                                         ||
267                                         ((t-prev_t)>time_ratio  && (next_t-t)>time_ratio)
268                                         )
269                                 {
270                                         set_selected_keyframe(selected_none);
271                                         selected_=false;
272                                         synfig::info("Selected keyframe set to none");
273                                         synfig::info("Distance to prev %s", (t-prev_t).get_string().c_str());
274                                         synfig::info("Distance to next %s", (next_t-t).get_string().c_str());
275                                         queue_draw();
276                                         return true;
277                                 }
278                                 else if ((t-prev_t)<(next_t-t))
279                                 {
280                                         set_selected_keyframe(*(kf_list_->find_prev(t)));
281                                         synfig::info("Selected keyframe set to previous");
282                                         queue_draw();
283                                         selected_=true;
284                                         return true;
285                                 }
286                                 else
287                                 {
288                                         set_selected_keyframe(*(kf_list_->find_next(t)));
289                                         synfig::info("Selected keyframe set to next");
290                                         queue_draw();
291                                         selected_=true;
292                                         return true;
293                                 }
294
295                                 return false;
296                         }
297                         else
298                         {
299                                 return false;
300                         }
301                 }
302                 break;
303         case GDK_BUTTON_RELEASE:
304                 if(editable_ && event->button.button==1)
305                 {
306                 // stick to integer frames.
307                 if(fps)
308                         {
309                                 t = floor(t*fps + 0.5)/fps;
310                         }
311                 bool stat=false;
312                 if(dragging_)
313                         stat=perform_move_kf();
314                 dragging_=false;
315                 synfig::info("Dropping keyframe time at: %s", t.get_string().c_str());
316                 synfig::info("perform move result: %i", stat);
317                 return stat;
318                 }
319         default:
320                 break;
321         }
322
323
324         return false;
325 }
326
327
328 void Widget_Keyframe_List::set_time_adjustment(Gtk::Adjustment *x)
329 {
330         //disconnect old connections
331         time_value_change.disconnect();
332         time_other_change.disconnect();
333
334         //connect update function to new adjustment
335         adj_timescale = x;
336
337         if(x)
338         {
339                 time_value_change = x->signal_value_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
340                 time_other_change = x->signal_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
341         }
342 }
343
344 void
345 Widget_Keyframe_List::set_fps(float d)
346 {
347         if(fps != d)
348         {
349                 fps = d;
350                 //update everything since we need to redraw already
351                 queue_draw();
352         }
353 }
354
355 void
356 Widget_Keyframe_List::set_canvas_interface(etl::loose_handle<synfigapp::CanvasInterface> h)
357 {
358         canvas_interface_=h;
359         if (canvas_interface_)
360         {
361                 set_fps(canvas_interface_->get_canvas()->rend_desc().get_frame_rate());
362                 set_kf_list(&canvas_interface_->get_canvas()->keyframe_list());
363         }
364 }
365
366