Rename 'Normal Tool' to 'Transform Tool'.
[synfig.git] / synfig-studio / src / gtkmm / state_normal.cpp
index 68f1750..e9667cc 100644 (file)
@@ -35,6 +35,7 @@
 #include <gtkmm/entry.h>
 
 #include <synfig/valuenode_animated.h>
+#include <synfig/valuenode_blinecalcvertex.h>
 #include <synfig/valuenode_composite.h>
 #include <synfig/valuenode_const.h>
 #include <synfig/valuenode_dynamiclist.h>
@@ -95,7 +96,7 @@ class DuckDrag_Combo : public DuckDrag_Base
        bool move_only;
 
 public:
-       etl::handle<CanvasView> canvas_view_;
+       CanvasView* canvas_view_;
        bool scale;
        bool rotate;
        bool constrain;
@@ -110,7 +111,7 @@ public:
 
 class studio::StateNormal_Context : public sigc::trackable
 {
-       etl::handle<CanvasView> canvas_view_;
+       CanvasView* canvas_view_;
 
        synfigapp::Settings& settings;
 
@@ -139,14 +140,11 @@ public:
        void set_constrain_flag(bool x) { checkbutton_constrain.set_active(x); refresh_constrain_flag(); }
        void refresh_constrain_flag() { if(duck_dragger_)duck_dragger_->constrain=get_constrain_flag(); }
 
-       Smach::event_result event_refresh_tool_options(const Smach::event& x);
-       void refresh_tool_options();
-
        StateNormal_Context(CanvasView* canvas_view);
 
        ~StateNormal_Context();
 
-       const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
+       CanvasView* get_canvas_view()const{return canvas_view_;}
        etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
        synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
        WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
@@ -157,6 +155,18 @@ public:
        bool key_pressed(GdkEventKey *event);
        bool key_released(GdkEventKey *event);
 
+       Smach::event_result event_stop_handler(const Smach::event& x);
+       Smach::event_result event_refresh_handler(const Smach::event& x);
+       Smach::event_result event_refresh_ducks_handler(const Smach::event& x);
+       Smach::event_result event_undo_handler(const Smach::event& x);
+       Smach::event_result event_redo_handler(const Smach::event& x);
+       Smach::event_result event_mouse_button_down_handler(const Smach::event& x);
+       Smach::event_result event_multiple_ducks_clicked_handler(const Smach::event& x);
+       Smach::event_result event_refresh_tool_options(const Smach::event& x);
+       void refresh_tool_options();
+       Smach::event_result event_layer_click(const Smach::event& x);
+
+
 };     // END of class StateNormal_Context
 
 /* === M E T H O D S ======================================================= */
@@ -164,7 +174,16 @@ public:
 StateNormal::StateNormal():
        Smach::state<StateNormal_Context>("normal")
 {
+       insert(event_def(EVENT_STOP,&StateNormal_Context::event_stop_handler));
+       insert(event_def(EVENT_REFRESH,&StateNormal_Context::event_refresh_handler));
+       insert(event_def(EVENT_REFRESH_DUCKS,&StateNormal_Context::event_refresh_ducks_handler));
+       insert(event_def(EVENT_UNDO,&StateNormal_Context::event_undo_handler));
+       insert(event_def(EVENT_REDO,&StateNormal_Context::event_redo_handler));
+       insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateNormal_Context::event_mouse_button_down_handler));
+       insert(event_def(EVENT_WORKAREA_MULTIPLE_DUCKS_CLICKED,&StateNormal_Context::event_multiple_ducks_clicked_handler));
        insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateNormal_Context::event_refresh_tool_options));
+       insert(event_def(EVENT_WORKAREA_LAYER_CLICKED,&StateNormal_Context::event_layer_click));
+
 }
 
 StateNormal::~StateNormal()
@@ -212,7 +231,7 @@ StateNormal_Context::StateNormal_Context(CanvasView* canvas_view):
        duck_dragger_->canvas_view_=get_canvas_view();
 
        // Set up the tool options dialog
