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