Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_stroke.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_stroke.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_stroke.h"
38 #include "canvasview.h"
39 #include "workarea.h"
40 #include "app.h"
41 #include <synfig/valuenode_bline.h>
42 #include <ETL/hermite>
43 #include <ETL/calculus>
44 #include <utility>
45 #include "event_mouse.h"
46 #include "event_layerclick.h"
47 #include "toolbox.h"
48 #include <synfigapp/main.h>
49
50 #endif
51
52 /* === U S I N G =========================================================== */
53
54 using namespace std;
55 using namespace etl;
56 using namespace synfig;
57 using namespace studio;
58
59 /* === M A C R O S ========================================================= */
60
61 /* === G L O B A L S ======================================================= */
62
63 StateStroke studio::state_stroke;
64
65 /* === C L A S S E S & S T R U C T S ======================================= */
66
67 class studio::StateStroke_Context : public sigc::trackable
68 {
69         etl::handle<CanvasView> canvas_view_;
70         CanvasView::IsWorking is_working;
71
72         Duckmatic::Push duckmatic_push;
73
74         etl::smart_ptr<std::list<synfig::Point> > stroke_data;
75
76         etl::smart_ptr<std::list<synfig::Real> > width_data;
77
78         Gdk::ModifierType modifier;
79
80 public:
81
82         Smach::event_result event_stop_handler(const Smach::event& x);
83
84         Smach::event_result event_refresh_handler(const Smach::event& x);
85
86         Smach::event_result event_mouse_up_handler(const Smach::event& x);
87
88         Smach::event_result event_mouse_draw_handler(const Smach::event& x);
89         Smach::event_result event_refresh_tool_options(const Smach::event& x);
90
91         StateStroke_Context(CanvasView* canvas_view);
92
93         ~StateStroke_Context();
94
95         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
96         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
97         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
98         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
99
100 };      // END of class StateStroke_Context
101
102
103 /* === M E T H O D S ======================================================= */
104
105 StateStroke::StateStroke():
106         Smach::state<StateStroke_Context>("stroke")
107 {
108         insert(event_def(EVENT_STOP,&StateStroke_Context::event_stop_handler));
109         insert(event_def(EVENT_REFRESH,&StateStroke_Context::event_refresh_handler));
110 //      insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateStroke_Context::event_mouse_down_handler));
111         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateStroke_Context::event_mouse_up_handler));
112         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateStroke_Context::event_mouse_draw_handler));
113         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateStroke_Context::event_refresh_tool_options));
114 }
115
116 StateStroke::~StateStroke()
117 {
118 }
119
120
121 StateStroke_Context::StateStroke_Context(CanvasView* canvas_view):
122         canvas_view_(canvas_view),
123         is_working(*canvas_view),
124         duckmatic_push(get_work_area())
125 {
126         width_data.spawn();
127         stroke_data.spawn();
128
129         get_work_area()->add_stroke(stroke_data, synfigapp::Main::get_foreground_color());
130
131         synfig::info("Now Scribbling...");
132 }
133
134 StateStroke_Context::~StateStroke_Context()
135 {
136         duckmatic_push.restore();
137
138         App::toolbox->refresh();
139         synfig::info("No longer scribbling");
140
141         // Send the stroke data to whatever previously called this state.
142         if(stroke_data->size()>=2)
143                 get_canvas_view()->get_smach().process_event(EventStroke(stroke_data,width_data,modifier));
144 }
145
146 Smach::event_result
147 StateStroke_Context::event_refresh_tool_options(const Smach::event& /*x*/)
148 {
149         return Smach::RESULT_ACCEPT;
150 }
151
152 Smach::event_result
153 StateStroke_Context::event_stop_handler(const Smach::event& /*x*/)
154 {
155         throw Smach::pop_exception();
156 }
157
158 Smach::event_result
159 StateStroke_Context::event_refresh_handler(const Smach::event& /*x*/)
160 {
161         return Smach::RESULT_ACCEPT;
162 }
163
164 Smach::event_result
165 StateStroke_Context::event_mouse_up_handler(const Smach::event& x)
166 {
167         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
168         switch(event.button)
169         {
170         case BUTTON_LEFT:
171                 {
172                         modifier=event.modifier;
173                         throw Smach::pop_exception();
174                 }
175
176         case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
177                 return Smach::RESULT_ACCEPT;
178
179         default:
180                 return Smach::RESULT_OK;
181         }
182 }
183
184 Smach::event_result
185 StateStroke_Context::event_mouse_draw_handler(const Smach::event& x)
186 {
187         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
188         switch(event.button)
189         {
190         case BUTTON_LEFT:
191                 {
192                         stroke_data->push_back(event.pos);
193                         width_data->push_back(event.pressure);
194                         get_work_area()->queue_draw();
195                         return Smach::RESULT_ACCEPT;
196                 }
197
198         case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
199                 return Smach::RESULT_ACCEPT;
200
201         default:
202                 return Smach::RESULT_OK;
203         }
204 }