Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_normal.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_normal.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 "state_normal.h"
33 #include "workarea.h"
34 #include "event_mouse.h"
35 #include "event_layerclick.h"
36 #include "toolbox.h"
37 #include "dialog_tooloptions.h"
38 #include <gtkmm/dialog.h>
39 #include "widget_waypointmodel.h"
40 #include <synfig/valuenode_animated.h>
41 #include <synfig/valuenode_composite.h>
42 #include <synfig/valuenode_const.h>
43 #include "canvasview.h"
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace std;
49 using namespace etl;
50 using namespace synfig;
51 using namespace studio;
52
53 /* === M A C R O S ========================================================= */
54
55 /* === C L A S S E S & S T R U C T S ======================================= */
56
57 class studio::StateNormal_Context : public sigc::trackable
58 {
59         CanvasView *canvas_view;
60
61         CanvasView* get_canvas_view() { return canvas_view; }
62         Canvas::Handle get_canvas() { return canvas_view->get_canvas(); }
63         WorkArea* get_work_area() { return canvas_view->get_work_area(); }
64         etl::handle<synfigapp::CanvasInterface> get_canvas_interface() { return canvas_view->canvas_interface(); }
65
66 public:
67         StateNormal_Context(CanvasView *canvas_view);
68         ~StateNormal_Context();
69
70         Smach::event_result event_stop_handler(const Smach::event& x);
71
72         Smach::event_result event_refresh_handler(const Smach::event& x);
73
74         Smach::event_result event_refresh_ducks_handler(const Smach::event& x);
75
76         Smach::event_result event_undo_handler(const Smach::event& x);
77
78         Smach::event_result event_redo_handler(const Smach::event& x);
79
80         Smach::event_result event_mouse_button_down_handler(const Smach::event& x);
81
82         Smach::event_result event_multiple_ducks_clicked_handler(const Smach::event& x);
83
84         Smach::event_result event_refresh_tool_options(const Smach::event& x);
85
86         Smach::event_result event_layer_click(const Smach::event& x);
87
88         void edit_several_waypoints(std::list<synfigapp::ValueDesc> value_desc_list);
89
90         void refresh_tool_options();
91 }; // END of class StateNormal_Context
92
93 /* === G L O B A L S ======================================================= */
94
95 StateNormal studio::state_normal;
96
97 /* === P R O C E D U R E S ================================================= */
98
99 /* === M E T H O D S ======================================================= */
100
101 StateNormal::StateNormal():
102         Smach::state<StateNormal_Context>("normal")
103 {
104         insert(event_def(EVENT_STOP,&StateNormal_Context::event_stop_handler));
105         insert(event_def(EVENT_REFRESH,&StateNormal_Context::event_refresh_handler));
106         insert(event_def(EVENT_REFRESH_DUCKS,&StateNormal_Context::event_refresh_ducks_handler));
107         insert(event_def(EVENT_UNDO,&StateNormal_Context::event_undo_handler));
108         insert(event_def(EVENT_REDO,&StateNormal_Context::event_redo_handler));
109         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateNormal_Context::event_mouse_button_down_handler));
110         insert(event_def(EVENT_WORKAREA_MULTIPLE_DUCKS_CLICKED,&StateNormal_Context::event_multiple_ducks_clicked_handler));
111         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateNormal_Context::event_refresh_tool_options));
112         insert(event_def(EVENT_WORKAREA_LAYER_CLICKED,&StateNormal_Context::event_layer_click));
113 }
114
115 StateNormal::~StateNormal()
116 {
117 }
118
119 StateNormal_Context::StateNormal_Context(CanvasView *canvas_view):
120         canvas_view(canvas_view)
121 {
122         synfig::info("Enterted Normal State");
123 }
124
125 StateNormal_Context::~StateNormal_Context()
126 {
127         synfig::info("Left Normal State");
128 }
129
130 void
131 StateNormal_Context::refresh_tool_options()
132 {
133         App::dialog_tool_options->clear();
134         App::dialog_tool_options->set_name("normal");
135 }
136
137 Smach::event_result
138 StateNormal_Context::event_refresh_tool_options(const Smach::event& /*x*/)
139 {
140         refresh_tool_options();
141         return Smach::RESULT_ACCEPT;
142 }
143
144 Smach::event_result
145 StateNormal_Context::event_stop_handler(const Smach::event& /*x*/)
146 {
147         synfig::info("STATE NORMAL: Received Stop Event");
148         canvas_view->stop();
149         return Smach::RESULT_ACCEPT;
150 }
151
152 Smach::event_result
153 StateNormal_Context::event_refresh_handler(const Smach::event& /*x*/)
154 {
155         synfig::info("STATE NORMAL: Received Refresh Event");
156         canvas_view->rebuild_tables();
157         canvas_view->work_area->queue_render_preview();
158         return Smach::RESULT_ACCEPT;
159 }
160
161 Smach::event_result
162 StateNormal_Context::event_refresh_ducks_handler(const Smach::event& /*x*/)
163 {
164         synfig::info("STATE NORMAL: Received Refresh Ducks");
165         canvas_view->queue_rebuild_ducks();
166         return Smach::RESULT_ACCEPT;
167 }
168
169 Smach::event_result
170 StateNormal_Context::event_undo_handler(const Smach::event& /*x*/)
171 {
172         synfig::info("STATE NORMAL: Received Undo Event");
173         canvas_view->get_instance()->undo();
174         return Smach::RESULT_ACCEPT;
175 }
176
177 Smach::event_result
178 StateNormal_Context::event_redo_handler(const Smach::event& /*x*/)
179 {
180         synfig::info("STATE NORMAL: Received Redo Event");
181         canvas_view->get_instance()->redo();
182         return Smach::RESULT_ACCEPT;
183 }
184
185 Smach::event_result
186 StateNormal_Context::event_mouse_button_down_handler(const Smach::event& x)
187 {
188         synfig::info("STATE NORMAL: Received mouse button down Event");
189
190         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
191
192         switch(event.button)
193         {
194         case BUTTON_RIGHT:
195                 canvas_view->popup_main_menu();
196                 return Smach::RESULT_ACCEPT;
197         default:
198                 return Smach::RESULT_OK;
199         }
200 }
201
202 Smach::event_result
203 StateNormal_Context::event_layer_click(const Smach::event& x)
204 {
205         const EventLayerClick& event(*reinterpret_cast<const EventLayerClick*>(&x));
206
207         if(event.layer)
208         {
209                 synfig::info("STATE NORMAL: Received layer click Event, \"%s\"",event.layer->get_name().c_str());
210         }
211         else
212         {
213                 synfig::info("STATE NORMAL: Received layer click Event with an empty layer.");
214         }
215
216         switch(event.button)
217         {
218         case BUTTON_LEFT:
219                 if(!(event.modifier&Gdk::CONTROL_MASK))
220                         canvas_view->get_selection_manager()->clear_selected_layers();
221                 if(event.layer)
222                 {
223                         std::list<Layer::Handle> layer_list(canvas_view->get_selection_manager()->get_selected_layers());
224                         std::set<Layer::Handle> layers(layer_list.begin(),layer_list.end());
225                         if(layers.count(event.layer))
226                         {
227                                 layers.erase(event.layer);
228                                 layer_list=std::list<Layer::Handle>(layers.begin(),layers.end());
229                                 canvas_view->get_selection_manager()->clear_selected_layers();
230                                 canvas_view->get_selection_manager()->set_selected_layers(layer_list);
231                         }
232                         else
233                         {
234                                 canvas_view->get_selection_manager()->set_selected_layer(event.layer);
235                         }
236                 }
237                 return Smach::RESULT_ACCEPT;
238         case BUTTON_RIGHT:
239                 canvas_view->popup_layer_menu(event.layer);
240                 return Smach::RESULT_ACCEPT;
241         default:
242                 return Smach::RESULT_OK;
243         }
244 }
245
246 /*
247 void
248 StateNormal_Context::edit_several_waypoints(std::list<synfigapp::ValueDesc> value_desc_list)
249 {
250         Gtk::Dialog dialog(
251                 "Edit Multiple Waypoints",              // Title
252                 true,           // Modal
253                 true            // use_separator
254         );
255
256         Widget_WaypointModel widget_waypoint_model;
257         widget_waypoint_model.show();
258
259         dialog.get_vbox()->pack_start(widget_waypoint_model);
260
261
262         dialog.add_button(Gtk::StockID("gtk-apply"),1);
263         dialog.add_button(Gtk::StockID("gtk-cancel"),0);
264         dialog.show();
265
266         DEBUGPOINT();
267         if(dialog.run()==0)
268                 return;
269         DEBUGPOINT();
270         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Set Waypoints"));
271
272         std::list<synfigapp::ValueDesc>::iterator iter;
273         for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
274         {
275                 synfigapp::ValueDesc value_desc(*iter);
276
277                 if(!value_desc.is_valid())
278                         continue;
279
280                 ValueNode_Animated::Handle value_node;
281
282                 // If this value isn't a ValueNode_Animated, but
283                 // it is somewhat constant, then go ahead and convert
284                 // it to a ValueNode_Animated.
285                 if(!value_desc.is_value_node() || ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
286                 {
287                         ValueBase value;
288                         if(value_desc.is_value_node())
289                                 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
290                         else
291                                 value=value_desc.get_value();
292
293                         value_node=ValueNode_Animated::create(value,get_canvas()->get_time());
294
295                         synfigapp::Action::Handle action;
296
297                         if(!value_desc.is_value_node())
298                         {
299                                 action=synfigapp::Action::create("value_desc_connect");
300                                 action->set_param("dest",value_desc);
301                                 action->set_param("src",ValueNode::Handle(value_node));
302                         }
303                         else
304                         {
305                                 action=synfigapp::Action::create("value_node_replace");
306                                 action->set_param("dest",value_desc.get_value_node());
307                                 action->set_param("src",ValueNode::Handle(value_node));
308                         }
309
310                         action->set_param("canvas",get_canvas());
311                         action->set_param("canvas_interface",get_canvas_interface());
312
313
314                         if(!get_canvas_interface()->get_instance()->perform_action(action))
315                         {
316                                 get_canvas_view()->get_ui_interface()->error(_("Unable to convert to animated waypoint"));
317                                 group.cancel();
318                                 return;
319                         }
320                 }
321                 else
322                 {
323                         if(value_desc.is_value_node())
324                                 value_node=ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node());
325                 }
326
327
328                 if(value_node)
329                 {
330
331                         synfigapp::Action::Handle action(synfigapp::Action::create("waypoint_set_smart"));
332
333                         if(!action)
334                         {
335                                 get_canvas_view()->get_ui_interface()->error(_("Unable to find waypoint_set_smart action"));
336                                 group.cancel();
337                                 return;
338                         }
339
340
341                         action->set_param("canvas",get_canvas());
342                         action->set_param("canvas_interface",get_canvas_interface());
343                         action->set_param("value_node",ValueNode::Handle(value_node));
344                         action->set_param("time",get_canvas()->get_time());
345                         action->set_param("model",widget_waypoint_model.get_waypoint_model());
346
347                         if(!get_canvas_interface()->get_instance()->perform_action(action))
348                         {
349                                 get_canvas_view()->get_ui_interface()->error(_("Unable to set a specific waypoint"));
350                                 group.cancel();
351                                 return;
352                         }
353                 }
354                 else
355                 {
356                         //get_canvas_view()->get_ui_interface()->error(_("Unable to animate a specific valuedesc"));
357                         //group.cancel();
358                         //return;
359                 }
360
361         }
362 }
363 */
364
365 Smach::event_result
366 StateNormal_Context::event_multiple_ducks_clicked_handler(const Smach::event& /*x*/)
367 {
368         synfig::info("STATE NORMAL: Received multiple duck click event");
369
370         //const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
371
372         std::list<synfigapp::ValueDesc> value_desc_list;
373
374         // Create a list of value_descs associated with selection
375         const DuckList selected_ducks(get_work_area()->get_selected_ducks());
376         DuckList::const_iterator iter;
377         for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
378         {
379                 synfigapp::ValueDesc value_desc((*iter)->get_value_desc());
380
381                 if(!value_desc.is_valid())
382                         continue;
383
384                 if(value_desc.get_value_type()==ValueBase::TYPE_BLINEPOINT && value_desc.is_value_node() && ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
385                 {
386                         value_desc_list.push_back(
387                                 synfigapp::ValueDesc(
388                                         ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())
389                                         ,0
390                                 )
391                         );
392                 }
393                 else
394                         value_desc_list.push_back(value_desc);
395         }
396
397         Gtk::Menu *menu=manage(new Gtk::Menu());
398
399         canvas_view->get_instance()->make_param_menu(menu,canvas_view->get_canvas(),value_desc_list);
400
401         /*
402         synfigapp::Action::ParamList param_list;
403         param_list=get_canvas_interface()->generate_param_list(value_desc_list);
404
405         canvas_view->add_actions_to_menu(menu, param_list,synfigapp::Action::CATEGORY_VALUEDESC|synfigapp::Action::CATEGORY_VALUENODE);
406
407         menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Edit Waypoints"),
408                 sigc::bind(
409                         sigc::mem_fun(
410                                 *this,
411                                 &studio::StateNormal_Context::edit_several_waypoints
412                         ),
413                         value_desc_list
414                 )
415         ));
416         */
417         menu->popup(3,gtk_get_current_event_time());
418
419         return Smach::RESULT_ACCEPT;
420 }