Implements PXEGeek's http://wiki.synfig.com/Wish_list entry: "Optionally display...
[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 no_egress_on_selection_change;
132         Smach::event_result event_layer_selection_changed_handler(const Smach::event& x)
133         {
134                 if(!no_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()->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         no_egress_on_selection_change=false;
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()->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         // Hide the time bar
287         //get_canvas_view()->hide_timebar();
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()->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         // Show the time bar
323         if(get_canvas_view()->get_canvas()->rend_desc().get_time_start()!=get_canvas_view()->get_canvas()->rend_desc().get_time_end())
324                 get_canvas_view()->show_timebar();
325
326         // Bring back the tables if they were out before
327         //if(prev_table_status)get_canvas_view()->show_tables();
328
329         // Refresh the work area
330         get_work_area()->queue_draw();
331
332         App::toolbox->refresh();
333 }
334
335 Smach::event_result
336 StateRectangle_Context::event_stop_handler(const Smach::event& x)
337 {
338         throw Smach::egress_exception();
339 }
340
341 Smach::event_result
342 StateRectangle_Context::event_refresh_handler(const Smach::event& x)
343 {
344         refresh_ducks();
345         return Smach::RESULT_ACCEPT;
346 }
347
348 void
349 StateRectangle_Context::make_rectangle(const Point& _p1, const Point& _p2)
350 {
351         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Rectangle"));
352         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
353
354         Layer::Handle layer;
355
356         Canvas::Handle canvas(get_canvas_view()->get_canvas());
357         int depth(0);
358
359         // we are temporarily using the layer to hold something
360         layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
361         if(layer)
362         {
363                 depth=layer->get_depth();
364                 canvas=layer->get_canvas();
365         }
366
367         //create the layer
368         layer=get_canvas_interface()->add_layer_to("rectangle",canvas,depth);
369
370         const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
371         const Point p1(transform.unperform(_p1));
372         const Point p2(transform.unperform(_p2));
373
374         //set all the parameters
375         layer->set_param("point1",p1);
376         get_canvas_interface()->signal_layer_param_changed()(layer,"point1");
377         layer->set_param("point2",p2);
378         get_canvas_interface()->signal_layer_param_changed()(layer,"point2");
379
380         layer->set_param("expand",get_expand());
381         get_canvas_interface()->signal_layer_param_changed()(layer,"expand");
382
383         layer->set_param("invert",get_invert());
384         get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
385
386         //name
387         layer->set_description(get_id());
388         get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
389
390         no_egress_on_selection_change=true;
391         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
392         get_canvas_interface()->get_selection_manager()->set_selected_layer(layer);
393         no_egress_on_selection_change=false;
394
395         //post clean up stuff...
396         reset();
397         increment_id();
398 }
399
400 Smach::event_result
401 StateRectangle_Context::event_mouse_click_handler(const Smach::event& x)
402 {
403         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
404
405         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
406         {
407                 point_holder=get_work_area()->snap_point_to_grid(event.pos);
408                 etl::handle<Duck> duck=new Duck();
409                 duck->set_point(point_holder);
410                 duck->set_name("p1");
411                 duck->set_type(Duck::TYPE_POSITION);
412                 get_work_area()->add_duck(duck);
413
414                 point2_duck=new Duck();
415                 point2_duck->set_point(point_holder);
416                 point2_duck->set_name("p2");
417                 point2_duck->set_type(Duck::TYPE_POSITION);
418                 point2_duck->set_box_duck(duck);
419                 get_work_area()->add_duck(point2_duck);
420
421                 return Smach::RESULT_ACCEPT;
422         }
423
424         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
425         {
426                 point2_duck->set_point(get_work_area()->snap_point_to_grid(event.pos));
427                 get_work_area()->queue_draw();
428                 return Smach::RESULT_ACCEPT;
429         }
430
431         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
432         {
433                 make_rectangle(point_holder, get_work_area()->snap_point_to_grid(event.pos));
434                 get_work_area()->clear_ducks();
435                 return Smach::RESULT_ACCEPT;
436         }
437
438         return Smach::RESULT_OK;
439 }
440
441
442 void
443 StateRectangle_Context::refresh_ducks()
444 {
445         get_work_area()->clear_ducks();
446         get_work_area()->queue_draw();
447 }