Use LinkableValueNode members functions when possible in the derived valuenodes.
[synfig.git] / synfig-studio / src / gui / trees / 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 **      Copyright (c) 2008 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 "trees/keyframetree.h"
34 #include "cellrenderer/cellrenderer_time.h"
35 #include <gtkmm/treemodelsort.h>
36 #include <ETL/misc>
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 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 /* === P R O C E D U R E S ================================================= */
54
55 /* === M E T H O D S ======================================================= */
56
57 KeyframeTree::KeyframeTree()
58 {
59         const KeyframeTreeStore::Model model;
60
61         {
62                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Time")) );
63
64                 cell_renderer_time = Gtk::manage( new CellRenderer_Time() );
65                 column->pack_start(*cell_renderer_time,true);
66                 column->add_attribute(cell_renderer_time->property_time(), model.time);
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(model.time);
73
74                 append_column(*column);
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                 cell_renderer_time_delta->signal_edited().connect(sigc::mem_fun(*this,&studio::KeyframeTree::on_edited_time_delta));
83
84                 column->set_reorderable();
85                 column->set_resizable();
86                 column->set_clickable(false);
87                 // column->set_sort_column(model.time_delta);
88
89                 append_column(*column);
90         }
91         {
92                 Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Jump")) );
93
94                 Gtk::CellRendererText* cell_renderer_jump=Gtk::manage(new Gtk::CellRendererText());
95                 column->pack_start(*cell_renderer_jump,true);
96                 cell_renderer_jump->property_text()=_("(JMP)");
97                 cell_renderer_jump->property_foreground()="#003a7f";
98
99                 column->set_reorderable();
100                 column->set_resizable();
101                 column->set_clickable(false);
102                 column->set_sort_column(COLUMNID_JUMP); // without this, (JMP) needs a double click?!
103
104                 append_column(*column);
105         }
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_func(model.time,                 sigc::ptr_fun(&studio::KeyframeTreeStore::time_sorter));
156                 sorted_store->set_sort_func(model.description,  sigc::ptr_fun(&studio::KeyframeTreeStore::description_sorter));
157                 Gtk::TreeView::set_model(sorted_store);
158         }
159         else
160                 Gtk::TreeView::set_model(keyframe_tree_store);
161
162         keyframe_tree_store_->canvas_interface()->signal_rend_desc_changed().connect(
163                 sigc::mem_fun(
164                         *this,
165                         &studio::KeyframeTree::on_rend_desc_changed
166                 )
167         );
168         cell_renderer_time->property_fps().set_value(keyframe_tree_store_->canvas_interface()->get_canvas()->rend_desc().get_frame_rate());
169         cell_renderer_time_delta->property_fps().set_value(keyframe_tree_store_->canvas_interface()->get_canvas()->rend_desc().get_frame_rate());
170 }
171
172 void
173 KeyframeTree::set_editable(bool x)
174 {
175         editable_=x;
176
177         if(editable_)
178         {
179                 cell_renderer_time->property_editable()=true;
180                 cell_renderer_time_delta->property_editable()=true;
181                 cell_renderer_description->property_editable()=true;
182         }
183         else
184         {
185                 cell_renderer_time->property_editable()=false;
186                 cell_renderer_time_delta->property_editable()=false;
187                 cell_renderer_description->property_editable()=false;
188         }
189 }
190
191 void
192 KeyframeTree::on_edited_time(const Glib::ustring&path_string,synfig::Time time)
193 {
194         Gtk::TreePath path(path_string);
195
196         const Gtk::TreeRow row(*(get_model()->get_iter(path)));
197
198         synfig::Keyframe keyframe(row[model.keyframe]);
199         if(time!=keyframe.get_time())
200         {
201                 row[model.time]=time;
202                 //keyframe.set_time(time);
203                 //signal_edited_time()(keyframe,time);
204                 //signal_edited()(keyframe);
205         }
206 }
207
208 void
209 KeyframeTree::on_edited_time_delta(const Glib::ustring&path_string,synfig::Time time)
210 {
211         Gtk::TreePath path(path_string);
212
213         const Gtk::TreeRow row(*(get_model()->get_iter(path)));
214
215         if(row)row[model.time_delta]=time;
216 }
217
218 void
219 KeyframeTree::on_edited_description(const Glib::ustring&path_string,const Glib::ustring &desc)
220 {
221         Gtk::TreePath path(path_string);
222
223         const Gtk::TreeRow row = *(get_model()->get_iter(path));
224
225         const synfig::String description(desc);
226         synfig::Keyframe keyframe(row[model.keyframe]);
227         if(description!=keyframe.get_description())
228         {
229                 row[model.description]=desc;
230                 keyframe.set_description(description);
231                 signal_edited_description()(keyframe,description);
232                 signal_edited()(keyframe);
233         }
234 }
235
236 bool
237 KeyframeTree::on_event(GdkEvent *event)
238 {
239     switch(event->type)
240     {
241         case GDK_BUTTON_PRESS:
242                 {
243                         Gtk::TreeModel::Path path;
244                         Gtk::TreeViewColumn *column;
245                         int cell_x, cell_y;
246                         int wx(round_to_int(event->button.x)),wy(round_to_int(event->button.y));
247                         //tree_to_widget_coords (,, wx, wy);
248                         if(!get_path_at_pos(
249                                 wx,wy,  // x, y
250                                 path, // TreeModel::Path&
251                                 column, //TreeViewColumn*&
252                                 cell_x,cell_y //int&cell_x,int&cell_y
253                                 )
254                         ) break;
255                         const Gtk::TreeRow row = *(get_model()->get_iter(path));
256
257                         signal_user_click()(event->button.button,row,(ColumnID)column->get_sort_column_id());
258                         if((ColumnID)column->get_sort_column_id()==COLUMNID_JUMP)
259                         {
260                                 keyframe_tree_store_->canvas_interface()->set_time(row[model.time]);
261                         }
262                 }
263                 break;
264         case GDK_2BUTTON_PRESS:
265                 {
266                         Gtk::TreeModel::Path path;
267                         Gtk::TreeViewColumn *column;
268                         int cell_x, cell_y;
269                         if(!get_path_at_pos(
270                                 int(event->button.x),int(event->button.y),      // x, y
271                                 path, // TreeModel::Path&
272                                 column, //TreeViewColumn*&
273                                 cell_x,cell_y //int&cell_x,int&cell_y
274                                 )
275                         ) break;
276                         const Gtk::TreeRow row = *(get_model()->get_iter(path));
277
278                         {
279                                 keyframe_tree_store_->canvas_interface()->set_time(row[model.time]);
280                                 return true;
281                         }
282                 }
283                 break;
284
285         case GDK_BUTTON_RELEASE:
286                 break;
287         default:
288                 break;
289         }
290         return false;
291 }