Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-studio / trunk / src / gtkmm / widget_keyframe_list.cpp
index e0f372f..ace2b98 100644 (file)
@@ -48,8 +48,9 @@ using namespace etl;
 using namespace synfig;
 using namespace studio;
 
-/* === M A C R O S ========================================================= */
 
+/* === M A C R O S ========================================================= */
+#define WIDGET_KEYFRAME_LIST_DEFAULT_FPS 24.0
 /* === G L O B A L S ======================================================= */
 
 /* === P R O C E D U R E S ================================================= */
@@ -57,13 +58,23 @@ using namespace studio;
 /* === M E T H O D S ======================================================= */
 
 Widget_Keyframe_List::Widget_Keyframe_List():
-       editable_(false)
+       adj_default(0,0,2,1/WIDGET_KEYFRAME_LIST_DEFAULT_FPS,10/WIDGET_KEYFRAME_LIST_DEFAULT_FPS),
+       kf_list_(&default_kf_list_),
+       time_ratio("4f", WIDGET_KEYFRAME_LIST_DEFAULT_FPS)
 {
+       adj_timescale=0;
+       editable_=true;
+       fps=WIDGET_KEYFRAME_LIST_DEFAULT_FPS;
        set_size_request(-1,64);
+       //!This signal is called when the widget need to be redrawn
        signal_expose_event().connect(sigc::mem_fun(*this, &studio::Widget_Keyframe_List::redraw));
+       //! The widget respond to mouse button press and release and to
+       //! left button motion
        add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
-       add_events(Gdk::BUTTON1_MOTION_MASK);
-
+       add_events(Gdk::BUTTON1_MOTION_MASK /*| Gdk::BUTTON3_MOTION_MASK*/);
+       add_events(Gdk::POINTER_MOTION_MASK);
+       set_time_adjustment(&adj_default);
+       queue_draw();
 }
 
 Widget_Keyframe_List::~Widget_Keyframe_List()
@@ -71,40 +82,48 @@ Widget_Keyframe_List::~Widget_Keyframe_List()
 }
 
 bool
-Widget_Gradient::redraw(GdkEventExpose */*bleh*/)
+Widget_Keyframe_List::redraw(GdkEventExpose */*bleh*/)
 {
+
        const int h(get_height());
        const int w(get_width());
 
+       //!Boundaries of the drawing area in time units.
+       synfig::Time top(adj_timescale->get_upper());
+       synfig::Time bottom(adj_timescale->get_lower());
+
+       //! The graphic context
        Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(get_window()));
+       //! A rectangle that defines the drawing area.
        Gdk::Rectangle area(0,0,w,h);
+
+       //! draw a background
+       gc->set_rgb_fg_color(Gdk::Color("#9d9d9d"));
+       get_window()->draw_rectangle(gc, true, 0, 0, w, h);
+
        if(!editable_)
        {
-               return true;
+               return true; //needs fixing!
        }
+       //!Returns if there are not keyframes to draw.
+       if (kf_list_->empty()) return false;
 
-       gc->set_rgb_fg_color(Gdk::Color("#7f7f7f"));
-       get_window()->draw_rectangle(gc, false, 0, 0, w, h);
-
-       KeyframeList::iterator iter,selected_iter;
+       //!Loop all the keyframes
+       synfig::KeyframeList::iterator iter,selected_iter;
        bool show_selected(false);
