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