b91751c59c72e048c6fa07f60efcb366b0eb58db
[synfig.git] /
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframeactionmanager.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 "keyframeactionmanager.h"
34 #include "keyframetree.h"
35 #include <synfigapp/action_param.h>
36 #include "instance.h"
37
38 #include "general.h"
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47 using namespace studio;
48
49 static const guint no_prev_popup((guint)-1);
50
51 /* === M A C R O S ========================================================= */
52
53 //#define ONE_ACTION_GROUP 1
54
55 /* === G L O B A L S ======================================================= */
56
57 /* === P R O C E D U R E S ================================================= */
58
59 /* === M E T H O D S ======================================================= */
60
61 KeyframeActionManager::KeyframeActionManager():
62         action_group_(Gtk::ActionGroup::create()),
63         popup_id_(no_prev_popup),
64         queued(false)
65 {
66 }
67
68 KeyframeActionManager::~KeyframeActionManager()
69 {
70 }
71
72 void
73 KeyframeActionManager::set_ui_manager(const Glib::RefPtr<Gtk::UIManager> &x)
74 {
75         clear();
76
77 #ifdef ONE_ACTION_GROUP
78         if(ui_manager_) get_ui_manager()->remove_action_group(action_group_);
79         ui_manager_=x;
80         if(ui_manager_) get_ui_manager()->insert_action_group(action_group_);
81 #else
82         ui_manager_=x;
83 #endif
84 }
85
86 void
87 KeyframeActionManager::set_keyframe_tree(KeyframeTree* x)
88 {
89         selection_changed_connection.disconnect();
90         keyframe_tree_=x;
91         if(keyframe_tree_)
92         {
93                 selection_changed_connection=keyframe_tree_->get_selection()->signal_changed().connect(
94                         sigc::mem_fun(*this,&KeyframeActionManager::queue_refresh)
95                 );
96         }
97 }
98
99 void
100 KeyframeActionManager::set_canvas_interface(const etl::handle<synfigapp::CanvasInterface> &x)
101 {
102         time_changed_connection.disconnect();
103         canvas_interface_=x;
104         if(canvas_interface_)
105         {
106                 canvas_interface_->signal_time_changed().connect(
107                         sigc::mem_fun(*this,&KeyframeActionManager::queue_refresh)
108                 );
109         }
110 }
111
112 void
113 KeyframeActionManager::clear()
114 {
115         if(ui_manager_)
116         {
117                 // Clear out old stuff
118                 if(popup_id_!=no_prev_popup)
119                 {
120                         get_ui_manager()->remove_ui(popup_id_);
121                         popup_id_=no_prev_popup;
122 #ifdef ONE_ACTION_GROUP
123                         while(!action_group_->get_actions().empty())action_group_->remove(*action_group_->get_actions().begin());
124 #else
125                         get_ui_manager()->remove_action_group(action_group_);
126                         action_group_=Gtk::ActionGroup::create();
127 #endif
128                 }
129         }
130 }
131
132 void
133 KeyframeActionManager::queue_refresh()
134 {
135         if(queued)
136                 return;
137
138         //queue_refresh_connection.disconnect();
139         queue_refresh_connection=Glib::signal_idle().connect(
140                 sigc::bind_return(
141                         sigc::mem_fun(*this,&KeyframeActionManager::refresh),
142                         false
143                 )
144         );
145
146         queued=true;
147 }
148
149 void
150 KeyframeActionManager::on_keyframe_properties()
151 {
152         signal_show_keyframe_properties_();
153 }
154
155 void
156 KeyframeActionManager::on_add_keyframe()
157 {
158         synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_add"));
159
160         if(!action)
161                 return;
162
163         action->set_param("canvas",canvas_interface_->get_canvas());
164         action->set_param("canvas_interface",canvas_interface_);
165         action->set_param("keyframe",Keyframe(canvas_interface_->get_time()));
166
167         canvas_interface_->get_instance()->perform_action(action);
168 }
169
170 void
171 KeyframeActionManager::refresh()
172 {
173         KeyframeTreeStore::Model model;
174
175         if(queued)
176         {
177                 queued=false;
178                 //queue_refresh_connection.disconnect();
179         }
180
181
182         clear();
183
184         // Make sure we are ready
185         if(!ui_manager_ || !keyframe_tree_ || !canvas_interface_)
186         {
187                 synfig::error("KeyframeActionManager::refresh(): Not ready!");
188                 return;
189         }
190
191         String ui_info;
192
193         {
194                 synfigapp::Action::ParamList param_list;
195                 param_list.add("time",get_canvas_interface()->get_time());
196                 param_list.add("canvas",get_canvas_interface()->get_canvas());
197                 param_list.add("canvas_interface",get_canvas_interface());
198                 if(keyframe_tree_->get_selection()->count_selected_rows()==1)
199                 {
200                         Keyframe keyframe((*keyframe_tree_->get_selection()->get_selected())[model.keyframe]);
201                         param_list.add("keyframe",keyframe);
202                 }
203
204                 handle<studio::Instance>::cast_static(
205                         get_canvas_interface()->get_instance()
206                 )->add_actions_to_group(
207                         action_group_,
208                         ui_info,
209                         param_list,
210                         synfigapp::Action::CATEGORY_KEYFRAME
211                 );
212         }
213         if(action_group_->get_action("action-keyframe_add"))
214         {
215                 action_group_->remove(action_group_->get_action("action-keyframe_add"));
216         }
217
218                 action_group_->add(Gtk::Action::create(
219                         "action-keyframe_add",
220                         Gtk::StockID("gtk-add"),
221                         _("Add New Keyframe"),_("Add New Keyframe")
222                 ),
223                         sigc::mem_fun(*this,&KeyframeActionManager::on_add_keyframe)
224                 );
225
226         try
227         {
228                 canvas_interface_->get_canvas()->keyframe_list().find(canvas_interface_->get_time());
229                 action_group_->get_action("action-keyframe_add")->set_sensitive(false);
230                 if(action_group_->get_action("action-keyframe_duplicate"))
231                         action_group_->get_action("action-keyframe_duplicate")->set_sensitive(false);
232         }
233         catch(...)
234         {
235         }
236
237         {
238                 Glib::RefPtr<Gtk::Action> action(Gtk::Action::create("keyframe-properties", Gtk::StockID("gtk-properties"),
239                                                                                                                          _("Keyframe Properties"), _("Keyframe Properties")));
240                 action_group_->add(action,sigc::mem_fun(*this,&KeyframeActionManager::on_keyframe_properties));
241                 if(keyframe_tree_->get_selection()->count_selected_rows()==0)
242                         action->set_sensitive(false);
243         }
244
245         ui_info="<ui><popup action='menu-main'><menu action='menu-keyframe'>"+ui_info+"</menu></popup></ui>";
246         popup_id_=get_ui_manager()->add_ui_from_string(ui_info);
247 #ifdef ONE_ACTION_GROUP
248 #else
249         get_ui_manager()->insert_action_group(action_group_);
250 #endif
251 }