-       for(iter=kf_list_.begin();iter!=kf_list_.end();iter++)
+       for(iter=kf_list_->begin();iter!=kf_list_->end();iter++)
        {
+               //!do not draw keyframes out of the widget boundaries
+               if (iter->get_time()>top || iter->get_time()<bottom)
+                       continue;
+               //! If the keyframe is not the selected one
                if(*iter!=selected_kf)
-               get_style()->paint_arrow(
-                       get_window(),
-                       (*iter==selected_kf)?Gtk::STATE_SELECTED:Gtk::STATE_ACTIVE,
-                       Gtk::SHADOW_OUT,
-                       area,
-                       *this,
-                       " ",
-                       Gtk::ARROW_DOWN,
-                       1,
-                       int(iter->get_time()*w)-h/2+1, /// to be fixed
-                       0,
-                       h,
-                       h
-               );
+               {
+                       const int x((int)((float)(iter->get_time()-bottom) * (w/(top-bottom)) ) );
+                       get_style()->paint_arrow(get_window(), Gtk::STATE_NORMAL,
+                       Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
+                       x-h/2+1, 0, h, h );
+               }
                else
                {
                        selected_iter=iter;
@@ -113,158 +132,294 @@ Widget_Gradient::redraw(GdkEventExpose */*bleh*/)
        }
 
        // we do this so that we can be sure that
-       // the selected marker is shown on top
+       // the selected keyframe is shown on top
        if(show_selected)
        {
-               get_style()->paint_arrow(
-                       get_window(),
-                       Gtk::STATE_SELECTED,
-                       Gtk::SHADOW_OUT,
-                       area,
-                       *this,
-                       " ",
-                       Gtk::ARROW_DOWN,
-                       1,
-                       round_to_int(selected_iter->get_time()*w)-h/2+1,
-                       0,
-                       h,
-                       h
-               );
+               // If not dragging just show the selected keyframe
+               if (!dragging_)
+               {
+                       int x((int)((float)(selected_iter->get_time()-bottom) * (w/(top-bottom)) ) );
+                       get_style()->paint_arrow(get_window(), Gtk::STATE_SELECTED,
+                       Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
+                       x-h/2+1, 0, h, h );
+               }
+               // If dragging then show the selected as insensitive and the
+               // dragged as selected
+               else
+               {
+                       int x((int)((float)(selected_iter->get_time()-bottom) * (w/(top-bottom)) ) );
+                       get_style()->paint_arrow(get_window(), Gtk::STATE_INSENSITIVE,
+                       Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
+                       x-h/2, 0, h, h );
+                       x=(int)((float)(dragging_kf_time-bottom) * (w/(top-bottom)) ) ;
+                       get_style()->paint_arrow(get_window(), Gtk::STATE_SELECTED,
+                       Gtk::SHADOW_OUT, area, *this, " ", Gtk::ARROW_DOWN, 1,
+                       x-h/2+1, 0, h, h );
+               }
        }
-
        return true;
 }
 
 
 void
-Widget_Keyframe_List::popup_menu(Time /*t*/)
-{
-/*     Gtk::Menu* menu(manage(new Gtk::Menu()));
-       menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
-
-       menu->items().clear();
-
-       menu->items().push_back(
-               Gtk::Menu_Helpers::MenuElem(
-                       _("Insert CPoint"),
-                       sigc::bind(
-                               sigc::mem_fun(*this,&studio::Widget_Gradient::insert_cpoint),
-                               x
-                       )
-               )
-       );
-
-       if(!gradient_.empty())
-       {
-               menu->items().push_back(
-                       Gtk::Menu_Helpers::MenuElem(
-                               _("Remove CPoint"),
-                               sigc::bind(
-                                       sigc::mem_fun(*this,&studio::Widget_Gradient::remove_cpoint),
-                                       x
-                               )
-                       )
-               );
-       }
-
-       menu->popup(0,0);
-*/}
-
-void
-Widget_Keyframe_List::set_value(const synfig::KeyframeList& x)
+Widget_Keyframe_List::set_kf_list(synfig::KeyframeList* x)
 {
        kf_list_=x;
-       if(kf_list_.size())
-               set_selected_keyframe(*kf_list_.find_next(synfig::Time::zero()));
-       queue_draw();
+       set_selected_keyframe(selected_none);
+       selected_=false;
+       dragging_=false;
 }
 
 void
 Widget_Keyframe_List::set_selected_keyframe(const synfig::Keyframe &x)
 {
        selected_kf=x;
-       signal_keyframe_selected_(selected_kf);
+       selected_=true;
+       dragging_kf_time=selected_kf.get_time();
+       //signal_keyframe_selected_(selected_kf);
+       dragging_=false;
        queue_draw();
 }
 
