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