Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / 0.61.08 / 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 **      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_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 #include <synfigapp/main.h>
50
51 #include "general.h"
52
53 #endif
54
55 /* === U S I N G =========================================================== */
56
57 using namespace std;
58 using namespace etl;
59 using namespace synfig;
60 using namespace studio;
61
62 /* === M A C R O S ========================================================= */
63
64 /* === G L O B A L S ======================================================= */
65
66 StateStroke studio::state_stroke;
67
68 /* === C L A S S E S & S T R U C T S ======================================= */
69
70 class studio::StateStroke_Context : public sigc::trackable
71 {
72         etl::handle<CanvasView> canvas_view_;
73         CanvasView::IsWorking is_working;
74
75         Duckmatic::Push duckmatic_push;
76
77         etl::smart_ptr<std::list<synfig::Point> > stroke_data;
78
79         etl::smart_ptr<std::list<synfig::Real> > width_data;
80
81         Gdk::ModifierType modifier;
82
83 public:
84
85         Smach::event_result event_stop_handler(const Smach::event& x);
86
87         Smach::event_result event_refresh_handler(const Smach::event& x);
88
89         Smach::event_result event_mouse_up_handler(const Smach::event& x);
90
91         Smach::event_result event_mouse_draw_handler(const Smach::event& x);
92         Smach::event_result event_refresh_tool_options(const Smach::event& x);
93
94         StateStroke_Context(CanvasView* canvas_view);
95
96         ~StateStroke_Context();
97
98         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
99         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
100         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
101         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
102
103 };      // END of class StateStroke_Context
104
105
106 /* === M E T H O D S ======================================================= */
107
108 StateStroke::StateStroke():
109         Smach::state<StateStroke_Context>("stroke")
110 {
111         insert(event_def(EVENT_STOP,&StateStroke_Context::event_stop_handler));
112         insert(event_def(EVENT_REFRESH,&StateStroke_Context::event_refresh_handler));
113 //      insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateStroke_Context::event_mouse_down_handler));
114         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateStroke_Context::event_mouse_up_handler));
115         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateStroke_Context::event_mouse_draw_handler));
116         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateStroke_Context::event_refresh_tool_options));
117 }
118
119 StateStroke::~StateStroke()
120 {
121 }
122
123
124 StateStroke_Context::StateStroke_Context(CanvasView* canvas_view):
125         canvas_view_(canvas_view),
126         is_working(*canvas_view),
127         duckmatic_push(get_work_area())
128 {
129         width_data.spawn();
130         stroke_data.spawn();
131
132         get_work_area()->add_stroke(stroke_data, synfigapp::Main::get_foreground_color());
133 }
134
135 StateStroke_Context::~StateStroke_Context()
136 {
137         duckmatic_push.restore();
138
139         App::toolbox->refresh();
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-circuit the pop-up menu
177                 printf("%s:%d BUTTON_RIGHT\n", __FILE__, __LINE__);
178                 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
179                         return Smach::RESULT_ACCEPT;
180
181         default:
182                 return Smach::RESULT_OK;
183         }
184 }
185
186 Smach::event_result
187 StateStroke_Context::event_mouse_draw_handler(const Smach::event& x)
188 {
189         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
190         switch(event.button)
191         {
192         case BUTTON_LEFT:
193                 {
194                         stroke_data->push_back(event.pos);
195                         width_data->push_back(event.pressure);
196                         get_work_area()->queue_draw();
197                         return Smach::RESULT_ACCEPT;
198                 }
199
200         case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
201                 printf("%s:%d BUTTON_RIGHT\n", __FILE__, __LINE__);
202                 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
203                         return Smach::RESULT_ACCEPT;
204
205         default:
206                 return Smach::RESULT_OK;
207         }
208 }