Don't ask translators to translate compview.cpp since it isn't compiled. Fix typo...
[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 "dialog_tooloptions.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         //Toolbox settings
78         synfigapp::Settings& settings;
79
80         //Toolbox display
81         Gtk::Table options_table;
82
83 public:
84
85         void refresh_tool_options(); //to refresh the toolbox
86
87         //events
88         Smach::event_result event_stop_handler(const Smach::event& x);
89         Smach::event_result event_refresh_handler(const Smach::event& x);
90         Smach::event_result event_mouse_click_handler(const Smach::event& x);
91         Smach::event_result event_refresh_tool_options(const Smach::event& x);
92
93         //constructor destructor
94         StateZoom_Context(CanvasView* canvas_view);
95         ~StateZoom_Context();
96
97         //Canvas interaction
98         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
99         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
100         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
101         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
102
103         //Modifying settings etc.
104         void load_settings();
105         void save_settings();
106         void reset();
107
108         //void zoom(const Point& p1, const Point& p2);
109
110 };      // END of class StateGradient_Context
111
112 /* === M E T H O D S ======================================================= */
113
114 StateZoom::StateZoom():
115         Smach::state<StateZoom_Context>("zoom")
116 {
117         insert(event_def(EVENT_STOP,&StateZoom_Context::event_stop_handler));
118         insert(event_def(EVENT_REFRESH,&StateZoom_Context::event_refresh_handler));
119         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateZoom_Context::event_mouse_click_handler));
120         //insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateZoom_Context::event_mouse_click_handler));
121         insert(event_def(EVENT_WORKAREA_BOX,&StateZoom_Context::event_mouse_click_handler));
122         //insert(event_def(EVENT_WORKAREA_BUTTON_CLICK,&StateZoom_Context::event_mouse_click_handler));
123         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateZoom_Context::event_mouse_click_handler));
124         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateZoom_Context::event_refresh_tool_options));
125 }
126
127 StateZoom::~StateZoom()
128 {
129 }
130
131 void
132 StateZoom_Context::load_settings()
133 {
134         String value;
135
136         //parse the arguments yargh!
137         /*if(settings.get_value("circle.feather",value))
138                 set_feather(atof(value.c_str()));
139         else
140                 set_feather(0);*/
141 }
142
143 void
144 StateZoom_Context::save_settings()
145 {
146         //settings.set_value("circle.fallofftype",strprintf("%d",get_falloff()));
147 }
148
149 void
150 StateZoom_Context::reset()
151 {
152         //refresh_ducks();
153 }
154
155 StateZoom_Context::StateZoom_Context(CanvasView* canvas_view):
156         canvas_view_(canvas_view),
157         is_working(*canvas_view),
158         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
159         settings(synfigapp::Main::get_selected_input_device()->settings())
160 {
161         // Set up the tool options dialog
162         //options_table.attach(*manage(new Gtk::Label(_("Zoom Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
163
164         load_settings();
165
166         options_table.show_all();
167
168         refresh_tool_options();
169         App::dialog_tool_options->present();
170
171         // Turn off layer clicking
172         get_work_area()->set_allow_layer_clicks(false);
173
174         // clear out the ducks
175         get_work_area()->clear_ducks(); //???
176
177         // Refresh the work area
178         get_work_area()->queue_draw();
179
180         // Hide the tables if they are showing
181         //prev_table_status=get_canvas_view()->tables_are_visible();
182         //if(prev_table_status)get_canvas_view()->hide_tables();
183
184         // Disable the time bar
185         //get_canvas_view()->set_sensitive_timebar(false);
186
187         // Connect a signal
188         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateZoom_Context::on_user_click));
189         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
190
191         App::toolbox->refresh();
192 }
193
194 void
195 StateZoom_Context::refresh_tool_options()
196 {
197         App::dialog_tool_options->clear();
198         App::dialog_tool_options->set_widget(options_table);
199         App::dialog_tool_options->set_local_name(_("Zoom Tool"));
200         App::dialog_tool_options->set_name("zoom");
201 }
202
203 Smach::event_result
204 StateZoom_Context::event_refresh_tool_options(const Smach::event& /*x*/)
205 {
206         refresh_tool_options();
207         return Smach::RESULT_ACCEPT;
208 }
209
210 StateZoom_Context::~StateZoom_Context()
211 {
212         save_settings();
213
214         // Restore layer clicking
215         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
216         get_canvas_view()->work_area->reset_cursor();
217
218         App::dialog_tool_options->clear();
219
220         // Enable the time bar
221         //get_canvas_view()->set_sensitive_timebar(true);
222
223         // Bring back the tables if they were out before
224         //if(prev_table_status)get_canvas_view()->show_tables();
225
226         // Refresh the work area
227         get_work_area()->queue_draw();
228
229         App::toolbox->refresh();
230
231         get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
232 }
233
234 Smach::event_result
235 StateZoom_Context::event_stop_handler(const Smach::event& /*x*/)
236 {
237         throw Smach::egress_exception();
238 }
239
240 Smach::event_result
241 StateZoom_Context::event_refresh_handler(const Smach::event& /*x*/)
242 {
243         return Smach::RESULT_ACCEPT;
244 }
245
246 Smach::event_result
247 StateZoom_Context::event_mouse_click_handler(const Smach::event& x)
248 {
249         if(x.key==EVENT_WORKAREA_BOX)
250         {
251                 const EventBox& event(*reinterpret_cast<const EventBox*>(&x));
252
253                 if(event.button==BUTTON_LEFT)
254                 {
255                         //respond to event box...
256
257
258                         //Center the new position at the center of the box
259
260                         //OH MY GOD HACK - the space is -1* and offset (by the value of the center of the canvas)...
261                         Point newpos;
262                         {
263                                 const Point evcenter = (event.p1+event.p2)/2;
264                                 const Point realcenter = (get_work_area()->get_window_tl() + get_work_area()->get_window_br())/2;
265                                 newpos = -(evcenter - realcenter) + get_work_area()->get_focus_point();
266                         }
267
268                         //The zoom will be whatever the required factor to convert current box size to desired box size
269                         Point tl = get_work_area()->get_window_tl();
270                         Point br = get_work_area()->get_window_br();
271
272                         Vector  span = br - tl;
273                         Vector  v = event.p2 - event.p1;
274
275                         //get the minimum zoom as long as it's greater than 1...
276                         v[0] = abs(v[0])/abs(span[0]);
277                         v[1] = abs(v[1])/abs(span[1]);
278
279                         float zdiv = max(v[0],v[1]);
280                         if(zdiv < 1 && zdiv > 0) //must be zoomable
281                         {
282                                 get_work_area()->set_focus_point(newpos);
283                                 get_work_area()->set_zoom(get_work_area()->get_zoom()/zdiv);
284                         }
285
286                         return Smach::RESULT_ACCEPT;
287                 }
288         }
289
290         if(x.key==EVENT_WORKAREA_MOUSE_BUTTON_UP)
291         {
292                 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
293
294                 if(event.button==BUTTON_LEFT)
295                 {
296                         Point evpos;
297
298                         //make the event pos be in the same space...
299                         //   The weird ass inverted center normalized space...
300                         {
301                                 const Point realcenter = (get_work_area()->get_window_tl() + get_work_area()->get_window_br())/2;
302                                 evpos = -(event.pos - realcenter) + get_work_area()->get_focus_point();
303                         }
304
305                         /*      Zooming:
306                                 focus point must zoom about the point evpos...
307
308                                 trans about an origin not 0:
309                                 p' = A(p - o) + o
310                         */
311
312                         Vector v = get_work_area()->get_focus_point() - evpos;
313
314                         if(event.modifier & Gdk::CONTROL_MASK) //zoom out...
315                         {
316                                 v*=ZOOMFACTOR;
317                                 //get_work_area()->zoom_out();
318                                 get_work_area()->set_focus_point(evpos + v);
319                                 get_work_area()->set_zoom(get_work_area()->get_zoom()/ZOOMFACTOR);
320                         }else //zoom in
321                         {
322                                 v/=ZOOMFACTOR;
323                                 //get_work_area()->zoom_in();
324                                 get_work_area()->set_focus_point(evpos + v);
325                                 get_work_area()->set_zoom(get_work_area()->get_zoom()*ZOOMFACTOR);
326                         }
327
328                         return Smach::RESULT_ACCEPT;
329                 }
330         }
331
332         return Smach::RESULT_OK;
333 }