Fix 1438545: hide and reshow the sketch after 'undo' or 'clear' to ensure it's update...
[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 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
34
35 #include <synfig/valuenode_dynamiclist.h>
36
37 #include "state_sketch.h"
38 #include "state_stroke.h"
39 #include "canvasview.h"
40 #include "workarea.h"
41 #include "app.h"
42 #include <synfig/valuenode_bline.h>
43 #include <ETL/hermite>
44 #include <ETL/calculus>
45 #include <utility>
46 #include "event_mouse.h"
47 #include "event_layerclick.h"
48 #include "toolbox.h"
49
50 #include <synfigapp/blineconvert.h>
51 #include <synfigapp/main.h>
52
53 #include <ETL/gaussian>
54
55 #include "dialog_tooloptions.h"
56
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>
62
63 #endif
64
65 /* === U S I N G =========================================================== */
66
67 using namespace std;
68 using namespace etl;
69 using namespace synfig;
70 using namespace studio;
71
72 /* === M A C R O S ========================================================= */
73
74 /* === G L O B A L S ======================================================= */
75
76 StateSketch studio::state_sketch;
77
78 /* === C L A S S E S & S T R U C T S ======================================= */
79
80 class studio::StateSketch_Context : public sigc::trackable
81 {
82         Glib::RefPtr<Gtk::ActionGroup> action_group;
83
84         etl::handle<CanvasView> canvas_view_;
85         CanvasView::IsWorking is_working;
86
87         bool prev_table_status;
88         bool prev_workarea_layer_status_;
89
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;
96
97         void clear_sketch();
98         void save_sketch();
99         void load_sketch();
100         void undo_stroke();
101         void toggle_show_sketch();
102
103 public:
104
105         Smach::event_result event_stop_handler(const Smach::event& x);
106
107         Smach::event_result event_refresh_handler(const Smach::event& x);
108
109         Smach::event_result event_mouse_down_handler(const Smach::event& x);
110
111         Smach::event_result event_stroke(const Smach::event& x);
112
113         Smach::event_result event_refresh_tool_options(const Smach::event& x);
114         Smach::event_result event_yield_tool_options(const Smach::event& x);
115
116         void refresh_tool_options();
117         void yield_tool_options();
118
119         StateSketch_Context(CanvasView* canvas_view);
120
121         ~StateSketch_Context();
122
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();}
128
129 };      // END of class StateSketch_Context
130
131
132 /* === M E T H O D S ======================================================= */
133
134 StateSketch::StateSketch():
135         Smach::state<StateSketch_Context>("sketch")
136 {
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));
144 }
145
146 StateSketch::~StateSketch()
147 {
148 }
149
150 void
151 StateSketch_Context::save_sketch()
152 {
153         synfig::String filename(basename(get_canvas()->get_file_name())+".sketch");
154
155         while(App::dialog_save_file(_("Save Sketch"), filename))
156         {
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())
160                         continue;
161
162                 if(get_work_area()->save_sketch(filename))
163                         break;
164
165                 get_canvas_view()->get_ui_interface()->error(_("Unable to save sketch"));
166         }
167 }
168
169 void
170 StateSketch_Context::load_sketch()
171 {
172         synfig::String filename(basename(get_canvas()->get_file_name())+".sketch");
173
174         while(App::dialog_open_file(_("Load Sketch"), filename))
175         {
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())
179                         continue;
180
181                 if(get_work_area()->load_sketch(filename))
182                         break;
183
184                 get_canvas_view()->get_ui_interface()->error(_("Unable to load sketch"));
185         }
186         get_work_area()->queue_draw();
187 }
188
189 void
190 StateSketch_Context::clear_sketch()
191 {
192         get_work_area()->clear_persistant_strokes();
193
194         // if the sketch is currently shown, make sure it is updated
195         //! \todo is there a better way than this of getting Duckmatic to update its stroke_list_?
196         if (checkbutton_show_sketch.get_active())
197         {
198                 get_work_area()->set_show_persistant_strokes(false);
199                 get_work_area()->set_show_persistant_strokes(true);
200                 get_canvas_view()->get_smach().process_event(EVENT_REFRESH);
201         }
202 }
203
204 void
205 StateSketch_Context::undo_stroke()
206 {
207         if(!get_work_area()->persistant_stroke_list().empty())
208         {
209                 get_work_area()->persistant_stroke_list().pop_back();
210
211                 // if the sketch is currently shown, make sure it is updated
212                 //! \todo is there a better way than this of getting Duckmatic to update its stroke_list_?
213                 if (checkbutton_show_sketch.get_active())
214                 {
215                         get_work_area()->set_show_persistant_strokes(false);
216                         get_work_area()->set_show_persistant_strokes(true);
217                         get_canvas_view()->get_smach().process_event(EVENT_REFRESH);
218                 }
219         }
220 }
221
222 void
223 StateSketch_Context::toggle_show_sketch()
224 {
225         get_work_area()->set_show_persistant_strokes(checkbutton_show_sketch.get_active());
226         get_work_area()->queue_draw();
227 }
228
229 StateSketch_Context::StateSketch_Context(CanvasView* canvas_view):
230         action_group(Gtk::ActionGroup::create()),
231         canvas_view_(canvas_view),
232         is_working(*canvas_view),
233         prev_workarea_layer_status_(get_work_area()->allow_layer_clicks),
234         button_clear_sketch(_("Clear Sketch")),
235         button_undo_stroke(_("Undo Stroke")),
236         button_save_sketch(_("Save Sketch")),
237         button_load_sketch(_("Load Sketch")),
238         checkbutton_show_sketch(_("Show Sketch"))
239 {
240     Glib::ustring ui_info =
241         "<ui>"
242         "       <toolbar action='toolbar-sketch'>"
243         "       <toolitem action='sketch-undo' />"
244         "       <toolitem action='sketch-clear' />"
245         "       <toolitem action='sketch-save-as' />"
246         "       <toolitem action='sketch-open' />"
247         "       </toolbar>"
248         "</ui>";
249
250         action_group->add(Gtk::Action::create(
251                 "sketch-undo",
252                 Gtk::StockID("gtk-undo"),
253                 _("Undo Last Stroke"),
254                 _("Undo Last Stroke")
255         ),
256                 sigc::mem_fun(
257                         *this,
258                         &studio::StateSketch_Context::undo_stroke
259                 )
260         );
261
262         action_group->add(Gtk::Action::create(
263                 "sketch-clear",
264                 Gtk::StockID("gtk-clear"),
265                 _("Clear Sketch"),
266                 _("Clear Sketch")
267         ),
268                 sigc::mem_fun(
269                         *this,
270                         &studio::StateSketch_Context::clear_sketch
271                 )
272         );
273
274         action_group->add(Gtk::Action::create(
275                 "sketch-save-as",
276                 Gtk::StockID("gtk-save-as"),
277                 _("Save Sketch As..."),
278                 _("Save Sketch As...")
279         ),
280                 sigc::mem_fun(
281                         *this,
282                         &studio::StateSketch_Context::save_sketch
283                 )
284         );
285
286         action_group->add(Gtk::Action::create(
287                 "sketch-save-as",
288                 Gtk::StockID("gtk-save-as"),
289                 _("Save Sketch As..."),
290                 _("Save Sketch As...")
291         ),
292                 sigc::mem_fun(
293                         *this,
294                         &studio::StateSketch_Context::save_sketch
295                 )
296         );
297
298         action_group->add(Gtk::Action::create(
299                 "sketch-open",
300                 Gtk::StockID("gtk-open"),
301                 _("Open a Sketch"),
302                 _("Open a Sketch")
303         ),
304                 sigc::mem_fun(
305                         *this,
306                         &studio::StateSketch_Context::load_sketch
307                 )
308         );
309
310         action_group->add( Gtk::Action::create("toolbar-sketch", "Sketch Toolbar") );
311
312
313         App::ui_manager()->add_ui_from_string(ui_info);
314
315
316         checkbutton_show_sketch.set_active(get_work_area()->get_show_persistant_strokes());
317
318         button_clear_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::clear_sketch));
319         button_undo_stroke.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::undo_stroke));
320         button_save_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::save_sketch));
321         button_load_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::load_sketch));
322         checkbutton_show_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::toggle_show_sketch));
323         //options_table.attach(*manage(new Gtk::Label(_("Sketch Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
324         options_table.attach(checkbutton_show_sketch, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
325         //options_table.attach(button_undo_stroke, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
326         //options_table.attach(button_clear_sketch, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
327         //options_table.attach(button_save_sketch, 0, 1, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
328         //options_table.attach(button_load_sketch, 1, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
329
330
331         options_table.show_all();
332         refresh_tool_options();
333         App::dialog_tool_options->present();
334
335         // Turn off layer clicking
336         get_work_area()->allow_layer_clicks=false;
337
338         get_canvas_view()->work_area->set_cursor(Gdk::PENCIL);
339
340         // Turn off duck clicking
341         get_work_area()->allow_duck_clicks=false;
342
343         // clear out the ducks
344         //get_work_area()->clear_ducks();
345
346         // Refresh the work area
347         //get_work_area()->queue_draw();
348
349         // Hide the tables if they are showing
350         prev_table_status=get_canvas_view()->tables_are_visible();
351         //if(prev_table_status)get_canvas_view()->hide_tables();
352
353         // Hide the time bar
354         //get_canvas_view()->hide_timebar();
355
356         // Connect a signal
357         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::on_user_click));
358
359         App::toolbox->refresh();
360 }
361
362 StateSketch_Context::~StateSketch_Context()
363 {
364         get_canvas_view()->work_area->reset_cursor();
365
366         App::dialog_tool_options->clear();
367
368         // Restore layer clicking
369         get_work_area()->allow_layer_clicks=prev_workarea_layer_status_;
370
371         // Restore duck clicking
372         get_work_area()->allow_duck_clicks=true;
373
374         // Show the time bar
375         if(get_canvas_view()->get_canvas()->rend_desc().get_time_start()!=get_canvas_view()->get_canvas()->rend_desc().get_time_end())
376                 get_canvas_view()->show_timebar();
377
378         // Bring back the tables if they were out before
379         if(prev_table_status)get_canvas_view()->show_tables();
380
381         // Refresh the work area
382         //get_work_area()->queue_draw();
383
384         App::toolbox->refresh();
385 }
386
387 void
388 StateSketch_Context::yield_tool_options()
389 {
390         App::dialog_tool_options->clear();
391         App::ui_manager()->remove_action_group(action_group);
392 }
393
394 void
395 StateSketch_Context::refresh_tool_options()
396 {
397         App::dialog_tool_options->clear();
398         App::dialog_tool_options->set_widget(options_table);
399         App::dialog_tool_options->set_local_name(_("Sketch Tool"));
400         App::dialog_tool_options->set_name("sketch");
401
402         App::ui_manager()->insert_action_group(action_group);
403         App::dialog_tool_options->set_toolbar(*dynamic_cast<Gtk::Toolbar*>(App::ui_manager()->get_widget("/toolbar-sketch")));
404
405         /*
406         App::dialog_tool_options->add_button(
407                 Gtk::StockID("gtk-undo"),
408                 _("Undo Last Stroke")
409         )->signal_clicked().connect(
410                 sigc::mem_fun(
411                         *this,
412                         &studio::StateSketch_Context::undo_stroke
413                 )
414         );
415         App::dialog_tool_options->add_button(
416                 Gtk::StockID("gtk-clear"),
417                 _("Clear Sketch")
418         )->signal_clicked().connect(
419                 sigc::mem_fun(
420                         *this,
421                         &studio::StateSketch_Context::clear_sketch
422                 )
423         );
424         App::dialog_tool_options->add_button(
425                 Gtk::StockID("gtk-save"),
426                 _("Save Sketch to a File")
427         )->signal_clicked().connect(
428                 sigc::mem_fun(
429                         *this,
430                         &studio::StateSketch_Context::save_sketch
431                 )
432         );
433
434         App::dialog_tool_options->add_button(
435                 Gtk::StockID("gtk-open"),
436                 _("Open a Sketch")
437         )->signal_clicked().connect(
438                 sigc::mem_fun(
439                         *this,
440                         &studio::StateSketch_Context::load_sketch
441                 )
442         );
443         */
444         //button_clear_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::clear_sketch));
445         //button_undo_stroke.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::undo_stroke));
446         //button_save_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::save_sketch));
447         //button_load_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::load_sketch));
448         //checkbutton_show_sketch.signal_clicked().connect(sigc::mem_fun(*this,&studio::StateSketch_Context::toggle_show_sketch));
449 }
450
451 Smach::event_result
452 StateSketch_Context::event_refresh_tool_options(const Smach::event& x)
453 {
454         refresh_tool_options();
455         return Smach::RESULT_ACCEPT;
456 }
457
458 Smach::event_result
459 StateSketch_Context::event_yield_tool_options(const Smach::event& x)
460 {
461         yield_tool_options();
462         return Smach::RESULT_ACCEPT;
463 }
464
465 Smach::event_result
466 StateSketch_Context::event_stop_handler(const Smach::event& x)
467 {
468         throw Smach::egress_exception();
469 }
470
471 Smach::event_result
472 StateSketch_Context::event_refresh_handler(const Smach::event& x)
473 {
474         return Smach::RESULT_ACCEPT;
475 }
476
477 Smach::event_result
478 StateSketch_Context::event_mouse_down_handler(const Smach::event& x)
479 {
480         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
481         switch(event.button)
482         {
483         case BUTTON_LEFT:
484                 {
485                         // Enter the stroke state to get the stroke
486                         get_canvas_view()->get_smach().push_state(&state_stroke);
487                         return Smach::RESULT_ACCEPT;
488                 }
489
490         case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
491                 return Smach::RESULT_ACCEPT;
492
493         default:
494                 return Smach::RESULT_OK;
495         }
496 }
497
498 Smach::event_result
499 StateSketch_Context::event_stroke(const Smach::event& x)
500 {
501         const EventStroke& event(*reinterpret_cast<const EventStroke*>(&x));
502
503         assert(event.stroke_data);
504
505         get_work_area()->add_persistant_stroke(event.stroke_data,synfigapp::Main::get_foreground_color());
506
507         return Smach::RESULT_ACCEPT;
508 }