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