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