Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_scale.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_scale.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_scale.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 <synfigapp/main.h>
51
52 #endif
53
54 /* === U S I N G =========================================================== */
55
56 using namespace std;
57 using namespace etl;
58 using namespace synfig;
59 using namespace studio;
60
61 /* === M A C R O S ========================================================= */
62
63 /* === G L O B A L S ======================================================= */
64
65 StateScale studio::state_scale;
66
67 /* === C L A S S E S & S T R U C T S ======================================= */
68
69 class DuckDrag_Scale : public DuckDrag_Base
70 {
71
72         synfig::Vector last_scale;
73         synfig::Vector drag_offset;
74         synfig::Vector center;
75         synfig::Vector snap;
76
77         std::vector<synfig::Vector> positions;
78
79         bool move_only;
80
81         bool bad_drag;
82 public:
83         bool lock_aspect;
84         DuckDrag_Scale();
85         void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
86         bool end_duck_drag(Duckmatic* duckmatic);
87         void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
88 };
89
90
91 class studio::StateScale_Context : public sigc::trackable
92 {
93         etl::handle<CanvasView> canvas_view_;
94
95         synfigapp::Settings& settings;
96
97         etl::handle<DuckDrag_Scale> duck_dragger_;
98
99         Gtk::Table options_table;
100
101
102         Gtk::CheckButton checkbutton_aspect_lock;
103
104 public:
105
106         bool get_aspect_lock_flag()const { return checkbutton_aspect_lock.get_active(); }
107         void set_aspect_lock_flag(bool x) { return checkbutton_aspect_lock.set_active(x); refresh_aspect_lock_flag(); }
108
109         void refresh_aspect_lock_flag() { if(duck_dragger_)duck_dragger_->lock_aspect=get_aspect_lock_flag(); }
110
111         Smach::event_result event_refresh_tool_options(const Smach::event& x);
112
113         void refresh_tool_options();
114
115         StateScale_Context(CanvasView* canvas_view);
116
117         ~StateScale_Context();
118
119         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
120         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
121         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
122         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
123
124         void load_settings();
125         void save_settings();
126 };      // END of class StateScale_Context
127
128 /* === M E T H O D S ======================================================= */
129
130 StateScale::StateScale():
131         Smach::state<StateScale_Context>("scale")
132 {
133         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateScale_Context::event_refresh_tool_options));
134 }
135
136 StateScale::~StateScale()
137 {
138 }
139
140 void
141 StateScale_Context::load_settings()
142 {
143         String value;
144
145         if(settings.get_value("scale.lock_aspect",value) && value=="0")
146                 set_aspect_lock_flag(false);
147         else
148                 set_aspect_lock_flag(true);
149 }
150
151 void
152 StateScale_Context::save_settings()
153 {
154         settings.set_value("scale.lock_aspect",get_aspect_lock_flag()?"1":"0");
155 }
156
157 StateScale_Context::StateScale_Context(CanvasView* canvas_view):
158         canvas_view_(canvas_view),
159         settings(synfigapp::Main::get_selected_input_device()->settings()),
160         duck_dragger_(new DuckDrag_Scale()),
161         checkbutton_aspect_lock(_("Lock Aspect Ratio"))
162 {
163         // Set up the tool options dialog
164         //options_table.attach(*manage(new Gtk::Label(_("Scale Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
165         options_table.attach(checkbutton_aspect_lock, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
166
167         checkbutton_aspect_lock.signal_toggled().connect(sigc::mem_fun(*this,&StateScale_Context::refresh_aspect_lock_flag));
168
169         options_table.show_all();
170         refresh_tool_options();
171         App::dialog_tool_options->present();
172
173         get_work_area()->set_allow_layer_clicks(true);
174         get_work_area()->set_duck_dragger(duck_dragger_);
175
176 //      get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
177         get_canvas_view()->work_area->reset_cursor();
178
179         App::toolbox->refresh();
180
181         set_aspect_lock_flag(true);
182         load_settings();
183 }
184
185 void
186 StateScale_Context::refresh_tool_options()
187 {
188         App::dialog_tool_options->clear();
189         App::dialog_tool_options->set_widget(options_table);
190         App::dialog_tool_options->set_local_name(_("Scale Tool"));
191         App::dialog_tool_options->set_name("scale");
192 }
193
194 Smach::event_result
195 StateScale_Context::event_refresh_tool_options(const Smach::event& /*x*/)
196 {
197         refresh_tool_options();
198         return Smach::RESULT_ACCEPT;
199 }
200
201 StateScale_Context::~StateScale_Context()
202 {
203         save_settings();
204
205         get_work_area()->clear_duck_dragger();
206         get_canvas_view()->work_area->reset_cursor();
207
208         App::dialog_tool_options->clear();
209
210         App::toolbox->refresh();
211 }
212
213
214
215
216 DuckDrag_Scale::DuckDrag_Scale():
217         lock_aspect(true)
218 {
219 }
220
221 #ifndef EPSILON
222 #define EPSILON 0.0000001
223 #endif
224
225 void
226 DuckDrag_Scale::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
227 {
228         last_scale=Vector(1,1);
229         const DuckList selected_ducks(duckmatic->get_selected_ducks());
230         DuckList::const_iterator iter;
231
232         //if(duckmatic->get_selected_ducks().size()<2)
233         //{
234         //      bad_drag=true;
235 //              return;
236 //      }
237         bad_drag=false;
238
239                 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
240
241                 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
242                 //snap=offset-drag_offset;
243                 snap=Vector(0,0);
244
245         // Calculate center
246         Point vmin(100000000,100000000);
247         Point vmax(-100000000,-100000000);
248         //std::set<etl::handle<Duck> >::iterator iter;
249         positions.clear();
250         int i;
251         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
252         {
253                 Point p((*iter)->get_trans_point());
254                 vmin[0]=min(vmin[0],p[0]);
255                 vmin[1]=min(vmin[1],p[1]);
256                 vmax[0]=max(vmax[0],p[0]);
257                 vmax[1]=max(vmax[1],p[1]);
258                 positions.push_back(p);
259         }
260         if((vmin-vmax).mag()<=EPSILON)
261                 move_only=true;
262         else
263                 move_only=false;
264
265         center=(vmin+vmax)*0.5;
266 }
267
268
269 void
270 DuckDrag_Scale::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
271 {
272         const DuckList selected_ducks(duckmatic->get_selected_ducks());
273         DuckList::const_iterator iter;
274
275         if(bad_drag)
276                 return;
277
278         //std::set<etl::handle<Duck> >::iterator iter;
279         synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center);
280         last_scale=vect;
281
282         if(move_only)
283         {
284                 int i;
285                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
286                 {
287                         if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
288
289                         Vector p(positions[i]);
290
291                         p[0]+=vect[0];
292                         p[1]+=vect[1];
293                         (*iter)->set_trans_point(p);
294                 }
295                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
296                 {
297                         if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
298
299                         Vector p(positions[i]);
300
301                         p[0]+=vect[0];
302                         p[1]+=vect[1];
303                         (*iter)->set_trans_point(p);
304                 }
305                 return;
306         }
307
308         if(!lock_aspect)
309         {
310                 if(abs(drag_offset[0]-center[0])>EPSILON)
311                         vect[0]/=drag_offset[0]-center[0];
312                 else
313                         vect[0]=1;
314                 if(abs(drag_offset[1]-center[1])>EPSILON)
315                         vect[1]/=drag_offset[1]-center[1];
316                 else
317                         vect[1]=1;
318                 }
319         else
320         {
321                 //vect[0]=vect[1]=vect.mag()*0.707106781;
322                 Real amount(vect.mag()/(drag_offset-center).mag());
323                 vect[0]=vect[1]=amount;
324         }
325
326         if(vect[0]<EPSILON && vect[0]>-EPSILON)
327                 vect[0]=1;
328         if(vect[1]<EPSILON && vect[1]>-EPSILON)
329                 vect[1]=1;
330
331         int i;
332         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
333         {
334                 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
335
336                 Vector p(positions[i]-center);
337
338                 p[0]*=vect[0];
339                 p[1]*=vect[1];
340                 p+=center;
341                 (*iter)->set_trans_point(p);
342         }
343         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
344         {
345                 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
346
347                 Vector p(positions[i]-center);
348
349                 p[0]*=vect[0];
350                 p[1]*=vect[1];
351                 p+=center;
352                 (*iter)->set_trans_point(p);
353         }
354
355         last_scale=vect;
356         //snap=Vector(0,0);
357 }
358
359 bool
360 DuckDrag_Scale::end_duck_drag(Duckmatic* duckmatic)
361 {
362         if(bad_drag)return false;
363
364         if((last_scale-Vector(1,1)).mag()>0.0001)
365         {
366                 duckmatic->signal_edited_selected_ducks();
367                 return true;
368         }
369         else
370         {
371                 duckmatic->signal_user_click_selected_ducks(0);
372                 return false;
373         }
374 }