5858cc0709d3159f4d4881ec92ff488ee325ba10
[synfig.git] / synfig-studio / src / gui / states / state_rectangle.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_rectangle.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) 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 #include <synfigapp/action_system.h>
38 #include <synfig/valuenode_bline.h>
39
40 #include "state_rectangle.h"
41 #include "state_normal.h"
42 #include "canvasview.h"
43 #include "workarea.h"
44 #include "app.h"
45
46 #include <synfigapp/action.h>
47 #include "event_mouse.h"
48 #include "event_layerclick.h"
49 #include "toolbox.h"
50 #include "docks/dialog_tooloptions.h"
51 #include <gtkmm/optionmenu.h>
52 #include "duck.h"
53 #include <synfigapp/main.h>
54
55 #include "general.h"
56
57 #endif
58
59 /* === U S I N G =========================================================== */
60
61 using namespace std;
62 using namespace etl;
63 using namespace synfig;
64 using namespace studio;
65
66 /* === M A C R O S ========================================================= */
67
68 /* === G L O B A L S ======================================================= */
69
70 StateRectangle studio::state_rectangle;
71
72 /* === C L A S S E S & S T R U C T S ======================================= */
73
74 class studio::StateRectangle_Context : public sigc::trackable
75 {
76         etl::handle<CanvasView> canvas_view_;
77         CanvasView::IsWorking is_working;
78
79         Duckmatic::Push duckmatic_push;
80
81         Point point_holder;
82
83         etl::handle<Duck> point2_duck;
84
85         void refresh_ducks();
86
87         bool prev_workarea_layer_status_;
88
89         //Toolbox settings
90         synfigapp::Settings& settings;
91
92         //Toolbox display
93         Gtk::Table options_table;
94
95         Gtk::Entry              entry_id; //what to name the layer
96
97         Gtk::Adjustment adj_expand;
98         Gtk::Adjustment adj_feather;
99         Gtk::SpinButton spin_expand;
100         Gtk::SpinButton spin_feather;
101
102         Gtk::CheckButton checkbutton_invert;
103         Gtk::CheckButton checkbutton_layer_rectangle;
104         Gtk::CheckButton checkbutton_layer_region;
105         Gtk::CheckButton checkbutton_layer_outline;
106         Gtk::CheckButton checkbutton_layer_curve_gradient;
107         Gtk::CheckButton checkbutton_layer_plant;
108         Gtk::CheckButton checkbutton_layer_link_origins;
109
110 public:
111
112         // this only counts the layers which use blines - they're the only
113         // ones we link the origins for
114         int layers_to_create()const
115         {
116                 return
117                         get_layer_region_flag() +
118                         get_layer_outline_flag() +
119                         get_layer_curve_gradient_flag() +
120                         get_layer_plant_flag();
121         }
122
123         synfig::String get_id()const { return entry_id.get_text(); }
124         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
125
126         Real get_expand()const { return adj_expand.get_value(); }
127         void set_expand(Real f) { adj_expand.set_value(f); }
128
129         Real get_feather()const { return adj_feather.get_value(); }
130         void set_feather(Real f) { adj_feather.set_value(f); }
131
132         bool get_invert()const { return checkbutton_invert.get_active(); }
133         void set_invert(bool i) { checkbutton_invert.set_active(i); }
134
135         bool get_layer_rectangle_flag()const { return checkbutton_layer_rectangle.get_active(); }
136         void set_layer_rectangle_flag(bool x) { return checkbutton_layer_rectangle.set_active(x); }
137
138         bool get_layer_region_flag()const { return checkbutton_layer_region.get_active(); }
139         void set_layer_region_flag(bool x) { return checkbutton_layer_region.set_active(x); }
140
141         bool get_layer_outline_flag()const { return checkbutton_layer_outline.get_active(); }
142         void set_layer_outline_flag(bool x) { return checkbutton_layer_outline.set_active(x); }
143
144         bool get_layer_curve_gradient_flag()const { return checkbutton_layer_curve_gradient.get_active(); }
145         void set_layer_curve_gradient_flag(bool x) { return checkbutton_layer_curve_gradient.set_active(x); }
146
147         bool get_layer_plant_flag()const { return checkbutton_layer_plant.get_active(); }
148         void set_layer_plant_flag(bool x) { return checkbutton_layer_plant.set_active(x); }
149
150         bool get_layer_link_origins_flag()const { return checkbutton_layer_link_origins.get_active(); }
151         void set_layer_link_origins_flag(bool x) { return checkbutton_layer_link_origins.set_active(x); }
152
153         void refresh_tool_options(); //to refresh the toolbox
154
155         //events
156         Smach::event_result event_stop_handler(const Smach::event& x);
157         Smach::event_result event_refresh_handler(const Smach::event& x);
158         Smach::event_result event_mouse_click_handler(const Smach::event& x);
159         Smach::event_result event_refresh_tool_options(const Smach::event& x);
160
161         //constructor destructor
162         StateRectangle_Context(CanvasView* canvas_view);
163         ~StateRectangle_Context();
164
165         //Canvas interaction
166         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
167         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
168         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
169         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
170
171         //Modifying settings etc.
172         void load_settings();
173         void save_settings();
174         void reset();
175         void increment_id();
176         bool egress_on_selection_change;
177         Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
178         {
179                 if(egress_on_selection_change)
180                         throw &state_normal; //throw Smach::egress_exception();
181                 return Smach::RESULT_OK;
182         }
183
184         void make_rectangle(const Point& p1, const Point& p2);
185
186 };      // END of class StateGradient_Context
187
188 /* === M E T H O D S ======================================================= */
189
190 StateRectangle::StateRectangle():
191         Smach::state<StateRectangle_Context>("rectangle")
192 {
193         insert(event_def(EVENT_STOP,&StateRectangle_Context::event_stop_handler));
194         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateRectangle_Context::event_layer_selection_changed_handler));
195         insert(event_def(EVENT_REFRESH,&StateRectangle_Context::event_refresh_handler));
196         insert(event_def(EVENT_REFRESH_DUCKS,&StateRectangle_Context::event_refresh_handler));
197         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateRectangle_Context::event_mouse_click_handler));
198         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateRectangle_Context::event_mouse_click_handler));
199         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateRectangle_Context::event_mouse_click_handler));
200         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateRectangle_Context::event_refresh_tool_options));
201 }
202
203 StateRectangle::~StateRectangle()
204 {
205 }
206
207 void
208 StateRectangle_Context::load_settings()
209 {
210         try
211         {
212                 SETTINGS_LOCALE_SAFE_AND_BACKUP
213                 String value;
214
215                 //parse the arguments yargh!
216                 if(settings.get_value("rectangle.id",value))
217                         set_id(value);
218                 else
219                         set_id("Rectangle");
220
221                 if(settings.get_value("rectangle.expand",value))
222                         set_expand(atof(value.c_str()));
223                 else
224                         set_expand(0);
225
226                 if(settings.get_value("rectangle.feather",value))
227                         set_feather(atof(value.c_str()));
228                 else
229                         set_feather(0);
230
231                 if(settings.get_value("rectangle.invert",value) && value != "0")
232                         set_invert(true);
233                 else
234                         set_invert(false);
235
236                 if(settings.get_value("rectangle.layer_rectangle",value) && value=="0")
237                         set_layer_rectangle_flag(false);
238                 else
239                         set_layer_rectangle_flag(true);
240
241                 if(settings.get_value("rectangle.layer_region",value) && value=="1")
242                         set_layer_region_flag(true);
243                 else
244                         set_layer_region_flag(false);
245
246                 if(settings.get_value("rectangle.layer_outline",value) && value=="1")
247                         set_layer_outline_flag(true);
248                 else
249                         set_layer_outline_flag(false);
250
251                 if(settings.get_value("rectangle.layer_curve_gradient",value) && value=="1")
252                         set_layer_curve_gradient_flag(true);
253                 else
254                         set_layer_curve_gradient_flag(false);
255
256                 if(settings.get_value("rectangle.layer_plant",value) && value=="1")
257                         set_layer_plant_flag(true);
258                 else
259                         set_layer_plant_flag(false);
260
261                 if(settings.get_value("rectangle.layer_link_origins",value) && value=="0")
262                         set_layer_link_origins_flag(false);
263                 else
264                         set_layer_link_origins_flag(true);
265                 SETTINGS_LOCALE_RESTORE
266         }
267         catch(...)
268         {
269                 synfig::warning("State Rectangle: Caught exception when attempting to load settings.");
270         }
271 }
272
273 void
274 StateRectangle_Context::save_settings()
275 {
276         try
277         {
278                 SETTINGS_LOCALE_SAFE_AND_BACKUP
279                 settings.set_value("rectangle.id",get_id().c_str());
280                 settings.set_value("rectangle.expand",strprintf("%f",get_expand()));
281                 settings.set_value("rectangle.feather",strprintf("%f",(float)get_feather()));
282                 settings.set_value("rectangle.invert",get_invert()?"1":"0");
283                 settings.set_value("rectangle.layer_rectangle",get_layer_rectangle_flag()?"1":"0");
284                 settings.set_value("rectangle.layer_outline",get_layer_outline_flag()?"1":"0");
285                 settings.set_value("rectangle.layer_region",get_layer_region_flag()?"1":"0");
286                 settings.set_value("rectangle.layer_curve_gradient",get_layer_curve_gradient_flag()?"1":"0");
287                 settings.set_value("rectangle.layer_plant",get_layer_plant_flag()?"1":"0");
288                 settings.set_value("rectangle.layer_link_origins",get_layer_link_origins_flag()?"1":"0");
289                 SETTINGS_LOCALE_RESTORE
290         }
291         catch(...)
292         {
293                 synfig::warning("State Rectangle: Caught exception when attempting to save settings.");
294         }
295 }
296
297 void
298 StateRectangle_Context::reset()
299 {
300         refresh_ducks();
301 }
302
303 void
304 StateRectangle_Context::increment_id()
305 {
306         String id(get_id());
307         int number=1;
308         int digits=0;
309
310         if(id.empty())
311                 id="Rectangle";
312
313         // If there is a number
314         // already at the end of the
315         // id, then remove it.
316         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
317         {
318                 // figure out how many digits it is
319                 for (digits = 0;
320                          (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
321                          digits++)
322                         ;
323
324                 String str_number;
325                 str_number=String(id,id.size()-digits,id.size());
326                 id=String(id,0,id.size()-digits);
327
328                 number=atoi(str_number.c_str());
329         }
330         else
331         {
332                 number=1;
333                 digits=3;
334         }
335
336         number++;
337
338         // Add the number back onto the id
339         {
340                 const String format(strprintf("%%0%dd",digits));
341                 id+=strprintf(format.c_str(),number);
342         }
343
344         // Set the ID
345         set_id(id);
346 }
347
348 StateRectangle_Context::StateRectangle_Context(CanvasView* canvas_view):
349         canvas_view_(canvas_view),
350         is_working(*canvas_view),
351         duckmatic_push(get_work_area()),
352         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
353         settings(synfigapp::Main::get_selected_input_device()->settings()),
354         entry_id(),
355         adj_expand(0,0,1,0.01,0.1),
356         adj_feather(0,0,1,0.01,0.1),
357         spin_expand(adj_expand,0.1,3),
358         spin_feather(adj_feather,0.1,3),
359         checkbutton_invert(_("Invert")),
360         checkbutton_layer_rectangle(_("Create Rectangle Layer")),
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 BLine Origins"))
366 {
367         egress_on_selection_change=true;
368         load_settings();
369
370         // Set up the tool options dialog
371         options_table.attach(*manage(new Gtk::Label(_("Rectangle Tool"))),      0, 2,  0,  1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
372         options_table.attach(entry_id,                                                                          0, 2,  1,  2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
373
374         options_table.attach(checkbutton_layer_rectangle,                                       0, 2,  2,  3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
375         options_table.attach(checkbutton_layer_outline,                                         0, 2,  3,  4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
376         options_table.attach(checkbutton_layer_region,                                          0, 2,  4,  5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
377         options_table.attach(checkbutton_layer_plant,                                           0, 2,  5,  6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
378         options_table.attach(checkbutton_layer_curve_gradient,                          0, 2,  6,  7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
379         options_table.attach(checkbutton_layer_link_origins,                            0, 2,  7,  8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
380
381         //invert flag
382         options_table.attach(checkbutton_invert,                                                        0, 2,  8,  9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
383
384         //expand stuff
385         options_table.attach(*manage(new Gtk::Label(_("Expansion:"))),          0, 1,  9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
386         options_table.attach(spin_expand,                                                                       1, 2,  9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
387
388         //feather stuff
389         options_table.attach(*manage(new Gtk::Label(_("Feather:"))),            0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
390         options_table.attach(spin_feather,                                                                      1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
391
392         options_table.show_all();
393
394         //App::dialog_tool_options->set_widget(options_table);
395         refresh_tool_options();
396         App::dialog_tool_options->present();
397
398         // Turn off layer clicking
399         get_work_area()->set_allow_layer_clicks(false);
400
401         // clear out the ducks
402         get_work_area()->clear_ducks();
403
404         // Refresh the work area
405         get_work_area()->queue_draw();
406
407         get_work_area()->set_cursor(Gdk::DOTBOX);
408
409         // Hide the tables if they are showing
410         //prev_table_status=get_canvas_view()->tables_are_visible();
411         //if(prev_table_status)get_canvas_view()->hide_tables();
412
413         // Disable the time bar
414         //get_canvas_view()->set_sensitive_timebar(false);
415
416         // Connect a signal
417         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateRectangle_Context::on_user_click));
418
419         App::toolbox->refresh();
420 }
421
422 void
423 StateRectangle_Context::refresh_tool_options()
424 {
425         App::dialog_tool_options->clear();
426         App::dialog_tool_options->set_widget(options_table);
427         App::dialog_tool_options->set_local_name(_("Rectangle Tool"));
428         App::dialog_tool_options->set_name("rectangle");
429 }
430
431 Smach::event_result
432 StateRectangle_Context::event_refresh_tool_options(const Smach::event& /*x*/)
433 {
434         refresh_tool_options();
435         return Smach::RESULT_ACCEPT;
436 }
437
438 StateRectangle_Context::~StateRectangle_Context()
439 {
440         save_settings();
441
442         // Restore layer clicking
443         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
444
445         get_work_area()->reset_cursor();
446
447         App::dialog_tool_options->clear();
448
449         // Enable the time bar
450         //get_canvas_view()->set_sensitive_timebar(true);
451
452         // Bring back the tables if they were out before
453         //if(prev_table_status)get_canvas_view()->show_tables();
454
455         // Refresh the work area
456         get_work_area()->queue_draw();
457
458         get_canvas_view()->queue_rebuild_ducks();
459
460         App::toolbox->refresh();
461 }
462
463 Smach::event_result
464 StateRectangle_Context::event_stop_handler(const Smach::event& /*x*/)
465 {
466         //throw Smach::egress_exception();
467         throw &state_normal;
468         return Smach::RESULT_OK;
469 }
470
471 Smach::event_result
472 StateRectangle_Context::event_refresh_handler(const Smach::event& /*x*/)
473 {
474         refresh_ducks();
475         return Smach::RESULT_ACCEPT;
476 }
477
478 void
479 StateRectangle_Context::make_rectangle(const Point& _p1, const Point& _p2)
480 {
481         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Rectangle"));
482         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
483
484         Layer::Handle layer;
485
486         Canvas::Handle canvas;
487         int depth(0);
488
489         // we are temporarily using the layer to hold something
490         layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
491         if(layer)
492         {
493                 depth=layer->get_depth();
494                 canvas=layer->get_canvas();
495         }
496
497         synfigapp::SelectionManager::LayerList layer_selection;
498         if (!getenv("SYNFIG_TOOLS_CLEAR_SELECTION"))
499                 layer_selection = get_canvas_view()->get_selection_manager()->get_selected_layers();
500
501         const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
502         const Point p1(transform.unperform(_p1));
503         const Point p2(transform.unperform(_p2));
504         Real x_min, x_max, y_min, y_max;
505         if (p1[0] < p2[0]) { x_min = p1[0]; x_max = p2[0]; } else { x_min = p2[0]; x_max = p1[0]; }
506         if (p1[1] < p2[1]) { y_min = p1[1]; y_max = p2[1]; } else { y_min = p2[1]; y_max = p1[1]; }
507         x_min -= get_expand(); x_max += get_expand(); y_min -= get_expand(); y_max += get_expand();
508
509         std::vector<BLinePoint> new_list;
510         for (int i = 0; i < 4; i++)
511         {
512                 new_list.push_back(*(new BLinePoint));
513                 new_list[i].set_width(1);
514                 new_list[i].set_vertex(Point((i==0||i==3)?x_min:x_max,
515                                                                          (i==0||i==1)?y_min:y_max));
516                 new_list[i].set_tangent(Point(0,0));
517         }
518
519         ValueNode_BLine::Handle value_node_bline(ValueNode_BLine::create(new_list));
520         assert(value_node_bline);
521
522         ValueNode_Const::Handle value_node_origin(ValueNode_Const::create(Vector()));
523         assert(value_node_origin);
524
525         // Set the looping flag
526         value_node_bline->set_loop(true);
527
528         if(!canvas)
529                 canvas=get_canvas_view()->get_canvas();
530
531         value_node_bline->set_member_canvas(canvas);
532
533         // count how many layers we're going to be creating
534         int layers_to_create = this->layers_to_create();
535
536         ///////////////////////////////////////////////////////////////////////////
537         //   R E C T A N G L E
538         ///////////////////////////////////////////////////////////////////////////
539
540         if (get_layer_rectangle_flag())
541         {
542                 layer=get_canvas_interface()->add_layer_to("rectangle",canvas,depth);
543                 if (!layer)
544                 {
545                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
546                         group.cancel();
547                         return;
548                 }
549                 layer_selection.push_back(layer);
550
551                 layer->set_param("point1",p1);
552                 get_canvas_interface()->signal_layer_param_changed()(layer,"point1");
553
554                 layer->set_param("point2",p2);
555                 get_canvas_interface()->signal_layer_param_changed()(layer,"point2");
556
557                 layer->set_param("expand",get_expand());
558                 get_canvas_interface()->signal_layer_param_changed()(layer,"expand");
559
560                 layer->set_param("invert",get_invert());
561                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
562
563                 layer->set_description(get_id());
564                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
565         }
566
567         ///////////////////////////////////////////////////////////////////////////
568         //   C U R V E   G R A D I E N T
569         ///////////////////////////////////////////////////////////////////////////
570
571         if(get_layer_curve_gradient_flag())
572         {
573                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
574
575                 Layer::Handle layer(get_canvas_interface()->add_layer_to("curve_gradient",canvas,depth));
576                 if (!layer)
577                 {
578                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
579                         group.cancel();
580                         return;
581                 }
582                 layer_selection.push_back(layer);
583                 layer->set_description(get_id()+_(" Gradient"));
584                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
585
586                 {
587                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
588                         assert(action);
589
590                         action->set_param("canvas",get_canvas());
591                         action->set_param("canvas_interface",get_canvas_interface());
592                         action->set_param("layer",layer);
593                         if(!action->set_param("param",String("bline")))
594                                 synfig::error("LayerParamConnect didn't like \"param\"");
595                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
596                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
597
598                         if(!get_canvas_interface()->get_instance()->perform_action(action))
599                         {
600                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
601                                 group.cancel();
602                                 throw String(_("Unable to create Gradient layer"));
603                                 return;
604                         }
605                 }
606
607                 // only link the curve gradient's origin parameter if the option is selected and we're creating more than one layer
608                 if (get_layer_link_origins_flag() && layers_to_create > 1)
609                 {
610                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
611                         assert(action);
612
613                         action->set_param("canvas",get_canvas());
614                         action->set_param("canvas_interface",get_canvas_interface());
615                         action->set_param("layer",layer);
616                         if(!action->set_param("param",String("origin")))
617                                 synfig::error("LayerParamConnect didn't like \"param\"");
618                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
619                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
620
621                         if(!get_canvas_interface()->get_instance()->perform_action(action))
622                         {
623                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
624                                 group.cancel();
625                                 throw String(_("Unable to create Gradient layer"));
626                                 return;
627                         }
628                 }
629         }
630
631         ///////////////////////////////////////////////////////////////////////////
632         //   P L A N T
633         ///////////////////////////////////////////////////////////////////////////
634
635         if(get_layer_plant_flag())
636         {
637                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
638
639                 Layer::Handle layer(get_canvas_interface()->add_layer_to("plant",canvas,depth));
640                 if (!layer)
641                 {
642                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
643                         group.cancel();
644                         return;
645                 }
646                 layer_selection.push_back(layer);
647                 layer->set_description(get_id()+_(" Plant"));
648                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
649
650                 {
651                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
652                         assert(action);
653
654                         action->set_param("canvas",get_canvas());
655                         action->set_param("canvas_interface",get_canvas_interface());
656                         action->set_param("layer",layer);
657                         if(!action->set_param("param",String("bline")))
658                                 synfig::error("LayerParamConnect didn't like \"param\"");
659                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
660                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
661
662                         if(!get_canvas_interface()->get_instance()->perform_action(action))
663                         {
664                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
665                                 group.cancel();
666                                 throw String(_("Unable to create Plant layer"));
667                                 return;
668                         }
669                 }
670
671                 // only link the plant's origin parameter if the option is selected and we're creating more than one layer
672                 if (get_layer_link_origins_flag() && layers_to_create > 1)
673                 {
674                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
675                         assert(action);
676
677                         action->set_param("canvas",get_canvas());
678                         action->set_param("canvas_interface",get_canvas_interface());
679                         action->set_param("layer",layer);
680                         if(!action->set_param("param",String("origin")))
681                                 synfig::error("LayerParamConnect didn't like \"param\"");
682                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
683                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
684
685                         if(!get_canvas_interface()->get_instance()->perform_action(action))
686                         {
687                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
688                                 group.cancel();
689                                 throw String(_("Unable to create Plant layer"));
690                                 return;
691                         }
692                 }
693         }
694
695         ///////////////////////////////////////////////////////////////////////////
696         //   R E G I O N
697         ///////////////////////////////////////////////////////////////////////////
698
699         if(get_layer_region_flag())
700         {
701                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
702
703                 Layer::Handle layer(get_canvas_interface()->add_layer_to("region",canvas,depth));
704                 if (!layer)
705                 {
706                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
707                         group.cancel();
708                         return;
709                 }
710                 layer_selection.push_back(layer);
711                 layer->set_description(get_id()+_(" Region"));
712                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
713
714                 layer->set_param("feather",get_feather());
715                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
716
717                 layer->set_param("invert",get_invert());
718                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
719
720                 // I don't know if it's safe to reuse the same LayerParamConnect action, so I'm
721                 // using 2 separate ones.
722                 {
723                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
724                         assert(action);
725
726                         action->set_param("canvas",get_canvas());
727                         action->set_param("canvas_interface",get_canvas_interface());
728                         action->set_param("layer",layer);
729                         if(!action->set_param("param",String("bline")))
730                                 synfig::error("LayerParamConnect didn't like \"param\"");
731                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
732                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
733
734                         if(!get_canvas_interface()->get_instance()->perform_action(action))
735                         {
736                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
737                                 group.cancel();
738                                 throw String(_("Unable to create Region layer"));
739                                 return;
740                         }
741                 }
742
743                 // only link the region's origin parameter if the option is selected and we're creating more than one layer
744                 if (get_layer_link_origins_flag() && layers_to_create > 1)
745                 {
746                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
747                         assert(action);
748
749                         action->set_param("canvas",get_canvas());
750                         action->set_param("canvas_interface",get_canvas_interface());
751                         action->set_param("layer",layer);
752                         if(!action->set_param("param",String("origin")))
753                                 synfig::error("LayerParamConnect didn't like \"param\"");
754                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
755                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
756
757                         if(!get_canvas_interface()->get_instance()->perform_action(action))
758                         {
759                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
760                                 group.cancel();
761                                 throw String(_("Unable to create Region layer"));
762                                 return;
763                         }
764                 }
765         }
766
767         ///////////////////////////////////////////////////////////////////////////
768         //   O U T L I N E
769         ///////////////////////////////////////////////////////////////////////////
770
771         if (get_layer_outline_flag())
772         {
773                 Layer::Handle layer(get_canvas_interface()->add_layer_to("outline",canvas,depth));
774                 if (!layer)
775                 {
776                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
777                         group.cancel();
778                         return;
779                 }
780                 layer_selection.push_back(layer);
781                 layer->set_description(get_id()+_(" Outline"));
782                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
783
784                 layer->set_param("feather",get_feather());
785                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
786
787                 layer->set_param("invert",get_invert());
788                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
789
790                 {
791                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
792                         assert(action);
793
794                         action->set_param("canvas",get_canvas());
795                         action->set_param("canvas_interface",get_canvas_interface());
796                         action->set_param("layer",layer);
797                         if(!action->set_param("param",String("bline")))
798                                 synfig::error("LayerParamConnect didn't like \"param\"");
799                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
800                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
801
802                         if(!get_canvas_interface()->get_instance()->perform_action(action))
803                         {
804                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
805                                 group.cancel();
806                                 throw String(_("Unable to create Outline layer"));
807                                 return;
808                         }
809                 }
810
811                 // only link the outline's origin parameter if the option is selected and we're creating more than one layer
812                 if (get_layer_link_origins_flag() && layers_to_create > 1)
813                 {
814                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
815                         assert(action);
816
817                         action->set_param("canvas",get_canvas());
818                         action->set_param("canvas_interface",get_canvas_interface());
819                         action->set_param("layer",layer);
820                         if(!action->set_param("param",String("origin")))
821                                 synfig::error("LayerParamConnect didn't like \"param\"");
822                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
823                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
824
825                         if(!get_canvas_interface()->get_instance()->perform_action(action))
826                         {
827                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
828                                 group.cancel();
829                                 throw String(_("Unable to create Outline layer"));
830                                 return;
831                         }
832                 }
833         }
834
835         egress_on_selection_change=false;
836         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
837         get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
838         egress_on_selection_change=true;
839
840         //post clean up stuff...
841         reset();
842         increment_id();
843 }
844
845 Smach::event_result
846 StateRectangle_Context::event_mouse_click_handler(const Smach::event& x)
847 {
848         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
849
850         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
851         {
852                 point_holder=get_work_area()->snap_point_to_grid(event.pos);
853                 etl::handle<Duck> duck=new Duck();
854                 duck->set_point(point_holder);
855                 duck->set_name("p1");
856                 duck->set_type(Duck::TYPE_POSITION);
857                 get_work_area()->add_duck(duck);
858
859                 point2_duck=new Duck();
860                 point2_duck->set_point(point_holder);
861                 point2_duck->set_name("p2");
862                 point2_duck->set_type(Duck::TYPE_POSITION);
863                 point2_duck->set_box_duck(duck);
864                 get_work_area()->add_duck(point2_duck);
865
866                 return Smach::RESULT_ACCEPT;
867         }
868
869         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
870         {
871                 if (!point2_duck) return Smach::RESULT_OK;
872                 point2_duck->set_point(get_work_area()->snap_point_to_grid(event.pos));
873                 get_work_area()->queue_draw();
874                 return Smach::RESULT_ACCEPT;
875         }
876
877         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
878         {
879                 make_rectangle(point_holder, get_work_area()->snap_point_to_grid(event.pos));
880                 get_work_area()->clear_ducks();
881                 return Smach::RESULT_ACCEPT;
882         }
883
884         return Smach::RESULT_OK;
885 }
886
887
888 void
889 StateRectangle_Context::refresh_ducks()
890 {
891         get_work_area()->clear_ducks();
892         get_work_area()->queue_draw();
893 }