Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / keyframetree.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframetree.cpp
3 **      \brief Template File
4 **
5 **      $Id$
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 "keyframetree.h"
33 #include "cellrenderer_time.h"
34 #include <gtkmm/treemodelsort.h>
35 #include <ETL/misc>
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 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 KeyframeTree::KeyframeTree()
55 {
56         const KeyframeTreeStore::Model model;
57
58         {
59                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Time")) );
60
61                 cell_renderer_time = Gtk::manage( new CellRenderer_Time() );
62                 column->pack_start(*cell_renderer_time,true);
63                 column->add_attribute(cell_renderer_time->property_time(), model.time);
64
65                 cell_renderer_time->signal_edited().connect(sigc::mem_fun(*this,&studio::KeyframeTree::on_edited_time));
66
67                 column->set_reorderable();
68                 column->set_resizable();
69                 column->set_clickable();
70                 //column->set_sort_column_id(COLUMNID_TIME);
71                 column->set_sort_column_id(model.time);
72
73                 append_column(*column);
74                 //column->clicked();
75         }
76         {
77                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Length")) );
78
79                 cell_renderer_time_delta = Gtk::manage( new CellRenderer_Time() );
80                 column->pack_start(*cell_renderer_time_delta,true);
81                 column->add_attribute(cell_renderer_time_delta->property_time(), model.time_delta);
82
83                 cell_renderer_time_delta->signal_edited().connect(sigc::mem_fun(*this,&studio::KeyframeTree::on_edited_time_delta));
84
85                 column->set_reorderable();
86                 column->set_resizable();
87
88                 column->set_sort_column_id(model.time_delta);
89
90                 column->set_clickable(false);
91
92                 append_column(*column);
93                 //column->clicked();
94         }
95         {
96                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Jump")) );
97
98                 Gtk::CellRendererText* cell_renderer_jump=Gtk::manage(new Gtk::CellRendererText());
99                 column->pack_start(*cell_renderer_jump,true);
100
101                 cell_renderer_jump->property_text()="(JMP)";
102                 cell_renderer_jump->property_foreground()="#003a7f";
103
104                 column->set_reorderable();
105                 column->set_resizable();
106
107                 column->set_sort_column_id(COLUMNID_JUMP);
108
109                 column->set_clickable(false);
110
111                 append_column(*column);
112                 //column->clicked();
113         }
114         //append_column_editable(_("Description"),model.description);
115         {
116                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Description")) );
117
118                 cell_renderer_description=Gtk::manage(new Gtk::CellRendererText());
119                 column->pack_start(*cell_renderer_description,true);
120                 column->add_attribute(cell_renderer_description->property_text(), model.description);
121                 cell_renderer_description->signal_edited().connect(sigc::mem_fun(*this,&studio::KeyframeTree::on_edited_description));
122
123                 column->set_reorderable();
124                 column->set_resizable();
125                 column->set_clickable();
126                 //column->set_sort_column_id(COLUMNID_DESCRIPTION);
127                 column->set_sort_column_id(model.description);
128
129                 append_column(*column);
130         }
131
132         set_enable_search(true);
133         set_search_column(model.description);
134
135         // This makes things easier to read.
136         set_rules_hint();
137
138         // Make us more sensitive to several events
139         add_events(Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
140 }
141
142 KeyframeTree::~KeyframeTree()
143 {
144         synfig::info("KeyframeTree::~KeyframeTree(): deleted");
145 }
146
147 void
148 KeyframeTree::on_rend_desc_changed()
149 {
150         cell_renderer_time->property_fps().set_value(keyframe_tree_store_->canvas_interface()->get_canvas()->rend_desc().get_frame_rate());
151         queue_draw();
152 }
153
154 void
155 KeyframeTree::set_model(Glib::RefPtr<KeyframeTreeStore> keyframe_tree_store)
156 {
157         keyframe_tree_store_=keyframe_tree_store;
158         KeyframeTreeStore::Model model;
159
160         if(true)
161         {
162                 Glib::RefPtr<Gtk::TreeModelSort> sorted_store(Gtk::TreeModelSort::create(keyframe_tree_store_));
163                 sorted_store->set_default_sort_func(sigc::ptr_fun(&studio::KeyframeTreeStore::time_sorter));
164                 sorted_store->set_sort_func(model.time.index(),sigc::ptr_fun(&studio::KeyframeTreeStore::time_sorter));
165                 sorted_store->set_sort_column_id(model.time.index(), Gtk::SORT_ASCENDING);
166                 Gtk::TreeView::set_model(sorted_store);
167         }
168         else
169                 Gtk::TreeView::set_model(keyframe_tree_store);
170
171         keyframe_tree_store_->canvas_interface()->signal_rend_desc_changed().connect(
172                 sigc::mem_fun(
173                         *this,
174                         &studio::KeyframeTree::on_rend_desc_changed
175                 )
176         );
177         cell_renderer_time->property_fps().set_value(keyframe_tree_store_->canvas_interface()->get_canvas()->rend_desc().get_frame_rate());
178         cell_renderer_time_delta->property_fps().set_value(keyframe_tree_store_->canvas_interface()->get_canvas()->rend_desc().get_frame_rate());
179 }
180
181 void
182 KeyframeTree::set_editable(bool x)
183 {
184         editable_=x;
185
186         if(editable_)
187         {
188                 cell_renderer_time->property_editable()=true;
189                 cell_renderer_time_delta->property_editable()=true;
190                 cell_renderer_description->property_editable()=true;
191         }
192         else
193         {
194                 cell_renderer_time->property_editable()=false;
195                 cell_renderer_time_delta->property_editable()=false;
196                 cell_renderer_description->property_editable()=false;
197         }
198 }
199
200 void
201 KeyframeTree::on_edited_time(const Glib::ustring&path_string,synfig::Time time)
202 {
203         Gtk::TreePath path(path_string);
204
205         const Gtk::TreeRow row(*(get_model()->get_iter(path)));
206
207         synfig::Keyframe keyframe(row[model.keyframe]);
208         if(time!=keyframe.get_time())
209         {
210                 row[model.time]=time;
211                 //keyframe.set_time(time);
212                 //signal_edited_time()(keyframe,time);
213                 //signal_edited()(keyframe);
214         }
215 }
216
217 void
218 KeyframeTree::on_edited_time_delta(const Glib::ustring&path_string,synfig::Time time)
219 {
220         Gtk::TreePath path(path_string);
221
222         const Gtk::TreeRow row(*(get_model()->get_iter(path)));
223
224         if(row)row[model.time_delta]=time;
225 }
226
227 void
228 KeyframeTree::on_edited_description(const Glib::ustring&path_string,const Glib::ustring &desc)
229 {
230         Gtk::TreePath path(path_string);
231
232         const Gtk::TreeRow row = *(get_model()->get_iter(path));
233
234         const synfig::String description(desc);
235         synfig::Keyframe keyframe(row[model.keyframe]);
236         if(description!=keyframe.get_description())
237         {
238                 row[model.description]=desc;
239                 keyframe.set_description(description);
240                 signal_edited_description()(keyframe,description);
241                 signal_edited()(keyframe);
242         }
243 }
244
245 bool
246 KeyframeTree::on_event(GdkEvent *event)
247 {
248     switch(event->type)
249     {
250         case GDK_BUTTON_PRESS:
251                 {
252                         Gtk::TreeModel::Path path;
253                         Gtk::TreeViewColumn *column;
254                         int cell_x, cell_y;
255                         int wx(round_to_int(event->button.x)),wy(round_to_int(event->button.y));
256                         //tree_to_widget_coords (,, wx, wy);
257                         if(!get_path_at_pos(
258                                 wx,wy,  // x, y
259                                 path, // TreeModel::Path&
260                                 column, //TreeViewColumn*&
261                                 cell_x,cell_y //int&cell_x,int&cell_y
262                                 )
263                         ) break;
264                         const Gtk::TreeRow row = *(get_model()->get_iter(path));
265
266                         signal_user_click()(event->button.button,row,(ColumnID)column->get_sort_column_id());
267                         if((ColumnID)column->get_sort_column_id()==COLUMNID_JUMP)
268                         {
269                                 keyframe_tree_store_->canvas_interface()->set_time(row[model.time]);
270                         }
271                 }
272                 break;
273         case GDK_2BUTTON_PRESS:
274                 {
275                         Gtk::TreeModel::Path path;
276                         Gtk::TreeViewColumn *column;
277                         int cell_x, cell_y;
278                         if(!get_path_at_pos(
279                                 int(event->button.x),int(event->button.y),      // x, y
280                                 path, // TreeModel::Path&
281                                 column, //TreeViewColumn*&
282                                 cell_x,cell_y //int&cell_x,int&cell_y
283                                 )
284                         ) break;
285                         const Gtk::TreeRow row = *(get_model()->get_iter(path));
286
287                         {
288                                 keyframe_tree_store_->canvas_interface()->set_time(row[model.time]);
289                                 return true;
290                         }
291                 }
292                 break;
293
294         case GDK_BUTTON_RELEASE:
295                 break;
296         default:
297                 break;
298         }
299         return false;
300 }