1 /* === S Y N F I G ========================================================= */
2 /*! \file state_rectangle.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
35 #include <synfig/valuenode_dynamiclist.h>
36 #include <synfigapp/action_system.h>
38 #include "state_rectangle.h"
39 #include "canvasview.h"
43 #include <synfigapp/action.h>
44 #include "event_mouse.h"
45 #include "event_layerclick.h"
47 #include "dialog_tooloptions.h"
48 #include <gtkmm/optionmenu.h>
50 #include <synfigapp/main.h>
56 /* === U S I N G =========================================================== */
60 using namespace synfig;
61 using namespace studio;
63 /* === M A C R O S ========================================================= */
65 /* === G L O B A L S ======================================================= */
67 StateRectangle studio::state_rectangle;
69 /* === C L A S S E S & S T R U C T S ======================================= */
71 class studio::StateRectangle_Context : public sigc::trackable
73 etl::handle<CanvasView> canvas_view_;
74 CanvasView::IsWorking is_working;
76 Duckmatic::Push duckmatic_push;
80 etl::handle<Duck> point2_duck;
84 bool prev_workarea_layer_status_;
87 synfigapp::Settings& settings;
90 Gtk::Table options_table;
92 Gtk::Entry entry_id; //what to name the layer
94 Gtk::Adjustment adj_expand;
95 Gtk::SpinButton spin_expand;
97 Gtk::CheckButton check_invert;
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); }
104 Real get_expand()const { return adj_expand.get_value(); }
105 void set_expand(Real f) { adj_expand.set_value(f); }
107 bool get_invert()const { return check_invert.get_active(); }
108 void set_invert(bool i) { check_invert.set_active(i); }
110 void refresh_tool_options(); //to refresh the toolbox
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);
118 //constructor destructor
119 StateRectangle_Context(CanvasView* canvas_view);
120 ~StateRectangle_Context();
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();}
128 //Modifying settings etc.
129 void load_settings();
130 void save_settings();
133 bool egress_on_selection_change;
134 Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
136 if(egress_on_selection_change)
137 throw Smach::egress_exception();
138 return Smach::RESULT_OK;
141 void make_rectangle(const Point& p1, const Point& p2);
143 }; // END of class StateGradient_Context
145 /* === M E T H O D S ======================================================= */
147 StateRectangle::StateRectangle():
148 Smach::state<StateRectangle_Context>("rectangle")
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));
160 StateRectangle::~StateRectangle()
165 StateRectangle_Context::load_settings()
169 //parse the arguments yargh!
170 if(settings.get_value("rectangle.id",value))
175 if(settings.get_value("rectangle.expand",value))
176 set_expand(atof(value.c_str()));
180 if(settings.get_value("rectangle.invert",value) && value != "0")
187 StateRectangle_Context::save_settings()
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");
195 StateRectangle_Context::reset()
201 StateRectangle_Context::increment_id()
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')
215 // figure out how many digits it is
217 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
222 str_number=String(id,id.size()-digits,id.size());
223 id=String(id,0,id.size()-digits);
225 number=atoi(str_number.c_str());
235 // Add the number back onto the id
237 const String format(strprintf("%%0%dd",digits));
238 id+=strprintf(format.c_str(),number);
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()),
252 adj_expand(0,0,1,0.01,0.1),
253 spin_expand(adj_expand,0.1,3),
254 check_invert(_("Invert"))
256 egress_on_selection_change=true;
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);
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);
268 options_table.attach(check_invert, 1, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
270 options_table.show_all();
272 //App::dialog_tool_options->set_widget(options_table);
273 refresh_tool_options();
274 App::dialog_tool_options->present();
276 // Turn off layer clicking
277 get_work_area()->set_allow_layer_clicks(false);
279 // clear out the ducks
280 get_work_area()->clear_ducks();
282 // Refresh the work area
283 get_work_area()->queue_draw();
285 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
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();
291 // Disable the time bar
292 //get_canvas_view()->set_sensitive_timebar(false);
295 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateRectangle_Context::on_user_click));
297 App::toolbox->refresh();
301 StateRectangle_Context::refresh_tool_options()
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");
310 StateRectangle_Context::event_refresh_tool_options(const Smach::event& /*x*/)
312 refresh_tool_options();
313 return Smach::RESULT_ACCEPT;
316 StateRectangle_Context::~StateRectangle_Context()
320 // Restore layer clicking
321 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
323 get_canvas_view()->work_area->reset_cursor();
325 App::dialog_tool_options->clear();
327 // Enable the time bar
328 //get_canvas_view()->set_sensitive_timebar(true);
330 // Bring back the tables if they were out before
331 //if(prev_table_status)get_canvas_view()->show_tables();
333 // Refresh the work area
334 get_work_area()->queue_draw();
336 App::toolbox->refresh();
340 StateRectangle_Context::event_stop_handler(const Smach::event& /*x*/)
342 throw Smach::egress_exception();
346 StateRectangle_Context::event_refresh_handler(const Smach::event& /*x*/)
349 return Smach::RESULT_ACCEPT;
353 StateRectangle_Context::make_rectangle(const Point& _p1, const Point& _p2)
355 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Rectangle"));
356 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
360 Canvas::Handle canvas(get_canvas_view()->get_canvas());
363 // we are temporarily using the layer to hold something
364 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
367 depth=layer->get_depth();
368 canvas=layer->get_canvas();
372 layer=get_canvas_interface()->add_layer_to("rectangle",canvas,depth);
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));
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");
384 layer->set_param("expand",get_expand());
385 get_canvas_interface()->signal_layer_param_changed()(layer,"expand");
387 layer->set_param("invert",get_invert());
388 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
391 layer->set_description(get_id());
392 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
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;
399 //post clean up stuff...
405 StateRectangle_Context::event_mouse_click_handler(const Smach::event& x)
407 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
409 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
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);
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);
425 return Smach::RESULT_ACCEPT;
428 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
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;
435 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
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;
442 return Smach::RESULT_OK;
447 StateRectangle_Context::refresh_ducks()
449 get_work_area()->clear_ducks();
450 get_work_area()->queue_draw();