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