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