Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / synfigstudio_0_61_05 / synfig-studio / src / gtkmm / groupactionmanager.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file template.cpp
3 **      \brief Template File
4 **
5 **      $Id: groupactionmanager.cpp,v 1.1.1.1 2005/01/07 03:34:36 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "groupactionmanager.h"
33 #include "layergrouptree.h"
34 #include <synfigapp/action_param.h>
35 #include "instance.h"
36 #include <gtkmm/stock.h>
37
38 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45 using namespace studio;
46
47 static const guint no_prev_popup((guint)-1);
48
49 /* === M A C R O S ========================================================= */
50
51 //#define ONE_ACTION_GROUP 1
52
53 /* === G L O B A L S ======================================================= */
54
55 /* === P R O C E D U R E S ================================================= */
56
57 /* === M E T H O D S ======================================================= */
58
59 GroupActionManager::GroupActionManager():
60         action_group_(Gtk::ActionGroup::create()),
61         popup_id_(no_prev_popup),
62         queued(false)
63 {
64 }
65
66 GroupActionManager::~GroupActionManager()
67 {
68 }
69
70 void
71 GroupActionManager::set_ui_manager(const Glib::RefPtr<Gtk::UIManager> &x)
72 {
73         clear();
74
75 #ifdef ONE_ACTION_GROUP
76         if(ui_manager_) get_ui_manager()->remove_action_group(action_group_);
77         ui_manager_=x;
78         if(ui_manager_) get_ui_manager()->insert_action_group(action_group_);
79 #else
80         ui_manager_=x;
81 #endif
82 }
83
84 void
85 GroupActionManager::set_group_tree(LayerGroupTree* x)
86 {
87         selection_changed_connection.disconnect();
88         group_tree_=x;
89         if(group_tree_)
90         {
91                 selection_changed_connection=group_tree_->get_selection()->signal_changed().connect(
92                         sigc::mem_fun(*this,&GroupActionManager::queue_refresh)
93                 );
94         }
95 }
96
97 void
98 GroupActionManager::set_canvas_interface(const etl::handle<synfigapp::CanvasInterface> &x)
99 {
100         canvas_interface_=x;
101 }
102
103 void
104 GroupActionManager::clear()
105 {
106         if(ui_manager_)
107         {
108                 // Clear out old stuff
109                 if(popup_id_!=no_prev_popup)
110                 {
111                         get_ui_manager()->remove_ui(popup_id_);
112                         popup_id_=no_prev_popup;
113                         action_group_->set_sensitive(false);
114 #ifdef ONE_ACTION_GROUP
115                         while(!action_group_->get_actions().empty())action_group_->remove(*action_group_->get_actions().begin());
116                         action_group_->set_sensitive(true);
117 #else
118                         get_ui_manager()->remove_action_group(action_group_);
119                         action_group_=Gtk::ActionGroup::create();
120 #endif
121                 }
122         }
123 }
124
125 void
126 GroupActionManager::queue_refresh()
127 {
128         if(queued)
129                 return;
130         
131         //queue_refresh_connection.disconnect();
132         queue_refresh_connection=Glib::signal_idle().connect(
133                 sigc::bind_return(
134                         sigc::mem_fun(*this,&GroupActionManager::refresh),
135                         false
136                 )
137         );
138         
139         queued=true;
140 }
141
142 void
143 GroupActionManager::refresh()
144 {
145         DEBUGPOINT();
146         if(queued)
147         {
148                 queued=false;
149                 //queue_refresh_connection.disconnect();
150         }
151
152
153         clear();
154         
155         // Make sure we are ready
156         if(!ui_manager_ || !group_tree_ || !canvas_interface_)
157         {
158                 synfig::error("GroupActionManager::refresh(): Not ready!");
159                 return;
160         }
161         
162         if(group_tree_->get_selection()->count_selected_rows()==0)
163                 return;
164         
165         String ui_info;
166         
167         {
168                 {
169                         action_group_->add(
170                                 Gtk::Action::create(
171                                         "action-group_add",
172                                         Gtk::Stock::ADD,
173                                         _("Add a New Group"),
174                                         _("Add a New Group")
175                                 ),
176                                 sigc::mem_fun(
177                                         *this,
178                                         &GroupActionManager::on_action_add
179                                 )
180                         );
181                 }
182
183
184 //              bool multiple_selected(group_tree_->get_selection()->count_selected_rows()>1);
185                 LayerGroupTree::LayerList selected_layers(group_tree_->get_selected_layers());
186                 std::list<synfig::String> selected_groups(group_tree_->get_selected_groups());
187                 
188                 synfig::info("selected_layers.size()=%d",selected_layers.size());
189                 synfig::info("selected_groups.size()=%d",selected_groups.size());
190                 
191                 {
192                         bool canvas_set(false);
193                         synfigapp::Action::ParamList param_list;
194                         param_list.add("time",get_canvas_interface()->get_time());
195                         param_list.add("canvas_interface",get_canvas_interface());
196                         
197                         {
198                                 LayerGroupTree::LayerList::iterator iter;
199                         
200                                 for(iter=selected_layers.begin();iter!=selected_layers.end();++iter)
201                                 {
202                                         if(!canvas_set)
203                                         {
204                                                 param_list.add("canvas",Canvas::Handle(Layer::Handle(*iter)->get_canvas()));
205                                                 canvas_set=true;
206                                         }
207                                         param_list.add("layer",Layer::Handle(*iter));
208                                 }
209                         }
210
211                         {
212                                 std::list<synfig::String>::iterator iter;
213                         
214                                 for(iter=selected_groups.begin();iter!=selected_groups.end();++iter)
215                                 {
216                                         param_list.add("group",(synfig::String)*iter);
217                                 }
218                         }
219
220                         if(!canvas_set)
221                         {
222                                 param_list.add("canvas",Canvas::Handle(get_canvas_interface()->get_canvas()));
223                                 canvas_set=true;
224                         }
225                         
226                         handle<studio::Instance>::cast_static(get_canvas_interface()->get_instance())->
227                                 add_actions_to_group(action_group_, ui_info,   param_list, synfigapp::Action::CATEGORY_GROUP);
228                         }
229         }
230         
231         if(true)
232         {
233                 ui_info="<ui><menubar action='menu-main'><menu action='menu-group'>"+ui_info+"</menu></menubar></ui>";
234                 popup_id_=get_ui_manager()->add_ui_from_string(ui_info);
235         }
236         else
237         {
238                 get_ui_manager()->ensure_update();
239         }
240
241 #ifdef ONE_ACTION_GROUP
242 #else
243         get_ui_manager()->insert_action_group(action_group_);
244 #endif
245 }
246
247 void
248 GroupActionManager::on_action_add()
249 {
250         LayerGroupTreeStore::Model model;
251
252         String group_name;
253         
254         Gtk::TreeIter selected_iter;
255         
256         if(group_tree_->get_selection()->count_selected_rows())
257         {
258                 selected_iter=(
259                         group_tree_->get_model()->get_iter(
260                                 (*group_tree_->get_selection()->get_selected_rows().begin())
261                         )
262                 );
263                 if(selected_iter && selected_iter->parent())
264                         group_name=(Glib::ustring)(*selected_iter->parent())[model.group_name]+'.';
265         }
266         
267         group_name+=_("UnnamedGroup");
268         
269         Gtk::TreePath path(group_tree_->get_model()->on_group_added(group_name));
270         
271         group_tree_->expand_to_path(path);
272         group_tree_->set_cursor(path,true);
273 }