7a6139ac95f4634f7c6f8bb9460d5250c20dd91c
[synfig.git] / dock_timetrack.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file dock_timetrack.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, 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 "dock_timetrack.h"
34 #include "app.h"
35
36 #include <gtkmm/scrolledwindow.h>
37 #include <cassert>
38 #include "instance.h"
39 #include <sigc++/signal.h>
40 #include <sigc++/hide.h>
41 #include <sigc++/slot.h>
42 #include "canvasview.h"
43 #include "layerparamtreestore.h"
44 #include "workarea.h"
45 #include "widget_timeslider.h"
46 #include "widget_keyframe_list.h"
47 #include "layerparamtreestore.h"
48 #include "general.h"
49 #include <synfig/timepointcollect.h>
50
51 #endif
52
53 /* === U S I N G =========================================================== */
54
55 using namespace std;
56 using namespace etl;
57 using namespace synfig;
58 using namespace studio;
59
60 /* === M A C R O S ========================================================= */
61
62 /* === C L A S S E S ======================================================= */
63
64 class TimeTrackView : public Gtk::TreeView
65 {
66         CellRenderer_TimeTrack *cellrenderer_time_track;
67
68         Glib::RefPtr<LayerParamTreeStore> param_tree_store_;
69
70         Gtk::TreeView *mimic_tree_view;
71 public:
72
73         sigc::signal<void,synfigapp::ValueDesc,std::set<synfig::Waypoint, std::less<UniqueID> >,int> signal_waypoint_clicked_timetrackview;
74
75         LayerParamTreeStore::Model model;
76
77         void set_canvas_view(handle<CanvasView> canvas_view)
78         {
79                 cellrenderer_time_track->set_adjustment(canvas_view->time_adjustment());
80         }
81
82         TimeTrackView()
83         {
84                 int label_index(append_column_editable(_("Name"),model.label));
85                 Gtk::TreeView::Column* label_column = get_column(label_index-1);
86
87                 {       // --- T I M E   T R A C K --------------------------------------------
88                         Gtk::TreeView::Column* column = Gtk::manage( new Gtk::TreeView::Column(_("Time Track")) );
89
90                         // Set up the value-node cell-renderer
91                         cellrenderer_time_track=LayerParamTreeStore::add_cell_renderer_value_node(column);
92                         cellrenderer_time_track->property_mode()=Gtk::CELL_RENDERER_MODE_ACTIVATABLE;
93                         cellrenderer_time_track->signal_waypoint_clicked_cellrenderer().connect(sigc::mem_fun(*this, &TimeTrackView::on_waypoint_clicked_timetrackview));
94                         cellrenderer_time_track->signal_waypoint_changed().connect(sigc::mem_fun(*this, &TimeTrackView::on_waypoint_changed) );
95                         column->add_attribute(cellrenderer_time_track->property_value_desc(), model.value_desc);
96                         column->add_attribute(cellrenderer_time_track->property_canvas(), model.canvas);
97                         //column->add_attribute(cellrenderer_time_track->property_visible(), model.is_value_node);
98
99                         //column->pack_start(*cellrenderer_time_track);
100
101                         // Finish setting up the column
102                         column->set_reorderable();
103                         column->set_resizable();
104                         column->set_min_width(200);
105
106                         append_column(*column);
107                 }
108                 set_rules_hint();
109
110                 set_expander_column(*label_column);
111                 label_column->set_visible(false);
112                 set_headers_visible(false);
113                 set_size_request(-1,64);
114         }
115
116         bool
117         on_event(GdkEvent *event)
118         {
119                 switch(event->type)
120                 {
121                 case GDK_SCROLL:
122                         if(mimic_tree_view)
123                         {
124                                 if(event->scroll.direction==GDK_SCROLL_DOWN)
125                                 {
126                                         mimic_tree_view->get_vadjustment()->set_value(
127                                                 std::min(
128                                                         mimic_tree_view->get_vadjustment()->get_value()+
129                                                         mimic_tree_view->get_vadjustment()->get_step_increment(),
130                                                         mimic_tree_view->get_vadjustment()->get_upper()-
131                                                         mimic_tree_view->get_vadjustment()->get_page_size()
132                                                 )
133                                         );
134                                         mimic_tree_view->get_vadjustment()->value_changed();
135                                 }
136                                 else if(event->scroll.direction==GDK_SCROLL_UP)
137                                 {
138                                         mimic_tree_view->get_vadjustment()->set_value(
139                                                 std::max(
140                                                         mimic_tree_view->get_vadjustment()->get_value()-
141                                                         mimic_tree_view->get_vadjustment()->get_step_increment(),
142                                                         mimic_tree_view->get_vadjustment()->get_lower()
143                                                 )
144                                         );
145                                         mimic_tree_view->get_vadjustment()->value_changed();
146                                 }
147                         }
148                         break;
149                 case GDK_BUTTON_PRESS:
150                         {
151                                 Gtk::TreeModel::Path path;
152                                 Gtk::TreeViewColumn *column;
153                                 int cell_x, cell_y;
154                                 if(!get_path_at_pos(
155                                         int(event->button.x),int(event->button.y),      // x, y
156                                         path, // TreeModel::Path&
157                                         column, //TreeViewColumn*&
158                                         cell_x,cell_y //int&cell_x,int&cell_y
159                                         )
160                                 ) break;
161                                 const Gtk::TreeRow row = *(get_model()->get_iter(path));
162
163                                 if(column && column->get_first_cell_renderer()==cellrenderer_time_track)
164                                 {
165                                         Gdk::Rectangle rect;
166                                         get_cell_area(path,*column,rect);
167                                         cellrenderer_time_track->property_value_desc()=row[model.value_desc];
168                                         cellrenderer_time_track->property_canvas()=row[model.canvas];
169                                         cellrenderer_time_track->activate(event,*this,path.to_string(),rect,rect,Gtk::CellRendererState());
170                                         queue_draw_area(rect.get_x(),rect.get_y(),rect.get_width(),rect.get_height());
171                                         return true;
172                                         //return signal_param_user_click()(event->button.button,row,COLUMNID_TIME_TRACK);
173                                 }
174 /*                              else
175                                 {
176                                         if(event->button.button==3)
177                                         {
178                                                 LayerList layer_list(get_selected_layers());
179                                                 if(layer_list.size()<=1)
180                                                 {
181                                                         synfigapp::ValueDesc value_desc(row[model.value_desc]);
182                                                         Gtk::Menu* menu(manage(new Gtk::Menu()));
183                                                         menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
184                                                         App::get_instance(param_tree_store_->canvas_interface()->get_canvas())->make_param_menu(menu,param_tree_store_->canvas_interface()->get_canvas(),value_desc,0.5f);
185                                                         menu->popup(event->button.button,gtk_get_current_event_time());
186                                                         return true;
187                                                 }
188                                                 Gtk::Menu* menu(manage(new Gtk::Menu()));
189                                                 menu->signal_hide().connect(sigc::bind(sigc::ptr_fun(&delete_widget), menu));
190                                                 std::list<synfigapp::ValueDesc> value_desc_list;
191                                                 ParamDesc param_desc(row[model.param_desc]);
192                                                 for(;!layer_list.empty();layer_list.pop_back())
193                                                         value_desc_list.push_back(synfigapp::ValueDesc(layer_list.back(),param_desc.get_name()));
194                                                 App::get_instance(param_tree_store_->canvas_interface()->get_canvas())->make_param_menu(menu,param_tree_store_->canvas_interface()->get_canvas(),value_desc_list);
195                                                 menu->popup(event->button.button,gtk_get_current_event_time());
196                                                 return true;
197                                         }
198                                         else
199                                         {
200                                                 if(column->get_first_cell_renderer()==cellrenderer_value)
201                                                         return signal_param_user_click()(event->button.button,row,COLUMNID_VALUE);
202                                                 else
203                                                         return signal_param_user_click()(event->button.button,row,COLUMNID_NAME);
204                                         }
205                                 }
206                                 */
207                         }
208                         break;
209
210                 case GDK_MOTION_NOTIFY:
211                         {
212                                 Gtk::TreeModel::Path path;
213                                 Gtk::TreeViewColumn *column;
214                                 int cell_x, cell_y;
215                                 if(!get_path_at_pos(
216                                         (int)event->motion.x,(int)event->motion.y,      // x, y
217                                         path, // TreeModel::Path&
218                                         column, //TreeViewColumn*&
219                                         cell_x,cell_y //int&cell_x,int&cell_y
220                                         )
221                                 ) break;
222
223                                 if(!get_model()->get_iter(path))
224                                         break;
225
226                                 Gtk::TreeRow row = *(get_model()->get_iter(path));
227
228                                 if ((event->motion.state&GDK_BUTTON1_MASK || event->motion.state&GDK_BUTTON3_MASK) &&
229                                         column &&
230                                         cellrenderer_time_track == column->get_first_cell_renderer())
231                                 {
232                                         Gdk::Rectangle rect;
233                                         get_cell_area(path,*column,rect);
234                                         cellrenderer_time_track->property_value_desc()=row[model.value_desc];
235                                         cellrenderer_time_track->property_canvas()=row[model.canvas];
236                                         cellrenderer_time_track->activate(event,*this,path.to_string(),rect,rect,Gtk::CellRendererState());
237                                         queue_draw();
238                                         //queue_draw_area(rect.get_x(),rect.get_y(),rect.get_width(),rect.get_height());
239                                         return true;
240                                 }
241 /*                              else
242                                 if(last_tooltip_path.get_depth()<=0 || path!=last_tooltip_path)
243                                 {
244                                         tooltips_.unset_tip(*this);
245                                         Glib::ustring tooltips_string(row[layer_model.tooltip]);
246                                         last_tooltip_path=path;
247                                         if(!tooltips_string.empty())
248                                         {
249                                                 tooltips_.set_tip(*this,tooltips_string);
250                                                 tooltips_.force_window();
251                                         }
252                                 }
253 */
254                                 return true;
255                         }
256                         break;
257                 case GDK_BUTTON_RELEASE:
258                         {
259                                 Gtk::TreeModel::Path path;
260                                 Gtk::TreeViewColumn *column;
261                                 int cell_x, cell_y;
262                                 if(!get_path_at_pos(
263                                         (int)event->button.x,(int)event->button.y,      // x, y
264                                         path, // TreeModel::Path&
265                                         column, //TreeViewColumn*&
266                                         cell_x,cell_y //int&cell_x,int&cell_y
267                                         )
268                                 ) break;
269
270                                 if(!get_model()->get_iter(path))
271                                         break;
272
273                                 Gtk::TreeRow row = *(get_model()->get_iter(path));
274
275                                 if(column && cellrenderer_time_track==column->get_first_cell_renderer())
276                                 {
277                                         Gdk::Rectangle rect;
278                                         get_cell_area(path,*column,rect);
279                                         cellrenderer_time_track->property_value_desc()=row[model.value_desc];
280                                         cellrenderer_time_track->property_canvas()=row[model.canvas];
281                                         cellrenderer_time_track->activate(event,*this,path.to_string(),rect,rect,Gtk::CellRendererState());
282                                         queue_draw();
283                                         queue_draw_area(rect.get_x(),rect.get_y(),rect.get_width(),rect.get_height());
284                                         return true;
285                                 }
286                         }
287                         break;
288                 default:
289                         break;
290                 }
291                 mimic_resync();
292                 return Gtk::TreeView::on_event(event);
293         }
294
295         void
296         queue_draw_msg()
297         {
298                 synfig::info("*************QUEUE_DRAW***************** (time track view)");
299                 Widget::queue_draw();
300         }
301         void set_model(Glib::RefPtr<LayerParamTreeStore> store)
302         {
303                 Gtk::TreeView::set_model(store);
304                 param_tree_store_=store;
305                 cellrenderer_time_track->set_canvas_interface(param_tree_store_->canvas_interface());
306                 store->signal_changed().connect(sigc::mem_fun(*this, &TimeTrackView::queue_draw));
307         }
308
309         void
310         on_waypoint_changed( synfig::Waypoint waypoint , synfig::ValueNode::Handle value_node)
311         {
312                 // is this code used?
313                 assert(0);
314
315                 synfigapp::Action::ParamList param_list;
316                 param_list.add("canvas",param_tree_store_->canvas_interface()->get_canvas());
317                 param_list.add("canvas_interface",param_tree_store_->canvas_interface());
318                 param_list.add("value_node",value_node);
319                 param_list.add("waypoint",waypoint);
320         //      param_list.add("time",canvas_interface()->get_time());
321
322                 etl::handle<studio::Instance>::cast_static(param_tree_store_->canvas_interface()->get_instance())->process_action("WaypointSetSmart", param_list);
323         }
324
325         void mimic(Gtk::TreeView *param_tree_view)
326         {
327                 mimic_tree_view=param_tree_view;
328                 param_tree_view->signal_row_expanded().connect(
329                         sigc::hide<0>(
330                         sigc::hide_return(
331                                 sigc::bind<-1>(
332                                         sigc::mem_fun(
333                                                 *this,
334                                                 &Gtk::TreeView::expand_row
335                                         ),
336                                         false
337                                 )
338                         ))
339                 );
340                 param_tree_view->signal_row_collapsed().connect(
341                         sigc::hide<0>(
342                         sigc::hide_return(
343                                         sigc::mem_fun(
344                                                 *this,
345                                                 &Gtk::TreeView::collapse_row
346                                         )
347                         ))
348                 );
349                 mimic_resync();
350         }
351
352         void mimic_resync()
353         {
354                 if(mimic_tree_view)
355                 {
356                         Gtk::Adjustment &adjustment(*mimic_tree_view->get_vadjustment());
357                         set_vadjustment(adjustment);
358
359                         if(adjustment.get_page_size()>get_height())
360                                 adjustment.set_page_size(get_height());
361
362                         int row_height = 0;
363                         if(getenv("SYNFIG_TIMETRACK_ROW_HEIGHT"))
364                                 row_height = atoi(getenv("SYNFIG_TIMETRACK_ROW_HEIGHT"));
365                         if (row_height < 3)
366                                 row_height = 18;
367
368                         cellrenderer_time_track->set_fixed_size(-1,row_height);
369                 }
370         }
371
372         void
373         on_waypoint_clicked_timetrackview(const etl::handle<synfig::Node>& node,
374                                                                           const synfig::Time& time,
375                                                                           const synfig::Time& time_offset __attribute__ ((unused)),
376                                                                           int button)
377         {
378                 std::set<synfig::Waypoint, std::less<UniqueID> > waypoint_set;
379                 synfig::waypoint_collect(waypoint_set,time,node);
380
381                 synfigapp::ValueDesc value_desc;
382
383                 if (waypoint_set.size() == 1)
384                 {
385                         ValueNode::Handle value_node(waypoint_set.begin()->get_parent_value_node());
386                         assert(value_node);
387
388                         Gtk::TreeRow row;
389                         if (param_tree_store_->find_first_value_node(value_node, row) && row)
390                                 value_desc = static_cast<synfigapp::ValueDesc>(row[model.value_desc]);
391                 }
392
393                 if (!waypoint_set.empty())
394                         signal_waypoint_clicked_timetrackview(value_desc,waypoint_set,button);
395         }
396 };
397
398 /* === G L O B A L S ======================================================= */
399
400 /* === P R O C E D U R E S ================================================= */
401
402 /* === M E T H O D S ======================================================= */
403
404 Dock_Timetrack::Dock_Timetrack():
405         Dock_CanvasSpecific("timetrack",_("Timetrack"),Gtk::StockID("synfig-timetrack"))
406 {
407         table_=0;
408         widget_timeslider_= new Widget_Timeslider();
409         widget_kf_list_= new Widget_Keyframe_List();
410
411         int header_height = 0;
412         if(getenv("SYNFIG_TIMETRACK_HEADER_HEIGHT"))
413                 header_height = atoi(getenv("SYNFIG_TIMETRACK_HEADER_HEIGHT"));
414         if (header_height < 3)
415                 header_height = 24;
416
417         widget_timeslider_->set_size_request(-1,header_height-header_height/3+1);
418         widget_kf_list_->set_size_request(-1,header_height/3+1);
419
420         hscrollbar_=new Gtk::HScrollbar();
421         vscrollbar_=new Gtk::VScrollbar();
422 }
423
424 Dock_Timetrack::~Dock_Timetrack()
425 {
426         if(table_)delete table_;
427         delete hscrollbar_;
428         delete vscrollbar_;
429         delete widget_timeslider_;
430         delete widget_kf_list_;
431 }
432
433 void
434 Dock_Timetrack::init_canvas_view_vfunc(etl::loose_handle<CanvasView> canvas_view)
435 {
436         LayerParamTreeStore::Model model;
437
438         Glib::RefPtr<LayerParamTreeStore> tree_store(
439                 Glib::RefPtr<LayerParamTreeStore>::cast_dynamic(
440                         canvas_view->get_tree_model("params")
441                 )
442         );
443
444         TimeTrackView* tree_view(new TimeTrackView());
445         tree_view->set_canvas_view(canvas_view);
446         tree_view->set_model(tree_store);
447         Gtk::TreeView* param_tree_view(dynamic_cast<Gtk::TreeView*>(canvas_view->get_ext_widget("params")));
448         tree_view->mimic(param_tree_view);
449
450         tree_view->signal_waypoint_clicked_timetrackview.connect(sigc::mem_fun(*canvas_view, &studio::CanvasView::on_waypoint_clicked_canvasview));
451
452         canvas_view->time_adjustment().signal_value_changed().connect(sigc::mem_fun(*tree_view,&Gtk::TreeView::queue_draw));
453         canvas_view->time_adjustment().signal_changed().connect(sigc::mem_fun(*tree_view,&Gtk::TreeView::queue_draw));
454
455         canvas_view->set_ext_widget(get_name(),tree_view);
456 }
457
458 void
459 Dock_Timetrack::refresh_selected_param()
460 {
461 /*      Gtk::TreeView* tree_view(
462                 static_cast<Gtk::TreeView*>(get_canvas_view()->get_ext_widget(get_name()))
463         );
464         Gtk::TreeModel::iterator iter(tree_view->get_selection()->get_selected());
465
466         if(iter)
467         {
468                 LayerParamTreeStore::Model model;
469                 get_canvas_view()->work_area->set_selected_value_node(
470                         (synfig::ValueNode::Handle)(*iter)[model.value_node]
471                 );
472         }
473         else
474         {
475                 get_canvas_view()->work_area->set_selected_value_node(0);
476         }
477 */
478 }
479
480 void
481 Dock_Timetrack::changed_canvas_view_vfunc(etl::loose_handle<CanvasView> canvas_view)
482 {
483         if(table_)
484         {
485                 table_->hide();
486                 delete table_;
487                 hscrollbar_->unset_adjustment();
488                 vscrollbar_->unset_adjustment();
489                 //widget_timeslider_->unset_adjustment();
490                 table_=0;
491         }
492
493         if(canvas_view)
494         {
495                 TimeTrackView* tree_view(dynamic_cast<TimeTrackView*>(canvas_view->get_ext_widget(get_name())));
496         Gtk::TreeView* param_tree_view(dynamic_cast<Gtk::TreeView*>(canvas_view->get_ext_widget("params")));
497         tree_view->set_vadjustment(*param_tree_view->get_vadjustment());
498
499                 assert(tree_view);
500
501
502                 widget_timeslider_->set_time_adjustment(&canvas_view->time_adjustment());
503                 widget_timeslider_->set_bounds_adjustment(&canvas_view->time_window_adjustment());
504                 widget_timeslider_->set_global_fps(canvas_view->get_canvas()->rend_desc().get_frame_rate());
505
506                 widget_kf_list_->set_time_adjustment(&canvas_view->time_adjustment());
507                 widget_kf_list_->set_canvas_interface(canvas_view->canvas_interface());
508
509                 vscrollbar_->set_adjustment(*tree_view->get_vadjustment());
510                 hscrollbar_->set_adjustment(canvas_view->time_window_adjustment());
511                 table_=new Gtk::Table(2,3);
512                 table_->attach(*widget_timeslider_, 0, 1, 1, 2, Gtk::FILL|Gtk::SHRINK, Gtk::FILL|Gtk::SHRINK);
513                 table_->attach(*widget_kf_list_, 0, 1, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::SHRINK);
514                 table_->attach(*tree_view, 0, 1, 2, 3, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
515                 table_->attach(*hscrollbar_, 0, 1, 3, 4, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::SHRINK);
516                 table_->attach(*vscrollbar_, 1, 2, 0, 3, Gtk::FILL|Gtk::SHRINK, Gtk::FILL|Gtk::EXPAND);
517                 add(*table_);
518
519                 //add(*last_widget_curves_);
520                 table_->show_all();
521                 show_all();
522         }
523         else
524         {
525                 //clear_previous();
526         }
527 }