Simplify the block structure without changing the behaviour.
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_draw.cpp
index 799eb75..e00baa3 100644 (file)
@@ -100,6 +100,7 @@ class studio::StateDraw_Context : public sigc::trackable
        SigC::Connection process_queue_connection;
 
        ValueNode_BLine::Handle last_stroke;
+       synfig::String last_stroke_id;
 
        Gtk::Menu menu;
 
@@ -124,12 +125,15 @@ class studio::StateDraw_Context : public sigc::trackable
        synfigapp::Settings& settings;
 
        Gtk::Table options_table;
+       Gtk::Entry entry_id;
        Gtk::CheckButton checkbutton_pressure_width;
        Gtk::CheckButton checkbutton_round_ends;
        Gtk::CheckButton checkbutton_auto_loop;   // whether to loop new strokes which start and end in the same place
        Gtk::CheckButton checkbutton_auto_extend; // whether to extend existing lines
        Gtk::CheckButton checkbutton_auto_link;   // whether to link new ducks to existing ducks
-       Gtk::CheckButton checkbutton_region_only;
+       Gtk::CheckButton checkbutton_region;      // whether to create regions
+       Gtk::CheckButton checkbutton_outline;     // whether to create outlines
+       Gtk::CheckButton checkbutton_auto_export;
        Gtk::Button button_fill_last_stroke;
 
        //pressure spinner and such
@@ -151,6 +155,9 @@ class studio::StateDraw_Context : public sigc::trackable
        synfigapp::BLineConverter blineconv;
 
 public:
+       synfig::String get_id()const { return entry_id.get_text(); }
+       void set_id(const synfig::String& x) { return entry_id.set_text(x); }
+
        bool get_pressure_width_flag()const { return checkbutton_pressure_width.get_active(); }
        void set_pressure_width_flag(bool x) { return checkbutton_pressure_width.set_active(x); }
 
@@ -163,8 +170,14 @@ public:
        bool get_auto_link_flag()const { return checkbutton_auto_link.get_active(); }
        void set_auto_link_flag(bool x) { return checkbutton_auto_link.set_active(x); }
 
-       bool get_region_only_flag()const { return checkbutton_region_only.get_active(); }
-       void set_region_only_flag(bool x) { return checkbutton_region_only.set_active(x); }
+       bool get_region_flag()const { return checkbutton_region.get_active(); }
+       void set_region_flag(bool x) { return checkbutton_region.set_active(x); }
+
+       bool get_outline_flag()const { return checkbutton_outline.get_active(); }
+       void set_outline_flag(bool x) { return checkbutton_outline.set_active(x); }
+
+       bool get_auto_export_flag()const { return checkbutton_auto_export.get_active(); }
+       void set_auto_export_flag(bool x) { return checkbutton_auto_export.set_active(x); }
 
        Real get_min_pressure() const { return adj_min_pressure.get_value(); }
        void set_min_pressure(Real x) { return adj_min_pressure.set_value(x); }
@@ -186,6 +199,7 @@ public:
 
        void load_settings();
        void save_settings();
+       void increment_id();
 
        Smach::event_result event_stop_handler(const Smach::event& x);
 
