Build rectanglular blines in the same handedness as all the other shapes.
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_zoom.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_zoom.cpp
3 **      \brief Zoom Tool Implementation 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 <sigc++/signal.h>
34 #include <sigc++/object.h>
35
36 #include <ETL/handle>
37 #include <synfig/vector.h>
38
39
40 #include "state_zoom.h"
41 #include "event_mouse.h"
42 #include "canvasview.h"
43 #include "workarea.h"
44 #include "app.h"
45 #include "toolbox.h"
46 #include <synfigapp/main.h>
47
48 #include "general.h"
49
50 #endif
51
52 /* === U S I N G =========================================================== */
53
54 using namespace std;
55 using namespace etl;
56 using namespace synfig;
57 using namespace studio;
58
59 /* === M A C R O S ========================================================= */
60
61 /* === G L O B A L S ======================================================= */
62 StateZoom studio::state_zoom;
63
64 const float ZOOMFACTOR = 1.25f;
65
66 /* === C L A S S E S & S T R U C T S ======================================= */
67
68 class studio::StateZoom_Context : public sigc::trackable
69 {
70         etl::handle<CanvasView> canvas_view_;
71         CanvasView::IsWorking is_working;
72
73         Point p1,p2;
74
75         bool prev_workarea_layer_status_;
76
77 public:
78
79         //events
80         Smach::event_result event_stop_handler(const Smach::event& x);
81         Smach::event_result event_refresh_handler(const Smach::event& x);
82         Smach::event_result event_mouse_click_handler(const Smach::event& x);
83
84         //constructor destructor
85         StateZoom_Context(CanvasView* canvas_view);
86         ~StateZoom_Context();
87
88         //Canvas interaction
89         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
90         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
91         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
92         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
93
94         //void zoom(const Point& p1, const Point& p2);
95
96 };      // END of class StateZoom_Context
97
98 /* === M E T H O D S ======================================================= */
99
100 StateZoom::StateZoom():
101         Smach::state<StateZoom_Context>("zoom")
102 {
103         insert(event_def(EVENT_STOP,&StateZoom_Context::event_stop_handler));
104         insert(event_def(EVENT_REFRESH,&StateZoom_Context::event_refresh_handler));
105         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateZoom_Context::event_mouse_click_handler));
106         //insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateZoom_Context::event_mouse_click_handler));
107         insert(event_def(EVENT_WORKAREA_BOX,&StateZoom_Context::event_mouse_click_handler));
108         //insert(event_def(EVENT_WORKAREA_BUTTON_CLICK,&StateZoom_Context::event_mouse_click_handler));
109         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateZoom_Context::event_mouse_click_handler));
110 }
111
112 StateZoom::~StateZoom()
113 {
114 }
115
116 StateZoom_Context::StateZoom_Context(CanvasView* canvas_view):
117         canvas_view_(canvas_view),
118         is_working(*canvas_view),
119         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks())
120 {
121         // Turn off layer clicking
122         get_work_area()->set_allow_layer_clicks(false);
123
124         // clear out the ducks
125         get_work_area()->clear_ducks(); //???
126
127         // Refresh the work area
128         get_work_area()->queue_draw();
129
130         // Hide the tables if they are showing
131         //prev_table_status=get_canvas_view()->tables_are_visible();
132         //if(prev_table_status)get_canvas_view()->hide_tables();
133
134         // Disable the time bar
135         //get_canvas_view()->set_sensitive_timebar(false);
136
137         // Connect a signal
138         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateZoom_Context::on_user_click));
139         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
140
141         App::toolbox->refresh();
142 }
143
144 StateZoom_Context::~StateZoom_Context()
145 {
146         // Restore layer clicking
147         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
148         get_canvas_view()->work_area->reset_cursor();
149
150         // Enable the time bar
151         //get_canvas_view()->set_sensitive_timebar(true);
152
153         // Bring back the tables if they were out before
154         //if(prev_table_status)get_canvas_view()->show_tables();
155
156         // Refresh the work area
157         get_work_area()->queue_draw();
158
159         App::toolbox->refresh();
160
161         get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
162 }
163
164 Smach::event_result
165 StateZoom_Context::event_stop_handler(const Smach::event& /*x*/)
166 {
167         throw Smach::egress_exception();
168 }
169
170 Smach::event_result
171 StateZoom_Context::event_refresh_handler(const Smach::event& /*x*/)
172 {
173         return Smach::RESULT_ACCEPT;
174 }
175
176 Smach::event_result
177 StateZoom_Context::event_mouse_click_handler(const Smach::event& x)
178 {
179         if(x.key==EVENT_WORKAREA_BOX)
180         {
181                 const EventBox& event(*reinterpret_cast<const EventBox*>(&x));
182
183                 if(event.button==BUTTON_LEFT)
184                 {
185                         //respond to event box...
186
187
188                         //Center the new position at the center of the box
189
190                         //OH MY GOD HACK - the space is -1* and offset (by the value of the center of the canvas)...
191                         Point newpos;
192                         {
193                                 const Point evcenter = (event.p1+event.p2)/2;
194                                 const Point realcenter = (get_work_area()->get_window_tl() + get_work_area()->get_window_br())/2;
195                                 newpos = -(evcenter - realcenter) + get_work_area()->get_focus_point();
196                         }
197
198                         //The zoom will be whatever the required factor to convert current box size to desired box size
199                         Point tl = get_work_area()->get_window_tl();
200                         Point br = get_work_area()->get_window_br();
201
202                         Vector  span = br - tl;
203                         Vector  v = event.p2 - event.p1;
204
205                         //get the minimum zoom as long as it's greater than 1...
206                         v[0] = abs(v[0])/abs(span[0]);
207                         v[1] = abs(v[1])/abs(span[1]);
208
209                         float zdiv = max(v[0],v[1]);
210                         if(zdiv < 1 && zdiv > 0) //must be zoomable
211                         {
212                                 get_work_area()->set_focus_point(newpos);
213                                 get_work_area()->set_zoom(get_work_area()->get_zoom()/zdiv);
214                         }
215
216                         return Smach::RESULT_ACCEPT;
217                 }
218         }
219
220         if(x.key==EVENT_WORKAREA_MOUSE_BUTTON_UP)
221         {
222                 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
223
224                 if(event.button==BUTTON_LEFT)
225                 {
226                         Point evpos;
227
228                         //make the event pos be in the same space...
229                         //   The weird ass inverted center normalized space...
230                         {
231                                 const Point realcenter = (get_work_area()->get_window_tl() + get_work_area()->get_window_br())/2;
232                                 evpos = -(event.pos - realcenter) + get_work_area()->get_focus_point();
233                         }
234
235                         /*      Zooming:
236                                 focus point must zoom about the point evpos...
237
238                                 trans about an origin not 0:
239                                 p' = A(p - o) + o
240                         */
241
242                         Vector v = get_work_area()->get_focus_point() - evpos;
243
244                         if(event.modifier & Gdk::CONTROL_MASK) //zoom out...
245                         {
246                                 v*=ZOOMFACTOR;
247                                 //get_work_area()->zoom_out();
248                                 get_work_area()->set_focus_point(evpos + v);
249                                 get_work_area()->set_zoom(get_work_area()->get_zoom()/ZOOMFACTOR);
250                         }else //zoom in
251                         {
252                                 v/=ZOOMFACTOR;
253                                 //get_work_area()->zoom_in();
254                                 get_work_area()->set_focus_point(evpos + v);
255                                 get_work_area()->set_zoom(get_work_area()->get_zoom()*ZOOMFACTOR);
256                         }
257
258                         return Smach::RESULT_ACCEPT;
259                 }
260         }
261
262         return Smach::RESULT_OK;
263 }