3378c6a61e89d7af74c9468e40e15b90012a2f5d
[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         DEBUGPOINT();
269         if(dialog.run()==0)
270                 return;
271         DEBUGPOINT();
272         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Set Waypoints"));
273
274         std::list<synfigapp::ValueDesc>::iterator iter;
275         for(iter=value_desc_list.begin();iter!=value_desc_list.end();++iter)
276         {
277                 synfigapp::ValueDesc value_desc(*iter);
278
279                 if(!value_desc.is_valid())
280                         continue;
281
282                 ValueNode_Animated::Handle value_node;
283
284                 // If this value isn't a ValueNode_Animated, but
285                 // it is somewhat constant, then go ahead and convert
286                 // it to a ValueNode_Animated.
287                 if(!value_desc.is_value_node() || ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node()))
288                 {
289                         ValueBase value;
290                         if(value_desc.is_value_node())
291                                 value=ValueNode_Const::Handle::cast_dynamic(value_desc.get_value_node())->get_value();
292                         else
293                                 value=value_desc.get_value();
294
295                         value_node=ValueNode_Animated::create(value,get_canvas()->get_time());
296
297                         synfigapp::Action::Handle action;
298
299                         if(!value_desc.is_value_node())
300                         {
301                                 action=synfigapp::Action::create("value_desc_connect");
302                                 action->set_param("dest",value_desc);
303                                 action->set_param("src",ValueNode::Handle(value_node));
304                         }
305                         else
306                         {
307                                 action=synfigapp::Action::create("value_node_replace");
308                                 action->set_param("dest",value_desc.get_value_node());
309                                 action->set_param("src",ValueNode::Handle(value_node));
310                         }
311
312                         action->set_param("canvas",get_canvas());
313                         action->set_param("canvas_interface",get_canvas_interface());
314
315
316                         if(!get_canvas_interface()->get_instance()->perform_action(action))
317                         {
318                                 get_canvas_view()->get_ui_interface()->error(_("Unable to convert to animated waypoint"));
319                                 group.cancel();
320                                 return;
321                         }
322                 }
323                 else
324                 {
325                         if(value_desc.is_value_node())
326                                 value_node=ValueNode_Animated::Handle::cast_dynamic(value_desc.get_value_node());
327                 }
328
329
330                 if(value_node)
331                 {
332
333                         synfigapp::Action::Handle action(synfigapp::Action::create("waypoint_set_smart"));
334
335                         if(!action)
336                         {
337                                 get_canvas_view()->get_ui_interface()->error(_("Unable to find waypoint_set_smart action"));
338                                 group.cancel();
339                                 return;
340                         }
341
342
343                         action->set_param("canvas",get_canvas());
344                         action->set_param("canvas_interface",get_canvas_interface());
345                         action->set_param("value_node",ValueNode::Handle(value_node));
346                         action->set_param("time",get_canvas()->get_time());
347                         action->set_param("model",widget_waypoint_model.get_waypoint_model());
348
349                         if(!get_canvas_interface()->get_instance()->perform_action(action))
350                         {
351                                 get_canvas_view()->get_ui_interface()->error(_("Unable to set a specific waypoint"));
352                                 group.cancel();
353                                 return;
354                         }
355                 }
356                 else
357                 {
358                         //get_canvas_view()->get_ui_interface()->error(_("Unable to animate a specific valuedesc"));
359                         //group.cancel();
360                         //return;
361                 }
362
363         }
364 }
365 */
366
367 Smach::event_result
368 StateNormal_Context::event_multiple_ducks_clicked_handler(const Smach::event& /*x*/)
369 {
370         synfig::info("STATE NORMAL: Received multiple duck click event");
371
372         //const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
373
374         std::list<synfigapp::ValueDesc> value_desc_list;
375
376         // Create a list of value_descs associated with selection
377         const DuckList selected_ducks(get_work_area()->get_selected_ducks());
378         DuckList::const_iterator iter;
379         for(iter=selected_ducks.begin();iter!=selected_ducks.end();++iter)
380         {
381                 synfigapp::ValueDesc value_desc((*iter)->get_value_desc());
382
383                 if(!value_desc.is_valid())
384                         continue;
385
386                 if(value_desc.get_value_type()==ValueBase::TYPE_BLINEPOINT && value_desc.is_value_node() && ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node()))
387                 {
388                         value_desc_list.push_back(
389                                 synfigapp::ValueDesc(
390                                         ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())
391                                         ,0
392                                 )
393                         );
394                 }
395                 else
396                         value_desc_list.push_back(value_desc);
397         }
398
399         Gtk::Menu *menu=manage(new Gtk::Menu());
400         menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
401
402         canvas_view->get_instance()->make_param_menu(menu,canvas_view->get_canvas(),value_desc_list);
403
404         /*
405         synfigapp::Action::ParamList param_list;
406         param_list=get_canvas_interface()->generate_param_list(value_desc_list);
407
408         canvas_view->add_actions_to_menu(menu, param_list,synfigapp::Action::CATEGORY_VALUEDESC|synfigapp::Action::CATEGORY_VALUENODE);
409
410         menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Edit Waypoints"),
411                 sigc::bind(
412                         sigc::mem_fun(
413                                 *this,
414                                 &studio::StateNormal_Context::edit_several_waypoints
415                         ),
416                         value_desc_list
417                 )
418         ));
419         */
420         menu->popup(3,gtk_get_current_event_time());
421
422         return Smach::RESULT_ACCEPT;
423 }