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