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