@@ -240,6 +254,11 @@ StateDraw_Context::load_settings()
 {
        String value;
 
+       if(settings.get_value("draw.id",value))
+               set_id(value);
+       else
+               set_id("NewDrawing");
+
        if(settings.get_value("draw.pressure_width",value) && value=="0")
                set_pressure_width_flag(false);
        else
@@ -260,10 +279,20 @@ StateDraw_Context::load_settings()
        else
                set_auto_link_flag(true);
 
-       if(settings.get_value("draw.region_only",value) && value=="1")
-               set_region_only_flag(true);
+       if(settings.get_value("draw.region",value) && value=="0")
+               set_region_flag(false);
+       else
+               set_region_flag(true);
+
+       if(settings.get_value("draw.outline",value) && value=="0")
+               set_outline_flag(false);
+       else
+               set_outline_flag(true);
+
+       if(settings.get_value("draw.auto_export",value) && value=="1")
+               set_auto_export_flag(true);
        else
-               set_region_only_flag(false);
+               set_auto_export_flag(false);
 
        if(settings.get_value("draw.min_pressure_on",value) && value=="0")
                set_min_pressure_flag(false);
@@ -305,11 +334,14 @@ StateDraw_Context::load_settings()
 void
 StateDraw_Context::save_settings()
 {
+       settings.set_value("draw.id",get_id().c_str());
        settings.set_value("draw.pressure_width",get_pressure_width_flag()?"1":"0");
        settings.set_value("draw.auto_loop",get_auto_loop_flag()?"1":"0");
        settings.set_value("draw.auto_extend",get_auto_extend_flag()?"1":"0");
        settings.set_value("draw.auto_link",get_auto_link_flag()?"1":"0");
-       settings.set_value("draw.region_only",get_region_only_flag()?"1":"0");
+       settings.set_value("draw.region",get_region_flag()?"1":"0");
+       settings.set_value("draw.outline",get_outline_flag()?"1":"0");
+       settings.set_value("draw.auto_export",get_auto_export_flag()?"1":"0");
        settings.set_value("draw.min_pressure",strprintf("%f",get_min_pressure()));
        settings.set_value("draw.feather",strprintf("%f",get_feather()));
        settings.set_value("draw.min_pressure_on",get_min_pressure_flag()?"1":"0");
@@ -318,17 +350,63 @@ StateDraw_Context::save_settings()
        settings.set_value("draw.localize",get_local_error_flag()?"1":"0");
 }
 
+void
+StateDraw_Context::increment_id()
+{
+       String id(get_id());
+       int number=1;
+       int digits=0;
+
+       if(id.empty())
+               id="Drawing";
+
+       // If there is a number
+       // already at the end of the
+       // id, then remove it.
+       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);
+
+               String str_number;
+               str_number=String(id,id.size()-digits,id.size());
+               id=String(id,0,id.size()-digits);
+               synfig::info("---------------- \"%s\"",str_number.c_str());
+
+               number=atoi(str_number.c_str());
+       }
+       else
+       {
+               number=1;
+               digits=3;
+       }
+
+       number++;
+
+       // Add the number back onto the id
+       {
+               const String format(strprintf("%%0%dd",digits));
+               id+=strprintf(format.c_str(),number);
+       }
+
+       // Set the ID
+       set_id(id);
+}
+
 StateDraw_Context::StateDraw_Context(CanvasView* canvas_view):
        canvas_view_(canvas_view),
        is_working(*canvas_view),
        loop_(false),
-       prev_workarea_layer_status_(get_work_area()->allow_layer_clicks),
+       prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
        settings(synfigapp::Main::get_selected_input_device()->settings()),
+       entry_id(),
        checkbutton_pressure_width(_("Pressure Width")),
        checkbutton_auto_loop(_("Auto Loop")),
        checkbutton_auto_extend(_("Auto Extend")),
        checkbutton_auto_link(_("Auto Link")),
-       checkbutton_region_only(_("Create Region Only")),
+       checkbutton_region(_("Create Region")),
+       checkbutton_outline(_("Create Outline")),
+       checkbutton_auto_export(_("Auto Export")),
        button_fill_last_stroke(_("Fill Last Stroke")),
        adj_min_pressure(0,0,1,0.01,0.1),
        spin_min_pressure(adj_min_pressure,0.1,3),
@@ -349,23 +427,26 @@ StateDraw_Context::StateDraw_Context(CanvasView* canvas_view):
        UpdateErrorBox();
 
        //options_table.attach(*manage(new Gtk::Label(_("Draw Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