-       options_table.attach(*manage(new Gtk::Label(_("Normal Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(*manage(new Gtk::Label(_("Transform Tool"))),      0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
        options_table.attach(checkbutton_rotate,                                                        0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
        options_table.attach(checkbutton_scale,                                                 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
        options_table.attach(checkbutton_constrain,                                                     0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
@@ -225,7 +244,7 @@ StateNormal_Context::StateNormal_Context(CanvasView* canvas_view):
        options_table.show_all();
        refresh_tool_options();
        //App::dialog_tool_options->set_widget(options_table);
-       App::dialog_tool_options->present();
+       //App::dialog_tool_options->present();
 
        get_work_area()->set_allow_layer_clicks(true);
        get_work_area()->set_duck_dragger(duck_dragger_);
@@ -233,8 +252,9 @@ StateNormal_Context::StateNormal_Context(CanvasView* canvas_view):
        keypress_connect=get_work_area()->signal_key_press_event().connect(sigc::mem_fun(*this,&StateNormal_Context::key_pressed),false);
        keyrelease_connect=get_work_area()->signal_key_release_event().connect(sigc::mem_fun(*this,&StateNormal_Context::key_released),false);
 
-//     get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
-       get_canvas_view()->work_area->reset_cursor();
+       //these will segfault
+//     get_work_area()->set_cursor(Gdk::CROSSHAIR);
+//     get_work_area()->reset_cursor();
 
        App::toolbox->refresh();
 
@@ -293,7 +313,7 @@ StateNormal_Context::refresh_tool_options()
 {
        App::dialog_tool_options->clear();
        App::dialog_tool_options->set_widget(options_table);
-       App::dialog_tool_options->set_local_name(_("Normal Tool"));
+       App::dialog_tool_options->set_local_name(_("Transform Tool"));
        App::dialog_tool_options->set_name("normal");
 }
 
@@ -304,7 +324,7 @@ StateNormal_Context::~StateNormal_Context()
        save_settings();
 
        get_work_area()->clear_duck_dragger();
-       get_canvas_view()->work_area->reset_cursor();
+       get_work_area()->reset_cursor();
 
        keypress_connect.disconnect();
        keyrelease_connect.disconnect();
@@ -314,14 +334,6 @@ StateNormal_Context::~StateNormal_Context()
        App::toolbox->refresh();
 }
 
-
-Smach::event_result
-StateNormal_Context::event_refresh_tool_options(const Smach::event& /*x*/)
-{
-       refresh_tool_options();
-       return Smach::RESULT_ACCEPT;
-}
-
 DuckDrag_Combo::DuckDrag_Combo():
        scale(false),
        rotate(false),
@@ -411,15 +423,6 @@ DuckDrag_Combo::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
                        if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)
                                (*iter)->set_trans_point(positions[i]+vect, time);
                }
-               DuckList duck_list(duckmatic->get_duck_list());
-               for (iter=duck_list.begin(); iter!=duck_list.end(); ++iter)
-               {
-                       if ((*iter)->get_type() == Duck::TYPE_TANGENT || (*iter)->get_type() == Duck::TYPE_WIDTH)
-                       {
-                               (*iter)->update(time);
-                       }
-               }
-               return;
        }
 
        if (rotate)
@@ -507,6 +510,10 @@ DuckDrag_Combo::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
                        (*iter)->set_trans_point(p, time);
                }
        }
+
+       // then patch up the tangents for the vertices we've moved
+       duckmatic->update_ducks();
+
        last_move=vect;
 }
 
@@ -528,3 +535,290 @@ DuckDrag_Combo::end_duck_drag(Duckmatic* duckmatic)
                return false;
        }
 }
