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