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