Fix 1675309: CanvasView::close() was previously just hiding the canvas, not closing it.
[synfig.git] / synfig-studio / trunk / src / gtkmm / keyframeactionmanager.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframeactionmanager.cpp
3 **      \brief Template File
4 **
5 **      $Id: keyframeactionmanager.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 "keyframeactionmanager.h"
33 #include "keyframetree.h"
34 #include <synfigapp/action_param.h>
35 #include "instance.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 KeyframeActionManager::KeyframeActionManager():
59         action_group_(Gtk::ActionGroup::create()),
60         popup_id_(no_prev_popup),
61         queued(false)
62 {
63 }
64
65 KeyframeActionManager::~KeyframeActionManager()
66 {
67 }
68
69 void
70 KeyframeActionManager::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 KeyframeActionManager::set_keyframe_tree(KeyframeTree* x)
85 {
86         selection_changed_connection.disconnect();
87         keyframe_tree_=x;
88         if(keyframe_tree_)
89         {
90                 selection_changed_connection=keyframe_tree_->get_selection()->signal_changed().connect(
91                         sigc::mem_fun(*this,&KeyframeActionManager::queue_refresh)
92                 );
93         }
94 }
95
96 void
97 KeyframeActionManager::set_canvas_interface(const etl::handle<synfigapp::CanvasInterface> &x)
98 {
99         time_changed_connection.disconnect();
100         canvas_interface_=x;
101         if(canvas_interface_)
102         {
103                 canvas_interface_->signal_time_changed().connect(
104                         sigc::mem_fun(*this,&KeyframeActionManager::queue_refresh)
105                 );
106         }
107 }
108
109 void
110 KeyframeActionManager::clear()
111 {
112         if(ui_manager_)
113         {
114                 // Clear out old stuff
115                 if(popup_id_!=no_prev_popup)
116                 {
117                         get_ui_manager()->remove_ui(popup_id_);
118                         popup_id_=no_prev_popup;
119 #ifdef ONE_ACTION_GROUP
120                         while(!action_group_->get_actions().empty())action_group_->remove(*action_group_->get_actions().begin());
121 #else
122                         get_ui_manager()->remove_action_group(action_group_);
123                         action_group_=Gtk::ActionGroup::create();
124 #endif
125                 }
126         }
127 }
128
129 void
130 KeyframeActionManager::queue_refresh()
131 {
132         if(queued)
133                 return;
134         
135         //queue_refresh_connection.disconnect();
136         queue_refresh_connection=Glib::signal_idle().connect(
137                 sigc::bind_return(
138                         sigc::mem_fun(*this,&KeyframeActionManager::refresh),
139                         false
140                 )
141         );
142         
143         queued=true;
144 }
145
146 void
147 KeyframeActionManager::on_keyframe_properties()
148 {
149         signal_show_keyframe_properties_();
150 }
151
152 void
153 KeyframeActionManager::on_add_keyframe()
154 {
155         synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_add"));
156
157         if(!action)
158                 return;
159         
160         action->set_param("canvas",canvas_interface_->get_canvas());
161         action->set_param("canvas_interface",canvas_interface_);
162         action->set_param("keyframe",Keyframe(canvas_interface_->get_time()));
163
164         canvas_interface_->get_instance()->perform_action(action);
165 }
166
167 void
168 KeyframeActionManager::refresh()
169 {
170         KeyframeTreeStore::Model model;
171         
172         if(queued)
173         {
174                 queued=false;
175                 //queue_refresh_connection.disconnect();
176         }
177
178
179         clear();
180         
181         // Make sure we are ready
182         if(!ui_manager_ || !keyframe_tree_ || !canvas_interface_)
183         {
184                 synfig::error("KeyframeActionManager::refresh(): Not ready!");
185                 return;
186         }
187                 
188         String ui_info;
189         
190         {
191                 synfigapp::Action::ParamList param_list;
192                 param_list.add("time",get_canvas_interface()->get_time());
193                 param_list.add("canvas",get_canvas_interface()->get_canvas());
194                 param_list.add("canvas_interface",get_canvas_interface());
195                 if(keyframe_tree_->get_selection()->count_selected_rows()==1)
196                 {
197                         Keyframe keyframe((*keyframe_tree_->get_selection()->get_selected())[model.keyframe]);
198                         param_list.add("keyframe",keyframe);
199                 }
200
201                 handle<studio::Instance>::cast_static(
202                         get_canvas_interface()->get_instance()
203                 )->add_actions_to_group(
204                         action_group_,
205                         ui_info,
206                         param_list,
207                         synfigapp::Action::CATEGORY_KEYFRAME
208                 );
209         }
210         if(action_group_->get_action("action-keyframe_add"))
211         {
212                 action_group_->remove(action_group_->get_action("action-keyframe_add"));
213         }
214         
215                 action_group_->add(Gtk::Action::create(
216                         "action-keyframe_add",
217                         Gtk::StockID("gtk-add"),
218                         _("Add new Keyframe"),_("Add new Keyframe")
219                 ),
220                         sigc::mem_fun(*this,&KeyframeActionManager::on_add_keyframe)
221                 );
222         
223         try
224         {
225                 canvas_interface_->get_canvas()->keyframe_list().find(canvas_interface_->get_time());
226                 action_group_->get_action("action-keyframe_add")->set_sensitive(false);
227                 if(action_group_->get_action("action-keyframe_duplicate"))
228                         action_group_->get_action("action-keyframe_duplicate")->set_sensitive(false);
229         }
230         catch(...)
231         {
232         }
233         
234         {
235                 Glib::RefPtr<Gtk::Action> action(Gtk::Action::create("keyframe-properties", Gtk::StockID("gtk-properties"), _("Keyframe Properties")));
236                 action_group_->add(action,sigc::mem_fun(*this,&KeyframeActionManager::on_keyframe_properties));
237                 if(keyframe_tree_->get_selection()->count_selected_rows()==0)
238                         action->set_sensitive(false);
239         }
240
241         ui_info="<ui><menubar action='menu-main'><menu action='menu-keyframe'>"+ui_info+"</menu></menubar></ui>";
242         popup_id_=get_ui_manager()->add_ui_from_string(ui_info);        
243 #ifdef ONE_ACTION_GROUP
244 #else
245         get_ui_manager()->insert_action_group(action_group_);
246 #endif
247 }