Build rectanglular blines in the same handedness as all the other shapes.
[synfig.git] / synfig-studio / trunk / src / gtkmm / layeractionmanager.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layeractionmanager.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 "layeractionmanager.h"
34 #include "layertree.h"
35 #include <synfig/context.h>
36 #include <synfigapp/action_param.h>
37 #include "instance.h"
38 #include <synfigapp/selectionmanager.h>
39
40 #include "general.h"
41
42 #endif
43
44 /* === U S I N G =========================================================== */
45
46 using namespace std;
47 using namespace etl;
48 using namespace synfig;
49 using namespace studio;
50
51 static const guint no_prev_popup((guint)-1);
52
53 /* === M A C R O S ========================================================= */
54
55 //#define ONE_ACTION_GROUP 1
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 LayerActionManager::LayerActionManager():
64         action_group_(Gtk::ActionGroup::create()),
65         popup_id_(no_prev_popup),
66         action_group_copy_paste(Gtk::ActionGroup::create()),
67         queued(false)
68 {
69         action_cut_=Gtk::Action::create(
70                 "cut",
71                 Gtk::StockID("gtk-cut")
72         );
73         action_cut_->signal_activate().connect(
74                 sigc::mem_fun(
75                         *this,
76                         &LayerActionManager::cut
77                 )
78         );
79         action_copy_=Gtk::Action::create(
80                 "copy",
81                 Gtk::StockID("gtk-copy")
82         );
83         action_copy_->signal_activate().connect(
84                 sigc::mem_fun(
85                         *this,
86                         &LayerActionManager::copy
87                 )
88         );
89         action_paste_=Gtk::Action::create(
90                 "paste",
91                 Gtk::StockID("gtk-paste")
92         );
93         action_paste_->signal_activate().connect(
94                 sigc::mem_fun(
95                         *this,
96                         &LayerActionManager::paste
97                 )
98         );
99
100
101         action_amount_inc_=Gtk::Action::create(
102                 "amount-inc",
103                 Gtk::StockID("gtk-add"),
104                 _("Increase Amount"),_("Increase Amount")
105         );
106         action_amount_inc_->signal_activate().connect(
107                 sigc::mem_fun(
108                         *this,
109                         &LayerActionManager::amount_inc
110                 )
111         );
112
113         action_amount_dec_=Gtk::Action::create(
114                 "amount-dec",
115                 Gtk::StockID("gtk-remove"),
116                 _("Decrease Amount"),_("Decrease Amount")
117         );
118         action_amount_dec_->signal_activate().connect(
119                 sigc::mem_fun(
120                         *this,
121                         &LayerActionManager::amount_dec
122                 )
123         );
124
125         action_amount_=Gtk::Action::create(
126                 "amount",
127                 Gtk::StockID("gtk-index"),
128                 _("Amount"),_("Amount")
129         );
130
131         action_select_all_child_layers_=Gtk::Action::create(
132                 "select-all-child-layers",
133                 Gtk::StockID("synfig-select_all_child_layers"),
134                 _("Select All Child Layers"),_("Select All Child Layers")
135         );
136         action_select_all_child_layers_->set_sensitive(false);
137 }
138
139 LayerActionManager::~LayerActionManager()
140 {
141 }
142
143 void
144 LayerActionManager::set_ui_manager(const Glib::RefPtr<Gtk::UIManager> &x)
145 {
146         clear();
147
148 #ifdef ONE_ACTION_GROUP
149         if(ui_manager_) get_ui_manager()->remove_action_group(action_group_);
150         ui_manager_=x;
151         if(ui_manager_) get_ui_manager()->insert_action_group(action_group_);
152 #else
153         ui_manager_=x;
154 #endif
155 }
156
157 void
158 LayerActionManager::set_layer_tree(LayerTree* x)
159 {
160         selection_changed_connection.disconnect();
161         layer_tree_=x;
162         if(layer_tree_)
163         {
164                 selection_changed_connection=layer_tree_->get_selection()->signal_changed().connect(
165                         sigc::mem_fun(*this,&LayerActionManager::queue_refresh)
166                 );
167         }
168 }
169
170 void
171 LayerActionManager::set_canvas_interface(const etl::handle<synfigapp::CanvasInterface> &x)
172 {
173         canvas_interface_=x;
174 }
175
176 void
177 LayerActionManager::clear()
178 {
179         if(ui_manager_)
180         {
181                 // Clear out old stuff
182                 if(popup_id_!=no_prev_popup)
183                 {
184                         get_ui_manager()->remove_ui(popup_id_);
185                         if(action_group_)get_ui_manager()->ensure_update();
186                         popup_id_=no_prev_popup;
187                         if(action_group_)while(!action_group_->get_actions().empty())action_group_->remove(*action_group_->get_actions().begin());
188 #ifdef ONE_ACTION_GROUP
189 #else
190                         if(action_group_)get_ui_manager()->remove_action_group(action_group_);
191                         action_group_=Gtk::ActionGroup::create();
192 #endif
193                 }
194         }
195
196         while(!update_connection_list.empty())
197         {
198                 update_connection_list.front().disconnect();
199                 update_connection_list.pop_front();
200         }
201 }
202
203 void
204 LayerActionManager::queue_refresh()
205 {
206         if(queued)
207                 return;
208
209         //queue_refresh_connection.disconnect();
210         queue_refresh_connection=Glib::signal_idle().connect(
211                 sigc::bind_return(
212                         sigc::mem_fun(*this,&LayerActionManager::refresh),
213                         false
214                 )
215         );
216
217         queued=true;
218 }
219
220 void
221 LayerActionManager::refresh()
222 {
223         if(queued)
224         {
225                 queued=false;
226                 //queue_refresh_connection.disconnect();
227         }
228
229
230         clear();
231
232         // Make sure we are ready
233         if(!ui_manager_ || !layer_tree_ || !canvas_interface_)
234         {
235                 synfig::error("LayerActionManager::refresh(): Not ready!");
236                 return;
237         }
238
239         String ui_info;
240
241         action_paste_->set_sensitive(!clipboard_.empty());
242         action_group_->add(action_paste_);
243
244         if(layer_tree_->get_selection()->count_selected_rows()!=0)
245         {
246                 bool multiple_selected(layer_tree_->get_selection()->count_selected_rows()>1);
247                 Layer::Handle layer(layer_tree_->get_selected_layer());
248
249                 {
250                         bool canvas_set(false);
251                         synfigapp::Action::ParamList param_list;
252                         param_list.add("time",get_canvas_interface()->get_time());
253                         param_list.add("canvas_interface",get_canvas_interface());
254                         {
255                                 synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
256                                 synfigapp::SelectionManager::LayerList::iterator iter;
257                                 action_copy_->set_sensitive(!layer_list.empty());
258                                 action_cut_->set_sensitive(!layer_list.empty());
259                                 action_group_->add(action_copy_);
260                                 action_group_->add(action_cut_);
261
262                                 action_amount_inc_->set_sensitive(!layer_list.empty());
263                                 action_amount_dec_->set_sensitive(!layer_list.empty());
264                                 action_amount_->set_sensitive(!layer_list.empty());
265                                 action_group_->add(action_amount_inc_);
266                                 action_group_->add(action_amount_dec_);
267                                 action_group_->add(action_amount_);
268
269                                 for(iter=layer_list.begin();iter!=layer_list.end();++iter)
270                                 {
271                                         update_connection_list.push_back(
272                                                 (*iter)->signal_changed().connect(
273                                                         sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
274                                                 )
275                                         );
276
277                                         if(!canvas_set)
278                                         {
279                                                 param_list.add("canvas",Canvas::Handle((*iter)->get_canvas()));
280                                                 canvas_set=true;
281                                                 update_connection_list.push_back(
282                                                         (*iter)->get_canvas()->signal_changed().connect(
283                                                                 sigc::mem_fun(*this, &LayerActionManager::queue_refresh)
284                                                         )
285                                                 );
286                                         }
287                                         param_list.add("layer",Layer::Handle(*iter));
288                                 }
289                         }
290
291                         if(!multiple_selected && layer->get_name()=="PasteCanvas")
292                         {
293                                 if (select_all_child_layers_connection)
294                                         select_all_child_layers_connection.disconnect();
295
296                                 select_all_child_layers_connection = action_select_all_child_layers_->signal_activate().connect(
297                                         sigc::bind(sigc::mem_fun(*layer_tree_,
298                                                                                          &studio::LayerTree::select_all_children_layers),
299                                                            Layer::LooseHandle(layer)));
300
301                                 action_select_all_child_layers_->set_sensitive(true);
302
303                                 ui_info+="<menuitem action='select-all-child-layers'/>";
304                         }
305                         else
306                                 action_select_all_child_layers_->set_sensitive(false);
307
308                         handle<studio::Instance>::cast_static(get_canvas_interface()->get_instance())->
309                                 add_actions_to_group(action_group_, ui_info,   param_list, synfigapp::Action::CATEGORY_LAYER);
310                 }
311         }
312
313         ui_info=("<ui>"
314                            "<popup action='menu-main'>"
315                              "<menu action='menu-layer'>" +
316                                    ui_info +
317                                    "<separator/>"
318                                "<menuitem action='cut' />"
319                                    "<menuitem action='copy' />"
320                                    "<menuitem action='paste' />"
321                                    "<separator/>"
322                              "</menu>"
323                            "</popup>" +
324                          "</ui>");
325         popup_id_=get_ui_manager()->add_ui_from_string(ui_info);
326 #ifdef ONE_ACTION_GROUP
327 #else
328         get_ui_manager()->insert_action_group(action_group_);
329 #endif
330 }
331
332 void
333 LayerActionManager::cut()
334 {
335         copy();
336         if(action_group_->get_action("action-layer_remove"))
337                 action_group_->get_action("action-layer_remove")->activate();
338 }
339
340 void
341 LayerActionManager::copy()
342 {
343         synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
344         clipboard_.clear();
345         synfig::GUID guid;
346
347         while(!layer_list.empty())
348         {
349                 clipboard_.push_back(layer_list.front()->clone(guid));
350                 layer_list.pop_front();
351         }
352
353         action_paste_->set_sensitive(!clipboard_.empty());
354
355         //queue_refresh();
356 }
357
358 void
359 LayerActionManager::paste()
360 {
361         synfig::GUID guid;
362
363         // Create the action group
364         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Paste"));
365
366         Canvas::Handle canvas(get_canvas_interface()->get_canvas());
367         int depth(0);
368
369         // we are temporarily using the layer to hold something
370         Layer::Handle layer(layer_tree_->get_selected_layer());
371         if(layer)
372         {
373                 depth=layer->get_depth();
374                 canvas=layer->get_canvas();
375         }
376
377         synfigapp::SelectionManager::LayerList layer_selection;
378
379         for(std::list<synfig::Layer::Handle>::iterator iter=clipboard_.begin();iter!=clipboard_.end();++iter)
380         {
381                 layer=(*iter)->clone(guid);
382                 layer_selection.push_back(layer);
383                 synfigapp::Action::Handle       action(synfigapp::Action::create("layer_add"));
384
385                 assert(action);
386                 if(!action)
387                         return;
388
389                 action->set_param("canvas",canvas);
390                 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
391                 action->set_param("new",layer);
392
393                 if(!action->is_ready())
394                 {
395                         return;
396                 }
397
398                 if(!get_instance()->perform_action(action))
399                 {
400                         return;
401                 }
402
403                 // synfig::info("DEPTH=%d",depth);
404
405                 // Action to move the layer (if necessary)
406                 if(depth>0)
407                 {
408                         synfigapp::Action::Handle       action(synfigapp::Action::create("layer_move"));
409
410                         assert(action);
411                         if(!action)
412                                 return;
413
414                         action->set_param("canvas",canvas);
415                         action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
416                         action->set_param("layer",layer);
417                         action->set_param("new_index",depth);
418
419                         if(!action->is_ready())
420                         {
421                                 //get_ui_interface()->error(_("Move Action Not Ready"));
422                                 //return 0;
423                                 return;
424                         }
425
426                         if(!get_instance()->perform_action(action))
427                         {
428                                 //get_ui_interface()->error(_("Move Action Not Ready"));
429                                 //return 0;
430                                 return;
431                         }
432                 }
433                 depth++;
434
435                 // automatically export the Index parameter of Duplicate layers when pasting
436                 int index = 1;
437                 export_dup_nodes(layer, canvas, index);
438         }
439         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
440         get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
441 }
442
443 void
444 LayerActionManager::export_dup_nodes(synfig::Layer::Handle layer, Canvas::Handle canvas, int &index)
445 {
446         // automatically export the Index parameter of Duplicate layers when pasting
447         if (layer->get_name() == "duplicate")
448                 while (true)
449                 {
450                         String name = strprintf(_("Index %d"), index++);
451                         try
452                         {
453                                 canvas->find_value_node(name);
454                         }
455                         catch (Exception::IDNotFound x)
456                         {
457                                 get_canvas_interface()->add_value_node(layer->dynamic_param_list().find("index")->second, name);
458                                 break;
459                         }
460                 }
461         else
462         {
463                 Layer::ParamList param_list(layer->get_param_list());
464                 for (Layer::ParamList::const_iterator iter(param_list.begin())
465                                  ; iter != param_list.end()
466                                  ; iter++)
467                         if (layer->dynamic_param_list().count(iter->first)==0 && iter->second.get_type()==ValueBase::TYPE_CANVAS)
468                         {
469                                 Canvas::Handle subcanvas(iter->second.get(Canvas::Handle()));
470                                 if (subcanvas && subcanvas->is_inline())
471                                         for (Context iter = subcanvas->get_context(); iter != subcanvas->end(); iter++)
472                                                 export_dup_nodes(*iter, canvas, index);
473                         }
474
475                 for (Layer::DynamicParamList::const_iterator iter(layer->dynamic_param_list().begin())
476                                  ; iter != layer->dynamic_param_list().end()
477                                  ; iter++)
478                         if (iter->second->get_type()==ValueBase::TYPE_CANVAS)
479                         {
480                                 Canvas::Handle canvas((*iter->second)(0).get(Canvas::Handle()));
481                                 if (canvas->is_inline())
482                                         //! \todo do we need to implement this?  and if so, shouldn't we check all canvases, not just the one at t=0s?
483                                         warning("%s:%d not yet implemented - do we need to export duplicate valuenodes in dynamic canvas parameters?", __FILE__, __LINE__);
484                         }
485         }
486 }
487
488 void
489 LayerActionManager::amount_inc()
490 {
491         float adjust(0.1);
492
493         // Create the action group
494         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Increase Amount"));
495         synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
496
497         for (; !layer_list.empty(); layer_list.pop_front())
498         {
499                 ValueBase value(layer_list.front()->get_param("amount"));
500                 if(value.same_type_as(Real()))
501                         get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
502         }
503 }
504
505 void
506 LayerActionManager::amount_dec()
507 {
508         float adjust(-0.1);
509
510         // Create the action group
511         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
512         synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
513
514         for (; !layer_list.empty(); layer_list.pop_front())
515         {
516                 ValueBase value(layer_list.front()->get_param("amount"));
517                 if(value.same_type_as(Real()))
518                         get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
519         }
520 }