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