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