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