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