67895d7bf293b9ec55eda8de139e282b58694684
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_sketch.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_sketch.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/entry.h>
35
36 #include <synfig/valuenode_dynamiclist.h>
37
38 #include "state_sketch.h"
39 #include "state_stroke.h"
40 #include "canvasview.h"
41 #include "workarea.h"
42 #include "app.h"
43 #include <synfig/valuenode_bline.h>
44 #include <ETL/hermite>
45 #include <ETL/calculus>
46 #include <utility>
47 #include "event_mouse.h"
48 #include "event_layerclick.h"
49 #include "toolbox.h"
50
51 #include <synfigapp/blineconvert.h>
52 #include <synfigapp/main.h>
53
54 #include <ETL/gaussian>
55
56 #include "dialog_tooloptions.h"
57
58 #include <gtkmm/table.h>
59 #include <gtkmm/label.h>
60 #include <gtkmm/button.h>
61 #include <gtkmm/checkbutton.h>
62 #include <gtkmm/actiongroup.h>
63
64 #include "general.h"
65
66 #endif
67
68 /* === U S I N G =========================================================== */
69
70 using namespace std;
71 using namespace etl;
72 using namespace synfig;
73 using namespace studio;
74
75 /* === M A C R O S ========================================================= */
76
77 /* === G L O B A L S ======================================================= */
78
79 StateSketch studio::state_sketch;
80
81 /* === C L A S S E S & S T R U C T S ======================================= */
82
83 class studio::StateSketch_Context : public sigc::trackable
84 {
85         Glib::RefPtr<Gtk::ActionGroup> action_group;
86
87         etl::handle<CanvasView> canvas_view_;
88         CanvasView::IsWorking is_working;
89
90         bool prev_table_status;
91         bool prev_workarea_layer_status_;
92
93         Gtk::Table options_table;
94         Gtk::Button button_clear_sketch;
95         Gtk::Button button_undo_stroke;
96         Gtk::Button button_save_sketch;
97         Gtk::Button button_load_sketch;
98         Gtk::CheckButton checkbutton_show_sketch;
99
100         void clear_sketch();
101         void save_sketch();
102         void load_sketch();
103         void undo_stroke();
104         void toggle_show_sketch();
105
106 public:
107
108         Smach::event_result event_stop_handler(const Smach::event& x);
109
110         Smach::event_result event_refresh_handler(const Smach::event& x);
111
112         Smach::event_result event_mouse_down_handler(const Smach::event& x);
113
114         Smach::event_result event_stroke(const Smach::event& x);
115
116         Smach::event_result event_refresh_tool_options(const Smach::event& x);
117         Smach::event_result event_yield_tool_options(const Smach::event& x);
118
119         void refresh_tool_options();
120         void yield_tool_options();
121
122         StateSketch_Context(CanvasView* canvas_view);
123
124         ~StateSketch_Context();
125
126         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
127         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
128         synfig::Time get_time()const { return get_canvas_interface()->get_time(); }
129         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
130         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
131
132 };      // END of class StateSketch_Context
133
134
135 /* === M E T H O D S ======================================================= */
136
137 StateSketch::StateSketch():
138         Smach::state<StateSketch_Context>("sketch")
139 {
140         insert(event_def(EVENT_STOP,&StateSketch_Context::event_stop_handler));
141         //insert(event_def(EVENT_REFRESH,&StateSketch_Context::event_refresh_handler));
142         insert(event_def(EVENT_REFRESH_DUCKS,&StateSketch_Context::event_refresh_handler));
143         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateSketch_Context::event_mouse_down_handler));
144         insert(event_def(EVENT_WORKAREA_STROKE,&StateSketch_Context::event_stroke));
145         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSketch_Context::event_refresh_tool_options));
146         insert(event_def(EVENT_YIELD_TOOL_OPTIONS,&StateSketch_Context::event_yield_tool_options));
147 }
148
149 StateSketch::~StateSketch()
150 {
151 }
152
153 void
154 StateSketch_Context::save_sketch()
155 {
156         synfig::String filename(basename(get_canvas()->get_file_name())+".sketch");
157
158         while(App::dialog_save_file(_("Save Sketch"), filename, SKETCH_DIR_PREFERENCE))
159         {
160                 // If the filename still has wildcards, then we should
161                 // continue looking for the file we want
162                 if(find(filename.begin(),filename.end(),'*')!=filename.end())
163                         continue;
164
165                 if(get_work_area()->save_sketch(filename))
166                         break;
167
168                 get_canvas_view()->get_ui_interface()->error(_("Unable to save sketch"));
169         }
170 }
171
172 void
173 StateSketch_Context::load_sketch()
174 {
175         synfig::String filename(basename(get_canvas()->get_file_name())+".sketch");
176
177         while(App::dialog_open_file(_("Load Sketch"), filename, SKETCH_DIR_PREFERENCE))
178         {
179                 // If the filename still has wildcards, then we should
180                 // continue looking for the file we want
181                 if(find(filename.begin(),filename.end(),'*')!=filename.end())
182                         continue;
183
184                 if(get_work_area()->load_sketch(filename))
185                         break;
186
187                 get_canvas_view()->get_ui_interface()->error(_("Unable to load sketch"));
188         }
189         get_work_area()->queue_draw();
190 }
191
192 void
193 StateSketch_Context::clear_sketch()
194 {
195         get_work_area()->clear_persistent_strokes();
196
197         // if the sketch is currently shown, make sure it is updated
198         //! \todo is there a better way than this of getting Duckmatic to update its stroke_list_?
199         if (checkbutton_show_sketch.get_active())
200         {
201                 get_work_area()->set_show_persistent_strokes(false);
202                 get_work_area()->set_show_persistent_strokes(true);
203                 get_canvas_view()->get_smach().process_event(EVENT_REFRESH);
204         }
205 }
206
207 void
208 StateSketch_Context::undo_stroke()
209 {
210         if(!get_work_area()->persistent_stroke_list().empty())
211         {
212                 get_work_area()->persistent_stroke_list().pop_back();
213
214                 // if the sketch is currently shown, make sure it is updated
215                 //! \todo is there a better way than this of getting Duckmatic to update its stroke_list_?
216                 if (checkbutton_show_sketch.get_active())
217                 {
218                         get_work_area()->set_show_persistent_strokes(false);
219                         get_work_area()->set_show_persistent_strokes(true);
220                         get_canvas_view()->get_smach().process_event(EVENT_REFRESH);
221                 }
222         }
223 }
224
225 void
226 StateSketch_Context::toggle_show_sketch()
227 {
228         get_work_area()->set_show_persistent_strokes(checkbutton_show_sketch.get_active());
229         get_work_area()->queue_draw();
230 }
231
232 StateSketch_Context::StateSketch_Context(CanvasView* canvas_view):
233         action_group(Gtk::ActionGroup::create()),
234         canvas_view_(canvas_view),
235         is_working(*canvas_view),
236         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
237         button_clear_sketch(_("Clear Sketch")),
238         button_undo_stroke(_("Undo Stroke")),
239         button_save_sketch(_("Save Sketch")),
240         button_load_sketch(_("Load Sketch")),
241         checkbutton_show_sketch(_("Show Sketch"))
242 {
243     Glib::ustring ui_info =
244         "<ui>"
245         "       <toolbar action='toolbar-sketch'>"
246         "       <toolitem action='sketch-undo' />"
247         "       <toolitem action='sketch-clear' />"
248         "       <toolitem action='sketch-save-as' />"
249         "       <toolitem action='sketch-open' />"
250         "       </toolbar>"
251         "</ui>";
252
253         action_group->add(Gtk::Action::create(
254                 "sketch-undo",
255                 Gtk::StockID("gtk-undo"),
256                 _("Undo Last Stroke"),
257                 _("Undo Last Stroke")
258         ),
259                 sigc::mem_fun(
260                         *this,
261                         &studio::StateSketch_Context::undo_stroke
262                 )
263         );
264
265         action_group->add(Gtk::Action::create(
266                 "sketch-clear",
267                 Gtk::StockID("gtk-clear"),
268                 _("Clear Sketch"),
269                 _("Clear Sketch")
270         ),
271                 sigc::mem_fun(
272                         *this,
273                         &studio::StateSketch_Context::clear_sketch
274                 )
275         );
276
277         action_group->add(Gtk::Action::create(
278                 "sketch-save-as",
279                 Gtk::StockID("gtk-save-as"),
280                 _("Save Sketch As..."),
281                 _("Save Sketch As...")
282         ),
283                 sigc::mem_fun(
284                         *this,
285                         &studio::StateSketch_Context::save_sketch
286                 )
287         );
288
289         action_group->add(Gtk::Action::create(
290                 "sketch-save-as",
291                 Gtk::StockID("gtk-save-as"),
292                 _("Save Sketch As..."),
293                 _("Save Sketch As...")
294         ),
295                 sigc::mem_fun(
296                         *this,
297                         &studio::StateSketch_Context::save_sketch
298                 )
299         );
300
301         action_group->add(Gtk::Action::create(
302                 "sketch-open",
303                 Gtk::StockID("gtk-open"),
304                 _("Open a Sketch"),
305                 _("Open a Sketch")
306         ),
307                 sigc::mem_fun(
308                         *this,
309                         &studio::StateSketch_Context::load_sketch
310                 )
311         );
312
313         action_group->add( Gtk::Action::create("toolbar-sketch", "Sketch Toolbar") );
314
315
316         App::ui_manager()->add_ui_from_string(ui_info);
317
318
319         checkbutton_show_sketch.set_active(get_work_area()->get_show_persistent_strokes());
320
321         button_clear_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::clear_sketch));
322         button_undo_stroke.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::undo_stroke));
323         button_save_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::save_sketch));
324         button_load_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::load_sketch));
325         checkbutton_show_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::toggle_show_sketch));
326         //options_table.attach(*manage(new Gtk::Label(_("Sketch Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
327         options_table.attach(checkbutton_show_sketch, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
328         //options_table.attach(button_undo_stroke, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
329         //options_table.attach(button_clear_sketch, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
330         //options_table.attach(button_save_sketch, 0, 1, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
331         //options_table.attach(button_load_sketch, 1, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
332
333
334         options_table.show_all();
335         refresh_tool_options();
336         App::dialog_tool_options->present();
337
338         // Turn off layer clicking
339         get_work_area()->set_allow_layer_clicks(false);
340
341         get_canvas_view()->work_area->set_cursor(Gdk::PENCIL);
342
343         // Turn off duck clicking
344         get_work_area()->set_allow_duck_clicks(false);
345
346         // clear out the ducks
347         //get_work_area()->clear_ducks();
348
349         // Refresh the work area
350         //get_work_area()->queue_draw();
351
352         // Hide the tables if they are showing
353         prev_table_status=get_canvas_view()->tables_are_visible();
354         //if(prev_table_status)get_canvas_view()->hide_tables();
355
356         // Disable the time bar
357         //get_canvas_view()->set_sensitive_timebar(false);
358
359         // Connect a signal
360         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::on_user_click));
361
362         App::toolbox->refresh();
363 }
364
365 StateSketch_Context::~StateSketch_Context()
366 {
367         get_canvas_view()->work_area->reset_cursor();
368
369         App::dialog_tool_options->clear();
370
371         // Restore layer clicking
372         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
373
374         // Restore duck clicking
375         get_work_area()->set_allow_duck_clicks(true);
376
377         // Enable the time bar
378         //get_canvas_view()->set_sensitive_timebar(true);
379
380         // Bring back the tables if they were out before
381         if(prev_table_status)get_canvas_view()->show_tables();
382
383         // Refresh the work area
384         //get_work_area()->queue_draw();
385
386         App::toolbox->refresh();
387 }
388
389 void
390 StateSketch_Context::yield_tool_options()
391 {
392         App::dialog_tool_options->clear();
393         App::ui_manager()->remove_action_group(action_group);
394 }
395
396 void
397 StateSketch_Context::refresh_tool_options()
398 {
399         App::dialog_tool_options->clear();
400         App::dialog_tool_options->set_widget(options_table);
401         App::dialog_tool_options->set_local_name(_("Sketch Tool"));
402         App::dialog_tool_options->set_name("sketch");
403
404         App::ui_manager()->insert_action_group(action_group);
405         App::dialog_tool_options->set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-sketch")));
406
407         /*
408         App::dialog_tool_options->add_button(
409                 Gtk::StockID("gtk-undo"),
410                 _("Undo Last Stroke")
411         )->signal_clicked().connect(
412                 sigc::mem_fun(
413                         *this,
414                         &studio::StateSketch_Context::undo_stroke
415                 )
416         );
417         App::dialog_tool_options->add_button(
418                 Gtk::StockID("gtk-clear"),
419                 _("Clear Sketch")
420         )->signal_clicked().connect(
421                 sigc::mem_fun(
422                         *this,
423                         &studio::StateSketch_Context::clear_sketch
424                 )
425         );
426         App::dialog_tool_options->add_button(
427                 Gtk::StockID("gtk-save"),
428                 _("Save Sketch to a File")
429         )->signal_clicked().connect(
430                 sigc::mem_fun(
431                         *this,
432                         &studio::StateSketch_Context::save_sketch
433                 )
434         );
435
436         App::dialog_tool_options->add_button(
437                 Gtk::StockID("gtk-open"),
438                 _("Open a Sketch")
439         )->signal_clicked().connect(
440                 sigc::mem_fun(
441                         *this,
442                         &studio::StateSketch_Context::load_sketch
443                 )
444         );
445         */
446         //button_clear_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::clear_sketch));
447         //button_undo_stroke.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::undo_stroke));
448         //button_save_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::save_sketch));
449         //button_load_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::load_sketch));
450         //checkbutton_show_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::toggle_show_sketch));
451 }
452
453 Smach::event_result
454 StateSketch_Context::event_refresh_tool_options(const Smach::event& /*x*/)
455 {
456         refresh_tool_options();
457         return Smach::RESULT_ACCEPT;
458 }
459
460 Smach::event_result
461 StateSketch_Context::event_yield_tool_options(const Smach::event& /*x*/)
462 {
463         yield_tool_options();
464         return Smach::RESULT_ACCEPT;
465 }
466
467 Smach::event_result
468 StateSketch_Context::event_stop_handler(const Smach::event& /*x*/)
469 {
470         throw Smach::egress_exception();
471 }
472
473 Smach::event_result
474 StateSketch_Context::event_refresh_handler(const Smach::event& /*x*/)
475 {
476         return Smach::RESULT_ACCEPT;
477 }
478
479 Smach::event_result
480 StateSketch_Context::event_mouse_down_handler(const Smach::event& x)
481 {
482         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
483         switch(event.button)
484         {
485         case BUTTON_LEFT:
486                 {
487                         // Enter the stroke state to get the stroke
488                         get_canvas_view()->get_smach().push_state(&state_stroke);
489                         return Smach::RESULT_ACCEPT;
490                 }
491
492         case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
493                 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
494                         return Smach::RESULT_ACCEPT;
495
496         default:
497                 return Smach::RESULT_OK;
498         }
499 }
500
501 Smach::event_result
502 StateSketch_Context::event_stroke(const Smach::event& x)
503 {
504         const EventStroke& event(*reinterpret_cast<const EventStroke*>(&x));
505
506         assert(event.stroke_data);
507
508         get_work_area()->add_persistent_stroke(event.stroke_data,synfigapp::Main::get_foreground_color());
509
510         return Smach::RESULT_ACCEPT;
511 }