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