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>
54 /* === U S I N G =========================================================== */
58 using namespace synfig;
59 using namespace studio;
61 /* === M A C R O S ========================================================= */
63 /* === G L O B A L S ======================================================= */
65 StateRectangle studio::state_rectangle;
67 /* === C L A S S E S & S T R U C T S ======================================= */
69 class studio::StateRectangle_Context : public sigc::trackable
71 etl::handle<CanvasView> canvas_view_;
72 CanvasView::IsWorking is_working;
74 Duckmatic::Push duckmatic_push;
78 etl::handle<Duck> point2_duck;
82 bool prev_workarea_layer_status_;
85 synfigapp::Settings& settings;
88 Gtk::Table options_table;
90 Gtk::Entry entry_id; //what to name the layer
92 Gtk::Adjustment adj_expand;
93 Gtk::SpinButton spin_expand;
95 Gtk::CheckButton check_invert;
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); }
102 Real get_expand()const { return adj_expand.get_value(); }
103 void set_expand(Real f) { adj_expand.set_value(f); }
105 bool get_invert()const { return check_invert.get_active(); }
106 void set_invert(bool i) { check_invert.set_active(i); }
108 void refresh_tool_options(); //to refresh the toolbox
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);
116 //constructor destructor
117 StateRectangle_Context(CanvasView* canvas_view);
118 ~StateRectangle_Context();
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();}
126 //Modifying settings etc.
127 void load_settings();
128 void save_settings();
131 bool no_egress_on_selection_change;
132 Smach::event_result event_layer_selection_changed_handler(const Smach::event& x)
134 if(!no_egress_on_selection_change)
135 throw Smach::egress_exception();
136 return Smach::RESULT_OK;
139 void make_rectangle(const Point& p1, const Point& p2);
141 }; // END of class StateGradient_Context
143 /* === M E T H O D S ======================================================= */
145 StateRectangle::StateRectangle():
146 Smach::state<StateRectangle_Context>("rectangle")
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));
158 StateRectangle::~StateRectangle()
163 StateRectangle_Context::load_settings()
167 //parse the arguments yargh!
168 if(settings.get_value("rectangle.id",value))
173 if(settings.get_value("rectangle.expand",value))
174 set_expand(atof(value.c_str()));
178 if(settings.get_value("rectangle.invert",value) && value != "0")
185 StateRectangle_Context::save_settings()
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");
193 StateRectangle_Context::reset()
199 StateRectangle_Context::increment_id()
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')
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);
217 str_number=String(id,id.size()-digits,id.size());
218 id=String(id,0,id.size()-digits);
220 number=atoi(str_number.c_str());
230 // Add the number back onto the id
232 const String format(strprintf("%%0%dd",digits));
233 id+=strprintf(format.c_str(),number);
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()),
247 adj_expand(0,0,1,0.01,0.1),
248 spin_expand(adj_expand,0.1,3),
249 check_invert(_("Invert"))
251 no_egress_on_selection_change=false;
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);
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);
263 options_table.attach(check_invert, 1, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
265 options_table.show_all();
267 //App::dialog_tool_options->set_widget(options_table);
268 refresh_tool_options();
269 App::dialog_tool_options->present();
271 // Turn off layer clicking
272 get_work_area()->allow_layer_clicks=false;
274 // clear out the ducks
275 get_work_area()->clear_ducks();
277 // Refresh the work area
278 get_work_area()->queue_draw();
280 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
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();
287 //get_canvas_view()->hide_timebar();
290 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateRectangle_Context::on_user_click));
292 App::toolbox->refresh();
296 StateRectangle_Context::refresh_tool_options()
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");
305 StateRectangle_Context::event_refresh_tool_options(const Smach::event& x)
307 refresh_tool_options();
308 return Smach::RESULT_ACCEPT;
311 StateRectangle_Context::~StateRectangle_Context()
315 // Restore layer clicking
316 get_work_area()->allow_layer_clicks = prev_workarea_layer_status_;
318 get_canvas_view()->work_area->reset_cursor();
320 App::dialog_tool_options->clear();
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();
326 // Bring back the tables if they were out before
327 //if(prev_table_status)get_canvas_view()->show_tables();
329 // Refresh the work area
330 get_work_area()->queue_draw();
332 App::toolbox->refresh();
336 StateRectangle_Context::event_stop_handler(const Smach::event& x)
338 throw Smach::egress_exception();
342 StateRectangle_Context::event_refresh_handler(const Smach::event& x)
345 return Smach::RESULT_ACCEPT;
349 StateRectangle_Context::make_rectangle(const Point& _p1, const Point& _p2)
351 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Rectangle"));
352 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
356 Canvas::Handle canvas(get_canvas_view()->get_canvas());
359 // we are temporarily using the layer to hold something
360 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
363 depth=layer->get_depth();
364 canvas=layer->get_canvas();
368 layer=get_canvas_interface()->add_layer_to("rectangle",canvas,depth);
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));
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");
380 layer->set_param("expand",get_expand());
381 get_canvas_interface()->signal_layer_param_changed()(layer,"expand");
383 layer->set_param("invert",get_invert());
384 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
387 layer->set_description(get_id());
388 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
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;
395 //post clean up stuff...
401 StateRectangle_Context::event_mouse_click_handler(const Smach::event& x)
403 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
405 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
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);
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);
421 return Smach::RESULT_ACCEPT;
424 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
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;
431 if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
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;
438 return Smach::RESULT_OK;
443 StateRectangle_Context::refresh_ducks()
445 get_work_area()->clear_ducks();
446 get_work_area()->queue_draw();