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