Add an icon to show the Setup dialog.
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_draw.cpp
index 5ffdd37..6122064 100644 (file)
@@ -62,6 +62,8 @@
 #include <gtkmm/scale.h>
 #include <sigc++/connection.h>
 
+#include "general.h"
+
 #endif
 
 /* === U S I N G =========================================================== */
@@ -97,7 +99,7 @@ class studio::StateDraw_Context : public sigc::trackable
        bool prev_workarea_layer_status_;
 
        int nested;
-       SigC::Connection process_queue_connection;
+       sigc::connection process_queue_connection;
 
        ValueNode_BLine::Handle last_stroke;
        synfig::String last_stroke_id;
@@ -112,7 +114,8 @@ class studio::StateDraw_Context : public sigc::trackable
 
        Duckmatic::Type old_duckmask;
 
-       void fill_last_stroke(int depth_offset);
+       void fill_last_stroke();
+       void fill_last_stroke_and_unselect_other_layers();
 
        Smach::event_result new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline_flag,float radius);
 
@@ -366,7 +369,10 @@ StateDraw_Context::increment_id()
        if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
        {
                // figure out how many digits it is
-               for(digits=0;(int)id.size()-1>=digits && id[id.size()-1-digits]<='9' && id[id.size()-1-digits]>='0';digits++)while(false);
+               for (digits = 0;
+                        (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
+                        digits++)
+                       ;
 
                String str_number;
                str_number=String(id,id.size()-digits,id.size());
@@ -448,7 +454,7 @@ StateDraw_Context::StateDraw_Context(CanvasView* canvas_view):
 
        //options_table.attach(button_fill_last_stroke, 0, 2, 13, 14, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
 
-       button_fill_last_stroke.signal_pressed().connect(sigc::bind(sigc::mem_fun(*this,&StateDraw_Context::fill_last_stroke), 0));
+       button_fill_last_stroke.signal_pressed().connect(sigc::mem_fun(*this,&StateDraw_Context::fill_last_stroke));
        check_localerror.signal_toggled().connect(sigc::mem_fun(*this,&StateDraw_Context::UpdateErrorBox));
 
        options_table.show_all();
@@ -519,11 +525,9 @@ StateDraw_Context::refresh_tool_options()
                Gtk::StockID("synfig-fill"),
                _("Fill Last Stroke")
        )->signal_clicked().connect(
-               sigc::bind(
-                       sigc::mem_fun(
-                               *this,
-                               &StateDraw_Context::fill_last_stroke),
-                       0));
+               sigc::mem_fun(
+                       *this,
+                       &StateDraw_Context::fill_last_stroke));
 }
 
 Smach::event_result
@@ -588,7 +592,8 @@ StateDraw_Context::event_mouse_down_handler(const Smach::event& x)
                }
 
        case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
-               return Smach::RESULT_ACCEPT;
+               if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
+                       return Smach::RESULT_ACCEPT;
 
        default:
                return Smach::RESULT_OK;
@@ -711,14 +716,16 @@ StateDraw_Context::process_stroke(StrokeData stroke_data, WidthData width_data,
        }
 
        // If the start and end points are similar, then make them the same point
-       if(get_auto_loop_flag() &&
-          bline.size()>2&&(bline.front().get_vertex()-bline.back().get_vertex()).mag()<=radius)
+       if (get_auto_loop_flag() &&
+               bline.size() > 2 &&
+               (bline.front().get_vertex() - bline.back().get_vertex()).mag() <= radius)
        {
                loop_bline_flag=true;
                Vector tangent;
                Real width(0);
 
-               while(bline.size()>2&&(bline.front().get_vertex()-bline.back().get_vertex()).mag()<=radius)
+               while (bline.size() > 2 &&
+                          (bline.front().get_vertex() - bline.back().get_vertex()).mag() <= radius)
                {
                        tangent=bline.back().get_tangent1();
                        width=bline.back().get_width();
@@ -748,8 +755,15 @@ StateDraw_Context::process_stroke(StrokeData stroke_data, WidthData width_data,
        }
 
        // If the bline only has one blinepoint, then there is nothing to do.
-       if(bline.size()<=1)
+       if(bline.size() < 2)
+       {
+               // hide the 'stroke' line we were drawing, unless the user
+               // explicitly requests that they are kept
+               if (!getenv("SYNFIG_KEEP_ABORTED_DRAW_LINES"))
+                       refresh_ducks();
+
                return Smach::RESULT_OK;
+       }
 
        if(region_flag)
                return new_region(bline,radius);
@@ -760,6 +774,8 @@ StateDraw_Context::process_stroke(StrokeData stroke_data, WidthData width_data,
 Smach::event_result
 StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline_flag,float radius)
 {
+       synfigapp::SelectionManager::LayerList layer_list = get_canvas_view()->get_selection_manager()->get_selected_layers();
+
        // Create the action group
        synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Sketch BLine"));
 
@@ -821,6 +837,7 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
                        // don't extend looped blines
                        if(finish_duck_value_node_bline&&!finish_duck_value_node_bline->get_loop()&&
                           (finish_duck_index==0||finish_duck_index==finish_duck_value_node_bline->link_count()-1))
+                       {
                                if(extend_start)
                                {
                                        // we've started and finished drawing at the end of a bline.  we can't
@@ -833,6 +850,7 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
                                        shift_offset=true;
                                        shift_offset_vector=finish_duck->get_origin();
                                }
+                       }
                }while(0);
 
                // if the new line's start didn't extend an existing line,
@@ -1000,8 +1018,11 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
                }
        }
 
