Get rid of all references to the bootstrap script since it was removed
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_circle.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_circle.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
38 #include "state_circle.h"
39 #include "canvasview.h"
40 #include "workarea.h"
41 #include "app.h"
42
43 #include <synfigapp/action.h>
44 #include "event_mouse.h"
45 #include "event_layerclick.h"
46 #include "toolbox.h"
47 #include "dialog_tooloptions.h"
48 #include <gtkmm/optionmenu.h>
49 #include "duck.h"
50 #include "widget_enum.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 enum CircleFalloff
66 {
67         CIRCLE_SQUARED  =0,
68         CIRCLE_INTERPOLATION_LINEAR     =1,
69         CIRCLE_COSINE   =2,
70         CIRCLE_SIGMOND  =3,
71         CIRCLE_SQRT             =4,
72         CIRCLE_NUM_FALLOFF
73 };
74
75 /* === G L O B A L S ======================================================= */
76
77 StateCircle studio::state_circle;
78
79 /* === C L A S S E S & S T R U C T S ======================================= */
80
81 class studio::StateCircle_Context : public sigc::trackable
82 {
83         etl::handle<CanvasView> canvas_view_;
84         CanvasView::IsWorking is_working;
85
86         Duckmatic::Push duckmatic_push;
87
88         Point point_holder;
89
90         etl::handle<Duck> point2_duck;
91
92         void refresh_ducks();
93
94         bool prev_workarea_layer_status_;
95
96         //Toolbox settings
97         synfigapp::Settings& settings;
98
99         //Toolbox display
100         Gtk::Table options_table;
101
102         Gtk::Entry              entry_id; //what to name the layer
103
104         Widget_Enum             enum_falloff;
105         Widget_Enum             enum_blend;
106
107         Gtk::Adjustment adj_feather;
108         Gtk::SpinButton spin_feather;
109
110         Gtk::CheckButton check_invert;
111
112 public:
113
114         synfig::String get_id()const { return entry_id.get_text(); }
115         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
116
117         int get_falloff()const { return enum_falloff.get_value(); }
118         void set_falloff(int x) { return enum_falloff.set_value(x); }
119
120         int get_blend()const { return enum_blend.get_value(); }
121         void set_blend(int x) { return enum_blend.set_value(x); }
122
123         Real get_feather()const { return adj_feather.get_value(); }
124         void set_feather(Real f) { adj_feather.set_value(f); }
125
126         bool get_invert()const { return check_invert.get_active(); }
127         void set_invert(bool i) { check_invert.set_active(i); }
128
129         void refresh_tool_options(); //to refresh the toolbox
130
131         //events
132         Smach::event_result event_stop_handler(const Smach::event& x);
133         Smach::event_result event_refresh_handler(const Smach::event& x);
134         Smach::event_result event_mouse_click_handler(const Smach::event& x);
135         Smach::event_result event_refresh_tool_options(const Smach::event& x);
136
137         //constructor destructor
138         StateCircle_Context(CanvasView* canvas_view);
139         ~StateCircle_Context();
140
141         //Canvas interaction
142         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
143         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
144         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
145         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
146
147         //Modifying settings etc.
148         void load_settings();
149         void save_settings();
150         void reset();
151         void increment_id();
152         bool egress_on_selection_change;
153         Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
154         {
155                 if(egress_on_selection_change)
156                         throw Smach::egress_exception();
157                 return Smach::RESULT_OK;
158         }
159
160         void make_circle(const Point& p1, const Point& p2);
161
162 };      // END of class StateGradient_Context
163
164 /* === M E T H O D S ======================================================= */
165
166 StateCircle::StateCircle():
167         Smach::state<StateCircle_Context>("circle")
168 {
169         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateCircle_Context::event_layer_selection_changed_handler));
170         insert(event_def(EVENT_STOP,&StateCircle_Context::event_stop_handler));
171         insert(event_def(EVENT_REFRESH,&StateCircle_Context::event_refresh_handler));
172         insert(event_def(EVENT_REFRESH_DUCKS,&StateCircle_Context::event_refresh_handler));
173         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateCircle_Context::event_mouse_click_handler));
174         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateCircle_Context::event_mouse_click_handler));
175         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateCircle_Context::event_mouse_click_handler));
176         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateCircle_Context::event_refresh_tool_options));
177 }
178
179 StateCircle::~StateCircle()
180 {
181 }
182
183 void
184 StateCircle_Context::load_settings()
185 {
186         String value;
187
188         //parse the arguments yargh!
189         if(settings.get_value("circle.id",value))
190                 set_id(value);
191         else
192                 set_id("Circle");
193
194         if(settings.get_value("circle.fallofftype",value) && value != "")
195                 set_falloff(atoi(value.c_str()));
196         else
197                 set_falloff(2);
198
199         if(settings.get_value("circle.blend",value) && value != "")
200                 set_blend(atoi(value.c_str()));
201         else
202                 set_blend(0);//(int)Color::BLEND_COMPOSITE); //0 should be blend composites value
203
204         if(settings.get_value("circle.feather",value))
205                 set_feather(atof(value.c_str()));
206         else
207                 set_feather(0);
208
209         if(settings.get_value("circle.invert",value) && value != "0")
210                 set_invert(true);
211         else
212                 set_invert(false);
213 }
214
215 void
216 StateCircle_Context::save_settings()
217 {
218         settings.set_value("circle.id",get_id());
219         settings.set_value("circle.fallofftype",strprintf("%d",get_falloff()));
220         settings.set_value("circle.blend",strprintf("%d",get_blend()));
221         settings.set_value("circle.feather",strprintf("%f",(float)get_feather()));
222         settings.set_value("circle.invert",get_invert()?"1":"0");
223 }
224
225 void
226 StateCircle_Context::reset()
227 {
228         refresh_ducks();
229 }
230
231 void
232 StateCircle_Context::increment_id()
233 {
234         String id(get_id());
235         int number=1;
236         int digits=0;
237
238         if(id.empty())
239                 id="Circle";
240
241         // If there is a number
242         // already at the end of the
243         // id, then remove it.
244         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
245         {
246                 // figure out how many digits it is
247                 for (digits = 0;
248                          (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
249                          digits++)
250                         ;
251
252                 String str_number;
253                 str_number=String(id,id.size()-digits,id.size());
254                 id=String(id,0,id.size()-digits);
255
256                 number=atoi(str_number.c_str());
257         }
258         else
259         {
260                 number=1;
261                 digits=3;
262         }
263
264         number++;
265
266         // Add the number back onto the id
267         {
268                 const String format(strprintf("%%0%dd",digits));
269                 id+=strprintf(format.c_str(),number);
270         }
271
272         // Set the ID
273         set_id(id);
274 }
275
276 StateCircle_Context::StateCircle_Context(CanvasView* canvas_view):
277         canvas_view_(canvas_view),
278         is_working(*canvas_view),
279         duckmatic_push(get_work_area()),
280         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
281         settings(synfigapp::Main::get_selected_input_device()->settings()),
282         entry_id(),
283         adj_feather(0,0,1,0.01,0.1),
284         spin_feather(adj_feather,0.1,3),
285         check_invert(_("Invert"))
286 {
287         egress_on_selection_change=true;
288         // Set up the tool options dialog
289         //options_table.attach(*manage(new Gtk::Label(_("Circle Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
290         options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
291
292         enum_falloff.set_param_desc(ParamDesc("falloff")
293                 .set_local_name(_("Falloff"))
294                 .set_description(_("Determines the falloff function for the feather"))
295                 .set_hint("enum")
296                 .add_enum_value(CIRCLE_INTERPOLATION_LINEAR,"linear",_("Linear"))
297                 .add_enum_value(CIRCLE_SQUARED,"squared",_("Squared"))
298                 .add_enum_value(CIRCLE_SQRT,"sqrt",_("Square Root"))
299                 .add_enum_value(CIRCLE_SIGMOND,"sigmond",_("Sigmond"))
300                 .add_enum_value(CIRCLE_COSINE,"cosine",_("Cosine")));
301
302         enum_blend.set_param_desc(ParamDesc(Color::BLEND_COMPOSITE,"blend_method")
303                 .set_local_name(_("Blend Method"))
304                 .set_description(_("Defines the blend method to be used for circles")));
305
306         load_settings();
307
308         //feather stuff
309         options_table.attach(*manage(new Gtk::Label(_("Feather:"))), 0, 1, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
310         options_table.attach(spin_feather, 1, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
311         options_table.attach(enum_falloff, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
312         options_table.attach(enum_blend, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
313
314         //invert flag
315         options_table.attach(check_invert, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
316
317         options_table.show_all();
318
319         refresh_tool_options();
320         App::dialog_tool_options->present();
321
322         // Turn off layer clicking
323         get_work_area()->set_allow_layer_clicks(false);
324
325         // clear out the ducks
326         get_work_area()->clear_ducks();
327
328         // Refresh the work area
329         get_work_area()->queue_draw();
330
331         // Hide the tables if they are showing
332         //prev_table_status=get_canvas_view()->tables_are_visible();
333         //if(prev_table_status)get_canvas_view()->hide_tables();
334
335         // Disable the time bar
336         //get_canvas_view()->set_sensitive_timebar(false);
337
338         // Connect a signal
339         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateCircle_Context::on_user_click));
340         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
341
342         App::toolbox->refresh();
343 }
344
345 void
346 StateCircle_Context::refresh_tool_options()
347 {
348         App::dialog_tool_options->clear();
349         App::dialog_tool_options->set_widget(options_table);
350         App::dialog_tool_options->set_local_name(_("Circle Tool"));
351         App::dialog_tool_options->set_name("circle");
352 }
353
354 Smach::event_result
355 StateCircle_Context::event_refresh_tool_options(const Smach::event& /*x*/)
356 {
357         refresh_tool_options();
358         return Smach::RESULT_ACCEPT;
359 }
360
361 StateCircle_Context::~StateCircle_Context()
362 {
363         save_settings();
364
365         // Restore layer clicking
366         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
367         get_canvas_view()->work_area->reset_cursor();
368
369         App::dialog_tool_options->clear();
370
371         // Enable the time bar
372         //get_canvas_view()->set_sensitive_timebar(true);
373
374         // Bring back the tables if they were out before
375         //if(prev_table_status)get_canvas_view()->show_tables();
376
377         // Refresh the work area
378         get_work_area()->queue_draw();
379
380         App::toolbox->refresh();
381 }
382
383 Smach::event_result
384 StateCircle_Context::event_stop_handler(const Smach::event& /*x*/)
385 {
386         throw Smach::egress_exception();
387 }
388
389 Smach::event_result
390 StateCircle_Context::event_refresh_handler(const Smach::event& /*x*/)
391 {
392         refresh_ducks();
393         return Smach::RESULT_ACCEPT;
394 }
395
396 void
397 StateCircle_Context::make_circle(const Point& _p1, const Point& _p2)
398 {
399         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Circle"));
400         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
401
402         Layer::Handle layer;
403
404         Canvas::Handle canvas(get_canvas_view()->get_canvas());
405         int depth(0);
406
407         // we are temporarily using the layer to hold something
408         layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
409         if(layer)
410         {
411                 depth=layer->get_depth();
412                 canvas=layer->get_canvas();
413         }
414
415         const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
416         const Point p1(transform.unperform(_p1));
417         const Point p2(transform.unperform(_p2));
418
419         if(get_falloff() >= 0 && get_falloff() < CIRCLE_NUM_FALLOFF)
420         {
421
422                 layer=get_canvas_interface()->add_layer_to("circle",canvas,depth);
423
424                 layer->set_param("pos",p1);
425                 get_canvas_interface()->signal_layer_param_changed()(layer,"pos");
426
427                 layer->set_param("radius",(p2-p1).mag());
428                 get_canvas_interface()->signal_layer_param_changed()(layer,"radius");
429
430                 layer->set_param("falloff",get_falloff());
431                 get_canvas_interface()->signal_layer_param_changed()(layer,"falloff");
432
433                 layer->set_param("feather",get_feather());
434                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
435
436                 layer->set_param("invert",get_invert());
437                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
438
439                 layer->set_param("blend_method",get_blend());
440                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
441
442                 layer->set_description(get_id());
443                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
444
445                 egress_on_selection_change=false;
446                 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
447                 get_canvas_interface()->get_selection_manager()->set_selected_layer(layer);
448                 egress_on_selection_change=true;
449         }
450
451         reset();
452         increment_id();
453 }
454
455 Smach::event_result
456 StateCircle_Context::event_mouse_click_handler(const Smach::event& x)
457 {
458         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
459
460         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
461         {
462                 point_holder=get_work_area()->snap_point_to_grid(event.pos);
463                 etl::handle<Duck> duck=new Duck();
464                 duck->set_point(point_holder);
465                 duck->set_name("p1");
466                 duck->set_type(Duck::TYPE_POSITION);
467                 duck->set_editable(false);
468                 get_work_area()->add_duck(duck);
469
470                 point2_duck=new Duck();
471                 point2_duck->set_point(Vector(0,0));
472                 point2_duck->set_name("radius");
473                 point2_duck->set_origin(duck);
474                 point2_duck->set_radius(true);
475                 point2_duck->set_scalar(-1);
476                 point2_duck->set_type(Duck::TYPE_RADIUS);
477                 get_work_area()->add_duck(point2_duck);
478
479                 return Smach::RESULT_ACCEPT;
480         }
481
482         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
483         {
484                 point2_duck->set_point(point_holder-get_work_area()->snap_point_to_grid(event.pos));
485                 get_work_area()->queue_draw();
486                 return Smach::RESULT_ACCEPT;
487         }
488
489         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
490         {
491                 make_circle(point_holder, get_work_area()->snap_point_to_grid(event.pos));
492                 get_work_area()->clear_ducks();
493                 return Smach::RESULT_ACCEPT;
494         }
495
496         return Smach::RESULT_OK;
497 }
498
499
500 void
501 StateCircle_Context::refresh_ducks()
502 {
503         get_work_area()->clear_ducks();
504         get_work_area()->queue_draw();
505 }