Merged two 'if' statements into one.
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_rectangle.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_rectangle.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_rectangle.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 /* === G L O B A L S ======================================================= */
64
65 StateRectangle studio::state_rectangle;
66
67 /* === C L A S S E S & S T R U C T S ======================================= */
68
69 class studio::StateRectangle_Context : public sigc::trackable
70 {
71         etl::handle<CanvasView> canvas_view_;
72         CanvasView::IsWorking is_working;
73
74         Duckmatic::Push duckmatic_push;
75
76         Point point_holder;
77
78         etl::handle<Duck> point2_duck;
79
80         void refresh_ducks();
81
82         bool prev_workarea_layer_status_;
83
84         //Toolbox settings
85         synfigapp::Settings& settings;
86
87         //Toolbox display
88         Gtk::Table options_table;
89
90         Gtk::Entry              entry_id; //what to name the layer
91
92         Gtk::Adjustment adj_expand;
93         Gtk::SpinButton spin_expand;
94
95         Gtk::CheckButton check_invert;
96
97 public:
98
99         synfig::String get_id()const { return entry_id.get_text(); }
100         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
101
102         Real get_expand()const { return adj_expand.get_value(); }
103         void set_expand(Real f) { adj_expand.set_value(f); }
104
105         bool get_invert()const { return check_invert.get_active(); }
106         void set_invert(bool i) { check_invert.set_active(i); }
107
108         void refresh_tool_options(); //to refresh the toolbox
109
110         //events
111         Smach::event_result event_stop_handler(const Smach::event& x);
112         Smach::event_result event_refresh_handler(const Smach::event& x);
113         Smach::event_result event_mouse_click_handler(const Smach::event& x);
114         Smach::event_result event_refresh_tool_options(const Smach::event& x);
115
116         //constructor destructor
117         StateRectangle_Context(CanvasView* canvas_view);
118         ~StateRectangle_Context();
119
120         //Canvas interaction
121         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
122         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
123         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
124         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
125
126         //Modifying settings etc.
127         void load_settings();
128         void save_settings();
129         void reset();
130         void increment_id();
131         bool egress_on_selection_change;
132         Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
133         {
134                 if(egress_on_selection_change)
135                         throw Smach::egress_exception();
136                 return Smach::RESULT_OK;
137         }
138
139         void make_rectangle(const Point& p1, const Point& p2);
140
141 };      // END of class StateGradient_Context
142
143 /* === M E T H O D S ======================================================= */
144
145 StateRectangle::StateRectangle():
146         Smach::state<StateRectangle_Context>("rectangle")
147 {
148         insert(event_def(EVENT_STOP,&StateRectangle_Context::event_stop_handler));
149         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateRectangle_Context::event_layer_selection_changed_handler));
150         insert(event_def(EVENT_REFRESH,&StateRectangle_Context::event_refresh_handler));
151         insert(event_def(EVENT_REFRESH_DUCKS,&StateRectangle_Context::event_refresh_handler));
152         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateRectangle_Context::event_mouse_click_handler));
153         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateRectangle_Context::event_mouse_click_handler));
154         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateRectangle_Context::event_mouse_click_handler));
155         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateRectangle_Context::event_refresh_tool_options));
156 }
157
158 StateRectangle::~StateRectangle()
159 {
160 }
161
162 void
163 StateRectangle_Context::load_settings()
164 {
165         String value;
166
167         //parse the arguments yargh!
168         if(settings.get_value("rectangle.id",value))
169                 set_id(value);
170         else
171                 set_id("Rectangle");
172
173         if(settings.get_value("rectangle.expand",value))
174                 set_expand(atof(value.c_str()));
175         else
176                 set_expand(0);
177
178         if(settings.get_value("rectangle.invert",value) && value != "0")
179                 set_invert(true);
180         else
181                 set_invert(false);
182 }
183
184 void
185 StateRectangle_Context::save_settings()
186 {
187         settings.set_value("rectangle.id",get_id().c_str());
188         settings.set_value("rectangle.expand",strprintf("%f",get_expand()));
189         settings.set_value("rectangle.invert",get_invert()?"1":"0");
190 }
191
192 void
193 StateRectangle_Context::reset()
194 {
195         refresh_ducks();
196 }
197
198 void
199 StateRectangle_Context::increment_id()
200 {
201         String id(get_id());
202         int number=1;
203         int digits=0;
204
205         if(id.empty())
206                 id="Circle";
207
208         // If there is a number
209         // already at the end of the
210         // id, then remove it.
211         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
212         {
213                 // figure out how many digits it is
214                 for(digits=0;(int)id.size()-1>=digits && id[id.size()-1-digits]<='9' && id[id.size()-1-digits]>='0';digits++)while(false);
215
216                 String str_number;
217                 str_number=String(id,id.size()-digits,id.size());
218                 id=String(id,0,id.size()-digits);
219
220                 number=atoi(str_number.c_str());
221         }
222         else
223         {
224                 number=1;
225                 digits=3;
226         }
227
228         number++;
229
230         // Add the number back onto the id
231         {
232                 const String format(strprintf("%%0%dd",digits));
233                 id+=strprintf(format.c_str(),number);
234         }
235
236         // Set the ID
237         set_id(id);
238 }
239
240 StateRectangle_Context::StateRectangle_Context(CanvasView* canvas_view):
241         canvas_view_(canvas_view),
242         is_working(*canvas_view),
243         duckmatic_push(get_work_area()),
244         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
245         settings(synfigapp::Main::get_selected_input_device()->settings()),
246         entry_id(),
247         adj_expand(0,0,1,0.01,0.1),
248         spin_expand(adj_expand,0.1,3),
249         check_invert(_("Invert"))
250 {
251         egress_on_selection_change=true;
252         load_settings();
253
254         // Set up the tool options dialog
255         //options_table.attach(*manage(new Gtk::Label(_("Circle Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
256         options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
257
258         //expand stuff
259         options_table.attach(*manage(new Gtk::Label(_("Expansion:"))), 0, 1, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
260         options_table.attach(spin_expand, 1, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
261
262         //invert flag
263         options_table.attach(check_invert, 1, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
264
265         options_table.show_all();
266
267         //App::dialog_tool_options->set_widget(options_table);
268         refresh_tool_options();
269         App::dialog_tool_options->present();
270
271         // Turn off layer clicking
272         get_work_area()->set_allow_layer_clicks(false);
273
274         // clear out the ducks
275         get_work_area()->clear_ducks();
276
277         // Refresh the work area
278         get_work_area()->queue_draw();
279
280         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
281
282         // Hide the tables if they are showing
283         //prev_table_status=get_canvas_view()->tables_are_visible();
284         //if(prev_table_status)get_canvas_view()->hide_tables();
285
286         // Disable the time bar
287         //get_canvas_view()->set_sensitive_timebar(false);
288
289         // Connect a signal
290         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateRectangle_Context::on_user_click));
291
292         App::toolbox->refresh();
293 }
294
295 void
296 StateRectangle_Context::refresh_tool_options()
297 {
298         App::dialog_tool_options->clear();
299         App::dialog_tool_options->set_widget(options_table);
300         App::dialog_tool_options->set_local_name(_("Rectangle Tool"));
301         App::dialog_tool_options->set_name("rectangle");
302 }
303
304 Smach::event_result
305 StateRectangle_Context::event_refresh_tool_options(const Smach::event& /*x*/)
306 {
307         refresh_tool_options();
308         return Smach::RESULT_ACCEPT;
309 }
310
311 StateRectangle_Context::~StateRectangle_Context()
312 {
313         save_settings();
314
315         // Restore layer clicking
316         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
317
318         get_canvas_view()->work_area->reset_cursor();
319
320         App::dialog_tool_options->clear();
321
322         // Enable the time bar
323         //get_canvas_view()->set_sensitive_timebar(true);
324
325         // Bring back the tables if they were out before
326         //if(prev_table_status)get_canvas_view()->show_tables();
327
328         // Refresh the work area
329         get_work_area()->queue_draw();
330
331         App::toolbox->refresh();
332 }
333
334 Smach::event_result
335 StateRectangle_Context::event_stop_handler(const Smach::event& /*x*/)
336 {
337         throw Smach::egress_exception();
338 }
339
340 Smach::event_result
341 StateRectangle_Context::event_refresh_handler(const Smach::event& /*x*/)
342 {
343         refresh_ducks();
344         return Smach::RESULT_ACCEPT;
345 }
346
347 void
348 StateRectangle_Context::make_rectangle(const Point& _p1, const Point& _p2)
349 {
350         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Rectangle"));
351         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
352
353         Layer::Handle layer;
354
355         Canvas::Handle canvas(get_canvas_view()->get_canvas());
356         int depth(0);
357
358         // we are temporarily using the layer to hold something
359         layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
360         if(layer)
361         {
362                 depth=layer->get_depth();
363                 canvas=layer->get_canvas();
364         }
365
366         //create the layer
367         layer=get_canvas_interface()->add_layer_to("rectangle",canvas,depth);
368
369         const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
370         const Point p1(transform.unperform(_p1));
371         const Point p2(transform.unperform(_p2));
372
373         //set all the parameters
374         layer->set_param("point1",p1);
375         get_canvas_interface()->signal_layer_param_changed()(layer,"point1");
376         layer->set_param("point2",p2);
377         get_canvas_interface()->signal_layer_param_changed()(layer,"point2");
378
379         layer->set_param("expand",get_expand());
380         get_canvas_interface()->signal_layer_param_changed()(layer,"expand");
381
382         layer->set_param("invert",get_invert());
383         get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
384
385         //name
386         layer->set_description(get_id());
387         get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
388
389         egress_on_selection_change=false;
390         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
391         get_canvas_interface()->get_selection_manager()->set_selected_layer(layer);
392         egress_on_selection_change=true;
393
394         //post clean up stuff...
395         reset();
396         increment_id();
397 }
398
399 Smach::event_result
400 StateRectangle_Context::event_mouse_click_handler(const Smach::event& x)
401 {
402         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
403
404         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
405         {
406                 point_holder=get_work_area()->snap_point_to_grid(event.pos);
407                 etl::handle<Duck> duck=new Duck();
408                 duck->set_point(point_holder);
409                 duck->set_name("p1");
410                 duck->set_type(Duck::TYPE_POSITION);
411                 get_work_area()->add_duck(duck);
412
413                 point2_duck=new Duck();
414                 point2_duck->set_point(point_holder);
415                 point2_duck->set_name("p2");
416                 point2_duck->set_type(Duck::TYPE_POSITION);
417                 point2_duck->set_box_duck(duck);
418                 get_work_area()->add_duck(point2_duck);
419
420                 return Smach::RESULT_ACCEPT;
421         }
422
423         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
424         {
425                 point2_duck->set_point(get_work_area()->snap_point_to_grid(event.pos));
426                 get_work_area()->queue_draw();
427                 return Smach::RESULT_ACCEPT;
428         }
429
430         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
431         {
432                 make_rectangle(point_holder, get_work_area()->snap_point_to_grid(event.pos));
433                 get_work_area()->clear_ducks();
434                 return Smach::RESULT_ACCEPT;
435         }
436
437         return Smach::RESULT_OK;
438 }
439
440
441 void
442 StateRectangle_Context::refresh_ducks()
443 {
444         get_work_area()->clear_ducks();
445         get_work_area()->queue_draw();
446 }