1 /* === S Y N F I G ========================================================= */
2 /*! \file state_sketch.cpp
3 ** \brief Template File
5 ** $Id: state_sketch.cpp,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include <gtkmm/dialog.h>
32 #include <gtkmm/entry.h>
34 #include <synfig/valuenode_dynamiclist.h>
36 #include "state_sketch.h"
37 #include "state_stroke.h"
38 #include "canvasview.h"
41 #include <synfig/valuenode_bline.h>
42 #include <ETL/hermite>
43 #include <ETL/calculus>
45 #include "event_mouse.h"
46 #include "event_layerclick.h"
49 #include <synfigapp/blineconvert.h>
50 #include <synfigapp/main.h>
52 #include <ETL/gaussian>
54 #include "dialog_tooloptions.h"
56 #include <gtkmm/table.h>
57 #include <gtkmm/label.h>
58 #include <gtkmm/button.h>
59 #include <gtkmm/checkbutton.h>
60 #include <gtkmm/actiongroup.h>
64 /* === U S I N G =========================================================== */
68 using namespace synfig;
69 using namespace studio;
71 /* === M A C R O S ========================================================= */
73 /* === G L O B A L S ======================================================= */
75 StateSketch studio::state_sketch;
77 /* === C L A S S E S & S T R U C T S ======================================= */
79 class studio::StateSketch_Context : public sigc::trackable
81 Glib::RefPtr<Gtk::ActionGroup> action_group;
83 etl::handle<CanvasView> canvas_view_;
84 CanvasView::IsWorking is_working;
86 bool prev_table_status;
87 bool prev_workarea_layer_status_;
89 Gtk::Table options_table;
90 Gtk::Button button_clear_sketch;
91 Gtk::Button button_undo_stroke;
92 Gtk::Button button_save_sketch;
93 Gtk::Button button_load_sketch;
94 Gtk::CheckButton checkbutton_show_sketch;
100 void toggle_show_sketch();
104 Smach::event_result event_stop_handler(const Smach::event& x);
106 Smach::event_result event_refresh_handler(const Smach::event& x);
108 Smach::event_result event_mouse_down_handler(const Smach::event& x);
110 Smach::event_result event_stroke(const Smach::event& x);
112 Smach::event_result event_refresh_tool_options(const Smach::event& x);
113 Smach::event_result event_yield_tool_options(const Smach::event& x);
115 void refresh_tool_options();
116 void yield_tool_options();
118 StateSketch_Context(CanvasView* canvas_view);
120 ~StateSketch_Context();
122 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
123 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
124 synfig::Time get_time()const { return get_canvas_interface()->get_time(); }
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 }; // END of class StateSketch_Context
131 /* === M E T H O D S ======================================================= */
133 StateSketch::StateSketch():
134 Smach::state<StateSketch_Context>("sketch")
136 insert(event_def(EVENT_STOP,&StateSketch_Context::event_stop_handler));
137 //insert(event_def(EVENT_REFRESH,&StateSketch_Context::event_refresh_handler));
138 insert(event_def(EVENT_REFRESH_DUCKS,&StateSketch_Context::event_refresh_handler));
139 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateSketch_Context::event_mouse_down_handler));
140 insert(event_def(EVENT_WORKAREA_STROKE,&StateSketch_Context::event_stroke));
141 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSketch_Context::event_refresh_tool_options));
142 insert(event_def(EVENT_YIELD_TOOL_OPTIONS,&StateSketch_Context::event_yield_tool_options));
145 StateSketch::~StateSketch()
150 StateSketch_Context::save_sketch()
152 synfig::String filename(basename(get_canvas()->get_file_name())+".sketch");
154 while(App::dialog_save_file(_("Save Sketch"), filename))
156 // If the filename still has wildcards, then we should
157 // continue looking for the file we want
158 if(find(filename.begin(),filename.end(),'*')!=filename.end())
161 if(get_work_area()->save_sketch(filename))
164 get_canvas_view()->get_ui_interface()->error(_("Unable to save sketch"));
169 StateSketch_Context::load_sketch()
171 synfig::String filename(basename(get_canvas()->get_file_name())+".sketch");
173 while(App::dialog_open_file(_("Load Sketch"), filename))
175 // If the filename still has wildcards, then we should
176 // continue looking for the file we want
177 if(find(filename.begin(),filename.end(),'*')!=filename.end())
180 if(get_work_area()->load_sketch(filename))
183 get_canvas_view()->get_ui_interface()->error(_("Unable to load sketch"));
185 get_work_area()->queue_draw();
189 StateSketch_Context::clear_sketch()
191 get_work_area()->clear_persistant_strokes();
192 get_canvas_view()->get_smach().process_event(EVENT_REFRESH);
196 StateSketch_Context::undo_stroke()
198 if(!get_work_area()->persistant_stroke_list().empty())
200 get_work_area()->persistant_stroke_list().pop_back();
201 get_canvas_view()->get_smach().process_event(EVENT_REFRESH);
206 StateSketch_Context::toggle_show_sketch()
208 get_work_area()->set_show_persistant_strokes(checkbutton_show_sketch.get_active());
209 get_work_area()->queue_draw();
212 StateSketch_Context::StateSketch_Context(CanvasView* canvas_view):
213 action_group(Gtk::ActionGroup::create()),
214 canvas_view_(canvas_view),
215 is_working(*canvas_view),
216 prev_workarea_layer_status_(get_work_area()->allow_layer_clicks),
217 button_clear_sketch(_("Clear Sketch")),
218 button_undo_stroke(_("Undo Stroke")),
219 button_save_sketch(_("Save Sketch")),
220 button_load_sketch(_("Load Sketch")),
221 checkbutton_show_sketch(_("Show Sketch"))
223 Glib::ustring ui_info =
225 " <toolbar action='toolbar-sketch'>"
226 " <toolitem action='sketch-undo' />"
227 " <toolitem action='sketch-clear' />"
228 " <toolitem action='sketch-save-as' />"
229 " <toolitem action='sketch-open' />"
233 action_group->add(Gtk::Action::create(
235 Gtk::StockID("gtk-undo"),
236 _("Undo Last Stroke"),
237 _("Undo Last Stroke")
241 &studio::StateSketch_Context::undo_stroke
245 action_group->add(Gtk::Action::create(
247 Gtk::StockID("gtk-clear"),
253 &studio::StateSketch_Context::clear_sketch
257 action_group->add(Gtk::Action::create(
259 Gtk::StockID("gtk-save-as"),
260 _("Save Sketch As..."),
261 _("Save Sketch As...")
265 &studio::StateSketch_Context::save_sketch
269 action_group->add(Gtk::Action::create(
271 Gtk::StockID("gtk-save-as"),
272 _("Save Sketch As..."),
273 _("Save Sketch As...")
277 &studio::StateSketch_Context::save_sketch
281 action_group->add(Gtk::Action::create(
283 Gtk::StockID("gtk-open"),
289 &studio::StateSketch_Context::load_sketch
293 action_group->add( Gtk::Action::create("toolbar-sketch", "Sketch Toolbar") );
296 App::ui_manager()->add_ui_from_string(ui_info);
299 checkbutton_show_sketch.set_active(get_work_area()->get_show_persistant_strokes());
301 button_clear_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::clear_sketch));
302 button_undo_stroke.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::undo_stroke));
303 button_save_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::save_sketch));
304 button_load_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::load_sketch));
305 checkbutton_show_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::toggle_show_sketch));
306 //options_table.attach(*manage(new Gtk::Label(_("Sketch Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
307 options_table.attach(checkbutton_show_sketch, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
308 //options_table.attach(button_undo_stroke, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
309 //options_table.attach(button_clear_sketch, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
310 //options_table.attach(button_save_sketch, 0, 1, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
311 //options_table.attach(button_load_sketch, 1, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
314 options_table.show_all();
315 refresh_tool_options();
316 App::dialog_tool_options->present();
318 // Turn off layer clicking
319 get_work_area()->allow_layer_clicks=false;
321 get_canvas_view()->work_area->set_cursor(Gdk::PENCIL);
323 // Turn off duck clicking
324 get_work_area()->allow_duck_clicks=false;
326 // clear out the ducks
327 //get_work_area()->clear_ducks();
329 // Refresh the work area
330 //get_work_area()->queue_draw();
332 // Hide the tables if they are showing
333 prev_table_status=get_canvas_view()->tables_are_visible();
334 //if(prev_table_status)get_canvas_view()->hide_tables();
337 //get_canvas_view()->hide_timebar();
340 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::on_user_click));
342 App::toolbox->refresh();
345 StateSketch_Context::~StateSketch_Context()
347 get_canvas_view()->work_area->reset_cursor();
349 App::dialog_tool_options->clear();
351 // Restore layer clicking
352 get_work_area()->allow_layer_clicks=prev_workarea_layer_status_;
354 // Restore duck clicking
355 get_work_area()->allow_duck_clicks=true;
358 if(get_canvas_view()->get_canvas()->rend_desc().get_time_start()!=get_canvas_view()->get_canvas()->rend_desc().get_time_end())
359 get_canvas_view()->show_timebar();
361 // Bring back the tables if they were out before
362 if(prev_table_status)get_canvas_view()->show_tables();
364 // Refresh the work area
365 //get_work_area()->queue_draw();
367 App::toolbox->refresh();
371 StateSketch_Context::yield_tool_options()
373 App::dialog_tool_options->clear();
374 App::ui_manager()->remove_action_group(action_group);
378 StateSketch_Context::refresh_tool_options()
380 App::dialog_tool_options->clear();
381 App::dialog_tool_options->set_widget(options_table);
382 App::dialog_tool_options->set_local_name(_("Sketch Tool"));
383 App::dialog_tool_options->set_name("sketch");
385 App::ui_manager()->insert_action_group(action_group);
386 App::dialog_tool_options->set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-sketch")));
389 App::dialog_tool_options->add_button(
390 Gtk::StockID("gtk-undo"),
391 _("Undo Last Stroke")
392 )->signal_clicked().connect(
395 &studio::StateSketch_Context::undo_stroke
398 App::dialog_tool_options->add_button(
399 Gtk::StockID("gtk-clear"),
401 )->signal_clicked().connect(
404 &studio::StateSketch_Context::clear_sketch
407 App::dialog_tool_options->add_button(
408 Gtk::StockID("gtk-save"),
409 _("Save Sketch to a File")
410 )->signal_clicked().connect(
413 &studio::StateSketch_Context::save_sketch
417 App::dialog_tool_options->add_button(
418 Gtk::StockID("gtk-open"),
420 )->signal_clicked().connect(
423 &studio::StateSketch_Context::load_sketch
427 //button_clear_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::clear_sketch));
428 //button_undo_stroke.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::undo_stroke));
429 //button_save_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::save_sketch));
430 //button_load_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::load_sketch));
431 //checkbutton_show_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::toggle_show_sketch));
435 StateSketch_Context::event_refresh_tool_options(const Smach::event& x)
437 refresh_tool_options();
438 return Smach::RESULT_ACCEPT;
442 StateSketch_Context::event_yield_tool_options(const Smach::event& x)
444 yield_tool_options();
445 return Smach::RESULT_ACCEPT;
449 StateSketch_Context::event_stop_handler(const Smach::event& x)
451 throw Smach::egress_exception();
455 StateSketch_Context::event_refresh_handler(const Smach::event& x)
457 return Smach::RESULT_ACCEPT;
461 StateSketch_Context::event_mouse_down_handler(const Smach::event& x)
463 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
468 // Enter the stroke state to get the stroke
469 get_canvas_view()->get_smach().push_state(&state_stroke);
470 return Smach::RESULT_ACCEPT;
473 case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
474 return Smach::RESULT_ACCEPT;
477 return Smach::RESULT_OK;
482 StateSketch_Context::event_stroke(const Smach::event& x)
484 const EventStroke& event(*reinterpret_cast<const EventStroke*>(&x));
486 assert(event.stroke_data);
488 get_work_area()->add_persistant_stroke(event.stroke_data,synfigapp::Main::get_foreground_color());
490 return Smach::RESULT_ACCEPT;