Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_bline.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_bline.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/entry.h>
35
36 #include <synfig/valuenode_dynamiclist.h>
37
38 #include "state_bline.h"
39 #include "canvasview.h"
40 #include "workarea.h"
41 #include "app.h"
42 #include <synfig/valuenode_bline.h>
43 #include <ETL/hermite>
44 #include <ETL/calculus>
45 #include <utility>
46 #include "event_mouse.h"
47 #include "event_layerclick.h"
48 #include "toolbox.h"
49 #include "dialog_tooloptions.h"
50 #include <gtkmm/spinbutton.h>
51 #include <synfig/transform.h>
52 #include <synfigapp/main.h>
53
54 #endif
55
56 /* === U S I N G =========================================================== */
57
58 using namespace std;
59 using namespace etl;
60 using namespace synfig;
61 using namespace studio;
62
63 /* === M A C R O S ========================================================= */
64
65 /* === G L O B A L S ======================================================= */
66
67 StateBLine studio::state_bline;
68
69 /* === C L A S S E S & S T R U C T S ======================================= */
70
71 class studio::StateBLine_Context : public sigc::trackable
72 {
73         etl::handle<CanvasView> canvas_view_;
74         CanvasView::IsWorking is_working;
75
76         bool prev_table_status;
77         bool loop_;
78         bool prev_workarea_layer_status_;
79
80         int depth;
81         Canvas::Handle canvas;
82
83         Gtk::Menu menu;
84
85         Duckmatic::Push duckmatic_push;
86
87         etl::handle<Duck> curr_duck;
88
89         etl::handle<Duck> next_duck;
90
91         std::list<synfig::ValueNode_Const::Handle> bline_point_list;
92         synfigapp::Settings& settings;
93
94         bool on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
95         bool on_tangent1_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
96         bool on_tangent2_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
97
98
99         void popup_handle_menu(synfig::ValueNode_Const::Handle value_node);
100         void popup_vertex_menu(synfig::ValueNode_Const::Handle value_node);
101         void popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node);
102
103         void bline_detach_handle(synfig::ValueNode_Const::Handle value_node);
104         void bline_attach_handle(synfig::ValueNode_Const::Handle value_node);
105         void bline_delete_vertex(synfig::ValueNode_Const::Handle value_node);
106         void bline_insert_vertex(synfig::ValueNode_Const::Handle value_node,float origin=0.5);
107         void loop_bline();
108         void unloop_bline();
109
110         void refresh_ducks(bool x=true);
111
112         Gtk::Table options_table;
113         Gtk::Entry entry_id;
114         Gtk::CheckButton checkbutton_layer_region;
115         Gtk::CheckButton checkbutton_layer_bline;
116         Gtk::CheckButton checkbutton_layer_curve_gradient;
117         Gtk::CheckButton checkbutton_layer_link_offsets;
118         Gtk::CheckButton checkbutton_auto_export;
119         Gtk::Button button_make;
120         Gtk::Button button_clear;
121         Gtk::Adjustment  adj_feather;
122         Gtk::SpinButton  spin_feather;
123
124
125
126 public:
127
128         int layers_to_create()const
129         {
130                 return
131                         get_layer_region_flag()+
132                         get_layer_bline_flag()+
133                         get_layer_curve_gradient_flag();
134         }
135
136         void sanity_check()
137         {
138                 if(layers_to_create()==0)
139                         set_layer_region_flag(true);
140         }
141
142         bool get_auto_export_flag()const { return checkbutton_auto_export.get_active(); }
143         void set_auto_export_flag(bool x) { return checkbutton_auto_export.set_active(x); }
144
145         bool get_layer_region_flag()const { return checkbutton_layer_region.get_active(); }
146         void set_layer_region_flag(bool x) { return checkbutton_layer_region.set_active(x); }
147
148         bool get_layer_bline_flag()const { return checkbutton_layer_bline.get_active(); }
149         void set_layer_bline_flag(bool x) { return checkbutton_layer_bline.set_active(x); }
150
151         bool get_layer_curve_gradient_flag()const { return checkbutton_layer_curve_gradient.get_active(); }
152         void set_layer_curve_gradient_flag(bool x) { return checkbutton_layer_curve_gradient.set_active(x); }
153
154         bool get_layer_link_offsets_flag()const { return checkbutton_layer_link_offsets.get_active(); }
155         void set_layer_link_offsets_flag(bool x) { return checkbutton_layer_link_offsets.set_active(x); }
156
157         Real get_feather() const { return adj_feather.get_value(); }
158         void set_feather(Real x) { return adj_feather.set_value(x); }
159         synfig::String get_id()const { return entry_id.get_text(); }
160         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
161
162         Smach::event_result event_stop_handler(const Smach::event& x);
163
164         Smach::event_result event_refresh_handler(const Smach::event& x);
165
166         Smach::event_result event_mouse_click_handler(const Smach::event& x);
167         Smach::event_result event_mouse_release_handler(const Smach::event& x);
168         Smach::event_result event_mouse_motion_handler(const Smach::event& x);
169         Smach::event_result event_refresh_tool_options(const Smach::event& x);
170
171         Smach::event_result event_hijack(const Smach::event& /*x*/) { return Smach::RESULT_ACCEPT; }
172
173         void refresh_tool_options();
174
175         StateBLine_Context(CanvasView* canvas_view);
176
177         ~StateBLine_Context();
178
179         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
180         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
181         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
182         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
183         const synfig::TransformStack& get_transform_stack()const { return canvas_view_->get_curr_transform_stack(); }
184
185         void load_settings();
186         void save_settings();
187         void reset();
188         void increment_id();
189         //void on_user_click(synfig::Point point);
190
191         bool run_();
192         bool run();
193
194         bool egress_on_selection_change;
195         Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
196         {
197                 if(egress_on_selection_change)
198                         throw Smach::egress_exception();
199                 return Smach::RESULT_OK;
200         }
201
202 };      // END of class StateBLine_Context
203
204
205 /* === M E T H O D S ======================================================= */
206
207 StateBLine::StateBLine():
208         Smach::state<StateBLine_Context>("bline")
209 {
210         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateBLine_Context::event_layer_selection_changed_handler));
211         insert(event_def(EVENT_STOP,&StateBLine_Context::event_stop_handler));
212         insert(event_def(EVENT_REFRESH,&StateBLine_Context::event_refresh_handler));
213         insert(event_def(EVENT_REFRESH_DUCKS,&StateBLine_Context::event_hijack));
214         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateBLine_Context::event_mouse_click_handler));
215         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateBLine_Context::event_mouse_release_handler));
216         insert(event_def(EVENT_WORKAREA_MOUSE_MOTION,&StateBLine_Context::event_mouse_motion_handler));
217         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateBLine_Context::event_mouse_motion_handler));
218         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateBLine_Context::event_refresh_tool_options));
219 }
220
221 StateBLine::~StateBLine()
222 {
223 }
224
225 void
226 StateBLine_Context::load_settings()
227 {
228         String value;
229
230         if(settings.get_value("bline.layer_region",value) && value=="0")
231                 set_layer_region_flag(false);
232         else
233                 set_layer_region_flag(true);
234
235         if(settings.get_value("bline.layer_bline",value) && value=="0")
236                 set_layer_bline_flag(false);
237         else
238                 set_layer_bline_flag(true);
239
240         if(settings.get_value("bline.layer_curve_gradient",value) && value=="1")
241                 set_layer_curve_gradient_flag(true);
242         else
243                 set_layer_curve_gradient_flag(false);
244
245         if(settings.get_value("bline.layer_link_offsets",value) && value=="0")
246                 set_layer_link_offsets_flag(false);
247         else
248                 set_layer_link_offsets_flag(true);
249
250         if(settings.get_value("bline.auto_export",value) && value=="1")
251                 set_auto_export_flag(true);
252         else
253                 set_auto_export_flag(false);
254
255         if(settings.get_value("bline.id",value))
256                 set_id(value);
257         else
258                 set_id("NewBLine");
259
260         if(settings.get_value("bline.feather",value))
261         {
262                 Real n = atof(value.c_str());
263                 set_feather(n);
264         }
265
266         sanity_check();
267 }
268
269 void
270 StateBLine_Context::save_settings()
271 {
272         sanity_check();
273         settings.set_value("bline.layer_bline",get_layer_bline_flag()?"1":"0");
274         settings.set_value("bline.layer_region",get_layer_region_flag()?"1":"0");
275         settings.set_value("bline.layer_curve_gradient",get_layer_curve_gradient_flag()?"1":"0");
276         settings.set_value("bline.layer_link_offsets",get_layer_link_offsets_flag()?"1":"0");
277         settings.set_value("bline.auto_export",get_auto_export_flag()?"1":"0");
278         settings.set_value("bline.id",get_id().c_str());
279         settings.set_value("bline.feather",strprintf("%f",get_feather()));
280 }
281
282 void
283 StateBLine_Context::reset()
284 {
285         loop_=false;
286         bline_point_list.clear();
287         refresh_ducks();
288 }
289
290 void
291 StateBLine_Context::increment_id()
292 {
293         String id(get_id());
294         int number=1;
295         int digits=0;
296
297         if(id.empty())
298                 id="NewBLine";
299
300         // If there is a number
301         // already at the end of the
302         // id, then remove it.
303         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
304         {
305                 // figure out how many digits it is
306                 for(digits=0;(int)id.size()-1>=digits && id[id.size()-1-digits]<='9' && id[id.size()-1-digits]>='0';digits++)while(false);
307
308                 String str_number;
309                 str_number=String(id,id.size()-digits,id.size());
310                 id=String(id,0,id.size()-digits);
311                 synfig::info("---------------- \"%s\"",str_number.c_str());
312
313                 number=atoi(str_number.c_str());
314         }
315         else
316         {
317                 number=1;
318                 digits=3;
319         }
320
321         number++;
322
323         // Add the number back onto the id
324         {
325                 const String format(strprintf("%%0%dd",digits));
326                 id+=strprintf(format.c_str(),number);
327         }
328
329         // Set the ID
330         set_id(id);
331 }
332
333
334 StateBLine_Context::StateBLine_Context(CanvasView* canvas_view):
335         canvas_view_(canvas_view),
336         is_working(*canvas_view),
337         loop_(false),
338         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
339         duckmatic_push(get_work_area()),
340         settings(synfigapp::Main::get_selected_input_device()->settings()),
341         entry_id(),
342         checkbutton_layer_region(_("Fill")),
343         checkbutton_layer_bline(_("Outline")),
344         checkbutton_layer_curve_gradient(_("Gradient")),
345         checkbutton_layer_link_offsets(_("Link Offsets")),
346         checkbutton_auto_export(_("Auto Export")),
347         button_make(_("Make")),
348         button_clear(_("Clear")),
349         adj_feather(0,0,10000,0.01,0.1),
350         spin_feather(adj_feather,0.01,4)
351 {
352         depth=-1;
353         egress_on_selection_change=true;
354         load_settings();
355
356         // Set up the tool options dialog
357         //options_table.attach(*manage(new Gtk::Label(_("BLine Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
358         options_table.attach(entry_id,                                                          0, 2,  1,  2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
359         options_table.attach(checkbutton_layer_region,                          0, 2,  2,  3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
360         options_table.attach(checkbutton_layer_bline,                           0, 2,  3,  4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
361         options_table.attach(checkbutton_layer_curve_gradient,          0, 2,  4,  5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
362         options_table.attach(checkbutton_layer_link_offsets,            0, 2,  5,  6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
363         options_table.attach(checkbutton_auto_export,                           0, 2,  6,  7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
364         options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
365         options_table.attach(spin_feather,                                                      1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
366         //options_table.attach(button_make, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
367         //button_make.signal_pressed().connect(sigc::mem_fun(*this,&StateBLine_Context::run));
368         options_table.show_all();
369         refresh_tool_options();
370         App::dialog_tool_options->present();
371
372         // Turn off layer clicking
373         get_work_area()->set_allow_layer_clicks(false);
374
375         // clear out the ducks
376         get_work_area()->clear_ducks();
377
378         // Refresh the work area
379         get_work_area()->queue_draw();
380
381         // Hide the tables if they are showing
382         prev_table_status=get_canvas_view()->tables_are_visible();
383         if(prev_table_status)get_canvas_view()->hide_tables();
384
385         // Disable the time bar
386         get_canvas_view()->set_sensitive_timebar(false);
387
388         // Connect a signal
389         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateBLine_Context::on_user_click));
390         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
391
392         App::toolbox->refresh();
393 }
394
395 void
396 StateBLine_Context::refresh_tool_options()
397 {
398         App::dialog_tool_options->clear();
399         App::dialog_tool_options->set_widget(options_table);
400         App::dialog_tool_options->set_local_name(_("BLine Tool"));
401         App::dialog_tool_options->set_name("bline");
402
403         App::dialog_tool_options->add_button(
404                 Gtk::StockID("gtk-execute"),
405                 _("Make BLine and/or Region")
406         )->signal_clicked().connect(
407                 sigc::hide_return(sigc::mem_fun(
408                         *this,
409                         &StateBLine_Context::run
410                 ))
411         );
412
413         App::dialog_tool_options->add_button(
414                 Gtk::StockID("gtk-clear"),
415                 _("Clear current BLine")
416         )->signal_clicked().connect(
417                 sigc::mem_fun(
418                         *this,
419                         &StateBLine_Context::reset
420                 )
421         );
422 }
423
424 Smach::event_result
425 StateBLine_Context::event_refresh_tool_options(const Smach::event& /*x*/)
426 {
427         refresh_tool_options();
428         return Smach::RESULT_ACCEPT;
429 }
430
431 StateBLine_Context::~StateBLine_Context()
432 {
433         run();
434
435         save_settings();
436         App::dialog_tool_options->clear();
437
438         get_canvas_view()->work_area->reset_cursor();
439
440         // Restore layer clicking
441         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
442
443         // Enable the time bar
444         get_canvas_view()->set_sensitive_timebar(true);
445
446         // Bring back the tables if they were out before
447         if(prev_table_status)get_canvas_view()->show_tables();
448
449 //      get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
450
451         // Refresh the work area
452         get_work_area()->queue_draw();
453
454         App::toolbox->refresh();
455 }
456
457 Smach::event_result
458 StateBLine_Context::event_stop_handler(const Smach::event& /*x*/)
459 {
460         synfig::info("STATE RotoBLine: Received Stop Event");
461 //      run();
462         reset();
463 //      throw Smach::egress_exception();
464 //      get_canvas_view()->get_smach().pop_state();
465         return Smach::RESULT_ACCEPT;
466 }
467
468 Smach::event_result
469 StateBLine_Context::event_refresh_handler(const Smach::event& /*x*/)
470 {
471         synfig::info("STATE RotoBLine: Received Refresh Event");
472         refresh_ducks();
473         return Smach::RESULT_ACCEPT;
474 }
475
476 bool
477 StateBLine_Context::run()
478 {
479         sanity_check();
480
481         String err;
482         bool success(false);
483         for(int i=5;i>0 && !success;i--)try
484         {
485                 success=run_();
486         }
487         catch(String s)
488         {
489                 err=s;
490         }
491         if(!success && !err.empty())
492         {
493                 get_canvas_view()->get_ui_interface()->error(err);
494         }
495         return success;
496 }
497
498 bool
499 StateBLine_Context::run_()
500 {
501         curr_duck=0;
502         next_duck=0;
503
504         // Now we need to generate it
505
506         if(bline_point_list.empty())
507         {
508                 return false;
509         }
510         if(bline_point_list.size()<2)
511         {
512                 //get_canvas_view()->get_ui_interface()->error(_("You need at least two (2) points to create a BLine"));
513                 return false;
514         }
515
516         do
517         {
518
519                 // Create the action group
520                 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New BLine"));
521
522                 std::vector<BLinePoint> new_list;
523                 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
524                 const synfig::TransformStack& transform(get_transform_stack());
525
526                 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
527                 {
528                         BLinePoint bline_point((*iter)->get_value().get(BLinePoint()));
529                         Point new_vertex(transform.unperform(bline_point.get_vertex()));
530
531                         bline_point.set_tangent1(
532                                 transform.unperform(
533                                         bline_point.get_tangent1()+bline_point.get_vertex()
534                                 ) -new_vertex
535                         );
536
537                         bline_point.set_tangent2(
538                                 transform.unperform(
539                                         bline_point.get_tangent2()+bline_point.get_vertex()
540                                 ) -new_vertex
541                         );
542
543                         bline_point.set_vertex(new_vertex);
544
545                         new_list.push_back(bline_point);
546                 }
547
548                 ValueNode_BLine::Handle value_node_bline(ValueNode_BLine::create(new_list));
549                 assert(value_node_bline);
550
551                 ValueNode_Const::Handle value_node_offset(ValueNode_Const::create(Vector()));
552                 assert(value_node_offset);
553
554                 // Set the looping flag
555                 value_node_bline->set_loop(loop_);
556
557                 // Add the BLine to the canvas
558                 if(get_auto_export_flag() && !get_canvas_interface()->add_value_node(value_node_bline,get_id()))
559                 {
560                         //get_canvas_view()->get_ui_interface()->error(_("Unable to add value node"));
561                         group.cancel();
562                         increment_id();
563                         throw String(_("Unable to add value node"));
564                         return false;
565                 }
566
567                 Layer::Handle layer;
568
569                 // we are temporarily using the layer to hold something
570                 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
571
572                 if(layer)
573                 {
574                         if(depth<0)
575                                 depth=layer->get_depth();
576                         if(!canvas)
577                                 canvas=layer->get_canvas();
578                 }
579                 else
580                         depth=0;
581
582                 if(!canvas)
583                         canvas=get_canvas_view()->get_canvas();
584
585                 synfigapp::SelectionManager::LayerList layer_selection;
586
587                 // If we were asked to create a region layer, go ahead and do so
588                 if(get_layer_region_flag())
589                 {
590                         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
591
592                         Layer::Handle layer(get_canvas_interface()->add_layer_to("region",canvas,depth));
593                         assert(layer);
594                         layer_selection.push_back(layer);
595                         layer->set_description(get_id()+_(" Region"));
596                         get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
597
598                         if(get_feather())
599                         {
600                                 layer->set_param("feather",get_feather());
601                                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
602                         }
603
604                         if(get_layer_bline_flag())
605                                 layer->set_param("color",synfigapp::Main::get_background_color());
606
607                         // I don't know if it's safe to reuse the same layer_param_connect action, so I'm
608                         // using 2 separate ones.
609                         {
610                                 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
611                                 assert(action);
612
613                                 action->set_param("canvas",get_canvas());
614                                 action->set_param("canvas_interface",get_canvas_interface());
615                                 action->set_param("layer",layer);
616                                 if(!action->set_param("param",String("bline")))
617                                         synfig::error("LayerParamConnect didn't like \"param\"");
618                                 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
619                                         synfig::error("LayerParamConnect didn't like \"value_node\"");
620
621                                 if(!get_canvas_interface()->get_instance()->perform_action(action))
622                                 {
623                                         //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
624                                         group.cancel();
625                                         throw String(_("Unable to create Region layer"));
626                                         return false;
627                                 }
628                         }
629
630                         if (get_layer_link_offsets_flag())
631                         {
632                                 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
633                                 assert(action);
634
635                                 action->set_param("canvas",get_canvas());
636                                 action->set_param("canvas_interface",get_canvas_interface());
637                                 action->set_param("layer",layer);
638                                 if(!action->set_param("param",String("offset")))
639                                         synfig::error("LayerParamConnect didn't like \"param\"");
640                                 if(!action->set_param("value_node",ValueNode::Handle(value_node_offset)))
641                                         synfig::error("LayerParamConnect didn't like \"value_node\"");
642
643                                 if(!get_canvas_interface()->get_instance()->perform_action(action))
644                                 {
645                                         //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
646                                         group.cancel();
647                                         throw String(_("Unable to create Region layer"));
648                                         return false;
649                                 }
650                         }
651                 }
652
653                 // If we were asked to create a BLine layer, go ahead and do so
654                 if(get_layer_bline_flag())
655                 {
656                         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
657
658                         Layer::Handle layer(get_canvas_interface()->add_layer_to("outline",canvas,depth));
659                         assert(layer);
660                         layer_selection.push_back(layer);
661                         layer->set_description(get_id()+_(" Outline"));
662                         get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
663                         if(get_feather())
664                         {
665                                 layer->set_param("feather",get_feather());
666                                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
667                         }
668
669                         {
670                                 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
671                                 assert(action);
672
673                                 action->set_param("canvas",get_canvas());
674                                 action->set_param("canvas_interface",get_canvas_interface());
675                                 action->set_param("layer",layer);
676                                 if(!action->set_param("param",String("bline")))
677                                         synfig::error("LayerParamConnect didn't like \"param\"");
678                                 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
679                                         synfig::error("LayerParamConnect didn't like \"value_node\"");
680
681                                 if(!get_canvas_interface()->get_instance()->perform_action(action))
682                                 {
683                                         //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
684                                         group.cancel();
685                                         throw String(_("Unable to create Outline layer"));
686                                         return false;
687                                 }
688                         }
689
690                         if (get_layer_link_offsets_flag())
691                         {
692                                 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
693                                 assert(action);
694
695                                 action->set_param("canvas",get_canvas());
696                                 action->set_param("canvas_interface",get_canvas_interface());
697                                 action->set_param("layer",layer);
698                                 if(!action->set_param("param",String("offset")))
699                                         synfig::error("LayerParamConnect didn't like \"param\"");
700                                 if(!action->set_param("value_node",ValueNode::Handle(value_node_offset)))
701                                         synfig::error("LayerParamConnect didn't like \"value_node\"");
702
703                                 if(!get_canvas_interface()->get_instance()->perform_action(action))
704                                 {
705                                         //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
706                                         group.cancel();
707                                         throw String(_("Unable to create Outline layer"));
708                                         return false;
709                                 }
710                         }
711
712                         /*if(get_layer_region_flag() && !get_auto_export_flag())
713                         {
714                                 get_canvas_interface()->auto_export(synfigapp::ValueDesc(layer,"bline"));
715                         }*/
716                 }
717
718
719
720                 // If we were asked to create a CurveGradient layer, go ahead and do so
721                 if(get_layer_curve_gradient_flag())
722                 {
723                         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
724
725                         Layer::Handle layer(get_canvas_interface()->add_layer_to("curve_gradient",canvas,depth));
726                         assert(layer);
727                         layer_selection.push_back(layer);
728                         layer->set_description(get_id()+_(" Gradient"));
729                         get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
730
731                         {
732                                 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
733                                 assert(action);
734
735                                 action->set_param("canvas",get_canvas());
736                                 action->set_param("canvas_interface",get_canvas_interface());
737                                 action->set_param("layer",layer);
738                                 if(!action->set_param("param",String("bline")))
739                                         synfig::error("LayerParamConnect didn't like \"param\"");
740                                 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
741                                         synfig::error("LayerParamConnect didn't like \"value_node\"");
742
743                                 if(!get_canvas_interface()->get_instance()->perform_action(action))
744                                 {
745                                         //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
746                                         group.cancel();
747                                         throw String(_("Unable to create Gradient layer"));
748                                         return false;
749                                 }
750                         }
751
752                         if (get_layer_link_offsets_flag())
753                         {
754                                 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
755                                 assert(action);
756
757                                 action->set_param("canvas",get_canvas());
758                                 action->set_param("canvas_interface",get_canvas_interface());
759                                 action->set_param("layer",layer);
760                                 if(!action->set_param("param",String("offset")))
761                                         synfig::error("LayerParamConnect didn't like \"param\"");
762                                 if(!action->set_param("value_node",ValueNode::Handle(value_node_offset)))
763                                         synfig::error("LayerParamConnect didn't like \"value_node\"");
764
765                                 if(!get_canvas_interface()->get_instance()->perform_action(action))
766                                 {
767                                         //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
768                                         group.cancel();
769                                         throw String(_("Unable to create Gradient layer"));
770                                         return false;
771                                 }
772                         }
773
774                         /*if(get_layer_region_flag() && !get_auto_export_flag())
775                         {
776                                 get_canvas_interface()->auto_export(synfigapp::ValueDesc(layer,"bline"));
777                         }*/
778                 }
779
780                 egress_on_selection_change=false;
781                 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
782                 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
783                 egress_on_selection_change=true;
784
785                 //if(finish_bline_dialog.get_region_flag() || finish_bline_dialog.get_bline_flag())
786                 //      get_canvas_interface()->signal_dirty_preview()();
787
788         } while(0);
789
790         reset();
791         increment_id();
792         return true;
793 }
794
795 Smach::event_result
796 StateBLine_Context::event_mouse_motion_handler(const Smach::event& x)
797 {
798         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
799
800         if(curr_duck)
801         {
802                 //synfig::info("Moved Duck");
803                 Point p(get_work_area()->snap_point_to_grid(event.pos));
804                 curr_duck->set_trans_point(p);
805                 if(next_duck)
806                         next_duck->set_trans_point(p);
807                 get_work_area()->queue_draw();
808                 return Smach::RESULT_ACCEPT;
809         }
810
811         return Smach::RESULT_OK;
812 }
813
814 Smach::event_result
815 StateBLine_Context::event_mouse_release_handler(const Smach::event& /*x*/)
816 {
817         if(curr_duck)
818         {
819                 //synfig::info("Released current duck");
820                 curr_duck->signal_edited()(curr_duck->get_point());
821                 if(next_duck)
822                 {
823                         //synfig::info("grabbing next duck");
824                         curr_duck=next_duck;
825                         next_duck=0;
826                 }
827                 return Smach::RESULT_ACCEPT;
828         }
829         return Smach::RESULT_OK;
830 }
831
832 Smach::event_result
833 StateBLine_Context::event_mouse_click_handler(const Smach::event& x)
834 {
835         synfig::info("STATE BLINE: Received mouse button down Event");
836         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
837         switch(event.button)
838         {
839         case BUTTON_LEFT:
840                 {
841                         // If we are already looped up, then don't try to add anything else
842                         if(loop_)
843                                 return Smach::RESULT_OK;
844
845                         BLinePoint bline_point;
846
847                         bline_point.set_vertex(get_work_area()->snap_point_to_grid(event.pos));
848                         //bline_point.set_width(synfigapp::Main::get_bline_width());
849                         bline_point.set_width(1.0f);
850                         bline_point.set_origin(0.5f);
851                         bline_point.set_split_tangent_flag(false);
852                         bline_point.set_tangent1(Vector(0,0));
853
854                         // set the tangent
855                         /*
856                         if(bline_point_list.empty())
857                         {
858                                 bline_point.set_tangent1(Vector(1,1));
859                         }
860                         else
861                         {
862                                 const Vector t(event.pos-bline_point_list.back()->get_value().get(BLinePoint()).get_vertex());
863                                 bline_point.set_tangent1(t);
864                         }
865
866                         if(bline_point_list.size()>1)
867                         {
868                                 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
869                                 iter=bline_point_list.end();
870                                 iter--;iter--;
871                                 BLinePoint prev(bline_point_list.back()->get_value().get(BLinePoint()));
872                                 prev.set_tangent1(event.pos-(*iter)->get_value().get(BLinePoint()).get_vertex());
873                                 bline_point_list.back()->set_value(prev);
874                         };
875                         */
876
877                         bline_point_list.push_back(ValueNode_Const::create(bline_point));
878
879                         refresh_ducks();
880                         return Smach::RESULT_ACCEPT;
881                 }
882
883         case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
884                 return Smach::RESULT_ACCEPT;
885
886         default:
887                 return Smach::RESULT_OK;
888         }
889 }
890
891 void
892 StateBLine_Context::refresh_ducks(bool button_down)
893 {
894         get_work_area()->clear_ducks();
895         get_work_area()->queue_draw();
896
897         if(bline_point_list.empty())
898                 return;
899
900         list<ValueNode_Const::Handle>::iterator iter;
901
902         handle<WorkArea::Bezier> bezier;
903         handle<WorkArea::Duck> duck,tduck;
904         BLinePoint bline_point;
905
906         for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
907         {
908                 ValueNode_Const::Handle value_node(*iter);
909                 bline_point=(value_node->get_value().get(BLinePoint()));
910                 assert(value_node);
911
912
913                 // First add the duck associated with this vertex
914                 duck=new WorkArea::Duck(bline_point.get_vertex());
915                 duck->set_editable(true);
916                 duck->set_type(Duck::TYPE_VERTEX);
917                 duck->set_name(strprintf("%x-vertex",value_node.get()));
918                 duck->signal_edited().connect(
919                         sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),value_node)
920                 );
921                 duck->signal_user_click(2).connect(
922                         sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),value_node)
923                 );
924                 duck->set_guid(value_node->get_guid()^GUID::hasher(0));
925
926                 get_work_area()->add_duck(duck);
927
928                 // Add the tangent1 duck
929                 tduck=new WorkArea::Duck(bline_point.get_tangent1());
930                 tduck->set_editable(true);
931                 tduck->set_name(strprintf("%x-tangent1",value_node.get()));
932                 tduck->set_origin(duck);
933                 tduck->set_scalar(-0.33333333333333333);
934                 tduck->set_tangent(true);
935                 tduck->set_guid(value_node->get_guid()^GUID::hasher(3));
936                 tduck->signal_edited().connect(
937                         sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
938                 );
939                 tduck->signal_user_click(2).connect(
940                         sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
941                 );
942
943                 // See if we need to add that duck to the previous bezier
944                 if(bezier)
945                 {
946                         get_work_area()->add_duck(tduck);
947                         bezier->p2=duck;
948                         bezier->c2=tduck;
949
950                         bezier->signal_user_click(2).connect(
951                                 sigc::bind(
952                                         sigc::mem_fun(
953                                                 *this,
954                                                 &studio::StateBLine_Context::popup_bezier_menu
955                                         ),
956                                         value_node
957                                 )
958                         );
959
960                         //get_work_area()->add_duck(bezier->c1);
961                         //get_work_area()->add_duck(bezier->c2);
962                         get_work_area()->add_bezier(bezier);
963
964                         bezier=0;
965                 }
966
967                 // Now we see if we need to create a bezier
968                 list<ValueNode_Const::Handle>::iterator next(iter);
969                 next++;
970
971                 // If our next iterator is the end, then we don't need
972                 // to add a bezier.
973                 //if(next==bline_point_list.end() && !loop_)
974                 //      continue;
975
976                 bezier=new WorkArea::Bezier();
977
978                 // Add the tangent2 duck
979                 tduck=new WorkArea::Duck(bline_point.get_tangent2());
980                 tduck->set_editable(true);
981                 tduck->set_origin(duck);
982                 tduck->set_scalar(0.33333333333333333);
983                 tduck->set_tangent(true);
984                 if(bline_point.get_split_tangent_flag())
985                 {
986                         tduck->set_name(strprintf("%x-tangent2",value_node.get()));
987                         tduck->signal_edited().connect(
988                                 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent2_change),value_node)
989                         );
990                 }
991                 else
992                 {
993                         tduck->set_name(strprintf("%x-tangent1",value_node.get()));
994                         tduck->signal_edited().connect(
995                                 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
996                         );
997                 }
998                 tduck->set_guid(value_node->get_guid()^GUID::hasher(4));
999                 tduck->signal_user_click(2).connect(
1000                         sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
1001                 );
1002
1003                 // Setup the next bezier
1004                 bezier->p1=duck;
1005                 bezier->c1=tduck;
1006
1007                 get_work_area()->add_duck(tduck);
1008                 curr_duck=tduck;
1009         }
1010
1011         // Add the loop, if requested
1012         if(bezier && loop_)
1013         {
1014                 curr_duck=0;
1015                 BLinePoint bline_point(bline_point_list.front()->get_value().get(BLinePoint()));
1016
1017                 duck=new WorkArea::Duck(bline_point.get_vertex());
1018                 duck->set_editable(true);
1019                 duck->set_name(strprintf("%x-vertex",bline_point_list.front().get()));
1020                 duck->signal_edited().connect(
1021                         sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),bline_point_list.front())
1022                 );
1023                 duck->signal_user_click(2).connect(
1024                         sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),bline_point_list.front())
1025                 );
1026                 get_work_area()->add_duck(duck);
1027
1028                 // Add the tangent1 duck
1029                 tduck=new WorkArea::Duck(bline_point.get_tangent1());
1030                 tduck->set_editable(true);
1031                 tduck->set_name(strprintf("%x-tangent1",bline_point_list.front().get()));
1032                 tduck->set_origin(duck);
1033                 tduck->set_scalar(-0.33333333333333333);
1034                 tduck->set_tangent(true);
1035                 tduck->signal_edited().connect(
1036                         sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),bline_point_list.front())
1037                 );
1038                 tduck->signal_user_click(2).connect(
1039                         sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),bline_point_list.front())
1040                 );
1041                 get_work_area()->add_duck(tduck);
1042
1043                 bezier->p2=duck;
1044                 bezier->c2=tduck;
1045
1046                 bezier->signal_user_click(2).connect(
1047                         sigc::bind(
1048                                 sigc::mem_fun(
1049                                         *this,
1050                                         &studio::StateBLine_Context::popup_bezier_menu
1051                                 ),
1052                                 bline_point_list.front()
1053                         )
1054                 );
1055
1056                 //get_work_area()->add_duck(bezier->c1);
1057                 get_work_area()->add_bezier(bezier);
1058         }
1059         if(bezier && !loop_)
1060         {
1061                 duck=new WorkArea::Duck(bline_point.get_vertex());
1062                 duck->set_editable(false);
1063                 duck->set_name("temp");
1064
1065                 // Add the tangent1 duck
1066                 tduck=new WorkArea::Duck(Vector(0,0));
1067                 tduck->set_editable(false);
1068                 tduck->set_name("ttemp");
1069                 tduck->set_origin(duck);
1070                 tduck->set_scalar(-0.33333333333333333);
1071
1072                 tduck->set_tangent(true);
1073                 bezier->p2=duck;
1074                 bezier->c2=tduck;
1075
1076                 get_work_area()->add_duck(bezier->p2);
1077                 //get_work_area()->add_duck(bezier->c2);
1078                 get_work_area()->add_bezier(bezier);
1079
1080                 duck->set_guid(GUID());
1081                 tduck->set_guid(GUID());
1082
1083                 next_duck=duck;
1084         }
1085
1086         if(!button_down)
1087         {
1088                 if(curr_duck)
1089                 {
1090                         if(next_duck)
1091                         {
1092                                 curr_duck=next_duck;
1093                                 next_duck=0;
1094                         }
1095                 }
1096         }
1097         get_work_area()->queue_draw();
1098 }
1099
1100
1101 bool
1102 StateBLine_Context::on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1103 {
1104         BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1105         bline_point.set_vertex(point);
1106         value_node->set_value(bline_point);
1107         //refresh_ducks();
1108         return true;
1109 }
1110
1111 bool
1112 StateBLine_Context::on_tangent1_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1113 {
1114         BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1115         bline_point.set_tangent1(point);
1116         value_node->set_value(bline_point);
1117         //refresh_ducks();
1118         return true;
1119 }
1120
1121 bool
1122 StateBLine_Context::on_tangent2_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1123 {
1124         BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1125         bline_point.set_tangent2(point);
1126         value_node->set_value(bline_point);
1127         //refresh_ducks();
1128         return true;
1129 }
1130
1131 void
1132 StateBLine_Context::loop_bline()
1133 {
1134         loop_=true;
1135
1136         refresh_ducks(false);
1137 }
1138
1139 void
1140 StateBLine_Context::unloop_bline()
1141 {
1142         loop_=false;
1143
1144         refresh_ducks(false);
1145 }
1146
1147 void
1148 StateBLine_Context::popup_vertex_menu(synfig::ValueNode_Const::Handle value_node)
1149 {
1150         menu.items().clear();
1151
1152         if(loop_)
1153         {
1154                 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Unloop BLine",
1155                                 sigc::mem_fun(*this,&studio::StateBLine_Context::unloop_bline)
1156                 ));
1157         } else {
1158                 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Loop BLine",
1159                                 sigc::mem_fun(*this,&studio::StateBLine_Context::loop_bline)
1160                 ));
1161         }
1162
1163         menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Delete Vertex",
1164                 sigc::bind(
1165                         sigc::mem_fun(*this,&studio::StateBLine_Context::bline_delete_vertex),
1166                         value_node
1167                 )
1168         ));
1169
1170         menu.popup(0,0);
1171 }
1172
1173 void
1174 StateBLine_Context::popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node)
1175 {
1176         menu.items().clear();
1177
1178         menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Insert Vertex",
1179                 sigc::bind(
1180                         sigc::bind(
1181                                 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_insert_vertex),
1182                                 location
1183                         ),
1184                         value_node
1185                 )
1186         ));
1187
1188         menu.popup(0,0);
1189 }
1190
1191 void
1192 StateBLine_Context::bline_insert_vertex(synfig::ValueNode_Const::Handle value_node, float origin)
1193 {
1194         list<ValueNode_Const::Handle>::iterator iter;
1195
1196         for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1197                 if(*iter==value_node)
1198                 {
1199                         list<ValueNode_Const::Handle>::iterator prev(iter);
1200                         --prev;
1201
1202                         BLinePoint bline_point;
1203
1204                         BLinePoint next_bline_point((*iter)->get_value().get(BLinePoint()));
1205                         BLinePoint prev_bline_point;
1206
1207                         if(iter!=bline_point_list.begin())
1208                         {
1209                                 prev_bline_point=(*prev)->get_value().get(BLinePoint());
1210                         }
1211                         else
1212                         {
1213                                 prev_bline_point.set_vertex(Point(0,0));
1214                                 prev_bline_point.set_width(next_bline_point.get_width());
1215                                 prev_bline_point.set_origin(0.5);
1216                                 prev_bline_point.set_split_tangent_flag(false);
1217                         }
1218
1219                         etl::hermite<Vector> curve(prev_bline_point.get_vertex(),next_bline_point.get_vertex(),prev_bline_point.get_tangent2(),next_bline_point.get_tangent1());
1220                         etl::derivative< etl::hermite<Vector> > deriv(curve);
1221
1222                         bline_point.set_vertex(curve(origin));
1223                         bline_point.set_width((next_bline_point.get_width()-prev_bline_point.get_width())*origin+prev_bline_point.get_width());
1224 #ifdef ETL_FIXED_DERIVATIVE
1225                         bline_point.set_tangent1(deriv(origin)*std::min(1.0f-origin,origin));
1226 #else
1227                         bline_point.set_tangent1(-deriv(origin)*std::min(1.0f-origin,origin));
1228 #endif
1229                         bline_point.set_tangent2(bline_point.get_tangent1());
1230                         bline_point.set_split_tangent_flag(false);
1231                         bline_point.set_origin(origin);
1232
1233 /*
1234                         bline_point.set_vertex((next_bline_point.get_vertex()+prev_bline_point.get_vertex())*0.5);
1235                         bline_point.set_width((next_bline_point.get_width()+prev_bline_point.get_width())*0.5);
1236                         bline_point.set_origin(origin);
1237                         bline_point.set_split_tangent_flag(false);
1238                         bline_point.set_tangent1((next_bline_point.get_vertex()-prev_bline_point.get_vertex())*0.5);
1239 */
1240
1241                         bline_point_list.insert(iter,ValueNode_Const::create(bline_point));
1242                         break;
1243                 }
1244
1245         if(iter==bline_point_list.end())
1246         {
1247                 get_canvas_view()->get_ui_interface()->error("Unable to find where to insert vertex, internal error, please report this bug");
1248         }
1249
1250         refresh_ducks(false);
1251 }
1252
1253 void
1254 StateBLine_Context::bline_delete_vertex(synfig::ValueNode_Const::Handle value_node)
1255 {
1256         list<ValueNode_Const::Handle>::iterator iter;
1257
1258         for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1259                 if(*iter==value_node)
1260                 {
1261                         bline_point_list.erase(iter);
1262                         break;
1263                 }
1264         if(iter==bline_point_list.end())
1265         {
1266                 get_canvas_view()->get_ui_interface()->error("Unable to remove vertex, internal error, please report this bug");
1267         }
1268
1269         refresh_ducks(false);
1270 }
1271
1272 void
1273 StateBLine_Context::popup_handle_menu(synfig::ValueNode_Const::Handle value_node)
1274 {
1275         menu.items().clear();
1276
1277         BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1278
1279         if(bline_point.get_split_tangent_flag())
1280                 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Merge Tangents",
1281                         sigc::bind(
1282                                 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_attach_handle),
1283                                 value_node
1284                         )
1285                 ));
1286         else
1287                 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Split Tangents",
1288                         sigc::bind(
1289                                 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_detach_handle),
1290                                 value_node
1291                         )
1292                 ));
1293
1294         menu.popup(0,0);
1295 }
1296
1297 void
1298 StateBLine_Context::bline_detach_handle(synfig::ValueNode_Const::Handle value_node)
1299 {
1300         BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1301         bline_point.set_split_tangent_flag(true);
1302         bline_point.set_tangent2(bline_point.get_tangent1());
1303         value_node->set_value(bline_point);
1304         refresh_ducks(false);
1305 }
1306
1307 void
1308 StateBLine_Context::bline_attach_handle(synfig::ValueNode_Const::Handle value_node)
1309 {
1310         BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1311         bline_point.set_tangent1((bline_point.get_tangent1()+bline_point.get_tangent2())*0.5);
1312         bline_point.set_split_tangent_flag(false);
1313         value_node->set_value(bline_point);
1314         refresh_ducks(false);
1315 }