Added code to fix the shown of the keyfrmae list widget. It is not fully working...
[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 {
66         set_size_request(-1,64);
67         //!This signal is called when the widget need to be redrawn
68         signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Keyframe_List::redraw));
69         //! The widget respond to mouse button press and release and to
70         //! left button motion
71         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
72         add_events(Gdk::BUTTON1_MOTION_MASK);
73         set_time_adjustment(&adj_default);
74
75 }
76
77 Widget_Keyframe_List::~Widget_Keyframe_List()
78 {
79 }
80
81 bool
82 Widget_Keyframe_List::redraw(GdkEventExpose */*bleh*/)
83 {
84         if (kf_list_->empty()) return false;
85         const int h(get_height());
86         const int w(get_width());
87
88         //!Boundaries of the drawing area in time units.
89         synfig::Time top(adj_timescale->get_upper());
90         synfig::Time bottom(adj_timescale->get_lower());
91
92         //! The graphic context
93         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
94         //! A rectangle that defines the drawing area.
95         Gdk::Rectangle area(0,0,w,h);
96
97         if(!editable_)
98         {
99                 return true;
100         }
101
102         //! draw a background
103         gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
104         get_window()->draw_rectangle(gc, true, 0, 0, w, h);
105
106         //!Loop all the keyframes
107         synfig::KeyframeList::iterator iter,selected_iter;
108         bool show_selected(false);
109         for(iter=kf_list_->begin();iter!=kf_list_->end();iter++)
110         {
111                 //!do not draw keyframes out of the widget boundaries
112                 if (iter->get_time()>top || iter->get_time()<bottom)
113                         continue;
114                 //! If the keyframe is not the selected one
115                 if(*iter!=selected_kf)
116                 {
117                         const int x((int)((float)(iter->get_time()-bottom) * (w/(top-bottom)) ) );
118                         get_style()->paint_arrow(get_window(), Gtk::STATE_NORMAL,
119                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
120                         x-h/2, 0, h, h );
121                 }
122                 else
123                 {
124                         selected_iter=iter;
125                         show_selected=true;
126                 }
127         }
128
129         // we do this so that we can be sure that
130         // the selected keyframe is shown on top
131         if(show_selected)
132         {
133                         const int x((int)((float)(selected_iter->get_time()-bottom) * (w/(top-bottom)) ) );
134                         get_style()->paint_arrow(get_window(), Gtk::STATE_SELECTED,
135                         Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
136                         x-h/2, 0, h, h );
137         }
138
139         return true;
140 }
141
142
143 void
144 Widget_Keyframe_List::set_kf_list(synfig::KeyframeList* x)
145 {
146         kf_list_=x;
147         if(kf_list_->size())
148                 set_selected_keyframe(*kf_list_->find_next(synfig::Time::zero()));
149 }
150
151 void
152 Widget_Keyframe_List::set_selected_keyframe(const synfig::Keyframe &x)
153 {
154         selected_kf=x;
155         //signal_keyframe_selected_(selected_kf);
156         queue_draw();
157 }
158
159 bool
160 Widget_Keyframe_List::perform_move_kf()
161 {
162         return false;
163 }
164
165 bool
166 Widget_Keyframe_List::on_event(GdkEvent *event)
167 {
168         const int x(static_cast<int>(event->button.x));
169         const int y(static_cast<int>(event->button.y));
170                 //!Boundaries of the drawing area in time units.
171         synfig::Time top(adj_timescale->get_upper());
172         synfig::Time bottom(adj_timescale->get_lower());
173                 //!pos is the [0,1] relative horizontal place on the widget
174         float pos((float)x/(float)get_width());
175         if(pos<0.0f)pos=0.0f;
176         if(pos>1.0f)pos=1.0f;
177                 //! The time where the event x is
178         synfig::Time t((float)(pos*(top-bottom)));
179
180         //Do not respond mouse events if the list is empty
181         if(!kf_list_->size())
182         {
183                 synfig::info("Keyframe list empty");
184                 return true;
185         }
186
187         //! here the guts of the event
188         switch(event->type)
189         {
190         case GDK_MOTION_NOTIFY:
191                 if(editable_)
192                 {
193
194                         // stick to integer frames.
195                         if(fps)
196                                 {
197                                         t = floor(t*fps + 0.5)/fps;
198                                 }
199                         dragging_kf_time=t;
200                         queue_draw();
201                         return true;
202                 }
203                 break;
204         case GDK_BUTTON_PRESS:
205                 changed_=false;
206                 if(event->button.button==1)
207                 {
208                         synfig::info("Looking keyframe at  %s", t.get_string().c_str());
209                         synfig::info("Total amount of keyframes %i", kf_list_->size());
210                         if(editable_)
211                         {
212                                 synfig::KeyframeList::iterator selected;
213                                 selected = kf_list_->find_next(t);
214                                 set_selected_keyframe(*selected);
215                                 queue_draw();
216                                 return true;
217                         }
218                         else
219                         {
220                                 return true;
221                         }
222                 }
223                 break;
224         case GDK_BUTTON_RELEASE:
225                 if(editable_ && event->button.button==1)
226                 {
227                 // stick to integer frames.
228                 if(fps)
229                         {
230                                 t = floor(t*fps + 0.5)/fps;
231                         }
232                 bool stat=perform_move_kf();
233                 synfig::info("Dropping keyframe at: %s", t.get_string().c_str());
234                 return stat;
235                 }
236         default:
237                 break;
238         }
239
240
241         return false;
242 }
243
244
245 void Widget_Keyframe_List::set_time_adjustment(Gtk::Adjustment *x)
246 {
247         //disconnect old connections
248         time_value_change.disconnect();
249         time_other_change.disconnect();
250
251         //connect update function to new adjustment
252         adj_timescale = x;
253
254         if(x)
255         {
256                 time_value_change = x->signal_value_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
257                 time_other_change = x->signal_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
258         }
259 }
260
261 void
262 Widget_Keyframe_List::set_fps(float d)
263 {
264         if(fps != d)
265         {
266                 fps = d;
267                 //update everything since we need to redraw already
268                 queue_draw();
269         }
270 }