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