Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_07_rc1 / src / gtkmm / groupactionmanager.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file groupactionmanager.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 "groupactionmanager.h"
34 #include "layergrouptree.h"
35 #include <synfigapp/action_param.h>
36 #include "instance.h"
37 #include <gtkmm/stock.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 GroupActionManager::GroupActionManager():
61         action_group_(Gtk::ActionGroup::create()),
62         popup_id_(no_prev_popup),
63         queued(false)
64 {
65 }
66
67 GroupActionManager::~GroupActionManager()
68 {
69 }
70
71 void
72 GroupActionManager::set_ui_manager(const Glib::RefPtr<Gtk::UIManager> &x)
73 {
74         clear();
75
76 #ifdef ONE_ACTION_GROUP
77         if(ui_manager_) get_ui_manager()->remove_action_group(action_group_);
78         ui_manager_=x;
79         if(ui_manager_) get_ui_manager()->insert_action_group(action_group_);
80 #else
81         ui_manager_=x;
82 #endif
83 }
84
85 void
86 GroupActionManager::set_group_tree(LayerGroupTree* x)
87 {
88         selection_changed_connection.disconnect();
89         group_tree_=x;
90         if(group_tree_)
91         {
92                 selection_changed_connection=group_tree_->get_selection()->signal_changed().connect(
93                         sigc::mem_fun(*this,&GroupActionManager::queue_refresh)
94                 );
95         }
96 }
97
98 void
99 GroupActionManager::set_canvas_interface(const etl::handle<synfigapp::CanvasInterface> &x)
100 {
101         canvas_interface_=x;
102 }
103
104 void
105 GroupActionManager::clear()
106 {
107         if(ui_manager_)
108         {
109                 // Clear out old stuff
110                 if(popup_id_!=no_prev_popup)
111                 {
112                         get_ui_manager()->remove_ui(popup_id_);
113                         popup_id_=no_prev_popup;
114                         action_group_->set_sensitive(false);
115 #ifdef ONE_ACTION_GROUP
116                         while(!action_group_->get_actions().empty())action_group_->remove(*action_group_->get_actions().begin());
117                         action_group_->set_sensitive(true);
118 #else
119                         get_ui_manager()->remove_action_group(action_group_);
120                         action_group_=Gtk::ActionGroup::create();
121 #endif
122                 }
123         }
124 }
125
126 void
127 GroupActionManager::queue_refresh()
128 {
129         if(queued)
130                 return;
131
132         //queue_refresh_connection.disconnect();
133         queue_refresh_connection=Glib::signal_idle().connect(
134                 sigc::bind_return(
135                         sigc::mem_fun(*this,&GroupActionManager::refresh),
136                         false
137                 )
138         );
139
140         queued=true;
141 }
142
143 void
144 GroupActionManager::refresh()
145 {
146         DEBUGPOINT();
147         if(queued)
148         {
149                 queued=false;
150                 //queue_refresh_connection.disconnect();
151         }
152
153
154         clear();
155
156         // Make sure we are ready
157         if(!ui_manager_ || !group_tree_ || !canvas_interface_)
158         {
159                 synfig::error("GroupActionManager::refresh(): Not ready!");
160                 return;
161         }
162
163         if(group_tree_->get_selection()->count_selected_rows()==0)
164                 return;
165
166         String ui_info;
167
168         {
169                 {
170                         action_group_->add(
171                                 Gtk::Action::create(
172                                         "action-group_add",
173                                         Gtk::Stock::ADD,
174                                         _("Add a New Group"),
175                                         _("Add a New Group")
176                                 ),
177                                 sigc::mem_fun(
178                                         *this,
179                                         &GroupActionManager::on_action_add
180                                 )
181                         );
182                 }
183
184
185 //              bool multiple_selected(group_tree_->get_selection()->count_selected_rows()>1);
186                 LayerGroupTree::LayerList selected_layers(group_tree_->get_selected_layers());
187                 std::list<synfig::String> selected_groups(group_tree_->get_selected_groups());
188
189                 synfig::info("selected_layers.size()=%d",selected_layers.size());
190                 synfig::info("selected_groups.size()=%d",selected_groups.size());
191
192                 {
193                         bool canvas_set(false);
194                         synfigapp::Action::ParamList param_list;
195                         param_list.add("time",get_canvas_interface()->get_time());
196                         param_list.add("canvas_interface",get_canvas_interface());
197
198                         {
199                                 LayerGroupTree::LayerList::iterator iter;
200
201                                 for(iter=selected_layers.begin();iter!=selected_layers.end();++iter)
202                                 {
203                                         if(!canvas_set)
204                                         {
205                                                 param_list.add("canvas",Canvas::Handle(Layer::Handle(*iter)->get_canvas()));
206                                                 canvas_set=true;
207                                         }
208                                         param_list.add("layer",Layer::Handle(*iter));
209                                 }
210                         }
211
212                         {
213                                 std::list<synfig::String>::iterator iter;
214
215                                 for(iter=selected_groups.begin();iter!=selected_groups.end();++iter)
216                                 {
217                                         param_list.add("group",(synfig::String)*iter);
218                                 }
219                         }
220
221                         if(!canvas_set)
222                         {
223                                 param_list.add("canvas",Canvas::Handle(get_canvas_interface()->get_canvas()));
224                                 canvas_set=true;
225                         }
226
227                         handle<studio::Instance>::cast_static(get_canvas_interface()->get_instance())->
228                                 add_actions_to_group(action_group_, ui_info,   param_list, synfigapp::Action::CATEGORY_GROUP);
229                         }
230         }
231
232         if(true)
233         {
234                 ui_info="<ui><popup action='menu-main'><menu action='menu-group'>"+ui_info+"</menu></popup></ui>";
235                 popup_id_=get_ui_manager()->add_ui_from_string(ui_info);
236         }
237         else
238         {
239                 get_ui_manager()->ensure_update();
240         }
241
242 #ifdef ONE_ACTION_GROUP
243 #else
244         get_ui_manager()->insert_action_group(action_group_);
245 #endif
246 }
247
248 void
249 GroupActionManager::on_action_add()
250 {
251         LayerGroupTreeStore::Model model;
252
253         String group_name;
254
255         Gtk::TreeIter selected_iter;
256
257         if(group_tree_->get_selection()->count_selected_rows())
258         {
259                 selected_iter=(
260                         group_tree_->get_model()->get_iter(
261                                 (*group_tree_->get_selection()->get_selected_rows().begin())
262                         )
263                 );
264                 if(selected_iter && selected_iter->parent())
265                         group_name=(Glib::ustring)(*selected_iter->parent())[model.group_name]+'.';
266         }
267
268         group_name+=_("UnnamedGroup");
269
270         Gtk::TreePath path(group_tree_->get_model()->on_group_added(group_name));
271
272         group_tree_->expand_to_path(path);
273         group_tree_->set_cursor(path,true);
274 }