Get rid of all references to the bootstrap script since it was removed
[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 #include "general.h"
45
46 #endif
47
48 /* === U S I N G =========================================================== */
49
50 using namespace std;
51 using namespace etl;
52 using namespace synfig;
53 using namespace studio;
54
55 /* === M A C R O S ========================================================= */
56
57 /* === C L A S S E S & S T R U C T S ======================================= */
58
59 class studio::StateNormal_Context : public sigc::trackable
60 {
61         CanvasView *canvas_view;
62
63         CanvasView* get_canvas_view() { return canvas_view; }
64         Canvas::Handle get_canvas() { return canvas_view->get_canvas(); }
65         WorkArea* get_work_area() { return canvas_view->get_work_area(); }
66         etl::handle<synfigapp::CanvasInterface> get_canvas_interface() { return canvas_view->canvas_interface(); }
67
68 public:
69         StateNormal_Context(CanvasView *canvas_view);
70         ~StateNormal_Context();
71
72         Smach::event_result event_stop_handler(const Smach::event& x);
73
74         Smach::event_result event_refresh_handler(const Smach::event& x);
75
76         Smach::event_result event_refresh_ducks_handler(const Smach::event& x);
77
78         Smach::event_result event_undo_handler(const Smach::event& x);
79
80         Smach::event_result event_redo_handler(const Smach::event& x);
81
82         Smach::event_result event_mouse_button_down_handler(const Smach::event& x);
83
84         Smach::event_result event_multiple_ducks_clicked_handler(const Smach::event& x);
85
86         Smach::event_result event_refresh_tool_options(const Smach::event& x);
87
88         Smach::event_result event_layer_click(const Smach::event& x);
89
90         void edit_several_waypoints(std::list<synfigapp::ValueDesc> value_desc_list);
91
92         void refresh_tool_options();
93 }; // END of class StateNormal_Context
94
95 /* === G L O B A L S ======================================================= */
96
97 StateNormal studio::state_normal;
98
99 /* === P R O C E D U R E S ================================================= */
100
101 /* === M E T H O D S ======================================================= */
102
103 StateNormal::StateNormal():
104         Smach::state<StateNormal_Context>("normal")
105 {
106         insert(event_def(EVENT_STOP,&StateNormal_Context::event_stop_handler));
107         insert(event_def(EVENT_REFRESH,&StateNormal_Context::event_refresh_handler));
108         insert(event_def(EVENT_REFRESH_DUCKS,&StateNormal_Context::event_refresh_ducks_handler));
109         insert(event_def(EVENT_UNDO,&StateNormal_Context::event_undo_handler));
110         insert(event_def(EVENT_REDO,&StateNormal_Context::event_redo_handler));
111         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateNormal_Context::event_mouse_button_down_handler));
112         insert(event_def(EVENT_WORKAREA_MULTIPLE_DUCKS_CLICKED,&StateNormal_Context::event_multiple_ducks_clicked_handler));
113         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateNormal_Context::event_refresh_tool_options));
114         insert(event_def(EVENT_WORKAREA_LAYER_CLICKED,&StateNormal_Context::event_layer_click));
115 }
116
117 StateNormal::~StateNormal()
118 {
119 }
120
121 StateNormal_Context::StateNormal_Context(CanvasView *canvas_view):
122         canvas_view(canvas_view)
123 {
124         // synfig::info("Entered Normal State");
125 }
126
127 StateNormal_Context::~StateNormal_Context()
128 {
129         // synfig::info("Left Normal State");
130 }
131
132 void
133 StateNormal_Context::refresh_tool_options()
134 {
135         App::dialog_tool_options->clear();
136         App::dialog_tool_options->set_name("normal");
137 }
138
139 Smach::event_result
140 StateNormal_Context::event_refresh_tool_options(const Smach::event& /*x*/)
141 {
142         refresh_tool_options();
143         return Smach::RESULT_ACCEPT;
144 }
145
146 Smach::event_result
147 StateNormal_Context::event_stop_handler(const Smach::event& /*x*/)
148 {
149         // synfig::info("STATE NORMAL: Received Stop Event");
150         canvas_view->stop();
151         return Smach::RESULT_ACCEPT;
152 }
153
154 Smach::event_result
155 StateNormal_Context::event_refresh_handler(const Smach::event& /*x*/)
156 {
157         // synfig::info("STATE NORMAL: Received Refresh Event");
158         canvas_view->rebuild_tables();
159         canvas_view->work_area->queue_render_preview();
160         return Smach::RESULT_ACCEPT;
161 }
162
163 Smach::event_result
164 StateNormal_Context::event_refresh_ducks_handler(const Smach::event& /*x*/)
165 {
166         // synfig::info("STATE NORMAL: Received Refresh Ducks");
167         canvas_view->queue_rebuild_ducks();
168         return Smach::RESULT_ACCEPT;
169 }
170
171 Smach::event_result
172 StateNormal_Context::event_undo_handler(const Smach::event& /*x*/)
173 {
174         // synfig::info("STATE NORMAL: Received Undo Event");
175         canvas_view->get_instance()->undo();
176         return Smach::RESULT_ACCEPT;
177 }
178
179 Smach::event_result
180 StateNormal_Context::event_redo_handler(const Smach::event& /*x*/)
181 {
182         // synfig::info("STATE NORMAL: Received Redo Event");
183         canvas_view->get_instance()->redo();
184         return Smach::RESULT_ACCEPT;
185 }
186
187 Smach::event_result
188 StateNormal_Context::event_mouse_button_down_handler(const Smach::event& x)
189 {
190         // synfig::info("STATE NORMAL: Received mouse button down Event");
191
192         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
193
194         switch(event.button)
195         {
196         case BUTTON_RIGHT:
197                 canvas_view->popup_main_menu();
198                 return Smach::RESULT_ACCEPT;
199         default:
200                 return Smach::RESULT_OK;
201         }
202 }
203
204 Smach::event_result
205 StateNormal_Context::event_layer_click(const Smach::event& x)
206 {
207         const EventLayerClick& event(*reinterpret_cast<const EventLayerClick*>(&x));
208
209         if(event.layer)
210         {
211                 // synfig::info("STATE NORMAL: Received layer click Event, \"%s\"",event.layer->get_name().c_str());
212         }
213         else
214         {
215                 // synfig::info("STATE NORMAL: Received layer click Event with an empty layer.");
216         }
217
218         switch(event.button)
219         {
220         case BUTTON_LEFT:
221                 if(!(event.modifier&Gdk::CONTROL_MASK))
222                         canvas_view->get_selection_manager()->clear_selected_layers();
223                 if(event.layer)
224                 {
225                         std::list<Layer::Handle> layer_list(canvas_view->get_selection_manager()->get_selected_layers());
226                         std::set<Layer::Handle> layers(layer_list.begin(),layer_list.end());
227                         if(layers.count(event.layer))
228                         {
229                                 layers.erase(event.layer);
230                                 layer_list=std::list<Layer::Handle>(layers.begin(),layers.end());
231                                 canvas_view->get_selection_manager()->clear_selected_layers();
232                                 canvas_view->get_selection_manager()->set_selected_layers(layer_list);
233                         }
234                         else
235                         {
236                                 canvas_view->get_selection_manager()->set_selected_layer(event.layer);
237                         }
238                 }
239                 return Smach::RESULT_ACCEPT;
240         case BUTTON_RIGHT:
241                 canvas_view->popup_layer_menu(event.layer);
242                 return Smach::RESULT_ACCEPT;
243         default:
244                 return Smach::RESULT_OK;
245         }
246 }
247
248 /*
249 void
250 StateNormal_Context::edit_several_waypoints(std::list<synfigapp::ValueDesc> value_desc_list)
251 {
252         Gtk::Dialog dialog(
253                 "Edit Multiple Waypoints",              // Title
254                 true,           // Modal
255                 true            // use_separator
256         );
257
258         Widget_WaypointModel widget_waypoint_model;
259         widget_waypoint_model.show();
260
261         dialog.get_vbox()->pack_start(widget_waypoint_model);
262
263
264         dialog.add_button(Gtk::StockID("gtk-apply"),1);
265         dialog.add_button(Gtk::StockID("gtk-cancel"),0);
266         dialog.show();
267
268         if(dialog.run()==0)
269                 return;
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         menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
399
400         canvas_view->get_instance()->make_param_menu(menu,canvas_view->get_canvas(),value_desc_list);
401
402         /*
403         synfigapp::Action::ParamList param_list;
404         param_list=get_canvas_interface()->generate_param_list(value_desc_list);
405
406         canvas_view->add_actions_to_menu(menu, param_list,synfigapp::Action::CATEGORY_VALUEDESC|synfigapp::Action::CATEGORY_VALUENODE);
407
408         menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Edit Waypoints"),
409                 sigc::bind(
410                         sigc::mem_fun(
411                                 *this,
412                                 &studio::StateNormal_Context::edit_several_waypoints
413                         ),
414                         value_desc_list
415                 )
416         ));
417         */
418         menu->popup(3,gtk_get_current_event_time());
419
420         return Smach::RESULT_ACCEPT;
421 }