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