Add Nikita Kitaev's copyright to files he modified.
[synfig.git] / synfig-studio / src / gtkmm / state_null.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_null.cpp
3 **      \brief Null State 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 **      Copyright (c) 2009 Nikita Kitaev
11 **
12 **      This package is free software; you can redistribute it and/or
13 **      modify it under the terms of the GNU General Public License as
14 **      published by the Free Software Foundation; either version 2 of
15 **      the License, or (at your option) any later version.
16 **
17 **      This package is distributed in the hope that it will be useful,
18 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **      General Public License for more details.
21 **      \endlegal
22 */
23 /* ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "state_null.h"
35 #include "workarea.h"
36 #include "event_mouse.h"
37 #include "event_layerclick.h"
38 #include "toolbox.h"
39 #include "dialog_tooloptions.h"
40 #include <gtkmm/dialog.h>
41 #include "widget_waypointmodel.h"
42 #include <synfig/valuenode_animated.h>
43 #include <synfig/valuenode_composite.h>
44 #include <synfig/valuenode_const.h>
45 #include "canvasview.h"
46 #include "general.h"
47
48 #endif
49
50 /* === U S I N G =========================================================== */
51
52 using namespace std;
53 using namespace etl;
54 using namespace synfig;
55 using namespace studio;
56
57 /* === M A C R O S ========================================================= */
58
59 /* === G L O B A L S ======================================================= */
60
61 StateNull studio::state_null;
62
63 /* === C L A S S E S & S T R U C T S ======================================= */
64
65 class studio::StateNull_Context : public sigc::trackable
66 {
67         CanvasView *canvas_view;
68
69         CanvasView* get_canvas_view() { return canvas_view; }
70         Canvas::Handle get_canvas() { return canvas_view->get_canvas(); }
71         WorkArea* get_work_area() { return canvas_view->get_work_area(); }
72         etl::handle<synfigapp::CanvasInterface> get_canvas_interface() { return canvas_view->canvas_interface(); }
73
74         Gtk::Table options_table;
75
76 public:
77         StateNull_Context(CanvasView *canvas_view);
78         ~StateNull_Context();
79
80         Smach::event_result event_stop_handler(const Smach::event& x);
81         Smach::event_result event_refresh_handler(const Smach::event& x);
82         Smach::event_result event_refresh_ducks_handler(const Smach::event& x);
83         Smach::event_result event_undo_handler(const Smach::event& x);
84         Smach::event_result event_redo_handler(const Smach::event& x);
85         Smach::event_result event_mouse_button_down_handler(const Smach::event& x);
86         Smach::event_result event_multiple_ducks_clicked_handler(const Smach::event& x);
87         Smach::event_result event_refresh_tool_options(const Smach::event& x);
88         Smach::event_result event_layer_click(const Smach::event& x);
89
90         void refresh_tool_options();
91 }; // END of class StateNull_Context
92
93 /* === P R O C E D U R E S ================================================= */
94
95 /* === M E T H O D S ======================================================= */
96
97 StateNull::StateNull():
98         Smach::state<StateNull_Context>("null")
99 {
100         insert(event_def(EVENT_STOP,&StateNull_Context::event_stop_handler));
101         insert(event_def(EVENT_REFRESH,&StateNull_Context::event_refresh_handler));
102         insert(event_def(EVENT_REFRESH_DUCKS,&StateNull_Context::event_refresh_ducks_handler));
103         insert(event_def(EVENT_UNDO,&StateNull_Context::event_undo_handler));
104         insert(event_def(EVENT_REDO,&StateNull_Context::event_redo_handler));
105         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateNull_Context::event_mouse_button_down_handler));
106         insert(event_def(EVENT_WORKAREA_MULTIPLE_DUCKS_CLICKED,&StateNull_Context::event_multiple_ducks_clicked_handler));
107         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateNull_Context::event_refresh_tool_options));
108         insert(event_def(EVENT_WORKAREA_LAYER_CLICKED,&StateNull_Context::event_layer_click));
109 }
110
111 StateNull::~StateNull()
112 {
113 }
114
115 StateNull_Context::StateNull_Context(CanvasView *canvas_view):
116         canvas_view(canvas_view)
117 {
118         // Synfig Studio's default state is initialized in the canvas view constructor
119         // As a result, it cannot reference canvas view or workarea when created
120         // Other states need to reference the workarea,
121         //    so a null state was created to be the default
122
123         options_table.attach(*manage(new Gtk::Label(_("Welcome to Synfig Studio"))),    0, 2,  0,  1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
124         options_table.show_all();
125         refresh_tool_options();
126 }
127
128 StateNull_Context::~StateNull_Context()
129 {
130 }
131
132 void
133 StateNull_Context::refresh_tool_options()
134 {
135         App::dialog_tool_options->clear();
136         App::dialog_tool_options->set_widget(options_table);
137         App::dialog_tool_options->set_local_name(_("Welcome to Synfig Studio"));
138         App::dialog_tool_options->set_name("null");
139 }
140
141 Smach::event_result
142 StateNull_Context::event_refresh_tool_options(const Smach::event& /*x*/)
143 {
144         refresh_tool_options();
145         return Smach::RESULT_ACCEPT;
146 }
147
148 Smach::event_result
149 StateNull_Context::event_stop_handler(const Smach::event& /*x*/)
150 {
151         // synfig::info("STATE NULL: Received Stop Event");
152         canvas_view->stop();
153         return Smach::RESULT_ACCEPT;
154 }
155
156 Smach::event_result
157 StateNull_Context::event_refresh_handler(const Smach::event& /*x*/)
158 {
159         // synfig::info("STATE NULL: Received Refresh Event");
160         canvas_view->rebuild_tables();
161         canvas_view->work_area->queue_render_preview();
162         return Smach::RESULT_ACCEPT;
163 }
164
165 Smach::event_result
166 StateNull_Context::event_refresh_ducks_handler(const Smach::event& /*x*/)
167 {
168         // synfig::info("STATE NULL: Received Refresh Ducks");
169         canvas_view->queue_rebuild_ducks();
170         return Smach::RESULT_ACCEPT;
171 }
172
173 Smach::event_result
174 StateNull_Context::event_undo_handler(const Smach::event& /*x*/)
175 {
176         // synfig::info("STATE NULL: Received Undo Event");
177         canvas_view->get_instance()->undo();
178         return Smach::RESULT_ACCEPT;
179 }
180
181 Smach::event_result
182 StateNull_Context::event_redo_handler(const Smach::event& /*x*/)
183 {
184         // synfig::info("STATE NULL: Received Redo Event");
185         canvas_view->get_instance()->redo();
186         return Smach::RESULT_ACCEPT;
187 }
188
189 Smach::event_result
190 StateNull_Context::event_mouse_button_down_handler(const Smach::event& x)
191 {
192         // synfig::info("STATE NULL: Received mouse button down Event");
193
194         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
195
196         switch(event.button)
197         {
198         case BUTTON_RIGHT:
199                 canvas_view->popup_main_menu();
200                 return Smach::RESULT_ACCEPT;
201         default:
202                 return Smach::RESULT_OK;
203         }
204 }
205
206 Smach::event_result
207 StateNull_Context::event_layer_click(const Smach::event& x)
208 {
209         const EventLayerClick& event(*reinterpret_cast<const EventLayerClick*>(&x));
210
211         if(event.layer)
212         {
213                 // synfig::info("STATE NULL: Received layer click Event, \"%s\"",event.layer->get_name().c_str());
214         }
215         else
216         {
217                 // synfig::info("STATE NULL: Received layer click Event with an empty layer.");
218         }
219
220         switch(event.button)
221         {
222         case BUTTON_LEFT:
223                 if(!(event.modifier&Gdk::CONTROL_MASK))
224                         canvas_view->get_selection_manager()->clear_selected_layers();
225                 if(event.layer)
226                 {
227                         std::list<Layer::Handle> layer_list(canvas_view->get_selection_manager()->get_selected_layers());
228                         std::set<Layer::Handle> layers(layer_list.begin(),layer_list.end());
229                         if(layers.count(event.layer))
230                         {
231                                 layers.erase(event.layer);
232                                 layer_list=std::list<Layer::Handle>(layers.begin(),layers.end());
233                                 canvas_view->get_selection_manager()->clear_selected_layers();
234                                 canvas_view->get_selection_manager()->set_selected_layers(layer_list);
235                         }
236                         else
237                         {
238                                 canvas_view->get_selection_manager()->set_selected_layer(event.layer);
239                         }
240                 }
241                 return Smach::RESULT_ACCEPT;
242         case BUTTON_RIGHT:
243                 canvas_view->popup_layer_menu(event.layer);
244                 return Smach::RESULT_ACCEPT;
245         default:
246                 return Smach::RESULT_OK;
247         }
248 }
249
250 /*
251 void
252 StateNull_Context::edit_several_waypoints(std::list<synfigapp::ValueDesc> value_desc_list)
253 {
254         Gtk::Dialog dialog(
255                 "Edit Multiple Waypoints",              // Title
256                 true,           // Modal
257                 true            // use_separator
258         );
259
260         Widget_WaypointModel widget_waypoint_model;
261         widget_waypoint_model.show();
262
263         dialog.get_vbox()->pack_start(widget_waypoint_model);
264
265
266         dialog.add_button(Gtk::StockID("gtk-apply"),1);
267         dialog.add_button(Gtk::StockID("gtk-cancel"),0);
268         dialog.show();
269
270         if(dialog.run()==0)
271                 return;
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("ValueDescConnect");
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("ValueNodeReplace");
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("WaypointSetSmart"));
334
335                         if(!action)
336                         {
337                                 get_canvas_view()->get_ui_interface()->error(_("Unable to find WaypointSetSmart 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 StateNull_Context::event_multiple_ducks_clicked_handler(const Smach::event& /*x*/)
369 {
370         // synfig::info("STATE NULL: 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                                         ,ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node())
392                                                                ->get_link_index_from_name("point")
393                                 )
394                         );
395                 }
396                 else
397                         value_desc_list.push_back(value_desc);
398         }
399
400         Gtk::Menu *menu=manage(new Gtk::Menu());
401         menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
402
403         canvas_view->get_instance()->make_param_menu(menu,canvas_view->get_canvas(),value_desc_list);
404
405         /*
406         synfigapp::Action::ParamList param_list;
407         param_list=get_canvas_interface()->generate_param_list(value_desc_list);
408
409         canvas_view->add_actions_to_menu(menu, param_list,synfigapp::Action::CATEGORY_VALUEDESC|synfigapp::Action::CATEGORY_VALUENODE);
410
411         menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Edit Waypoints"),
412                 sigc::bind(
413                         sigc::mem_fun(
414                                 *this,
415                                 &studio::StateNull_Context::edit_several_waypoints
416                         ),
417                         value_desc_list
418                 )
419         ));
420         */
421         menu->popup(3,gtk_get_current_event_time());
422
423         return Smach::RESULT_ACCEPT;
424 }
425