-       options_table.attach(checkbutton_pressure_width, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
-       options_table.attach(checkbutton_auto_loop, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
-       options_table.attach(checkbutton_auto_extend, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
-       options_table.attach(checkbutton_auto_link, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
-       options_table.attach(checkbutton_region_only, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(entry_id,                                                          0, 2,  1,  2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(checkbutton_region,                                        0, 2,  2,  3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(checkbutton_outline,                                       0, 2,  3,  4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(checkbutton_auto_loop,                                     0, 2,  4,  5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(checkbutton_auto_extend,                           0, 2,  5,  6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(checkbutton_auto_link,                                     0, 2,  6,  7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(checkbutton_auto_export,                           0, 2,  7,  8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(checkbutton_pressure_width,                        0, 2,  8,  9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(check_localerror,                                          0, 2,  9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
 
-       options_table.attach(check_min_pressure, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
-       options_table.attach(spin_min_pressure, 0, 2, 7, 8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(check_min_pressure,                                        0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(spin_min_pressure,                                         1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
 
-       options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 8, 9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
-       options_table.attach(spin_feather, 1, 2, 8, 9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(*manage(new Gtk::Label(_("Smooth"))),      0, 1, 11, 12, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(spin_globalthres,                                          1, 2, 11, 12, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
 
-       options_table.attach(check_localerror, 0, 2, 9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
-       options_table.attach(*manage(new Gtk::Label(_("Smooth"))), 0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
-       options_table.attach(spin_globalthres, 1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 12, 13, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       options_table.attach(spin_feather,                                                      1, 2, 12, 13, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
 
-       //options_table.attach(button_fill_last_stroke, 0, 2, 11, 12, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
+       //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::mem_fun(*this,&StateDraw_Context::fill_last_stroke));
        check_localerror.signal_toggled().connect(sigc::mem_fun(*this,&StateDraw_Context::UpdateErrorBox));
@@ -380,10 +461,10 @@ StateDraw_Context::StateDraw_Context(CanvasView* canvas_view):
        get_work_area()->set_type_mask(Duck::TYPE_ALL-Duck::TYPE_TANGENT-Duck::TYPE_WIDTH);
 
        // Turn off layer clicking
-       get_work_area()->allow_layer_clicks=false;
+       get_work_area()->set_allow_layer_clicks(false);
 
        // Turn off duck clicking
-       get_work_area()->allow_duck_clicks=false;
+       get_work_area()->set_allow_duck_clicks(false);
 
        // clear out the ducks
        //get_work_area()->clear_ducks();
@@ -447,7 +528,7 @@ StateDraw_Context::refresh_tool_options()
 }
 
 Smach::event_result
-StateDraw_Context::event_refresh_tool_options(const Smach::event& x)
+StateDraw_Context::event_refresh_tool_options(const Smach::event& /*x*/)
 {
        refresh_tool_options();
        return Smach::RESULT_ACCEPT;
@@ -464,10 +545,10 @@ StateDraw_Context::~StateDraw_Context()
        get_canvas_view()->work_area->reset_cursor();
 
        // Restore layer clicking
-       get_work_area()->allow_layer_clicks=prev_workarea_layer_status_;
+       get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
 
        // Restore duck clicking
-       get_work_area()->allow_duck_clicks=true;
+       get_work_area()->set_allow_duck_clicks(true);
 
        // Enable the time bar
        get_canvas_view()->set_sensitive_timebar(true);
@@ -482,13 +563,13 @@ StateDraw_Context::~StateDraw_Context()
 }
 
 Smach::event_result
-StateDraw_Context::event_stop_handler(const Smach::event& x)
+StateDraw_Context::event_stop_handler(const Smach::event& /*x*/)
 {
        throw Smach::egress_exception();
 }
 
 Smach::event_result
-StateDraw_Context::event_refresh_handler(const Smach::event& x)
+StateDraw_Context::event_refresh_handler(const Smach::event& /*x*/)
 {
        refresh_ducks();
        return Smach::RESULT_ACCEPT;
@@ -507,7 +588,7 @@ StateDraw_Context::event_mouse_down_handler(const Smach::event& x)
                        return Smach::RESULT_ACCEPT;
                }
 
-       case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
+       case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
                return Smach::RESULT_ACCEPT;
 
        default:
@@ -661,13 +742,13 @@ StateDraw_Context::process_stroke(StrokeData stroke_data, WidthData width_data,
 
                // Add the widths of the two points
                {
-                       Real width(bline.front().get_width()+width);
-                       width=width<=1?width:1;
-                       bline.front().set_width(width);
+                       Real tmp_width(bline.front().get_width()+width);
+                       tmp_width=tmp_width<=1?tmp_width:1;
+                       bline.front().set_width(tmp_width);
                }
        }
 
-       // If the bline only has once blinepoint, then there is nothing to do.
+       // If the bline only has one blinepoint, then there is nothing to do.
        if(bline.size()<=1)
                return Smach::RESULT_OK;
 
@@ -690,11 +771,11 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
        bool extend_start=false,extend_finish=false,complete_loop=false;
        bool extend_start_join_same=false,extend_start_join_different=false;
        bool extend_finish_join_same=false,extend_finish_join_different=false;
-       int start_duck_index,finish_duck_index;
+       int start_duck_index = 0,finish_duck_index = 0; // initialized to keep the compiler happy; shouldn't be needed though
        ValueNode_BLine::Handle start_duck_value_node_bline=NULL,finish_duck_value_node_bline=NULL;
 
        // Find any ducks at the start or end that we might attach to
-       // (this used to only run if we aren't a loop - ie. !loop_bline_flag
+       // (this used to only run if we didn't just draw a loop - ie. !loop_bline_flag
        // but having loops auto-connect can be useful as well)
        if(get_auto_extend_flag() || get_auto_link_flag())
        {
@@ -708,14 +789,15 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
                if(start_duck)do
                {
                        if(!(start_duck_value_desc=start_duck->get_value_desc()))break;
-                       if(loop_bline_flag)break;
+                       if(loop_bline_flag)break; // loops don't extend anything
                        if(!start_duck_value_desc.parent_is_value_node())break;
-                       start_duck_index=start_duck_value_desc.get_index();
+                       start_duck_index=start_duck_value_desc.get_index(); // which point on the line did we start drawing at
                        start_duck_value_node_bline=ValueNode_BLine::Handle::cast_dynamic(start_duck_value_desc.get_parent_value_node());
                        if(!get_auto_extend_flag())break;
 
                        // don't extend looped blines
                        if(start_duck_value_node_bline&&!start_duck_value_node_bline->get_loop()&&
+                          // did we start drawing at either end of the line?
                           (start_duck_index==0||start_duck_index==start_duck_value_node_bline->link_count()-1))
                        {
                                extend_start=true;
@@ -841,11 +923,11 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
 
        Smach::event_result result;
        synfig::ValueNode_DynamicList::ListEntry source;
-       int target_index;
 
        // the new line's start extends an existing line
        if(extend_start)
        {
+               int target_offset = 0;
                if(complete_loop)trans_bline.pop_back();
                trans_bline.pop_front();
                if(start_duck_index==0)
@@ -853,13 +935,12 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
                        reverse_bline(trans_bline);
                        result=extend_bline_from_begin(start_duck_value_node_bline,trans_bline,complete_loop);
                        source=start_duck_value_node_bline->list.front();
-                       target_index=trans_bline.size()+finish_duck_index;
+                       target_offset=trans_bline.size();
                }
                else
                {
                        result=extend_bline_from_end(start_duck_value_node_bline,trans_bline,complete_loop);
                        source=start_duck_value_node_bline->list.back();
-                       target_index=finish_duck_index;
                }
 
                if(extend_start_join_different)
@@ -868,26 +949,26 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
                else if(extend_start_join_same)
                        LinkableValueNode::Handle::cast_dynamic(source.value_node)->
                                set_link(0,synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(start_duck_value_node_bline->
-                                                                                                       list[target_index].value_node),0).get_value_node());
+                                                                                                       list[target_offset+finish_duck_index].value_node),0).get_value_node());
                return result;
        }
 
        // the new line's end extends an existing line
        if(extend_finish)
-       {       // SPECIAL CASE -- EXTENSION
+       {
+               int target_offset = 0;
                trans_bline.pop_back();
                if(finish_duck_index==0)
                {
                        result=extend_bline_from_begin(finish_duck_value_node_bline,trans_bline,false);
                        source=finish_duck_value_node_bline->list.front();
-                       target_index=trans_bline.size()+start_duck_index;
+                       target_offset=trans_bline.size();
                }
                else
                {       // We need to reverse the BLine first.
                        reverse_bline(trans_bline);
                        result=extend_bline_from_end(finish_duck_value_node_bline,trans_bline,false);
                        source=finish_duck_value_node_bline->list.back();
-                       target_index=start_duck_index;
                }
 
                if(extend_finish_join_different)
@@ -896,7 +977,7 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
                else if(extend_finish_join_same)
                        LinkableValueNode::Handle::cast_dynamic(source.value_node)->
                                set_link(0,synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(finish_duck_value_node_bline->
-                                                                                                       list[target_index].value_node),0).get_value_node());
+                                                                                                       list[target_offset+start_duck_index].value_node),0).get_value_node());
                return result;
        }
 
@@ -908,6 +989,18 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
                LinkableValueNode::Handle::cast_dynamic(value_node->list.back().value_node)->
                  set_link(0,finish_duck_value_desc.get_value_node());
 
+       if(get_auto_export_flag()) {
+               printf("this is where we would export the new line\n");
+               if (!get_canvas_interface()->add_value_node(value_node,get_id()))
+               {
+                       /* it's no big deal, is it?  let's keep the shape anyway */
+                       // get_canvas_view()->get_ui_interface()->error(_("Unable to add value node"));
+                       // group.cancel();
+                       // increment_id();
+                       // return Smach::RESULT_ERROR;
+               }
+       }
+
        // Create the layer
        {
                Layer::Handle layer;
@@ -926,10 +1019,17 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
 
                synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
 
-               if(get_region_only_flag())
-                       layer=get_canvas_interface()->add_layer_to("region",canvas,depth);
-               else
+               // if they're both defined, we'll add the region later
+               if(get_outline_flag())
+               {
                        layer=get_canvas_interface()->add_layer_to("outline",canvas,depth);
+                       layer->set_description(get_id()+_(" Outline"));
+               }
+               else
+               {
+                       layer=get_canvas_interface()->add_layer_to("region",canvas,depth);
+                       layer->set_description(get_id()+_(" Region"));
+               }
 
                if(get_feather())
                {
@@ -960,6 +1060,7 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
                {
                        get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
                        group.cancel();
+                       increment_id();
                        //refresh_ducks();
                        return Smach::RESULT_ERROR;
                }
@@ -968,6 +1069,12 @@ StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline
        }
 
        last_stroke=value_node;
+       last_stroke_id=get_id();
+
+       if(get_outline_flag() && get_region_flag())
+               fill_last_stroke();
+
+       increment_id();
        return Smach::RESULT_ACCEPT;
 }
 
@@ -1079,8 +1186,8 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                        */
 
                        // Remove duplicate vertices
-                       if(value_prev.get_value_node()==value_desc.get_value_node()
-                               || value_desc.get_value_node()==value_next.get_value_node())
+                       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);
@@ -1095,84 +1202,83 @@ StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real
                                break;
                        }
 
-                       if(value_desc.parent_is_value_node() && value_next.parent_is_value_node())
-                       if(value_desc.get_parent_value_node()==value_next.get_parent_value_node() && (next!=vertex_list.end()))
-                       {
-                               // Fill in missing vertices
-                               if(value_desc.get_index()<value_next.get_index()-1)
-                               {
-                                       DEBUGPOINT();
-                                       vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()+1));
-                                       done=false;
-                                       break;
-                               }
-                               if(value_next.get_index()<value_desc.get_index()-1)
-                               {
-                                       DEBUGPOINT();
-                                       vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_next.get_index()+1));
-                                       done=false;
-                                       break;
-                               }
-                       }
-
-                       // Ensure that connections
-                       // between blines are properly
-                       // connected
-                       if(value_desc.parent_is_value_node() && value_next.parent_is_value_node())
-                       if(value_desc.get_parent_value_node()!=value_next.get_parent_value_node() &&
-                               value_desc.get_value_node()!=value_next.get_value_node())
+                       if (value_desc.parent_is_value_node() && value_next.parent_is_value_node())
                        {
-                               BLinePoint vertex(value_desc.get_value(get_time()).get(BLinePoint()));
-                               BLinePoint vertex_next(value_next.get_value(get_time()).get(BLinePoint()));
-
-                               //synfig::info("--------");
-                               //synfig::info(__FILE__":%d: vertex: [%f, %f]",__LINE__,vertex.get_vertex()[0],vertex.get_vertex()[1]);
-                               //synfig::info(__FILE__":%d: vertex_next: [%f, %f]",__LINE__,vertex_next.get_vertex()[0],vertex_next.get_vertex()[1]);
-
-                               if((vertex.get_vertex()-vertex_next.get_vertex()).mag_squared()<radius*radius)
+                               if (value_desc.get_parent_value_node() == value_next.get_parent_value_node())
                                {
-                                       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());
-                                       value_node_next=ValueNode_Composite::Handle::cast_dynamic(value_next.get_value_node().clone());
-                                       if(!value_node || !value_node_next)
+                                       if (next != vertex_list.end())
                                        {
-                                               synfig::info(__FILE__":%d: Unable to properly connect blines.",__LINE__);
-                                               continue;
+                                               // Fill in missing vertices
+                                               if(value_desc.get_index()<value_next.get_index()-1)
+                                               {
+                                                       DEBUGPOINT();
+                                                       vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()+1));
+                                                       done=false;
+                                                       break;
+                                               }
+                                               if(value_next.get_index()<value_desc.get_index()-1)
+                                               {
+                                                       DEBUGPOINT();
+                                                       vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_next.get_index()+1));
+                                                       done=false;
+                                                       break;
+                                               }
                                        }
-                                       DEBUGPOINT();
-                                       value_node->set_link(5,value_node_next->get_link(5));
-                                       value_node->set_link(3,ValueNode_Const::create(true));
-
-                                       get_canvas_interface()->auto_export(value_node);
-                                       assert(value_node->is_exported());
-                                       *iter=synfigapp::ValueDesc(get_canvas(),value_node->get_id());
-                                       vertex_list.erase(next);
-                                       done=false;
-                                       break;
                                }
-                               else
+                               // Ensure that connections between blines are properly connected
+                               else if (value_desc.get_value_node() != value_next.get_value_node())
                                {
-                                       DEBUGPOINT();
-                                       bool positive_trend(value_desc.get_index()>value_prev.get_index());
+                                       BLinePoint vertex(value_desc.get_value(get_time()).get(BLinePoint()));
+                                       BLinePoint vertex_next(value_next.get_value(get_time()).get(BLinePoint()));
+
+                                       //synfig::info("--------");
+                                       //synfig::info(__FILE__":%d: vertex: [%f, %f]",__LINE__,vertex.get_vertex()[0],vertex.get_vertex()[1]);
+                                       //synfig::info(__FILE__":%d: vertex_next: [%f, %f]",__LINE__,vertex_next.get_vertex()[0],vertex_next.get_vertex()[1]);
 
-                                       if(!positive_trend && value_desc.get_index()>0)
+                                       if((vertex.get_vertex()-vertex_next.get_vertex()).mag_squared()<radius*radius)
                                        {
                                                DEBUGPOINT();
-                                               vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()-1));
+                                               ValueNode_Composite::Handle value_node;
+                                               ValueNode_Composite::Handle value_node_next;
+                                               value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node().clone());
+                                               value_node_next=ValueNode_Composite::Handle::cast_dynamic(value_next.get_value_node().clone());
+                                               if(!value_node || !value_node_next)
+                                               {
+                                                       synfig::info(__FILE__":%d: Unable to properly connect blines.",__LINE__);
+                                                       continue;
+                                               }
+                                               DEBUGPOINT();
+                                               value_node->set_link(5,value_node_next->get_link(5));
+                                               value_node->set_link(3,ValueNode_Const::create(true));
+
+                                               get_canvas_interface()->auto_export(value_node);
+                                               assert(value_node->is_exported());
+                                               *iter=synfigapp::ValueDesc(get_canvas(),value_node->get_id());
+                                               vertex_list.erase(next);
                                                done=false;
                                                break;
                                        }
-                                       if(positive_trend && value_desc.get_index()<LinkableValueNode::Handle::cast_static(value_desc.get_value_node())->link_count()-1)
+                                       else
                                        {
                                                DEBUGPOINT();
-                                               vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()+1));
-                                               done=false;
-                                               break;
+                                               bool positive_trend(value_desc.get_index()>value_prev.get_index());
+
+                                               if(!positive_trend && value_desc.get_index()>0)
+                                               {
+                                                       DEBUGPOINT();
+                                                       vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()-1));
+                                                       done=false;
+                                                       break;
+                                               }
+                                               if(positive_trend && value_desc.get_index()<LinkableValueNode::Handle::cast_static(value_desc.get_value_node())->link_count()-1)
+                                               {
+                                                       DEBUGPOINT();
+                                                       vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()+1));
+                                                       done=false;
+                                                       break;
+                                               }
                                        }
                                }
-
                        }
                }
        }
@@ -1437,6 +1543,7 @@ StateDraw_Context::fill_last_stroke()
        layer=get_canvas_interface()->add_layer("region");
        assert(layer);
        layer->set_param("color",synfigapp::Main::get_background_color());
+       layer->set_description(last_stroke_id + _(" Region"));
 
        synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
 
@@ -1445,7 +1552,7 @@ StateDraw_Context::fill_last_stroke()
        action->set_param("canvas",get_canvas());
        action->set_param("canvas_interface",get_canvas_interface());
        action->set_param("layer",layer);
-       if(!action->set_param("param",String("segment_list")))
+       if(!action->set_param("param",String("bline")))
                synfig::error("LayerParamConnect didn't like \"param\"");
        if(!action->set_param("value_node",ValueNode::Handle(last_stroke)))
                synfig::error("LayerParamConnect didn't like \"value_node\"");