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