Fix 1368904: disable the timebar instead of hiding it when switching to tools that...
[synfig.git] / synfig-studio / trunk / src / gtkmm / mod_mirror / state_mirror.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_mirror.cpp
3 **      \brief Template 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 <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
34
35 #include <synfig/valuenode_dynamiclist.h>
36 #include <synfigapp/action_system.h>
37
38 #include "state_mirror.h"
39 #include "../canvasview.h"
40 #include "../workarea.h"
41 #include "../app.h"
42
43 #include <synfigapp/action.h>
44 #include "../event_mouse.h"
45 #include "../event_layerclick.h"
46 #include "../toolbox.h"
47 #include "../dialog_tooloptions.h"
48 #include <gtkmm/optionmenu.h>
49 #include "../duck.h"
50 #include <synfigapp/main.h>
51
52 #endif
53
54 /* === U S I N G =========================================================== */
55
56 using namespace std;
57 using namespace etl;
58 using namespace synfig;
59 using namespace studio;
60
61 /* === M A C R O S ========================================================= */
62
63 enum Axis {
64         AXIS_X,
65         AXIS_Y
66 } ;
67
68 /* === G L O B A L S ======================================================= */
69
70 StateMirror studio::state_mirror;
71
72 /* === C L A S S E S & S T R U C T S ======================================= */
73
74 class DuckDrag_Mirror : public DuckDrag_Base
75 {
76         synfig::Vector center;
77
78         std::vector<synfig::Vector> positions;
79
80
81 public:
82         Axis axis;
83
84         DuckDrag_Mirror();
85         void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
86         bool end_duck_drag(Duckmatic* duckmatic);
87         void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
88 };
89
90
91 class studio::StateMirror_Context : public sigc::trackable
92 {
93         etl::handle<CanvasView> canvas_view_;
94
95         synfigapp::Settings& settings;
96
97         etl::handle<DuckDrag_Mirror> duck_dragger_;
98
99         Gtk::Table options_table;
100
101
102         Gtk::CheckButton checkbutton_axis_x;
103         Gtk::CheckButton checkbutton_axis_y;
104
105 public:
106
107         Axis get_axis()const { return checkbutton_axis_x.get_active()?AXIS_X:AXIS_Y; }
108         void set_axis(Axis a)
109         {
110                 if(a==AXIS_X)
111                 {
112                         checkbutton_axis_x.set_active(true);
113                         checkbutton_axis_y.set_active(false);
114                 }
115                 else
116                 {
117                         checkbutton_axis_y.set_active(true);
118                         checkbutton_axis_x.set_active(false);
119                 }
120
121                 duck_dragger_->axis=get_axis();
122         }
123
124         void update_axis_y()
125         {
126                 checkbutton_axis_x.set_active(!checkbutton_axis_y.get_active());
127                 duck_dragger_->axis=get_axis();
128         }
129         void update_axis_x()
130         {
131                 checkbutton_axis_y.set_active(!checkbutton_axis_x.get_active());
132                 duck_dragger_->axis=get_axis();
133         }
134         Smach::event_result event_refresh_tool_options(const Smach::event& x);
135
136         void refresh_tool_options();
137
138         StateMirror_Context(CanvasView* canvas_view);
139
140         ~StateMirror_Context();
141
142         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
143         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
144         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
145         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
146
147         void load_settings();
148         void save_settings();
149 };      // END of class StateMirror_Context
150
151 /* === M E T H O D S ======================================================= */
152
153 StateMirror::StateMirror():
154         Smach::state<StateMirror_Context>("mirror")
155 {
156         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateMirror_Context::event_refresh_tool_options));
157 }
158
159 StateMirror::~StateMirror()
160 {
161 }
162
163 void
164 StateMirror_Context::load_settings()
165 {
166         String value;
167
168         settings.get_value("mirror.axis",value);
169         set_axis((Axis)atoi(value.c_str()));
170 }
171
172 void
173 StateMirror_Context::save_settings()
174 {
175         settings.set_value("mirror.lock_aspect",strprintf("%d",(int)get_axis()));
176 }
177
178 StateMirror_Context::StateMirror_Context(CanvasView* canvas_view):
179         canvas_view_(canvas_view),
180         settings(synfigapp::Main::get_selected_input_device()->settings()),
181         duck_dragger_(new DuckDrag_Mirror()),
182         checkbutton_axis_x(_("Horizontal")),
183         checkbutton_axis_y(_("Vertical"))
184 {
185         // Set up the tool options dialog
186         options_table.attach(*manage(new Gtk::Label(_("Mirror Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
187         options_table.attach(checkbutton_axis_x, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
188         options_table.attach(checkbutton_axis_y, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
189
190         checkbutton_axis_x.signal_toggled().connect(sigc::mem_fun(*this,&StateMirror_Context::update_axis_x));
191         checkbutton_axis_y.signal_toggled().connect(sigc::mem_fun(*this,&StateMirror_Context::update_axis_y));
192
193         options_table.show_all();
194         refresh_tool_options();
195         App::dialog_tool_options->present();
196
197         get_work_area()->allow_layer_clicks=true;
198         get_work_area()->set_duck_dragger(duck_dragger_);
199
200 //      get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
201         get_canvas_view()->work_area->reset_cursor();
202
203         App::toolbox->refresh();
204
205         set_axis(AXIS_X);
206         load_settings();
207 }
208
209 void
210 StateMirror_Context::refresh_tool_options()
211 {
212         App::dialog_tool_options->clear();
213         App::dialog_tool_options->set_widget(options_table);
214         App::dialog_tool_options->set_local_name(_("Mirror Tool"));
215         App::dialog_tool_options->set_name("mirror");
216 }
217
218 Smach::event_result
219 StateMirror_Context::event_refresh_tool_options(const Smach::event& x)
220 {
221         refresh_tool_options();
222         return Smach::RESULT_ACCEPT;
223 }
224
225 StateMirror_Context::~StateMirror_Context()
226 {
227         save_settings();
228
229         get_work_area()->clear_duck_dragger();
230         get_canvas_view()->work_area->reset_cursor();
231
232         App::dialog_tool_options->clear();
233
234         App::toolbox->refresh();
235 }
236
237
238
239
240 DuckDrag_Mirror::DuckDrag_Mirror():
241         axis(AXIS_X)
242 {
243 }
244
245 #ifndef EPSILON
246 #define EPSILON 0.0000001
247 #endif
248
249 void
250 DuckDrag_Mirror::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
251 {
252
253
254         const DuckList selected_ducks(duckmatic->get_selected_ducks());
255         DuckList::const_iterator iter;
256
257         positions.clear();
258         int i;
259         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
260         {
261                 Point p((*iter)->get_trans_point());
262                 positions.push_back(p);
263         }
264
265 }
266
267
268 void
269 DuckDrag_Mirror::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
270 {
271         center=vector;
272         int i;
273
274                 const DuckList selected_ducks(duckmatic->get_selected_ducks());
275                 DuckList::const_iterator iter;
276         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
277         {
278                 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
279
280                 Vector p(positions[i]);
281                 //Point p((*iter)->get_trans_point());
282
283                 if(axis==AXIS_X)
284                         p[0]=-(p[0]-center[0])+center[0];
285                 if(axis==AXIS_Y)
286                         p[1]=-(p[1]-center[1])+center[1];
287
288                 (*iter)->set_trans_point(p);
289         }
290         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
291         {
292                 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
293
294                 Vector p(positions[i]);
295                 //Point p((*iter)->get_trans_point());
296
297                 if(axis==AXIS_X)
298                         p[0]=-(p[0]-center[0])+center[0];
299                 if(axis==AXIS_Y)
300                         p[1]=-(p[1]-center[1])+center[1];
301
302                 (*iter)->set_trans_point(p);
303         }
304 }
305
306 bool
307 DuckDrag_Mirror::end_duck_drag(Duckmatic* duckmatic)
308 {
309         duckmatic->signal_edited_selected_ducks();
310         return true;
311 }