-       // Create the layer
+       last_stroke=value_node;
+       last_stroke_id=get_id();
+
        {
+               // Create the layer(s)
                Layer::Handle layer;
                Canvas::Handle canvas(get_canvas_view()->get_canvas());
                int depth(0);
@@ -1013,7 +1034,12 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
                        depth=layer->get_depth();
                        canvas=layer->get_canvas();
                }
-               get_canvas_interface()->get_selection_manager()->clear_selected_layers();
+
+               // fill_last_stroke() will take care of clearing the selection if we're calling it
+               if(get_outline_flag() && get_region_flag())
+                       fill_last_stroke_and_unselect_other_layers();
+               else
+                       get_canvas_interface()->get_selection_manager()->clear_selected_layers();
 
                //int number(synfig::UniqueID().get_uid());
 
@@ -1064,16 +1090,11 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
                        //refresh_ducks();
                        return Smach::RESULT_ERROR;
                }
-               get_canvas_view()->get_selection_manager()->set_selected_layer(layer);
+               layer_list.push_back(layer);
+               get_canvas_view()->get_selection_manager()->set_selected_layers(layer_list);
                //refresh_ducks();
        }
 
-       last_stroke=value_node;
-       last_stroke_id=get_id();
-
-       if(get_outline_flag() && get_region_flag())
-               fill_last_stroke(1);
-
        increment_id();
        return Smach::RESULT_ACCEPT;
 }
