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