Remove ancient trunk folder from svn repository
[synfig.git] / synfig-studio / 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 #define WIDGET_KEYFRAME_LIST_DEFAULT_FPS 24.0
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         adj_default(0,0,2,1/WIDGET_KEYFRAME_LIST_DEFAULT_FPS,10/WIDGET_KEYFRAME_LIST_DEFAULT_FPS),
62         kf_list_(&default_kf_list_),
63         time_ratio("4f", WIDGET_KEYFRAME_LIST_DEFAULT_FPS)
64 {
65         adj_timescale=0;
66         editable_=true;
67         fps=WIDGET_KEYFRAME_LIST_DEFAULT_FPS;
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 /*| Gdk::BUTTON3_MOTION_MASK*/);
75         add_events(Gdk::POINTER_MOTION_MASK);
76         set_time_adjustment(&adj_default);
77         queue_draw();
78 }
79
80 Widget_Keyframe_List::~Widget_Keyframe_List()
81 {
82 }
83
84 bool
85 Widget_Keyframe_List::redraw(GdkEventExpose */*bleh*/)
86 {
87
88         const int h(get_height());
89         const int w(get_width());
90
91         //!Boundaries of the drawing area in time units.
92         synfig::Time top(adj_timescale->get_upper());
93         synfig::Time bottom(adj_timescale->get_lower());
94
95         //! The graphic context
96         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
97         //! A rectangle that defines the drawing area.
98         Gdk::Rectangle area(0,0,w,h);
99
100         //! draw a background
101         gc->set_rgb_fg_color(Gdk::Color("#9d9d9d"));
102         get_window()->draw_rectangle(gc, true, 0, 0, w, h);
103
104         if(!editable_)
105         {
106                 return true; //needs fixing!
107         }
108         //!Returns if there are not keyframes to draw.
109         if (kf_list_->empty()) return false;
110
111         //!Loop all the keyframes
112         synfig::KeyframeList::iterator iter,selected_iter;
113         bool show_selected(false);
114         for(iter=kf_list_->begin();iter!=kf_list_->end();iter++)
115         {
116                 //!do not draw keyframes out of the widget boundaries
117                 if (iter->get_time()>top || iter->get_time()<bottom)
118                         continue;
119                 //! If the keyframe is not the selected one
120                 if(*iter!=selected_kf)
121                 {
122                         const int x((int)((float)(iter->get_time()-bottom) * (w/(top-bottom)) ) );
123                         get_style()->paint_arrow(get_window(), Gtk::STATE_NORMAL,
124                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
125                         x-h/2+1, 0, h, h );
126                 }
127                 else
128                 {
129                         selected_iter=iter;
130                         show_selected=true;
131                 }
132         }
133
134         // we do this so that we can be sure that
135         // the selected keyframe is shown on top
136         if(show_selected)
137         {
138                 // If not dragging just show the selected keyframe
139                 if (!dragging_)
140                 {
141                         int x((int)((float)(selected_iter->get_time()-bottom) * (w/(top-bottom)) ) );
142                         get_style()->paint_arrow(get_window(), Gtk::STATE_SELECTED,
143                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
144                         x-h/2+1, 0, h, h );
145                 }
146                 // If dragging then show the selected as insensitive and the
147                 // dragged as selected
148                 else
149                 {
150                         int x((int)((float)(selected_iter->get_time()-bottom) * (w/(top-bottom)) ) );
151                         get_style()->paint_arrow(get_window(), Gtk::STATE_INSENSITIVE,
152                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
153                         x-h/2, 0, h, h );
154                         x=(int)((float)(dragging_kf_time-bottom) * (w/(top-bottom)) ) ;
155                         get_style()->paint_arrow(get_window(), Gtk::STATE_SELECTED,
156                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
157                         x-h/2+1, 0, h, h );
158                 }
159         }
160         return true;
161 }
162
163
164 void
165 Widget_Keyframe_List::set_kf_list(synfig::KeyframeList* x)
166 {
167         kf_list_=x;
168         set_selected_keyframe(selected_none);
169         selected_=false;
170         dragging_=false;
171 }
172
173 void
174 Widget_Keyframe_List::set_selected_keyframe(const synfig::Keyframe &x)
175 {
176         selected_kf=x;
177         selected_=true;
178         dragging_kf_time=selected_kf.get_time();
179         //signal_keyframe_selected_(selected_kf);
180         dragging_=false;
181         queue_draw();
182 }
183
184 bool
185 Widget_Keyframe_List::perform_move_kf(bool delta=false)
186 {
187         if(!selected_)
188                 return false;
189         if(dragging_kf_time == selected_kf.get_time())
190                 return false; // change this checking if not sticked to integer frames
191         Time selected_kf_time(selected_kf.get_time());
192         Time prev, next;
193         kf_list_->find_prev_next(selected_kf_time, prev, next);
194         // Not possible to set delta to the first keyframe
195         // perform normal movement
196         // As suggested by Zelgadis it is better to not perform anything.
197         if (prev==Time::begin() && delta==true)
198         {
199                 synfig::info(_("Not possible to ALT-drag the first keyframe"));
200                 return false;
201         }
202         if(!delta)
203                 {
204                         synfigapp::Action::Handle action(synfigapp::Action::create("KeyframeSet"));
205                         if(!action)
206                         return false;
207                         selected_kf.set_time(dragging_kf_time);
208                         action->set_param("canvas",canvas_interface_->get_canvas());
209                         action->set_param("canvas_interface",canvas_interface_);
210                         action->set_param("keyframe",selected_kf);
211                         try
212                         {
213                                 canvas_interface_->get_instance()->perform_action(action);
214                         }
215                         catch(...)
216                         {
217                                 return false;
218                         }
219                 }
220         else
221                 {
222                         Keyframe prev_kf(*kf_list_->find_prev(selected_kf_time));
223                         Time prev_kf_time(prev_kf.get_time());
224                         if (prev_kf_time >= dragging_kf_time) //Not allowed
225                         {
226                                 synfig::warning(_("Delta set not allowed"));
227                                 synfig::info(_("Widget_Keyframe_List::perform_move_kf(%i)::prev_kf_time=%s"), delta, prev_kf_time.get_string().c_str());
228                                 synfig::info(_("Widget_Keyframe_List::perform_move_kf(%i)::dragging_kf_time=%s"), delta, dragging_kf_time.get_string().c_str());
229                                 return false;
230                         }
231                         else
232                         {
233                                 Time old_delta_time(selected_kf_time-prev_kf_time);
234                                 Time new_delta_time(dragging_kf_time-prev_kf_time);
235                                 Time change_delta(new_delta_time-old_delta_time);
236                                 synfigapp::Action::Handle action(synfigapp::Action::create("KeyframeSetDelta"));
237                                 if(!action)
238                                         return false;
239                                 action->set_param("canvas",canvas_interface_->get_canvas());
240                                 action->set_param("canvas_interface",canvas_interface_);
241                                 action->set_param("keyframe",prev_kf);
242                                 action->set_param("delta",change_delta);
243                                 canvas_interface_->get_instance()->perform_action(action);
244                         }
245                 }
246         queue_draw();
247         return true;
248 }
249
250 bool
251 Widget_Keyframe_List::on_event(GdkEvent *event)
252 {
253         const int x(static_cast<int>(event->button.x));
254         //const int y(static_cast<int>(event->button.y));
255         //!Boundaries of the drawing area in time units.
256         synfig::Time top(adj_timescale->get_upper());
257         synfig::Time bottom(adj_timescale->get_lower());
258         //!pos is the [0,1] relative horizontal place on the widget
259         float pos((float)x/(get_width()));
260         if(pos<0.0f)pos=0.0f;
261         if(pos>1.0f)pos=1.0f;
262         //! The time where the event x is
263         synfig::Time t((float)(bottom+pos*(top-bottom)));
264         //Do not respond mouse events if the list is empty
265         if(!kf_list_->size())
266                 return true;
267
268         //! here the guts of the event
269         switch(event->type)
270         {
271         case GDK_MOTION_NOTIFY:
272                 if(editable_)
273                 {
274                         // here is captured mouse motion
275                         // AND left or right mouse button pressed
276                         if (event->motion.state & (GDK_BUTTON1_MASK /*| GDK_BUTTON3_MASK*/))
277                         {
278                                 // stick to integer frames. It can be optional in the future
279                                 if(fps) t = floor(t*fps + 0.5)/fps;
280                                 dragging_kf_time=t;
281                                 dragging_=true;
282                                 queue_draw();
283                                 return true;
284                         }
285                         // here is captured mouse motion
286                         // AND NOT left or right mouse button pressed
287                         else
288                         {
289                                 Glib::ustring ttip="";
290                                 synfig::Time p_t,n_t;
291                                 kf_list_->find_prev_next(t, p_t, n_t);
292                                 if( (p_t==Time::begin()         &&      n_t==Time::end())
293                                 ||
294                                 ((t-p_t)>time_ratio     && (n_t-t)>time_ratio)
295                                 )
296                                 {
297                                         ttip = _("Click and drag keyframes");
298                                 }
299                                 else if ((t-p_t)<(n_t-t))
300                                 {
301                                         synfig::Keyframe kf(*kf_list_->find_prev(t));
302                                         synfig::String kf_name(kf.get_description().c_str());
303                                         ttip = kf_name.c_str();
304                                 }
305                                 else
306                                 {
307                                         synfig::Keyframe kf(*kf_list_->find_next(t));
308                                         synfig::String kf_name(kf.get_description().c_str());
309                                         ttip = kf_name.c_str();
310                                 }
311                                 tooltips.set_tip(*this, ttip);
312                                 dragging_=false;
313                                 queue_draw();
314                                 return true;
315                         }
316                 }
317                 break;
318         case GDK_BUTTON_PRESS:
319                 changed_=false;
320                 dragging_=false;
321                 if(event->button.button==1 /*|| event->button.button==3*/)
322                 {
323                         if(editable_)
324                         {
325                                 synfig::Time prev_t,next_t;
326                                 kf_list_->find_prev_next(t, prev_t, next_t);
327                                 if( (prev_t==Time::begin()      &&      next_t==Time::end())
328                                 ||
329                                 ((t-prev_t)>time_ratio  && (next_t-t)>time_ratio)
330                                 )
331                                 {
332                                         set_selected_keyframe(selected_none);
333                                         selected_=false;
334                                         queue_draw();
335                                 }
336                                 else if ((t-prev_t)<(next_t-t))
337                                 {
338                                         set_selected_keyframe(*(kf_list_->find_prev(t)));
339                                         queue_draw();
340                                         selected_=true;
341                                 }
342                                 else
343                                 {
344                                         set_selected_keyframe(*(kf_list_->find_next(t)));
345                                         queue_draw();
346                                         selected_=true;
347                                 }
348                                 return true;
349                         }
350                         else
351                         {
352                                 return false;
353                         }
354                 }
355
356                 break;
357         case GDK_BUTTON_RELEASE:
358                 if(editable_ && (event->button.button==1 /*|| event->button.button==3*/))
359                 {
360                         // stick to integer frames.
361                         if(fps) t = floor(t*fps + 0.5)/fps;
362                         bool stat=false;
363                         if(dragging_)
364                                 {
365                                         //if (event->button.button==3)
366                                         if(event->button.state & GDK_MOD1_MASK)
367                                         {
368                                                 stat=perform_move_kf(true);
369                                         }
370                                         else
371                                         {
372                                                 stat=perform_move_kf(false);
373                                         }
374                                 }
375                         dragging_=false;
376                         return stat;
377                 }
378                 break;
379         default:
380                 break;
381         }
382         return false;
383 }
384
385
386 void Widget_Keyframe_List::set_time_adjustment(Gtk::Adjustment *x)
387 {
388         //disconnect old connections
389         time_value_change.disconnect();
390         time_other_change.disconnect();
391
392         //connect update function to new adjustment
393         adj_timescale = x;
394
395         if(x)
396         {
397                 time_value_change = x->signal_value_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
398                 time_other_change = x->signal_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
399         }
400 }
401
402 void
403 Widget_Keyframe_List::set_fps(float d)
404 {
405         if(fps != d)
406         {
407                 fps = d;
408                 //update everything since we need to redraw already
409                 queue_draw();
410         }
411 }
412
413 void
414 Widget_Keyframe_List::set_canvas_interface(etl::loose_handle<synfigapp::CanvasInterface> h)
415 {
416         canvas_interface_=h;
417         // Store the values used fomr the canvas interface.
418         if (canvas_interface_)
419         {
420                 set_fps(canvas_interface_->get_canvas()->rend_desc().get_frame_rate());
421                 set_kf_list(&canvas_interface_->get_canvas()->keyframe_list());
422         }
423 }
424
425