3bf3d1cf05521dd17e0e8e350f242c692d9d9aaf
[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 **      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         synfig::info("Now Scribbling...");
135 }
136
137 StateStroke_Context::~StateStroke_Context()
138 {
139         duckmatic_push.restore();
140
141         App::toolbox->refresh();
142         synfig::info("No longer scribbling");
143
144         // Send the stroke data to whatever previously called this state.
145         if(stroke_data->size()>=2)
146                 get_canvas_view()->get_smach().process_event(EventStroke(stroke_data,width_data,modifier));
147 }
148
149 Smach::event_result
150 StateStroke_Context::event_refresh_tool_options(const Smach::event& /*x*/)
151 {
152         return Smach::RESULT_ACCEPT;
153 }
154
155 Smach::event_result
156 StateStroke_Context::event_stop_handler(const Smach::event& /*x*/)
157 {
158         throw Smach::pop_exception();
159 }
160
161 Smach::event_result
162 StateStroke_Context::event_refresh_handler(const Smach::event& /*x*/)
163 {
164         return Smach::RESULT_ACCEPT;
165 }
166
167 Smach::event_result
168 StateStroke_Context::event_mouse_up_handler(const Smach::event& x)
169 {
170         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
171         switch(event.button)
172         {
173         case BUTTON_LEFT:
174                 {
175                         modifier=event.modifier;
176                         throw Smach::pop_exception();
177                 }
178
179         case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
180                 printf("%s:%d BUTTON_RIGHT\n", __FILE__, __LINE__);
181                 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
182                         return Smach::RESULT_ACCEPT;
183
184         default:
185                 return Smach::RESULT_OK;
186         }
187 }
188
189 Smach::event_result
190 StateStroke_Context::event_mouse_draw_handler(const Smach::event& x)
191 {
192         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
193         switch(event.button)
194         {
195         case BUTTON_LEFT:
196                 {
197                         stroke_data->push_back(event.pos);
198                         width_data->push_back(event.pressure);
199                         get_work_area()->queue_draw();
200                         return Smach::RESULT_ACCEPT;
201                 }
202
203         case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
204                 printf("%s:%d BUTTON_RIGHT\n", __FILE__, __LINE__);
205                 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
206                         return Smach::RESULT_ACCEPT;
207
208         default:
209                 return Smach::RESULT_OK;
210         }
211 }