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-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>
37 #include "state_sketch.h"
38 #include "state_stroke.h"
39 #include "canvasview.h"
42 #include <synfig/valuenode_bline.h>
43 #include <ETL/hermite>
44 #include <ETL/calculus>
46 #include "event_mouse.h"
47 #include "event_layerclick.h"
50 #include <synfigapp/blineconvert.h>
51 #include <synfigapp/main.h>
53 #include <ETL/gaussian>
55 #include "dialog_tooloptions.h"
57 #include <gtkmm/table.h>
58 #include <gtkmm/label.h>
59 #include <gtkmm/button.h>
60 #include <gtkmm/checkbutton.h>
61 #include <gtkmm/actiongroup.h>
65 /* === U S I N G =========================================================== */
69 using namespace synfig;
70 using namespace studio;
72 /* === M A C R O S ========================================================= */
74 /* === G L O B A L S ======================================================= */
76 StateSketch studio::state_sketch;
78 /* === C L A S S E S & S T R U C T S ======================================= */
80 class studio::StateSketch_Context : public sigc::trackable
82 Glib::RefPtr<Gtk::ActionGroup> action_group;
84 etl::handle<CanvasView> canvas_view_;
85 CanvasView::IsWorking is_working;
87 bool prev_table_status;
88 bool prev_workarea_layer_status_;
90 Gtk::Table options_table;
91 Gtk::Button button_clear_sketch;
92 Gtk::Button button_undo_stroke;
93 Gtk::Button button_save_sketch;
94 Gtk::Button button_load_sketch;
95 Gtk::CheckButton checkbutton_show_sketch;
101 void toggle_show_sketch();
105 Smach::event_result event_stop_handler(const Smach::event& x);
107 Smach::event_result event_refresh_handler(const Smach::event& x);
109 Smach::event_result event_mouse_down_handler(const Smach::event& x);
111 Smach::event_result event_stroke(const Smach::event& x);
113 Smach::event_result event_refresh_tool_options(const Smach::event& x);
114 Smach::event_result event_yield_tool_options(const Smach::event& x);
116 void refresh_tool_options();
117 void yield_tool_options();
119 StateSketch_Context(CanvasView* canvas_view);
121 ~StateSketch_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::Time get_time()const { return get_canvas_interface()->get_time(); }
126 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
127 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
129 }; // END of class StateSketch_Context
132 /* === M E T H O D S ======================================================= */
134 StateSketch::StateSketch():
135 Smach::state<StateSketch_Context>("sketch")
137 insert(event_def(EVENT_STOP,&StateSketch_Context::event_stop_handler));
138 //insert(event_def(EVENT_REFRESH,&StateSketch_Context::event_refresh_handler));
139 insert(event_def(EVENT_REFRESH_DUCKS,&StateSketch_Context::event_refresh_handler));
140 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateSketch_Context::event_mouse_down_handler));
141 insert(event_def(EVENT_WORKAREA_STROKE,&StateSketch_Context::event_stroke));
142 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSketch_Context::event_refresh_tool_options));
143 insert(event_def(EVENT_YIELD_TOOL_OPTIONS,&StateSketch_Context::event_yield_tool_options));
146 StateSketch::~StateSketch()
151 StateSketch_Context::save_sketch()
153 synfig::String filename(basename(get_canvas()->get_file_name())+".sketch");
155 while(App::dialog_save_file(_("Save Sketch"), filename))
157 // If the filename still has wildcards, then we should
158 // continue looking for the file we want
159 if(find(filename.begin(),filename.end(),'*')!=filename.end())
162 if(get_work_area()->save_sketch(filename))
165 get_canvas_view()->get_ui_interface()->error(_("Unable to save sketch"));
170 StateSketch_Context::load_sketch()
172 synfig::String filename(basename(get_canvas()->get_file_name())+".sketch");
174 while(App::dialog_open_file(_("Load Sketch"), filename))
176 // If the filename still has wildcards, then we should
177 // continue looking for the file we want
178 if(find(filename.begin(),filename.end(),'*')!=filename.end())
181 if(get_work_area()->load_sketch(filename))
184 get_canvas_view()->get_ui_interface()->error(_("Unable to load sketch"));
186 get_work_area()->queue_draw();
190 StateSketch_Context::clear_sketch()
192 get_work_area()->clear_persistant_strokes();
193 get_canvas_view()->get_smach().process_event(EVENT_REFRESH);
197 StateSketch_Context::undo_stroke()
199 if(!get_work_area()->persistant_stroke_list().empty())
201 get_work_area()->persistant_stroke_list().pop_back();
202 get_canvas_view()->get_smach().process_event(EVENT_REFRESH);
207 StateSketch_Context::toggle_show_sketch()
209 get_work_area()->set_show_persistant_strokes(checkbutton_show_sketch.get_active());
210 get_work_area()->queue_draw();
213 StateSketch_Context::StateSketch_Context(CanvasView* canvas_view):
214 action_group(Gtk::ActionGroup::create()),
215 canvas_view_(canvas_view),
216 is_working(*canvas_view),
217 prev_workarea_layer_status_(get_work_area()->allow_layer_clicks),
218 button_clear_sketch(_("Clear Sketch")),
219 button_undo_stroke(_("Undo Stroke")),
220 button_save_sketch(_("Save Sketch")),
221 button_load_sketch(_("Load Sketch")),
222 checkbutton_show_sketch(_("Show Sketch"))
224 Glib::ustring ui_info =
226 " <toolbar action='toolbar-sketch'>"
227 " <toolitem action='sketch-undo' />"
228 " <toolitem action='sketch-clear' />"
229 " <toolitem action='sketch-save-as' />"
230 " <toolitem action='sketch-open' />"
234 action_group->add(Gtk::Action::create(
236 Gtk::StockID("gtk-undo"),
237 _("Undo Last Stroke"),
238 _("Undo Last Stroke")
242 &studio::StateSketch_Context::undo_stroke
246 action_group->add(Gtk::Action::create(
248 Gtk::StockID("gtk-clear"),
254 &studio::StateSketch_Context::clear_sketch
258 action_group->add(Gtk::Action::create(
260 Gtk::StockID("gtk-save-as"),
261 _("Save Sketch As..."),
262 _("Save Sketch As...")
266 &studio::StateSketch_Context::save_sketch
270 action_group->add(Gtk::Action::create(
272 Gtk::StockID("gtk-save-as"),
273 _("Save Sketch As..."),
274 _("Save Sketch As...")
278 &studio::StateSketch_Context::save_sketch
282 action_group->add(Gtk::Action::create(
284 Gtk::StockID("gtk-open"),
290 &studio::StateSketch_Context::load_sketch
294 action_group->add( Gtk::Action::create("toolbar-sketch", "Sketch Toolbar") );
297 App::ui_manager()->add_ui_from_string(ui_info);
300 checkbutton_show_sketch.set_active(get_work_area()->get_show_persistant_strokes());
302 button_clear_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::clear_sketch));
303 button_undo_stroke.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::undo_stroke));
304 button_save_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::save_sketch));
305 button_load_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::load_sketch));
306 checkbutton_show_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::toggle_show_sketch));
307 //options_table.attach(*manage(new Gtk::Label(_("Sketch Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
308 options_table.attach(checkbutton_show_sketch, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
309 //options_table.attach(button_undo_stroke, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
310 //options_table.attach(button_clear_sketch, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
311 //options_table.attach(button_save_sketch, 0, 1, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
312 //options_table.attach(button_load_sketch, 1, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
315 options_table.show_all();
316 refresh_tool_options();
317 App::dialog_tool_options->present();
319 // Turn off layer clicking
320 get_work_area()->allow_layer_clicks=false;
322 get_canvas_view()->work_area->set_cursor(Gdk::PENCIL);
324 // Turn off duck clicking
325 get_work_area()->allow_duck_clicks=false;
327 // clear out the ducks
328 //get_work_area()->clear_ducks();
330 // Refresh the work area
331 //get_work_area()->queue_draw();
333 // Hide the tables if they are showing
334 prev_table_status=get_canvas_view()->tables_are_visible();
335 //if(prev_table_status)get_canvas_view()->hide_tables();
338 //get_canvas_view()->hide_timebar();
341 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::on_user_click));
343 App::toolbox->refresh();
346 StateSketch_Context::~StateSketch_Context()
348 get_canvas_view()->work_area->reset_cursor();
350 App::dialog_tool_options->clear();
352 // Restore layer clicking
353 get_work_area()->allow_layer_clicks=prev_workarea_layer_status_;
355 // Restore duck clicking
356 get_work_area()->allow_duck_clicks=true;
359 if(get_canvas_view()->get_canvas()->rend_desc().get_time_start()!=get_canvas_view()->get_canvas()->rend_desc().get_time_end())
360 get_canvas_view()->show_timebar();
362 // Bring back the tables if they were out before
363 if(prev_table_status)get_canvas_view()->show_tables();
365 // Refresh the work area
366 //get_work_area()->queue_draw();
368 App::toolbox->refresh();
372 StateSketch_Context::yield_tool_options()
374 App::dialog_tool_options->clear();
375 App::ui_manager()->remove_action_group(action_group);
379 StateSketch_Context::refresh_tool_options()
381 App::dialog_tool_options->clear();
382 App::dialog_tool_options->set_widget(options_table);
383 App::dialog_tool_options->set_local_name(_("Sketch Tool"));
384 App::dialog_tool_options->set_name("sketch");
386 App::ui_manager()->insert_action_group(action_group);
387 App::dialog_tool_options->set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-sketch")));
390 App::dialog_tool_options->add_button(
391 Gtk::StockID("gtk-undo"),
392 _("Undo Last Stroke")
393 )->signal_clicked().connect(
396 &studio::StateSketch_Context::undo_stroke
399 App::dialog_tool_options->add_button(
400 Gtk::StockID("gtk-clear"),
402 )->signal_clicked().connect(
405 &studio::StateSketch_Context::clear_sketch
408 App::dialog_tool_options->add_button(
409 Gtk::StockID("gtk-save"),
410 _("Save Sketch to a File")
411 )->signal_clicked().connect(
414 &studio::StateSketch_Context::save_sketch
418 App::dialog_tool_options->add_button(
419 Gtk::StockID("gtk-open"),
421 )->signal_clicked().connect(
424 &studio::StateSketch_Context::load_sketch
428 //button_clear_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::clear_sketch));
429 //button_undo_stroke.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::undo_stroke));
430 //button_save_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::save_sketch));
431 //button_load_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::load_sketch));
432 //checkbutton_show_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::toggle_show_sketch));
436 StateSketch_Context::event_refresh_tool_options(const Smach::event& x)
438 refresh_tool_options();
439 return Smach::RESULT_ACCEPT;
443 StateSketch_Context::event_yield_tool_options(const Smach::event& x)
445 yield_tool_options();
446 return Smach::RESULT_ACCEPT;
450 StateSketch_Context::event_stop_handler(const Smach::event& x)
452 throw Smach::egress_exception();
456 StateSketch_Context::event_refresh_handler(const Smach::event& x)
458 return Smach::RESULT_ACCEPT;
462 StateSketch_Context::event_mouse_down_handler(const Smach::event& x)
464 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
469 // Enter the stroke state to get the stroke
470 get_canvas_view()->get_smach().push_state(&state_stroke);
471 return Smach::RESULT_ACCEPT;
474 case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
475 return Smach::RESULT_ACCEPT;
478 return Smach::RESULT_OK;
483 StateSketch_Context::event_stroke(const Smach::event& x)
485 const EventStroke& event(*reinterpret_cast<const EventStroke*>(&x));
487 assert(event.stroke_data);
489 get_work_area()->add_persistant_stroke(event.stroke_data,synfigapp::Main::get_foreground_color());
491 return Smach::RESULT_ACCEPT;