Include tooltip on the 'show all child layers' toolbar button.
[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 <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, ui_toolbar_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                                         Gtk::StockID("synfig-select_all_child_layers"),
291                                         _("Select All Child Layers"),
292                                         _("Select All Child Layers")),
293                                         sigc::bind(sigc::mem_fun(*layer_tree_,
294                                                                                          &studio::LayerTree::select_all_children_layers),
295                                                            Layer::LooseHandle(layer)));
296                                 ui_info+="<menuitem action='select-all-child-layers'/>";
297                                 ui_toolbar_info+="<toolbar action='toolbar-layer'><toolitem action='select-all-child-layers'/></toolbar>";
298                         }
299                         handle<studio::Instance>::cast_static(get_canvas_interface()->get_instance())->
300                                 add_actions_to_group(action_group_, ui_info,   param_list, synfigapp::Action::CATEGORY_LAYER);
301                 }
302         }
303
304         ui_info=("<ui>"
305                            "<popup action='menu-main'>"
306                              "<menu action='menu-layer'>" +
307                                    ui_info +
308                                    "<separator/>"
309                                "<menuitem action='cut' />"
310                                    "<menuitem action='copy' />"
311                                    "<menuitem action='paste' />"
312                                    "<separator/>"
313                              "</menu>"
314                            "</popup>" +
315                          ui_toolbar_info +
316                          "</ui>");
317         popup_id_=get_ui_manager()->add_ui_from_string(ui_info);
318 #ifdef ONE_ACTION_GROUP
319 #else
320         get_ui_manager()->insert_action_group(action_group_);
321 #endif
322 }
323
324 void
325 LayerActionManager::cut()
326 {
327         copy();
328         if(action_group_->get_action("action-layer_remove"))
329                 action_group_->get_action("action-layer_remove")->activate();
330 }
331
332 void
333 LayerActionManager::copy()
334 {
335         synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
336         clipboard_.clear();
337         synfig::GUID guid;
338
339         while(!layer_list.empty())
340         {
341                 clipboard_.push_back(layer_list.front()->clone(guid));
342                 layer_list.pop_front();
343         }
344
345         action_paste_->set_sensitive(!clipboard_.empty());
346
347         //queue_refresh();
348 }
349
350 void
351 LayerActionManager::paste()
352 {
353         synfig::GUID guid;
354
355         // Create the action group
356         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Paste"));
357
358         Canvas::Handle canvas(get_canvas_interface()->get_canvas());
359         int depth(0);
360
361         // we are temporarily using the layer to hold something
362         Layer::Handle layer(layer_tree_->get_selected_layer());
363         if(layer)
364         {
365                 depth=layer->get_depth();
366                 canvas=layer->get_canvas();
367         }
368
369         synfigapp::SelectionManager::LayerList layer_selection;
370
371         for(std::list<synfig::Layer::Handle>::iterator iter=clipboard_.begin();iter!=clipboard_.end();++iter)
372         {
373                 layer=(*iter)->clone(guid);
374                 layer_selection.push_back(layer);
375                 synfigapp::Action::Handle       action(synfigapp::Action::create("layer_add"));
376
377                 assert(action);
378                 if(!action)
379                         return;
380
381                 action->set_param("canvas",canvas);
382                 action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
383                 action->set_param("new",layer);
384
385                 if(!action->is_ready())
386                 {
387                         return;
388                 }
389
390                 if(!get_instance()->perform_action(action))
391                 {
392                         return;
393                 }
394
395                 // synfig::info("DEPTH=%d",depth);
396
397                 // Action to move the layer (if necessary)
398                 if(depth>0)
399                 {
400                         synfigapp::Action::Handle       action(synfigapp::Action::create("layer_move"));
401
402                         assert(action);
403                         if(!action)
404                                 return;
405
406                         action->set_param("canvas",canvas);
407                         action->set_param("canvas_interface",etl::loose_handle<synfigapp::CanvasInterface>(get_canvas_interface()));
408                         action->set_param("layer",layer);
409                         action->set_param("new_index",depth);
410
411                         if(!action->is_ready())
412                         {
413                                 //get_ui_interface()->error(_("Move Action Not Ready"));
414                                 //return 0;
415                                 return;
416                         }
417
418                         if(!get_instance()->perform_action(action))
419                         {
420                                 //get_ui_interface()->error(_("Move Action Not Ready"));
421                                 //return 0;
422                                 return;
423                         }
424                 }
425                 depth++;
426
427                 // automatically export the Index parameter of Duplicate layers when pasting
428                 if (layer->get_name() == "duplicate")
429                         for (int i = 1; ; i++)
430                         {
431                                 String name = strprintf(_("Index %d"), i);
432                                 try
433                                 {
434                                         canvas->find_value_node(name);
435                                 }
436                                 catch (Exception::IDNotFound x)
437                                 {
438                                         get_canvas_interface()->add_value_node(layer->dynamic_param_list().find("index")->second, name);
439                                         break;
440                                 }
441                         }
442         }
443         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
444         get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
445 }
446
447 void
448 LayerActionManager::amount_inc()
449 {
450         float adjust(0.1);
451
452         // Create the action group
453         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
454
455         if(adjust>0)
456                 group.set_name(_("Increase Amount"));
457
458         synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
459
460         while(!layer_list.empty())
461         {
462                 ValueBase value(layer_list.front()->get_param("amount"));
463                 if(value.same_type_as(Real()))
464                 {
465                         get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
466                 }
467                 layer_list.pop_front();
468         }
469 }
470
471 void
472 LayerActionManager::amount_dec()
473 {
474         float adjust(-0.1);
475
476         // Create the action group
477         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Decrease Amount"));
478
479         if(adjust>0)
480                 group.set_name(_("Increase Amount"));
481
482         synfigapp::SelectionManager::LayerList layer_list(layer_tree_->get_selected_layers());
483
484         while(!layer_list.empty())
485         {
486                 ValueBase value(layer_list.front()->get_param("amount"));
487                 if(value.same_type_as(Real()))
488                 {
489                         get_canvas_interface()->change_value(synfigapp::ValueDesc(layer_list.front(),"amount"),value.get(Real())+adjust);
490                 }
491                 layer_list.pop_front();
492         }
493 }