+
+Smach::event_result
+StateNormal_Context::event_refresh_tool_options(const Smach::event& /*x*/)
+{
+       refresh_tool_options();
+       return Smach::RESULT_ACCEPT;
+}
+
+Smach::event_result
+StateNormal_Context::event_stop_handler(const Smach::event& /*x*/)
+{
+       // synfig::info("STATE NORMAL: Received Stop Event");
+       canvas_view_->stop();
+       return Smach::RESULT_ACCEPT;
+}
+
+Smach::event_result
+StateNormal_Context::event_refresh_handler(const Smach::event& /*x*/)
+{
+       // synfig::info("STATE NORMAL: Received Refresh Event");
+       canvas_view_->rebuild_tables();
+       canvas_view_->work_area->queue_render_preview();
+       return Smach::RESULT_ACCEPT;
+}
+
+Smach::event_result
+StateNormal_Context::event_refresh_ducks_handler(const Smach::event& /*x*/)
+{
+       // synfig::info("STATE NORMAL: Received Refresh Ducks");
+       canvas_view_->queue_rebuild_ducks();
+       return Smach::RESULT_ACCEPT;
+}
+
+Smach::event_result
+StateNormal_Context::event_undo_handler(const Smach::event& /*x*/)
+{
+       // synfig::info("STATE NORMAL: Received Undo Event");
+       canvas_view_->get_instance()->undo();
+       return Smach::RESULT_ACCEPT;
+}
+
+Smach::event_result
+StateNormal_Context::event_redo_handler(const Smach::event& /*x*/)
+{
+       // synfig::info("STATE NORMAL: Received Redo Event");
+       canvas_view_->get_instance()->redo();
+       return Smach::RESULT_ACCEPT;
+}
+
+Smach::event_result
+StateNormal_Context::event_mouse_button_down_handler(const Smach::event& x)
+{
+       // synfig::info("STATE NORMAL: Received mouse button down Event");
+
+       const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
+
+       switch(event.button)
+       {
+       case BUTTON_RIGHT:
+               canvas_view_->popup_main_menu();
+               return Smach::RESULT_ACCEPT;
+       default:
+               return Smach::RESULT_OK;
+       }
+}
+
+Smach::event_result
+StateNormal_Context::event_layer_click(const Smach::event& x)
+{
+       const EventLayerClick& event(*reinterpret_cast<const EventLayerClick*>(&x));
+
+       if(event.layer)
+       {
+               // synfig::info("STATE NORMAL: Received layer click Event, \"%s\"",event.layer->get_name().c_str());
+       }
+       else
+       {
+               // synfig::info("STATE NORMAL: Received layer click Event with an empty layer.");
+       }
+
+       switch(event.button)
+       {
+       case BUTTON_LEFT:
+               if(!(event.modifier&Gdk::CONTROL_MASK))
+                       canvas_view_->get_selection_manager()->clear_selected_layers();
+               if(event.layer)
+               {
+                       std::list<Layer::Handle> layer_list(canvas_view_->get_selection_manager()->get_selected_layers());
+                       std::set<Layer::Handle> layers(layer_list.begin(),layer_list.end());
+                       if(layers.count(event.layer))
+                       {
+                               layers.erase(event.layer);
+                               layer_list=std::list<Layer::Handle>(layers.begin(),layers.end());
+                               canvas_view_->get_selection_manager()->clear_selected_layers();
+                               canvas_view_->get_selection_manager()->set_selected_layers(layer_list);
+                       }
+                       else
+                       {
+                               canvas_view_->get_selection_manager()->set_selected_layer(event.layer);
+                       }
+               }
+               return Smach::RESULT_ACCEPT;
+       case BUTTON_RIGHT:
+               canvas_view_->popup_layer_menu(event.layer);
+               return Smach::RESULT_ACCEPT;
+       default:
+               return Smach::RESULT_OK;
+       }
+}
+
+/*
+void
+StateNormal_Context::edit_several_waypoints(std::list<synfigapp::ValueDesc> value_desc_list)
+{
+       Gtk::Dialog dialog(
+               "Edit Multiple Waypoints",              // Title
+               true,           // Modal
+               true            // use_separator
+       );
+
+       Widget_WaypointModel widget_waypoint_model;
+       widget_waypoint_model.show();
+
+       dialog.get_vbox()->pack_start(widget_waypoint_model);
+
+
+       dialog.add_button(Gtk::StockID("gtk-apply"),1);
+       dialog.add_button(Gtk::StockID("gtk-cancel"),0);
+       dialog.show();
+
+       if(dialog.run()==0)
+               return;
+       synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Set Waypoints"));
+
+       std::list<synfigapp::ValueDesc>::iterator iter;
+       for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
+       {
+               synfigapp::ValueDesc value_desc(*iter);
+
+               if(!value_desc.is_valid())
+                       continue;
+
+               ValueNode_Animated::Handle value_node;
+
+               // If this value isn't a ValueNode_Animated, but
+               // it is somewhat constant, then go ahead and convert
+               // it to a ValueNode_Animated.
+               if(!value_desc.is_value_node() || ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
+               {
+                       ValueBase value;
+                       if(value_desc.is_value_node())
+                               value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
+                       else
+                               value=value_desc.get_value();
+
+                       value_node=ValueNode_Animated::create(value,get_canvas()->get_time());
+
+                       synfigapp::Action::Handle action;
+
+                       if(!value_desc.is_value_node())
+                       {
+                               action=synfigapp::Action::create("ValueDescConnect");
+                               action->set_param("dest",value_desc);
+                               action->set_param("src",ValueNode::Handle(value_node));
+                       }
+                       else
+                       {
+                               action=synfigapp::Action::create("ValueNodeReplace");
+                               action->set_param("dest",value_desc.get_value_node());
+                               action->set_param("src",ValueNode::Handle(value_node));
+                       }
+
+                       action->set_param("canvas",get_canvas());
+                       action->set_param("canvas_interface",get_canvas_interface());
+
+
+                       if(!get_canvas_interface()->get_instance()->perform_action(action))
+                       {
+                               get_canvas_view()->get_ui_interface()->error(_("Unable to convert to animated waypoint"));
+                               group.cancel();
+                               return;
+                       }
+               }
+               else
+               {
+                       if(value_desc.is_value_node())
+                               value_node=ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node());
+               }
+
+
+               if(value_node)
+               {
+
+                       synfigapp::Action::Handle action(synfigapp::Action::create("WaypointSetSmart"));
+
+                       if(!action)
+                       {
+                               get_canvas_view()->get_ui_interface()->error(_("Unable to find WaypointSetSmart action"));
+                               group.cancel();
+                               return;
+                       }
+
+
+                       action->set_param("canvas",get_canvas());
+                       action->set_param("canvas_interface",get_canvas_interface());
+                       action->set_param("value_node",ValueNode::Handle(value_node));
+                       action->set_param("time",get_canvas()->get_time());
+                       action->set_param("model",widget_waypoint_model.get_waypoint_model());
+
+                       if(!get_canvas_interface()->get_instance()->perform_action(action))
+                       {
+                               get_canvas_view()->get_ui_interface()->error(_("Unable to set a specific waypoint"));
+                               group.cancel();
+                               return;
+                       }
+               }
+               else
+               {
+                       //get_canvas_view()->get_ui_interface()->error(_("Unable to animate a specific valuedesc"));
+                       //group.cancel();
+                       //return;
+               }
+
+       }
+}
+*/
+
+Smach::event_result
+StateNormal_Context::event_multiple_ducks_clicked_handler(const Smach::event& /*x*/)
+{
+       // synfig::info("STATE NORMAL: Received multiple duck click event");
+
+       //const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
+
+       std::list<synfigapp::ValueDesc> value_desc_list;
+
+       // Create a list of value_descs associated with selection
+       const DuckList selected_ducks(get_work_area()->get_selected_ducks());
+       DuckList::const_iterator iter;
+       for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
+       {
+               synfigapp::ValueDesc value_desc((*iter)->get_value_desc());
+
+               if(!value_desc.is_valid())
+                       continue;
+
+               if(value_desc.get_value_type()==ValueBase::TYPE_BLINEPOINT && value_desc.is_value_node() && ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
+               {
+                       value_desc_list.push_back(
+                               synfigapp::ValueDesc(
+                                       ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())
+                                       ,ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())
+                                                               ->get_link_index_from_name("point")
+                               )
+                       );
+               }
+               else
+                       value_desc_list.push_back(value_desc);
+       }
+
+       Gtk::Menu *menu=manage(new Gtk::Menu());
+       menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
+
+       canvas_view_->get_instance()->make_param_menu(menu,canvas_view_->get_canvas(),value_desc_list);
+
+       /*
+       synfigapp::Action::ParamList param_list;
+       param_list=get_canvas_interface()->generate_param_list(value_desc_list);
+
+       canvas_view_->add_actions_to_menu(menu, param_list,synfigapp::Action::CATEGORY_VALUEDESC|synfigapp::Action::CATEGORY_VALUENODE);
+
+       menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Edit Waypoints"),
+               sigc::bind(
+                       sigc::mem_fun(
+                               *this,
+                               &studio::StateNormal_Context::edit_several_waypoints
+                       ),
+                       value_desc_list
+               )
+       ));
+       */
+       menu->popup(3,gtk_get_current_event_time());
+
+       return Smach::RESULT_ACCEPT;
+}
+
+