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