-void
-Widget_Keyframe_List::update_keyframe(const synfig::Keyframe &x)
+bool
+Widget_Keyframe_List::perform_move_kf(bool delta=false)
 {
-       try
+       if(!selected_)
+               return false;
+       if(dragging_kf_time == selected_kf.get_time())
+               return false; // change this checking if not sticked to integer frames
+       Time selected_kf_time(selected_kf.get_time());
+       Time prev, next;
+       kf_list_->find_prev_next(selected_kf_time, prev, next);
+       // Not possible to set delta to the first keyframe
+       // perform normal movement
+       // As suggested by Zelgadis it is better to not perform anything.
+       if (prev==Time::begin() && delta==true)
        {
-               KeyframeList::iterator iter(keyframe_list_.find(x));
-               iter->pos=x.pos;///////7
-               iter->color=x.color;/////////
-               gradient_.sort();/////////
-               queue_draw();
-       }
-       catch(synfig::Exception::NotFound)
-       {
-               // Yotta...
+               synfig::info(_("Not possible to ALT-drag the first keyframe"));
+               return false;
        }
+       if(!delta)
+               {
+                       synfigapp::Action::Handle action(synfigapp::Action::create("KeyframeSet"));
+                       if(!action)
+                       return false;
+                       selected_kf.set_time(dragging_kf_time);
+                       action->set_param("canvas",canvas_interface_->get_canvas());
+                       action->set_param("canvas_interface",canvas_interface_);
+                       action->set_param("keyframe",selected_kf);
+                       try
+                       {
+                               canvas_interface_->get_instance()->perform_action(action);
+                       }
+                       catch(...)
+                       {
+                               return false;
+                       }
+               }
+       else
+               {
+                       Keyframe prev_kf(*kf_list_->find_prev(selected_kf_time));
+                       Time prev_kf_time(prev_kf.get_time());
+                       if (prev_kf_time >= dragging_kf_time) //Not allowed
+                       {
+                               synfig::warning(_("Delta set not allowed"));
+                               synfig::info(_("Widget_Keyframe_List::perform_move_kf(%i)::prev_kf_time=%s"), delta, prev_kf_time.get_string().c_str());
+                               synfig::info(_("Widget_Keyframe_List::perform_move_kf(%i)::dragging_kf_time=%s"), delta, dragging_kf_time.get_string().c_str());
+                               return false;
+                       }
+                       else
+                       {
+                               Time old_delta_time(selected_kf_time-prev_kf_time);
+                               Time new_delta_time(dragging_kf_time-prev_kf_time);
+                               Time change_delta(new_delta_time-old_delta_time);
+                               synfigapp::Action::Handle action(synfigapp::Action::create("KeyframeSetDelta"));
+                               if(!action)
+                                       return false;
+                               action->set_param("canvas",canvas_interface_->get_canvas());
+                               action->set_param("canvas_interface",canvas_interface_);
+                               action->set_param("keyframe",prev_kf);
+                               action->set_param("delta",change_delta);
+                               canvas_interface_->get_instance()->perform_action(action);
+                       }
+               }
+       queue_draw();
+       return true;
 }
 
 bool
 Widget_Keyframe_List::on_event(GdkEvent *event)
 {
-       //if(editable_)
-       {
-               const int x(static_cast<int>(event->button.x));
-               const int y(static_cast<int>(event->button.y));
-
-               float pos((float)x/(float)get_width());
-               if(pos<0.0f)pos=0.0f;////////
-               if(pos>1.0f)pos=1.0f;////////
+       const int x(static_cast<int>(event->button.x));
+       //const int y(static_cast<int>(event->button.y));
+       //!Boundaries of the drawing area in time units.
+       synfig::Time top(adj_timescale->get_upper());
+       synfig::Time bottom(adj_timescale->get_lower());
+       //!pos is the [0,1] relative horizontal place on the widget
+       float pos((float)x/(get_width()));
+       if(pos<0.0f)pos=0.0f;
+       if(pos>1.0f)pos=1.0f;
+       //! The time where the event x is
+       synfig::Time t((float)(bottom+pos*(top-bottom)));
+       //Do not respond mouse events if the list is empty
+       if(!kf_list_->size())
+               return true;
 
-               switch(event->type)
+       //! here the guts of the event
+       switch(event->type)
+       {
+       case GDK_MOTION_NOTIFY:
+               if(editable_)
                {
-               case GDK_MOTION_NOTIFY:
-                       if(editable_ && y>get_height()-h)
+                       // here is captured mouse motion
+                       // AND left or right mouse button pressed
+                       if (event->motion.state & (GDK_BUTTON1_MASK /*| GDK_BUTTON3_MASK*/))
                        {
-                               if(!keyframe_list_.size()) return true;
-                               synfig::KeyframeList::iterator iter(keyframe_list_.find(selected_kf.get_guid()));
-                               iter->pos=pos;/////
-                               gradient_.sort();
-
-//                             signal_value_changed_();
-                               changed_=true;
+                               // stick to integer frames. It can be optional in the future
+                               if(fps) t = floor(t*fps + 0.5)/fps;
+                               dragging_kf_time=t;
+                               dragging_=true;
                                queue_draw();
                                return true;
                        }
-                       break;
-               case GDK_BUTTON_PRESS:
-                       changed_=false;
-                       if(event->button.button==1)
+                       // here is captured mouse motion
+                       // AND NOT left or right mouse button pressed
+                       else
                        {
-                               if(editable_ && y>get_height()-CONTROL_HEIGHT)
+                               Glib::ustring ttip="";
+                               synfig::Time p_t,n_t;
+                               kf_list_->find_prev_next(t, p_t, n_t);
+                               if( (p_t==Time::begin()         &&      n_t==Time::end())
+                               ||
+                               ((t-p_t)>time_ratio     && (n_t-t)>time_ratio)
+                               )
                                {
-                                       set_selected_cpoint(*gradient_.proximity(pos));
-                                       queue_draw();
-                                       return true;
+                                       ttip = _("Click and drag keyframes");
+                               }
+                               else if ((t-p_t)<(n_t-t))
+                               {
+                                       synfig::Keyframe kf(*kf_list_->find_prev(t));
+                                       synfig::String kf_name(kf.get_description().c_str());
+                                       ttip = kf_name.c_str();
                                }
                                else
                                {
-                                       signal_clicked_();
-                                       return true;
+                                       synfig::Keyframe kf(*kf_list_->find_next(t));
+                                       synfig::String kf_name(kf.get_description().c_str());
+                                       ttip = kf_name.c_str();
                                }
+                               tooltips.set_tip(*this, ttip);
+                               dragging_=false;
+                               queue_draw();
+                               return true;
                        }
-                       else if(editable_ && event->button.button==3)
+               }
+               break;
+       case GDK_BUTTON_PRESS:
+               changed_=false;
+               dragging_=false;
+               if(event->button.button==1 /*|| event->button.button==3*/)
+               {
+                       if(editable_)
                        {
-                               popup_menu(pos);
+                               synfig::Time prev_t,next_t;
+                               kf_list_->find_prev_next(t, prev_t, next_t);
+                               if( (prev_t==Time::begin()      &&      next_t==Time::end())
+                               ||
+                               ((t-prev_t)>time_ratio  && (next_t-t)>time_ratio)
+                               )
+                               {
+                                       set_selected_keyframe(selected_none);
+                                       selected_=false;
+                                       queue_draw();
+                               }
+                               else if ((t-prev_t)<(next_t-t))
+                               {
+                                       set_selected_keyframe(*(kf_list_->find_prev(t)));
+                                       queue_draw();
+                                       selected_=true;
+                               }
+                               else
+                               {
+                                       set_selected_keyframe(*(kf_list_->find_next(t)));
+                                       queue_draw();
+                                       selected_=true;
+                               }
                                return true;
                        }
-                       break;
-               case GDK_BUTTON_RELEASE:
-                       if(editable_ && event->button.button==1 && y>get_height()-CONTROL_HEIGHT)
+                       else
                        {
-                               set_selected_cpoint(*gradient_.proximity(pos));
-                               if(changed_)signal_value_changed_();
-                               return true;
+                               return false;
                        }
-               default:
-                       break;
                }
-       }
 
+               break;
+       case GDK_BUTTON_RELEASE:
+               if(editable_ && (event->button.button==1 /*|| event->button.button==3*/))
+               {
+                       // stick to integer frames.
+                       if(fps) t = floor(t*fps + 0.5)/fps;
+                       bool stat=false;
+                       if(dragging_)
+                               {
+                                       //if (event->button.button==3)
+                                       if(event->button.state & GDK_MOD1_MASK)
+                                       {
+                                               stat=perform_move_kf(true);
+                                       }
+                                       else
+                                       {
+                                               stat=perform_move_kf(false);
+                                       }
+                               }
+                       dragging_=false;
+                       return stat;
+               }
+               break;
+       default:
+               break;
+       }
        return false;
 }
+
+
+void Widget_Keyframe_List::set_time_adjustment(Gtk::Adjustment *x)
+{
+       //disconnect old connections
+       time_value_change.disconnect();
+       time_other_change.disconnect();
+
+       //connect update function to new adjustment
+       adj_timescale = x;
+
+       if(x)
+       {
+               time_value_change = x->signal_value_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
+               time_other_change = x->signal_changed().connect(sigc::mem_fun(*this,&Widget_Keyframe_List::queue_draw));
+       }
+}
+
+void
+Widget_Keyframe_List::set_fps(float d)
+{
+       if(fps != d)
+       {
+               fps = d;
+               //update everything since we need to redraw already
+               queue_draw();
+       }
+}
+
+void
+Widget_Keyframe_List::set_canvas_interface(etl::loose_handle<synfigapp::CanvasInterface> h)
+{
+       canvas_interface_=h;
+       // Store the values used fomr the canvas interface.
+       if (canvas_interface_)
+       {
+               set_fps(canvas_interface_->get_canvas()->rend_desc().get_frame_rate());
+               set_kf_list(&canvas_interface_->get_canvas()->keyframe_list());
+       }
+}
+
+