1 /* === S Y N F I G ========================================================= */
2 /*! \file widget_keyframe_list.cpp
3 ** \brief A custom widget to manage keyframes in the timeline.
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
10 ** Copyright (c) 2009 Carlos López
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.
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.
23 /* ========================================================================= */
25 /* === H E A D E R S ======================================================= */
34 #include "widget_keyframe_list.h"
36 #include <gtkmm/menu.h>
37 #include <synfig/exception.h>
44 /* === U S I N G =========================================================== */
48 using namespace synfig;
49 using namespace studio;
52 /* === M A C R O S ========================================================= */
54 /* === G L O B A L S ======================================================= */
56 /* === P R O C E D U R E S ================================================= */
58 /* === M E T H O D S ======================================================= */
60 Widget_Keyframe_List::Widget_Keyframe_List():
62 adj_default(0,0,2,1/24,10/24),
65 kf_list_(&default_kf_list_),
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 add_events(Gdk::POINTER_MOTION_MASK);
76 set_time_adjustment(&adj_default);
80 Widget_Keyframe_List::~Widget_Keyframe_List()
85 Widget_Keyframe_List::redraw(GdkEventExpose */*bleh*/)
88 const int h(get_height());
89 const int w(get_width());
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());
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);
100 //! draw a background
101 gc->set_rgb_fg_color(Gdk::Color("#9d9d9d"));
102 get_window()->draw_rectangle(gc, true, 0, 0, w, h);
106 return true; //needs fixing!
108 //!Returns if there are not keyframes to draw.
109 if (kf_list_->empty()) return false;
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++)
116 //!do not draw keyframes out of the widget boundaries
117 if (iter->get_time()>top || iter->get_time()<bottom)
119 //! If the keyframe is not the selected one
120 if(*iter!=selected_kf)
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,
134 // we do this so that we can be sure that
135 // the selected keyframe is shown on top
138 // If not dragging just show the selected keyframe
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,
146 // If dragging then show the selected as insensitive and the
147 // dragged as selected
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,
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,
165 Widget_Keyframe_List::set_kf_list(synfig::KeyframeList* x)
168 set_selected_keyframe(selected_none);
174 Widget_Keyframe_List::set_selected_keyframe(const synfig::Keyframe &x)
178 dragging_kf_time=selected_kf.get_time();
179 //signal_keyframe_selected_(selected_kf);
185 Widget_Keyframe_List::perform_move_kf()
189 if(dragging_kf_time == selected_kf.get_time())
191 synfigapp::Action::Handle action(synfigapp::Action::create("KeyframeSet"));
194 selected_kf.set_time(dragging_kf_time);
195 action->set_param("canvas",canvas_interface_->get_canvas());
196 action->set_param("canvas_interface",canvas_interface_);
197 action->set_param("keyframe",selected_kf);
200 canvas_interface_->get_instance()->perform_action(action);
211 Widget_Keyframe_List::on_event(GdkEvent *event)
213 const int x(static_cast<int>(event->button.x));
214 const int y(static_cast<int>(event->button.y));
215 //!Boundaries of the drawing area in time units.
216 synfig::Time top(adj_timescale->get_upper());
217 synfig::Time bottom(adj_timescale->get_lower());
218 //!pos is the [0,1] relative horizontal place on the widget
219 float pos((float)x/(get_width()));
220 if(pos<0.0f)pos=0.0f;
221 if(pos>1.0f)pos=1.0f;
222 //! The time where the event x is
223 synfig::Time t((float)(bottom+pos*(top-bottom)));
224 //Do not respond mouse events if the list is empty
225 if(!kf_list_->size())
228 //! here the guts of the event
231 case GDK_MOTION_NOTIFY:
234 // here is captured mouse motion
235 // AND left mouse button pressed
236 if (event->motion.state & GDK_BUTTON1_MASK)
238 // stick to integer frames. It can be optional in the future
239 if(fps) t = floor(t*fps + 0.5)/fps;
245 // here is captured mouse motion
246 // AND NOT left mouse button pressed
249 synfig::Time p_t,n_t;
250 kf_list_->find_prev_next(t, p_t, n_t);
251 if( (p_t==Time::begin() && n_t==Time::end())
253 ((t-p_t)>time_ratio && (n_t-t)>time_ratio)
256 Glib::ustring ttip = _("Click and drag keyframes");
257 set_tooltip_text(ttip);
259 else if ((t-p_t)<(n_t-t))
261 synfig::Keyframe kf(*kf_list_->find_prev(t));
262 synfig::String kf_name(kf.get_description().c_str());
263 Glib::ustring ttip = kf_name.c_str();
264 set_tooltip_text(ttip);
268 synfig::Keyframe kf(*kf_list_->find_next(t));
269 synfig::String kf_name(kf.get_description().c_str());
270 Glib::ustring ttip = kf_name.c_str();
271 set_tooltip_text(ttip);
279 case GDK_BUTTON_PRESS:
282 if(event->button.button==1)
286 synfig::Time prev_t,next_t;
287 kf_list_->find_prev_next(t, prev_t, next_t);
288 if( (prev_t==Time::begin() && next_t==Time::end())
290 ((t-prev_t)>time_ratio && (next_t-t)>time_ratio)
293 set_selected_keyframe(selected_none);
298 else if ((t-prev_t)<(next_t-t))
300 set_selected_keyframe(*(kf_list_->find_prev(t)));
307 set_selected_keyframe(*(kf_list_->find_next(t)));
320 case GDK_BUTTON_RELEASE:
321 if(editable_ && event->button.button==1)
323 // stick to integer frames.
324 if(fps) t = floor(t*fps + 0.5)/fps;
327 stat=perform_move_kf();
339 void Widget_Keyframe_List::set_time_adjustment(Gtk::Adjustment *x)
341 //disconnect old connections
342 time_value_change.disconnect();
343 time_other_change.disconnect();
345 //connect update function to new adjustment
350 time_value_change = x->signal_value_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
351 time_other_change = x->signal_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
356 Widget_Keyframe_List::set_fps(float d)
361 //update everything since we need to redraw already
367 Widget_Keyframe_List::set_canvas_interface(etl::loose_handle<synfigapp::CanvasInterface> h)
370 // Store the values used fomr the canvas interface.
371 if (canvas_interface_)
373 set_fps(canvas_interface_->get_canvas()->rend_desc().get_frame_rate());
374 set_kf_list(&canvas_interface_->get_canvas()->keyframe_list());