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