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