@@ -1229,10 +1250,12 @@ debug_show_vertex_list(int iteration, std::list<synfigapp::ValueDesc>& vertex_li
        {
                if (started) printf(", "); else started = true;
                if (start != -1)
+               {
                        if (dir != 0)
                                printf("%d--%d", start, prev);
                        else
                                printf("%d", start);
+               }
                printf(")");
        }
        printf("\n");
@@ -1385,12 +1408,14 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                this_index = iter->get_index();
                                // printf("index went from %d to %d\n", last_index, this_index);
                                if (looped)
+                               {
                                        if (this_index - last_index > points_in_line/2)
                                                while (this_index - last_index > points_in_line/2)
                                                        this_index -= points_in_line;
                                        else if (last_index - this_index > points_in_line/2)
                                                while (last_index - this_index > points_in_line/2)
                                                        this_index += points_in_line;
+                               }
 
                                if (this_index < min_index) min_index = this_index;
                                if (this_index > max_index) max_index = this_index;
@@ -1519,7 +1544,6 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                  if(value_prev.get_parent_value_node()==value_next.get_parent_value_node() &&
                                  value_prev.get_parent_value_node()!=value_desc.get_parent_value_node())
                                  {
-                                 DEBUGPOINT();
                                  vertex_list.erase(iter);
                                  done=false;
                                  break;
@@ -1534,7 +1558,6 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                // if(value_prev.get_value_node()==value_desc.get_value_node() ||
                                //    value_desc.get_value_node()==value_next.get_value_node())
                                // {
-                               //      // DEBUGPOINT();
                                //      vertex_list.erase(iter);
                                //      done=false;
                                //      printf("erased node - i = %d\n", i);
@@ -1544,7 +1567,6 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                // // if previous is the same as next, remove previous?  or next?
                                // if(value_prev.get_value_node()==value_next.get_value_node())
                                // {
-                               //      // DEBUGPOINT();
                                //      vertex_list.erase(next);
                                //      // vertex_list.erase(prev);
                                //      done=false;
@@ -1573,7 +1595,6 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                                //                                                         strprintf("same parent, different points this %d < next-1 %d",
                                                //                                                                               value_desc.get_index(), ((value_next.get_index()-1))),
                                                //                                                         current);
-                                               //              // DEBUGPOINT();
                                                //              for (int index = value_desc.get_index()+1; index < value_next.get_index(); index++)
                                                //              {
                                                //                      printf("inserting up %d\n", index);
@@ -1589,7 +1610,6 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                                //                                                         strprintf("same parent, different points next %d < this-1 %d",
                                                //                                                                               value_next.get_index(), ((value_desc.get_index()-1))),
                                                //                                                         current);
-                                               //              // DEBUGPOINT();
                                                //              for (int index = value_desc.get_index()-1; index > value_next.get_index(); index--)
                                                //              {
                                                //                      printf("inserting down %d\n", index);
@@ -1629,7 +1649,6 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                                        if((vertex.get_vertex()-vertex_next.get_vertex()).mag_squared()<radius*radius)
                                                        {
                                                                printf("in one - it's close\n");
-                                                               // DEBUGPOINT();
                                                                ValueNode_Composite::Handle value_node;
                                                                ValueNode_Composite::Handle value_node_next;
                                                                value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node().clone());
@@ -1639,7 +1658,6 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                                                        synfig::info(__FILE__":%d: Unable to properly connect blines.",__LINE__);
                                                                        continue;
                                                                }
-                                                               // DEBUGPOINT();
                                                                // \todo if next isn't split, don't we want to copy its 'Tangent 1' instead?
                                                                value_node->set_link(5,value_node_next->get_link(5)); // Tangent 2
                                                                value_node->set_link(3,ValueNode_Const::create(true)); // Split Tangents
@@ -1660,7 +1678,6 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                                        else if (value_prev.parent_is_value_node())
                                                        {
                                                                printf("in two - it's far\n");
-                                                               // DEBUGPOINT();
                                                                // \todo this only makes sense if prev is on the same bline
                                                                printf("this is index %d\n", value_desc.get_index());
                                                                printf("prev is index %d\n", value_prev.get_index());
@@ -1676,7 +1693,6 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                                                        if (value_desc.get_index()<LinkableValueNode::Handle::cast_static(value_desc.get_parent_value_node())->link_count()-1)
                                                                        {
                                                                                printf("in two - b\n");
-                                                                               // DEBUGPOINT();
                                                                                printf("inserting node with index %d\n", value_desc.get_index()+1);
                                                                                vertex_list.insert(next,
                                                                                                                   synfigapp::ValueDesc(value_desc.get_parent_value_node(),
@@ -1691,7 +1707,6 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                                                        if(value_desc.get_index()>0)
                                                                        {
                                                                                printf("in two - a\n");
-                                                                               // DEBUGPOINT();
                                                                                printf("inserting node on this line with index %d\n",
                                                                                           value_desc.get_index()-1);
                                                                                vertex_list.insert(next,
@@ -1931,7 +1946,7 @@ StateDraw_Context::reverse_bline(std::list<synfig::BLinePoint> &bline)
 }
 
 void
-StateDraw_Context::fill_last_stroke(int depth_offset)
+StateDraw_Context::fill_last_stroke_and_unselect_other_layers()
 {
        if(!last_stroke)
                return;
@@ -1950,10 +1965,11 @@ StateDraw_Context::fill_last_stroke(int depth_offset)
        layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
        if(layer)
        {
-               depth=layer->get_depth() + depth_offset;
+               depth=layer->get_depth();
                canvas=layer->get_canvas();
        }
 
+       get_canvas_interface()->get_selection_manager()->clear_selected_layers();
        layer=get_canvas_interface()->add_layer_to("region", canvas, depth);
        assert(layer);
        layer->set_param("color",synfigapp::Main::get_background_color());
@@ -1979,3 +1995,14 @@ StateDraw_Context::fill_last_stroke(int depth_offset)
        }
        get_canvas_view()->get_selection_manager()->set_selected_layer(layer);
 }
+
+void
+StateDraw_Context::fill_last_stroke()
+{
+       if(!last_stroke)
+               return;
+
+       synfigapp::SelectionManager::LayerList layer_list = get_canvas_view()->get_selection_manager()->get_selected_layers();
+       fill_last_stroke_and_unselect_other_layers();
+       get_canvas_view()->get_selection_manager()->set_selected_layers(layer_list);
+}