dba098da24e4ef57ee93bfdba8db34e23b18fdcd
[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 /* === M A C R O S ========================================================= */
52
53 /* === G L O B A L S ======================================================= */
54
55 /* === P R O C E D U R E S ================================================= */
56
57 /* === M E T H O D S ======================================================= */
58
59 Widget_Keyframe_List::Widget_Keyframe_List():
60         editable_(true),
61         adj_default(0,0,2,1/24,10/24),
62         adj_timescale(0),
63         fps(24),
64         kf_list_(&default_kf_list_),
65         time_ratio("4f", fps)
66 {
67         set_size_request(-1,64);
68         //!This signal is called when the widget need to be redrawn
69         signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Keyframe_List::redraw));
70         //! The widget respond to mouse button press and release and to
71         //! left button motion
72         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
73         add_events(Gdk::BUTTON1_MOTION_MASK);
74         set_time_adjustment(&adj_default);
75
76 }
77
78 Widget_Keyframe_List::~Widget_Keyframe_List()
79 {
80 }
81
82 bool
83 Widget_Keyframe_List::redraw(GdkEventExpose */*bleh*/)
84 {
85         if (kf_list_->empty()) return false;
86         const int h(get_height());
87         const int w(get_width());
88
89         //!Boundaries of the drawing area in time units.
90         synfig::Time top(adj_timescale->get_upper());
91         synfig::Time bottom(adj_timescale->get_lower());
92
93         //! The graphic context
94         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
95         //! A rectangle that defines the drawing area.
96         Gdk::Rectangle area(0,0,w,h);
97
98         if(!editable_)
99         {
100                 return true;
101         }
102
103         //! draw a background
104         gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
105         get_window()->draw_rectangle(gc, true, 0, 0, w, h);
106
107         //!Loop all the keyframes
108         synfig::KeyframeList::iterator iter,selected_iter;
109         bool show_selected(false);
110         for(iter=kf_list_->begin();iter!=kf_list_->end();iter++)
111         {
112                 //!do not draw keyframes out of the widget boundaries
113                 if (iter->get_time()>top || iter->get_time()<bottom)
114                         continue;
115                 //! If the keyframe is not the selected one
116                 if(*iter!=selected_kf)
117                 {
118                         const int x((int)((float)(iter->get_time()-bottom) * (w/(top-bottom)) ) );
119                         get_style()->paint_arrow(get_window(), Gtk::STATE_NORMAL,
120                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
121                         x-h/2+1, 0, h, h );
122                 }
123                 else
124                 {
125                         selected_iter=iter;
126                         show_selected=true;
127                 }
128         }
129
130         // we do this so that we can be sure that
131         // the selected keyframe is shown on top
132         if(show_selected)
133         {
134                 // If not dragging just show the selected keyframe
135                 if (!dragging_)
136                 {
137                         int x((int)((float)(selected_iter->get_time()-bottom) * (w/(top-bottom)) ) );
138                         get_style()->paint_arrow(get_window(), Gtk::STATE_SELECTED,
139                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
140                         x-h/2+1, 0, h, h );
141                 }
142                 // If dragging then show the selected as insensitive and the
143                 // dragged as selected
144                 else
145                 {
146                         int x((int)((float)(selected_iter->get_time()-bottom) * (w/(top-bottom)) ) );
147                         get_style()->paint_arrow(get_window(), Gtk::STATE_INSENSITIVE,
148                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
149                         x-h/2, 0, h, h );
150                         x=(int)((float)(dragging_kf_time-bottom) * (w/(top-bottom)) ) ;
151                         get_style()->paint_arrow(get_window(), Gtk::STATE_SELECTED,
152                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
153                         x-h/2+1, 0, h, h );
154                 }
155
156
157
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         if(kf_list_->size())
169                 set_selected_keyframe(selected_none);
170 }
171
172 void
173 Widget_Keyframe_List::set_selected_keyframe(const synfig::Keyframe &x)
174 {
175         selected_kf=x;
176         dragging_kf_time=selected_kf.get_time();
177         //signal_keyframe_selected_(selected_kf);
178         queue_draw();
179 }
180
181 bool
182 Widget_Keyframe_List::perform_move_kf()
183 {
184         return false;
185 }
186
187 bool
188 Widget_Keyframe_List::on_event(GdkEvent *event)
189 {
190         const int x(static_cast<int>(event->button.x));
191         const int y(static_cast<int>(event->button.y));
192         //!Boundaries of the drawing area in time units.
193         synfig::Time top(adj_timescale->get_upper());
194         synfig::Time bottom(adj_timescale->get_lower());
195         //!pos is the [0,1] relative horizontal place on the widget
196         float pos((float)x/(get_width()));
197         if(pos<0.0f)pos=0.0f;
198         if(pos>1.0f)pos=1.0f;
199         //! The time where the event x is
200         synfig::Time t((float)(bottom+pos*(top-bottom)));
201
202         //Do not respond mouse events if the list is empty
203         if(!kf_list_->size())
204         {
205                 synfig::info("Keyframe list empty");
206                 return true;
207         }
208
209         //! here the guts of the event
210         switch(event->type)
211         {
212         case GDK_MOTION_NOTIFY:
213                 if(editable_)
214                 {
215                         // stick to integer frames.
216                         if(fps)
217                                 {
218                                         t = floor(t*fps + 0.5)/fps;
219                                 }
220                         dragging_kf_time=t;
221                         dragging_=true;
222                         queue_draw();
223                         return true;
224                 }
225                 break;
226         case GDK_BUTTON_PRESS:
227                 changed_=false;
228                 dragging_=false;
229                 if(event->button.button==1)
230                 {
231                         synfig::info("Looking keyframe at  %s", t.get_string().c_str());
232                         synfig::info("Total amount of keyframes %i", kf_list_->size());
233                         synfig::info("Time ratio %s",time_ratio.get_string().c_str());
234                         synfig::info("Bottom %s",bottom.get_string().c_str());
235                         synfig::info("Top %s",top.get_string().c_str());
236                         if(editable_)
237                         {
238                                 synfig::Time prev_t,next_t;
239                                 kf_list_->find_prev_next(t, prev_t, next_t);
240                                 if( (prev_t==Time::begin()      &&      next_t==Time::end())
241                                         ||
242                                         ((t-prev_t)>time_ratio  && (next_t-t)>time_ratio)
243                                         )
244                                 {
245                                         set_selected_keyframe(selected_none);
246                                         synfig::info("Selected keyframe set to none");
247                                         synfig::info("Distance to prev %s", (t-prev_t).get_string().c_str());
248                                         synfig::info("Distance to next %s", (next_t-t).get_string().c_str());
249                                         queue_draw();
250                                         return true;
251                                 }
252                                 else if ((t-prev_t)<(next_t-t))
253                                 {
254                                         set_selected_keyframe(*(kf_list_->find_prev(t)));
255                                         synfig::info("Selected keyframe set to previous");
256                                         queue_draw();
257                                         return true;
258                                 }
259                                 else
260                                 {
261                                         set_selected_keyframe(*(kf_list_->find_next(t)));
262                                         synfig::info("Selected keyframe set to next");
263                                         queue_draw();
264                                         return true;
265                                 }
266
267                                 return false;
268                         }
269                         else
270                         {
271                                 return false;
272                         }
273                 }
274                 break;
275         case GDK_BUTTON_RELEASE:
276                 if(editable_ && event->button.button==1)
277                 {
278                 // stick to integer frames.
279                 if(fps)
280                         {
281                                 t = floor(t*fps + 0.5)/fps;
282                         }
283                 bool stat=perform_move_kf();
284                 dragging_=false;
285                 synfig::info("Dropping keyframe time at: %s", t.get_string().c_str());
286                 return stat;
287                 }
288         default:
289                 break;
290         }
291
292
293         return false;
294 }
295
296
297 void Widget_Keyframe_List::set_time_adjustment(Gtk::Adjustment *x)
298 {
299         //disconnect old connections
300         time_value_change.disconnect();
301         time_other_change.disconnect();
302
303         //connect update function to new adjustment
304         adj_timescale = x;
305
306         if(x)
307         {
308                 time_value_change = x->signal_value_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
309                 time_other_change = x->signal_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
310         }
311 }
312
313 void
314 Widget_Keyframe_List::set_fps(float d)
315 {
316         if(fps != d)
317         {
318                 fps = d;
319                 //update everything since we need to redraw already
320                 queue_draw();
321         }
322 }