The 'polygon', 'draw', 'sketch', and 'width' tools are disabled by default. This...
[synfig.git] / synfig-studio / trunk / src / gtkmm / canvasview.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file canvasview.cpp
3 **      \brief Template File
4 **
5 **      $Id: canvasview.cpp,v 1.7 2005/01/16 19:55:57 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 <sigc++/adaptors/hide.h>
33
34 #include <ETL/clock>
35 #include <sstream>
36
37 #include <gtkmm/paned.h>
38 #include <gtkmm/scale.h>
39 #include <gtkmm/dialog.h>
40 #include <gtkmm/treemodelsort.h>
41 #include <gtkmm/buttonbox.h>
42
43 #include <gtk/gtktreestore.h>
44 #include <gtk/gtkversion.h>
45
46 #include <synfig/valuenode_reference.h>
47 #include <synfig/valuenode_subtract.h>          
48 #include <synfig/valuenode_linear.h>            
49 #include <synfig/valuenode_timedswap.h>         
50 #include <synfig/valuenode_scale.h>
51 #include <synfig/valuenode_dynamiclist.h>
52 #include <synfig/valuenode_twotone.h>
53 #include <synfig/valuenode_stripes.h>
54 #include <synfig/layer.h>
55
56 #include <synfigapp/uimanager.h>
57 #include <synfigapp/canvasinterface.h>
58 #include <synfigapp/selectionmanager.h>
59 //#include <synfigapp/action_setwaypoint.h>
60 //#include <synfigapp/action_deletewaypoint.h>
61
62 #include <sigc++/retype_return.h>
63 #include <sigc++/retype.h>
64 //#include <sigc++/hide.h>
65
66 #include "canvasview.h"
67 #include "instance.h"
68 #include "app.h"
69 #include "cellrenderer_value.h"
70 #include "cellrenderer_timetrack.h"
71 #include "workarea.h"
72 #include "dialog_color.h"
73 #include "eventkey.h"
74
75 #include "state_polygon.h"
76 #include "state_bline.h"
77 #include "state_normal.h"
78 #include "state_eyedrop.h"
79 #include "state_draw.h"
80
81 #include "ducktransform_scale.h"
82 #include "ducktransform_translate.h"
83 #include "ducktransform_rotate.h"
84
85 #include "event_mouse.h"
86 #include "event_layerclick.h"
87
88 #include "toolbox.h"
89
90 #include "dialog_preview.h"
91 #include "dialog_soundselect.h"
92
93 #include "preview.h"
94 #include "audiocontainer.h"
95 #include "widget_timeslider.h"
96
97 #include <synfigapp/main.h>
98 #include <synfigapp/inputdevice.h>
99
100 #endif
101
102 /* === U S I N G =========================================================== */
103
104 using namespace std;
105 using namespace etl;
106 using namespace synfig;
107 using namespace studio;
108 using namespace SigC;
109
110 /* === M A C R O S ========================================================= */
111
112 #define GRAB_HINT_DATA(y)       { \
113                 String x; \
114                 if(synfigapp::Main::settings().get_value(String("pref.")+y+"_hints",x)) \
115                 { \
116                         set_type_hint((Gdk::WindowTypeHint)atoi(x.c_str()));    \
117                 } \
118         }
119
120 #define DEFAULT_TIME_WINDOW_SIZE                (10.0)
121
122 /*
123 #ifdef DEBUGPOINT
124 #undef DEBUGPOINT
125 #endif
126 #define DEBUGPOINT()
127 */
128
129 #ifndef SMALL_BUTTON
130 #define SMALL_BUTTON(button,stockid,tooltip)    \
131         button = manage(new class Gtk::Button());       \
132         icon=manage(new Gtk::Image(Gtk::StockID(stockid),iconsize));    \
133         button->add(*icon);     \
134         tooltips.set_tip(*button,tooltip);      \
135         icon->set_padding(0,0);\
136         icon->show();   \
137         button->set_relief(Gtk::RELIEF_NONE); \
138         button->show()
139 #endif
140
141 #ifndef NORMAL_BUTTON
142 #define NORMAL_BUTTON(button,stockid,tooltip)   \
143         button = manage(new class Gtk::Button());       \
144         icon=manage(new Gtk::Image(Gtk::StockID(stockid),Gtk::ICON_SIZE_BUTTON));       \
145         button->add(*icon);     \
146         tooltips.set_tip(*button,tooltip);      \
147         icon->set_padding(0,0);\
148         icon->show();   \
149         /*button->set_relief(Gtk::RELIEF_NONE);*/ \
150         button->show()
151 #endif
152
153 #define NEW_SMALL_BUTTON(x,y,z) Gtk::Button *SMALL_BUTTON(x,y,z)
154
155 #define NOT_IMPLEMENTED_SLOT sigc::mem_fun(*reinterpret_cast<studio::CanvasViewUIInterface*>(get_ui_interface().get()),&studio::CanvasViewUIInterface::not_implemented)
156
157 #define SLOT_EVENT(x)   sigc::hide_return(sigc::bind(sigc::mem_fun(*this,&studio::CanvasView::process_event_key),x))
158         
159 /* === C L A S S E S ======================================================= */
160
161         
162 class studio::UniversalScrubber
163 {
164         CanvasView *canvas_view;
165
166         bool            scrubbing;
167         etl::clock      scrub_timer;
168         
169         sigc::connection end_scrub_connection;
170 public:
171         UniversalScrubber(CanvasView *canvas_view):
172                 canvas_view(canvas_view),
173                 scrubbing(false)
174         {
175                 canvas_view->canvas_interface()->signal_time_changed().connect(
176                         sigc::mem_fun(*this,&studio::UniversalScrubber::on_time_changed)
177                 );
178         }
179         
180         ~UniversalScrubber()
181         {
182                 end_scrub_connection.disconnect();
183         }
184         
185         void on_time_changed()
186         {
187                 // Make sure we are changing the time quickly
188                 // before we enable scrubbing
189                 if(!scrubbing && scrub_timer()>1)
190                 {
191                         scrub_timer.reset();
192                         return;
193                 }
194                 
195                 // If we aren't scrubbing already, enable it
196                 if(!scrubbing)
197                 {
198                         scrubbing=true;                 
199                         audio_container()->start_scrubbing(canvas_view->get_time());
200                 }
201                 
202                 // Reset the scrubber ender
203                 end_scrub_connection.disconnect();
204                 end_scrub_connection=Glib::signal_timeout().connect(
205                         sigc::bind_return(
206                                 sigc::mem_fun(*this,&UniversalScrubber::end_of_scrubbing),
207                                 false
208                         ),
209                         1000
210                 );
211
212                 // Scrub!
213                 audio_container()->scrub(canvas_view->get_time());
214
215                 scrub_timer.reset();
216         }
217         
218         void end_of_scrubbing()
219         {               
220                 scrubbing=false;
221                 audio_container()->stop_scrubbing();
222                 scrub_timer.reset();
223         }
224         
225         handle<AudioContainer> audio_container()
226         {
227                 assert(canvas_view->audio);
228                 return canvas_view->audio;
229         }
230 };
231
232         
233 class studio::CanvasViewUIInterface : public synfigapp::UIInterface
234 {
235         CanvasView *view;
236
237 public:
238
239         CanvasViewUIInterface(CanvasView *view):
240                 view(view)
241         {
242         
243                 view->statusbar->push("Idle");
244         }
245
246         ~CanvasViewUIInterface()
247         {
248                 //view->statusbar->pop();
249                 //view->progressbar->set_fraction(0);
250         }
251
252         virtual Response yes_no(const std::string &title, const std::string &message,Response dflt=RESPONSE_YES)
253         {
254                 view->present();
255                 //while(studio::App::events_pending())studio::App::iteration(false);
256                 Gtk::Dialog dialog(
257                         title,          // Title
258                         *view,          // Parent
259                         true,           // Modal
260                         true            // use_separator
261                 );
262                 Gtk::Label label(message);
263                 label.show();
264                 
265                 dialog.get_vbox()->pack_start(label);
266                 dialog.add_button(Gtk::StockID("gtk-yes"),RESPONSE_YES);
267                 dialog.add_button(Gtk::StockID("gtk-no"),RESPONSE_NO);
268                 
269                 dialog.set_default_response(dflt);
270                 dialog.show();
271                 return (Response)dialog.run();
272         }
273         virtual Response yes_no_cancel(const std::string &title, const std::string &message,Response dflt=RESPONSE_YES)
274         {
275                 view->present();
276                 //while(studio::App::events_pending())studio::App::iteration(false);
277                 Gtk::Dialog dialog(
278                         title,          // Title
279                         *view,          // Parent
280                         true,           // Modal
281                         true            // use_separator
282                 );
283                 Gtk::Label label(message);
284                 label.show();
285                 
286                 dialog.get_vbox()->pack_start(label);
287                 dialog.add_button(Gtk::StockID("gtk-yes"),RESPONSE_YES);
288                 dialog.add_button(Gtk::StockID("gtk-no"),RESPONSE_NO);
289                 dialog.add_button(Gtk::StockID("gtk-cancel"),RESPONSE_CANCEL);
290                 
291                 dialog.set_default_response(dflt);
292                 dialog.show();
293                 return (Response)dialog.run();
294         }
295         virtual Response ok_cancel(const std::string &title, const std::string &message,Response dflt=RESPONSE_OK)
296         {
297                 view->present();
298                 //while(studio::App::events_pending())studio::App::iteration(false);
299                 Gtk::Dialog dialog(
300                         title,          // Title
301                         *view,          // Parent
302                         true,           // Modal
303                         true            // use_separator
304                 );
305                 Gtk::Label label(message);
306                 label.show();
307                 
308                 dialog.get_vbox()->pack_start(label);
309                 dialog.add_button(Gtk::StockID("gtk-ok"),RESPONSE_OK);
310                 dialog.add_button(Gtk::StockID("gtk-cancel"),RESPONSE_CANCEL);
311                 
312                 dialog.set_default_response(dflt);
313                 dialog.show();
314                 return (Response)dialog.run();
315         }
316
317         virtual bool
318         task(const std::string &task)
319         {
320                 if(!view->is_playing_)
321                 {       
322                         view->statusbar->pop();
323                         view->statusbar->push(task);
324                 }
325                 //while(studio::App::events_pending())studio::App::iteration(false);
326                 if(view->cancel){return false;}
327                 return true;
328         }
329
330         virtual bool
331         error(const std::string &err)
332         {
333                 view->statusbar->push("ERROR");
334                 
335                 // If we are in the process of canceling,
336                 // then just go ahead and return false --
337                 // don't bother displaying a dialog
338                 if(view->cancel)return false;
339                 
340                 Gtk::Dialog dialog(
341                         "Error",                // Title
342                         *view,          // Parent
343                         true,           // Modal
344                         true            // use_separator
345                 );
346                 Gtk::Label label(err);
347                 label.show();
348                 
349                 dialog.get_vbox()->pack_start(label);
350                 dialog.add_button(Gtk::StockID("gtk-ok"),RESPONSE_OK);
351                 dialog.show();
352                 dialog.run();
353                 view->statusbar->pop();
354                 return true;
355         }
356
357         virtual bool
358         warning(const std::string &err)
359         {
360                 view->statusbar->pop();
361                 view->statusbar->push(err);
362                 
363                 //while(studio::App::events_pending())studio::App::iteration(false);
364                 if(view->cancel)return false;
365                 return true;
366         }
367
368         virtual bool
369         amount_complete(int current, int total)
370         {
371                 if(!view->is_playing_)
372                 {       
373                         if(!view->working_depth)
374                         {
375                                 if(current)
376                                         view->stopbutton->set_sensitive(true);
377                                 else
378                                         view->stopbutton->set_sensitive(false);
379                         }
380                         float x((float)current/(float)total);
381                         if(x<0)x=0;
382                         else if(x>1)x=1;
383                         view->progressbar->set_fraction(x);
384                 }
385                 //while(studio::App::events_pending())studio::App::iteration(false);
386                 if(view->cancel){/*view->cancel=false;*/return false;}
387                 return true;
388         }
389         
390         void
391         not_implemented()
392         {
393                 error("Feature not yet implemented");
394         }
395 };
396
397 class studio::CanvasViewSelectionManager : public synfigapp::SelectionManager
398 {
399         CanvasView *view;
400         CanvasView::LayerTreeModel layer_tree_model;
401         CanvasView::ChildrenTreeModel children_tree_model;
402 public:
403
404         CanvasViewSelectionManager(CanvasView *view): view(view)
405 {       
406         
407  }
408                 
409
410 private:
411         void _set_selected_layer(const synfig::Layer::Handle &layer)
412         {
413                 view->layer_tree->select_layer(layer);
414 /*
415                 // Don't change the selection while we are busy
416                 // I cannot remember exactly why I put this here...
417                 // It musta been for some reason, but I cannot recall.
418                 //if(App::Busy::count)
419                 //      return;
420                 
421                 if(view->layer_tree->get_selection()->get_selected())
422                 {
423                         const Gtk::TreeRow row = *(view->layer_tree->get_selection()->get_selected());
424
425                         // Don't do anything if that layer is already selected
426                         if(layer == static_cast<synfig::Layer::Handle>(row[layer_tree_model.layer]))
427                                 return;
428                 }
429                 Gtk::TreeModel::Children::iterator iter;
430                 if(view->layer_tree_store()->find_layer_row(layer,iter))
431                 {
432                         Gtk::TreePath path(iter);
433                         for(int i=path.get_depth();i;i--)
434                         {
435                                 int j;
436                                 path=Gtk::TreePath(iter);
437                                 for(j=i;j;j--)
438                                         path.up();
439                                 view->layer_tree->get_tree_view().expand_row(path,false);
440                         }
441                         view->layer_tree->get_tree_view().scroll_to_row(Gtk::TreePath(iter));
442                         view->layer_tree->get_selection()->select(iter);
443                 }
444 */
445         }
446 public:
447         
448         //! Returns the number of layers selected.
449         virtual int get_selected_layer_count()const
450         {
451                 return get_selected_layers().size();
452         }
453
454         //! Returns a list of the currently selected layers.
455         virtual LayerList get_selected_layers()const
456         {
457 //              assert(view->layer_tree);
458                 
459                 if(!view->layer_tree) { DEBUGPOINT(); synfig::error("canvas_view.layer_tree not defined!?"); return LayerList(); }
460                 return view->layer_tree->get_selected_layers();
461         }
462         
463         //! Returns the first layer selected or an empty handle if none are selected.
464         virtual synfig::Layer::Handle get_selected_layer()const
465         {
466 //              assert(view->layer_tree);
467                 
468                 if(!view->layer_tree) { DEBUGPOINT(); synfig::error("canvas_view.layer_tree not defined!?"); return 0; }
469                 return view->layer_tree->get_selected_layer();
470         }
471         
472         //! Sets which layers should be selected
473         virtual void set_selected_layers(const LayerList &layer_list)
474         {
475 //              assert(view->layer_tree);
476                 
477                 if(!view->layer_tree) { DEBUGPOINT(); synfig::error("canvas_view.layer_tree not defined!?"); return; }
478                 view->layer_tree->select_layers(layer_list);
479                 //view->get_smach().process_event(EVENT_REFRESH_DUCKS);
480
481                 //view->queue_rebuild_ducks();
482         }
483
484         //! Sets which layer should be selected.
485         virtual void set_selected_layer(const synfig::Layer::Handle &layer)
486         {
487 //              assert(view->layer_tree);
488                 
489                 if(!view->layer_tree) { DEBUGPOINT(); synfig::error("canvas_view.layer_tree not defined!?"); return; }
490                 view->layer_tree->select_layer(layer);
491                 //view->queue_rebuild_ducks();
492         }
493
494         //! Clears the layer selection list
495         virtual void clear_selected_layers()
496         {
497                 if(!view->layer_tree) return;
498                 view->layer_tree->clear_selected_layers();
499         }
500
501
502
503         //! Returns the number of value_nodes selected.
504         virtual int get_selected_children_count()const
505         {
506                 return get_selected_children().size();
507         }
508         
509         static inline void __child_grabber(const Gtk::TreeModel::iterator& iter, ChildrenList* ret)
510         {
511                 const CanvasView::ChildrenTreeModel children_tree_model;
512                 synfigapp::ValueDesc value_desc((*iter)[children_tree_model.value_desc]);
513                 if(value_desc)
514                         ret->push_back(value_desc);
515         }
516         
517         //! Returns a list of the currently selected value_nodes.
518         virtual ChildrenList get_selected_children()const
519         {
520                 if(!view->children_tree) return ChildrenList();
521                         
522                 Glib::RefPtr<Gtk::TreeSelection> selection=view->children_tree->get_selection();
523
524                 if(!selection)
525                         return ChildrenList();
526         
527                 ChildrenList ret;
528                 
529                 selection->selected_foreach_iter(
530                         sigc::bind(
531                                 sigc::ptr_fun(
532                                         &studio::CanvasViewSelectionManager::__child_grabber
533                                 ),
534                                 &ret
535                         )
536                 );
537                 
538                 /*
539                 Gtk::TreeModel::Children::iterator iter(view->children_tree_store()->children().begin());
540                 iter++;
541                 Gtk::TreeModel::Children children = iter->children();
542                 for(iter = children.begin(); iter != children.end(); ++iter)
543                 {
544                         Gtk::TreeModel::Row row = *iter;
545                         if(selection->is_selected(row))
546                                 ret.push_back((synfigapp::ValueDesc)row[children_tree_model.value_desc]);
547                 }
548                 */
549                 return ret;
550         }
551         
552         //! Returns the first value_node selected or an empty handle if none are selected.
553         virtual ChildrenList::value_type get_selected_child()const
554         {
555                 if(!view->children_tree) return ChildrenList::value_type();
556
557                 ChildrenList children(get_selected_children());
558
559                 if(children.empty())
560                         return ChildrenList::value_type();
561
562                 return children.front();
563         }
564         
565         //! Sets which value_nodes should be selected
566         virtual void set_selected_children(const ChildrenList &children_list)
567         {
568                 return;
569         }
570
571         //! Sets which value_node should be selected. Empty handle if none.
572         virtual void set_selected_child(const ChildrenList::value_type &child)
573         {
574                 return;
575         }
576
577         //! Clears the value_node selection list
578         virtual void clear_selected_children()
579         {
580                 return;
581         }
582         
583         
584         
585         int get_selected_layer_parameter_count()const
586         {
587                 return get_selected_layer_parameters().size();
588         }
589         
590         LayerParamList get_selected_layer_parameters()const
591         {
592                 if(!view->layer_tree) return LayerParamList();
593
594                 Glib::RefPtr<Gtk::TreeSelection> selection=view->layer_tree->get_selection();
595
596                 if(!selection)
597                         return LayerParamList();
598         
599                 LayerParamList ret;
600         
601                 Gtk::TreeModel::Children children = const_cast<CanvasView*>(view)->layer_tree_store()->children();
602                 Gtk::TreeModel::Children::iterator iter;
603                 for(iter = children.begin(); iter != children.end(); ++iter)
604                 {
605                         Gtk::TreeModel::Row row = *iter;
606                         Gtk::TreeModel::Children::iterator iter;
607                         for(iter=row.children().begin();iter!=row.children().end();iter++)
608                         {
609                                 Gtk::TreeModel::Row row = *iter;
610                                 if(selection->is_selected(row))
611                                         ret.push_back(LayerParam(row[layer_tree_model.layer],(Glib::ustring)row[layer_tree_model.id]));
612                         }
613                 }
614                 return ret;
615         }
616         
617         LayerParam get_selected_layer_parameter() const
618         {
619                 if(!view->layer_tree) return LayerParam();
620                 return get_selected_layer_parameters().front();
621         }
622         
623         void set_selected_layer_parameters(const LayerParamList &layer_param_list)
624         {
625                 return;
626         }
627         
628         void set_selected_layer_param(const LayerParam &layer_param)
629         {
630                 return;
631         }
632         
633         void clear_selected_layer_parameters()
634         {
635                 return;
636         }
637
638 }; // END of class SelectionManager
639
640
641 CanvasView::IsWorking::IsWorking(CanvasView &canvas_view_):
642         canvas_view_(canvas_view_)
643 {
644         if(!canvas_view_.working_depth)
645                 canvas_view_.stopbutton->set_sensitive(true);
646         canvas_view_.working_depth++;
647         canvas_view_.cancel=false;
648 }
649
650 CanvasView::IsWorking::~IsWorking()
651 {
652         canvas_view_.working_depth--;
653         if(!canvas_view_.working_depth)
654                 canvas_view_.stopbutton->set_sensitive(false);
655 }
656
657 CanvasView::IsWorking::operator bool()const
658 {
659         if(canvas_view_.cancel)
660                 return false;
661         return true;
662 }
663
664 /* === M E T H O D S ======================================================= */
665
666 CanvasView::CanvasView(etl::loose_handle<Instance> instance,etl::handle<synfigapp::CanvasInterface> canvas_interface_):
667         smach_                                  (this),
668         instance_                               (instance),
669         canvas_interface_               (canvas_interface_),
670         //layer_tree_store_             (LayerTreeStore::create(canvas_interface_)),
671         //children_tree_store_  (ChildrenTreeStore::create(canvas_interface_)),
672         //keyframe_tree_store_  (KeyframeTreeStore::create(canvas_interface_)),
673         time_adjustment_                (0,0,25,0,0,0),
674         time_window_adjustment_ (0,0,25,0,0,0),
675         statusbar                               (manage(new class Gtk::Statusbar())),
676         
677         timeslider                              (new Widget_Timeslider),
678         
679         ui_interface_                   (new CanvasViewUIInterface(this)),
680         selection_manager_              (new CanvasViewSelectionManager(this)),
681         is_playing_                             (false),
682         
683         working_depth                   (0),
684         cancel                                  (false),
685         
686         canvas_properties               (*this,canvas_interface_),
687         canvas_options                  (this),
688         render_settings                 (*this,canvas_interface_),
689         waypoint_dialog                 (*this,canvas_interface_->get_canvas()),
690         keyframe_dialog                 (*this,canvas_interface_),
691         preview_dialog                  (new Dialog_Preview),
692         sound_dialog                    (new Dialog_SoundSelect(*this,canvas_interface_))
693 {
694         layer_tree=0;
695         children_tree=0;
696         duck_refresh_flag=true;
697         
698         smach_.set_default_state(&state_normal);
699         
700         disp_audio = new Widget_Sound();
701         
702         //synfig::info("Canvasview: Entered constructor");
703         // Minor hack
704         get_canvas()->set_time(0);
705         //layer_tree_store_->rebuild();
706                 
707         // Set up the UI and Selection managers
708         canvas_interface()->set_ui_interface(get_ui_interface());
709         canvas_interface()->set_selection_manager(get_selection_manager());
710         rebuild_ducks_queued=false;
711         
712         //notebook=manage(new class Gtk::Notebook());
713         //Gtk::VPaned *vpaned = manage(new class Gtk::VPaned());
714         //vpaned->pack1(*create_work_area(), Gtk::EXPAND|Gtk::SHRINK);
715         //vpaned->pack2(*notebook, Gtk::SHRINK);
716         //vpaned->show_all();
717
718
719         //notebook->show();
720         
721         //notebook->append_page(*create_layer_tree(),"Layers");
722         //notebook->append_page(*create_children_tree(),"Children");
723         //notebook->append_page(*create_keyframe_tree(),"Keyframes");
724         
725         //synfig::info("Canvasview: Before big chunk of allocation and tabling stuff");
726         //create all allocated stuff for this canvas
727         audio = new AudioContainer();
728         
729         Gtk::Table *layout_table= manage(new class Gtk::Table(1, 3, false));    
730         //layout_table->attach(*vpaned, 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
731         layout_table->attach(*create_work_area(), 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
732         init_menus();   
733         //layout_table->attach(*App::ui_manager()->get_widget("/menu-main"), 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
734         
735         
736         layout_table->attach(*create_time_bar(), 0, 1, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
737         layout_table->attach(*create_status_bar(), 0, 1, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
738         
739         update_title();
740
741         layout_table->show();
742         add(*layout_table);
743
744         //set_transient_for(*App::toolbox);
745
746         //synfig::info("Canvasview: Before Signals");
747         /*
748  --     ** -- Signals -------------------------------------------------------------
749         */
750
751         canvas_interface()->signal_dirty_preview().connect(sigc::mem_fun(*this,&studio::CanvasView::on_dirty_preview));
752         canvas_interface()->signal_mode_changed().connect(sigc::mem_fun(*this,&studio::CanvasView::on_mode_changed));
753
754         canvas_interface()->signal_time_changed().connect(sigc::mem_fun(*this,&studio::CanvasView::on_time_changed));
755
756         //canvas_interface()->signal_time_changed().connect(sigc::mem_fun(*this,&studio::CanvasView::refresh_tables));
757         canvas_interface()->signal_id_changed().connect(sigc::mem_fun(*this,&studio::CanvasView::on_id_changed));
758         canvas_interface()->signal_rend_desc_changed().connect(sigc::mem_fun(*this,&studio::CanvasView::refresh_rend_desc));
759         waypoint_dialog.signal_changed().connect(sigc::mem_fun(*this,&studio::CanvasView::on_waypoint_changed));
760         waypoint_dialog.signal_delete().connect(sigc::mem_fun(*this,&studio::CanvasView::on_waypoint_delete));
761         
762         //MODIFIED TIME ADJUSTMENT STUFF....
763         time_window_adjustment().set_child_adjustment(&time_adjustment());
764         time_window_adjustment().signal_value_changed().connect(sigc::mem_fun(*this,&studio::CanvasView::refresh_time_window)); 
765         time_adjustment().signal_value_changed().connect(sigc::mem_fun(*this,&studio::CanvasView::time_was_changed));
766
767
768         work_area->signal_layer_selected().connect(sigc::mem_fun(*this,&studio::CanvasView::workarea_layer_selected));
769         work_area->signal_input_device_changed().connect(sigc::mem_fun(*this,&studio::CanvasView::on_input_device_changed));
770
771         canvas_interface()->signal_canvas_added().connect(
772                 sigc::hide(
773                         sigc::mem_fun(*instance,&studio::Instance::refresh_canvas_tree)
774                 )
775         );
776         canvas_interface()->signal_canvas_removed().connect(
777                 sigc::hide(
778                         sigc::mem_fun(*instance,&studio::Instance::refresh_canvas_tree)
779                 )
780         );
781
782         canvas_interface()->signal_layer_param_changed().connect(
783                 sigc::hide(
784                         sigc::hide(
785                                 SLOT_EVENT(EVENT_REFRESH_DUCKS)
786                         )
787                 )
788         );
789         
790
791         //MUCH TIME STUFF TAKES PLACE IN HERE
792         refresh_rend_desc();
793         refresh_time_window();
794
795         /*! \todo We shouldn't need to do this at construction -- 
796         **      This should be preformed at the first time the window
797         **      becomes visible.
798         */
799         work_area->queue_render_preview();
800         
801         // If the canvas is really big, zoom out so that we can fit it all in the window
802         /*! \todo In other words, this is a zoom-to-fit, and should be 
803         ** in it's own function.
804         */
805         int w=get_canvas()->rend_desc().get_w()+70;
806         int h=get_canvas()->rend_desc().get_h()+70;
807         while(w>700 || h>600)
808         {
809                 work_area->zoom_out();
810                 w=round_to_int(get_canvas()->rend_desc().get_w()*work_area->get_zoom()+70);
811                 h=round_to_int(get_canvas()->rend_desc().get_h()*work_area->get_zoom()+70);
812         } 
813         if(w>700)w=700;
814         if(h>600)h=600; 
815         set_default_size(w,h);
816         property_window_position().set_value(Gtk::WIN_POS_NONE);
817         
818         
819         
820         
821         std::list<Gtk::TargetEntry> listTargets;
822         listTargets.push_back( Gtk::TargetEntry("STRING") );
823         listTargets.push_back( Gtk::TargetEntry("text/plain") );
824         listTargets.push_back( Gtk::TargetEntry("image") );
825
826         drag_dest_set(listTargets);
827         signal_drag_data_received().connect( sigc::mem_fun(*this, &studio::CanvasView::on_drop_drag_data_received) );
828
829
830         /*
831         Time length(get_canvas()->rend_desc().get_time_end()-get_canvas()->rend_desc().get_time_start());
832         if(length<10.0)
833         {
834                 time_window_adjustment().set_page_increment(length);
835                 time_window_adjustment().set_page_size(length);
836         }
837         else
838         {
839                 time_window_adjustment().set_page_increment(10.0);
840                 time_window_adjustment().set_page_size(10.0);
841         }
842         */
843         
844         //synfig::info("Canvasview: Before Sound Hookup");
845         //load sound info from meta data
846         {
847                 //synfig::warning("Should load Audio: %s with %s offset",apath.c_str(),aoffset.c_str());
848                 
849                 on_audio_file_notify(); //redundant setting of the metadata, but oh well, it's no big deal :)
850                 on_audio_offset_notify();
851                 
852                 //signal connection - since they are all associated with the canvas view
853                 
854                 //hook in signals for sound options box
855                 sound_dialog->signal_file_changed().connect(sigc::mem_fun(*this,&CanvasView::on_audio_file_change));
856                 sound_dialog->signal_offset_changed().connect(sigc::mem_fun(*this,&CanvasView::on_audio_offset_change));
857                 
858                 //attach to the preview when it's visible               
859                 //preview_dialog->get_widget().signal_play().connect(sigc::mem_fun(*this,&CanvasView::play_audio));
860                 //preview_dialog->get_widget().signal_stop().connect(sigc::mem_fun(*this,&CanvasView::stop_audio));
861                 
862                 //hook to metadata signals
863                 get_canvas()->signal_meta_data_changed("audiofile").connect(sigc::mem_fun(*this,&CanvasView::on_audio_file_notify));
864                 get_canvas()->signal_meta_data_changed("audiooffset").connect(sigc::mem_fun(*this,&CanvasView::on_audio_offset_notify));
865
866                 //universal_scrubber=std::auto_ptr<UniversalScrubber>(new UniversalScrubber(this));
867         }
868         
869         //synfig::info("Canvasview: Before Final time set up");
870         //MORE TIME STUFF
871         time_window_adjustment().set_value(get_canvas()->rend_desc().get_time_start());
872         time_window_adjustment().value_changed();
873
874         
875         GRAB_HINT_DATA("canvas_view");
876         /*
877         {
878         set_skip_taskbar_hint(true);
879         set_skip_pager_hint(true);
880         set_type_hint(Gdk::WINDOW_TYPE_HINT_UTILITY);
881         }
882         */
883         
884         refresh_rend_desc();    
885         hide_tables();
886
887         on_time_changed();      
888         //synfig::info("Canvasview: Constructor Done"); 
889 }
890
891 CanvasView::~CanvasView()
892 {
893         signal_deleted()();
894
895         App::ui_manager()->remove_action_group(action_group);
896
897         // Shut down the smach
898         smach_.egress();
899         smach_.set_default_state(0);
900
901         // We want to ensure that the UI_Manager and
902         // the selection manager get destructed right now.
903         ui_interface_.reset();
904         selection_manager_.reset();
905
906         // Delete any external widgets
907         for(;!ext_widget_book_.empty();ext_widget_book_.erase(ext_widget_book_.begin()))
908         {
909                 if(ext_widget_book_.begin()->second)
910                         delete ext_widget_book_.begin()->second;
911         }
912         
913         //delete preview
914         audio.reset();
915                 
916         hide();
917         
918         synfig::info("CanvasView:~CanvasView(): Destructor Finished");
919 }
920
921
922
923 Gtk::Widget *
924 CanvasView::create_time_bar()
925 {
926         Gtk::Image *icon;
927
928         Gtk::HScrollbar *time_window_scroll = manage(new class Gtk::HScrollbar(time_window_adjustment()));
929         //Gtk::HScrollbar *time_scroll = manage(new class Gtk::HScrollbar(time_adjustment()));
930         //TIME BAR TEMPORARY POSITION
931         //Widget_Timeslider *time_scroll = manage(new Widget_Timeslider);
932         timeslider->show();
933         timeslider->set_time_adjustment(&time_adjustment());
934         timeslider->set_bounds_adjustment(&time_window_adjustment());
935         //layout_table->attach(*timeslider, 0, 1, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK|Gtk::FILL);
936         
937         
938         tooltips.set_tip(*time_window_scroll,_("Moves the time window"));
939         tooltips.set_tip(*timeslider,_("Changes the current time"));
940         time_window_scroll->show();
941         timeslider->show();
942         time_window_scroll->set_flags(Gtk::CAN_FOCUS);
943         timeslider->set_flags(Gtk::CAN_FOCUS);
944
945         //time_scroll->signal_value_changed().connect(sigc::mem_fun(*work_area, &studio::WorkArea::render_preview_hook));
946         //time_scroll->set_update_policy(Gtk::UPDATE_DISCONTINUOUS);
947         
948         NORMAL_BUTTON(animatebutton,"gtk-yes","Animate");
949         animatebutton->signal_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_animate_button_pressed));
950         animatebutton->show();
951
952         NORMAL_BUTTON(keyframebutton,"synfig-keyframe_lock_all","All Keyframes Locked");
953         keyframebutton->signal_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_keyframe_button_pressed));
954         keyframebutton->show();
955         
956         Gtk::Table *table= manage(new class Gtk::Table(2, 3, false));
957
958         //setup the audio display
959         disp_audio->set_size_request(-1,32); //disp_audio.show(); 
960         disp_audio->set_time_adjustment(&time_adjustment());
961         disp_audio->signal_start_scrubbing().connect(
962                 sigc::mem_fun(*audio,&AudioContainer::start_scrubbing)
963         );
964         disp_audio->signal_scrub().connect(
965                 sigc::mem_fun(*audio,&AudioContainer::scrub)
966         );
967         disp_audio->signal_stop_scrubbing().connect(
968                 sigc::mem_fun(*audio,&AudioContainer::stop_scrubbing)
969         );
970         
971         table->attach(*manage(disp_audio), 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK);
972         table->attach(*timeslider, 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK, 0, 0);
973         table->attach(*time_window_scroll, 0, 1, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK, 0, 0);
974
975         table->attach(*animatebutton, 1, 2, 0, 3, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
976         table->attach(*keyframebutton, 2, 3, 0, 3, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
977         timebar=table;
978         table->show();
979         return table;
980 }
981
982 Gtk::Widget *
983 CanvasView::create_work_area()
984 {
985         work_area=std::auto_ptr<WorkArea>(new class studio::WorkArea(canvas_interface_));
986         work_area->set_instance(get_instance());
987         work_area->set_canvas(get_canvas());
988         work_area->set_canvas_view(this);
989         work_area->set_progress_callback(get_ui_interface().get());
990         work_area->signal_popup_menu().connect(sigc::mem_fun(*this, &studio::CanvasView::popup_main_menu));
991         work_area->show();
992         return work_area.get();
993 }
994
995
996 Gtk::Widget*
997 CanvasView::create_status_bar()
998 {
999         Gtk::Image *icon;
1000         Gtk::IconSize iconsize=Gtk::IconSize::from_name("synfig-small_icon");
1001         cancel=false;
1002
1003         // Create the status bar at the bottom of the window
1004         Gtk::Table *statusbartable= manage(new class Gtk::Table(7, 1, false));
1005 //      statusbar = manage(new class Gtk::Statusbar()); // This is already done at construction
1006         progressbar =manage(new class Gtk::ProgressBar());
1007         SMALL_BUTTON(stopbutton,"gtk-stop","Stop");
1008         SMALL_BUTTON(refreshbutton,"gtk-refresh","Refresh");
1009         //SMALL_BUTTON(treetogglebutton,"gtk-go-down","Toggle Layer Tree");
1010 //      NEW_SMALL_BUTTON(raisebutton,"gtk-go-up","Raise Layer");
1011 //      NEW_SMALL_BUTTON(lowerbutton,"gtk-go-down","Lower Layer");
1012         //statusbartable->attach(*treetogglebutton, 0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
1013 //      statusbartable->attach(*lowerbutton, 0, 1, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
1014 //      statusbartable->attach(*raisebutton, 1, 2, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
1015
1016
1017         current_time_widget=manage(new Widget_Time);
1018         current_time_widget->set_value(get_time());
1019         current_time_widget->set_fps(get_canvas()->rend_desc().get_frame_rate());
1020         current_time_widget->signal_value_changed().connect(
1021                 sigc::mem_fun(*this,&CanvasView::on_current_time_widget_changed)
1022         );
1023
1024         statusbartable->attach(*current_time_widget, 0, 1, 0, 1, Gtk::SHRINK|Gtk::FILL, Gtk::SHRINK|Gtk::FILL, 0, 0);
1025         statusbartable->attach(*statusbar, 3, 4, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
1026         statusbartable->attach(*progressbar, 4, 5, 0, 1, Gtk::SHRINK, Gtk::EXPAND|Gtk::FILL, 0, 0);
1027         statusbartable->attach(*refreshbutton, 5, 6, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
1028         statusbartable->attach(*stopbutton, 6, 7, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
1029         statusbar->set_has_resize_grip(false);
1030         statusbar->show();
1031         stopbutton->show();
1032         refreshbutton->show();
1033         progressbar->show();
1034         stopbutton->set_sensitive(false);
1035
1036         //refreshbutton->signal_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_refresh_pressed));
1037         //stopbutton->signal_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::stop));
1038         //treetogglebutton->signal_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::toggle_tables));
1039
1040         refreshbutton->signal_clicked().connect(SLOT_EVENT(EVENT_REFRESH));
1041         stopbutton->signal_clicked().connect(SLOT_EVENT(EVENT_STOP));
1042
1043         
1044         statusbartable->show_all();
1045         return statusbartable;
1046 }
1047
1048 void
1049 CanvasView::on_current_time_widget_changed()
1050 {
1051         set_time(current_time_widget->get_value());
1052 }
1053
1054 Gtk::Widget*
1055 CanvasView::create_children_tree()
1056 {
1057         // Create the layer tree
1058         children_tree=manage(new class ChildrenTree());
1059
1060         // Set up the layer tree
1061         //children_tree->set_model(children_tree_store());
1062         if(children_tree)children_tree->set_time_adjustment(time_adjustment());
1063         if(children_tree)children_tree->show();
1064
1065         // Connect Signals
1066         if(children_tree)children_tree->signal_edited_value().connect(sigc::mem_fun(*this, &studio::CanvasView::on_edited_value));
1067         if(children_tree)children_tree->signal_user_click().connect(sigc::mem_fun(*this, &studio::CanvasView::on_children_user_click));
1068         if(children_tree)children_tree->signal_waypoint_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_waypoint_clicked));
1069         if(children_tree)children_tree->get_selection()->signal_changed().connect(SLOT_EVENT(EVENT_REFRESH_DUCKS));
1070
1071         return children_tree;   
1072 }
1073
1074 Gtk::Widget*
1075 CanvasView::create_keyframe_tree()
1076 {
1077         keyframe_tree=manage(new KeyframeTree());
1078
1079         //keyframe_tree->get_selection()->set_mode(Gtk::SELECTION_MULTIPLE);
1080         //keyframe_tree->show();
1081         //keyframe_tree->set_model(keyframe_tree_store());
1082         keyframe_tree->set_editable(true);
1083         //keyframe_tree->signal_edited().connect(sigc::hide_return(sigc::mem_fun(*canvas_interface(), &synfigapp::CanvasInterface::update_keyframe)));
1084
1085         keyframe_tree->signal_event().connect(sigc::mem_fun(*this, &studio::CanvasView::on_keyframe_tree_event));
1086         
1087         Gtk::ScrolledWindow *scroll_layer_tree = manage(new class Gtk::ScrolledWindow());
1088         scroll_layer_tree->set_flags(Gtk::CAN_FOCUS);
1089         scroll_layer_tree->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
1090         scroll_layer_tree->add(*keyframe_tree);
1091         scroll_layer_tree->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
1092         //scroll_layer_tree->show();
1093
1094         
1095         Gtk::Table *layout_table= manage(new Gtk::Table(1, 2, false));  
1096         layout_table->attach(*scroll_layer_tree, 0, 1, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
1097         
1098         Gtk::Image *icon;
1099         Gtk::IconSize iconsize(Gtk::IconSize::from_name("synfig-small_icon"));
1100
1101         NEW_SMALL_BUTTON(button_add,"gtk-add","New Keyframe");
1102         NEW_SMALL_BUTTON(button_duplicate,"synfig-duplicate","Duplicate Keyframe");
1103         NEW_SMALL_BUTTON(button_delete,"gtk-delete","Delete Keyframe");
1104
1105         Gtk::HBox *hbox(manage(new Gtk::HBox()));
1106         layout_table->attach(*hbox, 0, 1, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::SHRINK, 0, 0);
1107         
1108         hbox->pack_start(*button_add,Gtk::PACK_SHRINK);
1109         hbox->pack_start(*button_duplicate,Gtk::PACK_SHRINK);
1110         hbox->pack_start(*button_delete,Gtk::PACK_SHRINK);
1111         
1112         /*
1113         button_raise->set_relief(Gtk::RELIEF_HALF);
1114         button_lower->set_relief(Gtk::RELIEF_HALF);
1115         button_duplicate->set_relief(Gtk::RELIEF_HALF);
1116         button_delete->set_relief(Gtk::RELIEF_HALF);
1117         */
1118         
1119         button_add->signal_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_keyframe_add_pressed));
1120         button_duplicate->signal_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_keyframe_duplicate_pressed));
1121         button_delete->signal_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_keyframe_remove_pressed));
1122
1123         //layout_table->show_all();
1124
1125         keyframe_tab_child=layout_table;
1126         
1127         
1128         layout_table->hide();
1129         
1130         return layout_table;    
1131 }
1132
1133 Gtk::Widget*
1134 CanvasView::create_layer_tree()
1135 {
1136         // Create the layer tree
1137         layer_tree=manage(new class LayerTree());
1138
1139         // Set up the layer tree
1140         //layer_tree->set_model(layer_tree_store());
1141         layer_tree->set_time_adjustment(time_adjustment());
1142         layer_tree->show();
1143
1144         // Connect Signals
1145         layer_tree->signal_layer_toggle().connect(sigc::mem_fun(*this, &studio::CanvasView::on_layer_toggle));
1146         layer_tree->signal_edited_value().connect(sigc::mem_fun(*this, &studio::CanvasView::on_edited_value));
1147         layer_tree->signal_layer_user_click().connect(sigc::mem_fun(*this, &studio::CanvasView::on_layer_user_click));
1148         layer_tree->signal_param_user_click().connect(sigc::mem_fun(*this, &studio::CanvasView::on_children_user_click));
1149         layer_tree->signal_waypoint_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_waypoint_clicked));
1150         layer_tree->get_selection()->signal_changed().connect(SLOT_EVENT(EVENT_REFRESH_DUCKS));
1151
1152         layer_tree->hide();
1153         return layer_tree;      
1154 }
1155
1156 void
1157 CanvasView::init_menus()
1158 {
1159 /*
1160         mainmenu.set_accel_group(get_accel_group());
1161         mainmenu.set_accel_path("<Canvas-view>");
1162
1163         filemenu.set_accel_group(get_accel_group());
1164         filemenu.set_accel_path("<Canvas-view>/File");
1165
1166         editmenu.set_accel_group(get_accel_group());
1167         editmenu.set_accel_path("<Canvas-view>/Edit");
1168
1169         layermenu.set_accel_group(get_accel_group());
1170         layermenu.set_accel_path("<Canvas-view>/Layer");
1171 */
1172         //cache the position of desired widgets
1173         
1174         /*Menus to worry about:
1175         - filemenu
1176         - editmenu
1177         - layermenu
1178         - duckmaskmenu
1179         - mainmenu
1180         - canvasmenu
1181         - viewmenu
1182         */
1183         action_group = Gtk::ActionGroup::create();
1184         
1185         //action_group->add( Gtk::Action::create("MenuFile", "_File") );
1186         action_group->add( Gtk::Action::create("save", Gtk::Stock::SAVE),
1187                 hide_return(sigc::mem_fun(*get_instance().get(), &studio::Instance::save))
1188         );
1189         action_group->add( Gtk::Action::create("save-as", Gtk::Stock::SAVE_AS),
1190                 sigc::hide_return(sigc::mem_fun(*get_instance().get(), &studio::Instance::dialog_save_as))
1191         );
1192         action_group->add( Gtk::Action::create("revert", Gtk::Stock::REVERT_TO_SAVED),
1193                 sigc::hide_return(sigc::mem_fun(*get_instance().get(), &studio::Instance::safe_revert))
1194         );
1195         action_group->add( Gtk::Action::create("cvs-add", Gtk::StockID("synfig-cvs_add")),
1196                 sigc::hide_return(sigc::mem_fun(*get_instance(), &studio::Instance::dialog_cvs_add))
1197         );
1198         action_group->add( Gtk::Action::create("cvs-update", Gtk::StockID("synfig-cvs_update")),
1199                 sigc::hide_return(sigc::mem_fun(*get_instance(), &studio::Instance::dialog_cvs_update))
1200         );
1201         action_group->add( Gtk::Action::create("cvs-revert", Gtk::StockID("synfig-cvs_revert")),
1202                 sigc::hide_return(sigc::mem_fun(*get_instance(), &studio::Instance::dialog_cvs_revert))
1203         );
1204         action_group->add( Gtk::Action::create("cvs-commit", Gtk::StockID("synfig-cvs_commit")),
1205                 sigc::hide_return(sigc::mem_fun(*get_instance(), &studio::Instance::dialog_cvs_commit))
1206         );
1207         action_group->add( Gtk::Action::create("import", _("Import")),
1208                 sigc::hide_return(sigc::mem_fun(*this, &studio::CanvasView::image_import))
1209         );
1210         action_group->add( Gtk::Action::create("render", _("Render")),
1211                 sigc::mem_fun0(render_settings,&studio::RenderSettings::present)
1212         );
1213         action_group->add( Gtk::Action::create("preview", _("Preview")),
1214                 sigc::mem_fun(*this,&CanvasView::on_preview_option)
1215         );
1216         action_group->add( Gtk::Action::create("sound", _("Sound File")),
1217                 sigc::mem_fun(*this,&CanvasView::on_audio_option)
1218         );
1219         action_group->add( Gtk::Action::create("options", _("Options")),
1220                 sigc::mem_fun0(canvas_options,&studio::CanvasOptions::present)
1221         );
1222         action_group->add( Gtk::Action::create("close", Gtk::StockID("gtk-close")),
1223                 sigc::hide_return(sigc::mem_fun(*this,&studio::CanvasView::close))      
1224         );
1225
1226         //action_group->add( Gtk::Action::create("undo", Gtk::StockID("gtk-undo")),
1227         //      SLOT_EVENT(EVENT_UNDO)
1228         //);
1229
1230         //action_group->add( Gtk::Action::create("redo", Gtk::StockID("gtk-redo")),
1231         //      SLOT_EVENT(EVENT_REDO)
1232         //);
1233
1234         action_group->add( Gtk::Action::create("select-all-ducks", _("Select All Ducks")),
1235                 sigc::mem_fun(*work_area,&studio::WorkArea::select_all_ducks)
1236         );
1237
1238         action_group->add( Gtk::Action::create("unselect-all-layers", _("Unselect All Layers")),
1239                 sigc::mem_fun(*this,&CanvasView::on_unselect_layers)
1240         );
1241
1242         action_group->add( Gtk::Action::create("stop", Gtk::StockID("gtk-stop")),
1243                 SLOT_EVENT(EVENT_STOP)
1244         );
1245         
1246         action_group->add( Gtk::Action::create("refresh", Gtk::StockID("gtk-refresh")),
1247                 SLOT_EVENT(EVENT_REFRESH)
1248         );
1249
1250         action_group->add( Gtk::Action::create("properties", Gtk::StockID("gtk-properties")),
1251                 sigc::mem_fun0(canvas_properties,&studio::CanvasProperties::present)
1252         );
1253
1254         // Preview Quality Menu
1255         {
1256                 int i;
1257                 action_group->add( Gtk::RadioAction::create(quality_group,"quality-00", _("Use Parametric Renderer")),
1258                         sigc::bind(
1259                                 sigc::mem_fun(*work_area, &studio::WorkArea::set_quality),
1260                                 0
1261                         )
1262                 );
1263                 for(i=1;i<=10;i++)
1264                 {
1265                         Glib::RefPtr<Gtk::RadioAction> action(Gtk::RadioAction::create(quality_group,strprintf("quality-%02d",i), strprintf("Set Quality to %d",i)));
1266                         if(i==10)
1267                                 action->property_value()=10;
1268                         action_group->add( action,
1269                                 sigc::bind(
1270                                         sigc::mem_fun(*work_area, &studio::WorkArea::set_quality),
1271                                         i
1272                                 )
1273                         );
1274                 }
1275         }
1276
1277         action_group->add( Gtk::Action::create("play", Gtk::StockID("synfig-play")),
1278                 sigc::mem_fun(*this, &studio::CanvasView::play)
1279         );
1280         
1281         action_group->add( Gtk::Action::create("dialog-flipbook", _("Flipbook Dialog")),
1282                 sigc::mem_fun0(*preview_dialog, &studio::Dialog_Preview::present)
1283         );
1284
1285         action_group->add( Gtk::Action::create("toggle-grid-show", _("Toggle Grid Show")),
1286                 sigc::mem_fun(*work_area, &studio::WorkArea::toggle_grid)
1287         );
1288         action_group->add( Gtk::Action::create("toggle-grid-snap", _("Toggle Grid Snap")),
1289                 sigc::mem_fun(*work_area, &studio::WorkArea::toggle_grid_snap)
1290         );
1291         action_group->add( Gtk::Action::create("toggle-guide-show", _("Toggle Guide Show")),
1292                 sigc::mem_fun(*work_area, &studio::WorkArea::toggle_guide_snap)
1293         );
1294         action_group->add( Gtk::Action::create("toggle-low-res", _("Toggle Low-Res")),
1295                 sigc::mem_fun(*work_area, &studio::WorkArea::toggle_low_resolution_flag)
1296         );
1297         action_group->add( Gtk::Action::create("toggle-onion-skin", _("Toggle Onion Skin")),
1298                 sigc::mem_fun(*work_area, &studio::WorkArea::toggle_onion_skin)
1299         );
1300
1301
1302         action_group->add( Gtk::Action::create("canvas-zoom-fit", Gtk::StockID("gtk-zoom-fit")),
1303                 sigc::mem_fun(*work_area, &studio::WorkArea::zoom_fit)
1304         );
1305         action_group->add( Gtk::Action::create("canvas-zoom-100", Gtk::StockID("gtk-zoom-100")),
1306                 sigc::mem_fun(*work_area, &studio::WorkArea::zoom_norm)
1307         );
1308
1309
1310         {
1311                 Glib::RefPtr<Gtk::Action> action;
1312                 
1313                 action=Gtk::Action::create("seek-next-frame", Gtk::Stock::GO_FORWARD,_("Next Frame"),_("Next Frame"));
1314                 action_group->add(action,sigc::bind(sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::seek_frame),1));
1315                 action=Gtk::Action::create("seek-prev-frame", Gtk::Stock::GO_BACK,_("Prev Frame"),_("Prev Frame"));
1316                 action_group->add( action, sigc::bind(sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::seek_frame),-1));
1317
1318                 action=Gtk::Action::create("seek-next-second", Gtk::Stock::GO_FORWARD,_("Seek Foward"),_("Seek Foward"));
1319                 action_group->add(action,sigc::bind(sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::seek_time),Time(1)));
1320                 action=Gtk::Action::create("seek-prev-second", Gtk::Stock::GO_BACK,_("Seek Backward"),_("Seek Backward"));
1321                 action_group->add( action, sigc::bind(sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::seek_time),Time(-1)));
1322
1323                 // Broken...!?
1324                 /*
1325                 action=Gtk::Action::create("seek-end", Gtk::Stock::GOTO_LAST,_("Seek to End"),_("Seek to End"));
1326                 action_group->add(action,sigc::bind(sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::seek_time),Time::end()));
1327                 */
1328                 action=Gtk::Action::create("seek-begin", Gtk::Stock::GOTO_FIRST,_("Seek to Begin"),_("Seek to Begin"));
1329                 action_group->add( action, sigc::bind(sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::seek_time),Time::begin()));
1330
1331                 action=Gtk::Action::create("jump-next-keyframe", Gtk::Stock::GO_FORWARD,_("Jump to Next Keyframe"),_("Jump to Next Keyframe"));
1332                 action_group->add( action,sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::jump_to_next_keyframe));
1333
1334                 action=Gtk::Action::create("jump-prev-keyframe", Gtk::Stock::GO_BACK,_("Jump to Prev Keyframe"),_("Jump to Prev Keyframe"));
1335                 action_group->add( action,sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::jump_to_prev_keyframe));
1336
1337                 action=Gtk::Action::create("canvas-zoom-in", Gtk::Stock::ZOOM_IN);
1338                 action_group->add( action,sigc::mem_fun(*work_area, &studio::WorkArea::zoom_in));
1339                 
1340                 action=Gtk::Action::create("canvas-zoom-out", Gtk::Stock::ZOOM_OUT);
1341                 action_group->add( action, sigc::mem_fun(*work_area, &studio::WorkArea::zoom_out) );
1342                 
1343                 action=Gtk::Action::create("time-zoom-in", Gtk::Stock::ZOOM_IN, _("Zoom In on Timeline"));
1344                 action_group->add( action, sigc::mem_fun(*this, &studio::CanvasView::time_zoom_in) );
1345                 
1346                 action=Gtk::Action::create("time-zoom-out", Gtk::Stock::ZOOM_OUT, _("Zoom Out on Timeline"));
1347                 action_group->add( action, sigc::mem_fun(*this, &studio::CanvasView::time_zoom_out) );
1348                 
1349         }
1350
1351
1352 #define DUCK_MASK(lower,upper)  \
1353         duck_mask_##lower=Gtk::ToggleAction::create("mask-" #lower "-ducks", _("Show "#lower" ducks")); \
1354         duck_mask_##lower->set_active((bool)(work_area->get_type_mask()&Duck::TYPE_##upper)); \
1355         action_group->add( duck_mask_##lower, \
1356                 sigc::bind( \
1357                         sigc::mem_fun(*this, &studio::CanvasView::toggle_duck_mask), \
1358                         Duck::TYPE_##upper \
1359                 ) \
1360         ) 
1361         DUCK_MASK(position,POSITION);
1362         DUCK_MASK(tangent,TANGENT);
1363         DUCK_MASK(vertex,VERTEX);
1364         DUCK_MASK(radius,RADIUS);
1365         DUCK_MASK(width,WIDTH);
1366         DUCK_MASK(angle,ANGLE);
1367 #undef DUCK_MASK
1368
1369         add_accel_group(App::ui_manager()->get_accel_group());
1370
1371 /*      // Here is where we add the actions that may have conflicting
1372         // keyboard accelerators.
1373         {
1374                 Glib::RefPtr<Gtk::ActionGroup> accel_action_group(Gtk::ActionGroup::create("canvas_view"));
1375                 Glib::RefPtr<Gtk::Action> action;
1376                 
1377                 action=Gtk::Action::create("seek-next-frame", Gtk::StockID("gtk-forward"),_("Next Frame"),_("Next Frame"));
1378                 accel_action_group->add(action,sigc::bind(sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::seek),1));
1379
1380                 action=Gtk::Action::create("seek-prev-frame", Gtk::StockID("gtk-forward"),_("Prev Frame"),_("Prev Frame"));
1381                 accel_action_group->add( action, sigc::bind(sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::seek),-1));
1382
1383                 action=Gtk::Action::create("jump-next-keyframe", Gtk::StockID("gtk-forward"),_("Jump to Next Keyframe"),_("Jump to Next Keyframe"));
1384                 accel_action_group->add( action,sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::jump_to_next_keyframe));
1385
1386                 action=Gtk::Action::create("jump-prev-keyframe", Gtk::StockID("gtk-back"),_("Jump to Prev Keyframe"),_("Jump to Prev Keyframe"));
1387                 accel_action_group->add( action,sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::jump_to_prev_keyframe));
1388
1389                 action=Gtk::Action::create("canvas-zoom-in", Gtk::StockID("gtk-zoom-in"));
1390                 accel_action_group->add( action,sigc::mem_fun(*work_area, &studio::WorkArea::zoom_in));
1391                 
1392                 action=Gtk::Action::create("canvas-zoom-out", Gtk::StockID("gtk-zoom-out"));
1393                 accel_action_group->add( action, sigc::mem_fun(*work_area, &studio::WorkArea::zoom_out) );
1394                 
1395                 action=Gtk::Action::create("time-zoom-in", Gtk::StockID("gtk-zoom-in"), _("Zoom In on Timeline"));
1396                 accel_action_group->add( action, sigc::mem_fun(*this, &studio::CanvasView::time_zoom_in) );
1397                 
1398                 action=Gtk::Action::create("time-zoom-out", Gtk::StockID("gtk-zoom-out"), _("Zoom Out on Timeline"));
1399                 accel_action_group->add( action, sigc::mem_fun(*this, &studio::CanvasView::time_zoom_out) );
1400                 
1401                 Glib::RefPtr<Gtk::UIManager> accel_ui_manager(Gtk::UIManager::create());
1402
1403                 Glib::ustring ui_info =
1404                 "
1405                 <ui>
1406                         <accelerator action='seek-next-frame' />
1407                         <accelerator action='seek-prev-frame' />
1408                         <accelerator action='jump-next-keyframe' />
1409                         <accelerator action='jump-prev-keyframe' />
1410                         <accelerator action='canvas-zoom-in' />
1411                         <accelerator action='canvas-zoom-out' />
1412                         <accelerator action='time-zoom-in' />
1413                         <accelerator action='time-zoom-out' />
1414                 </ui>
1415                 ";
1416
1417                 accel_ui_manager->add_ui_from_string(ui_info);
1418                 add_accel_group(accel_ui_manager->get_accel_group());
1419         
1420                 accel_ui_manager->insert_action_group(accel_action_group);
1421                 set_ref_obj("accel_ui_manager",accel_ui_manager);
1422                 set_ref_obj("accel_action_group",accel_action_group);
1423         }
1424 */
1425
1426
1427
1428 #if 0   
1429         
1430         //Test some key stuff
1431         
1432         filemenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-save"),
1433                 hide_return(sigc::mem_fun(*get_instance().get(), &studio::Instance::save))));
1434         filemenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-save-as"),sigc::hide_return(sigc::mem_fun(*get_instance(), &studio::Instance::dialog_save_as))));
1435         filemenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-revert-to-saved"),hide_return(sigc::mem_fun(*get_instance().get(), &studio::Instance::safe_revert))));
1436         filemenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1437
1438         filemenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-cvs_add"),sigc::hide_return(sigc::mem_fun(*get_instance(), &studio::Instance::dialog_cvs_add))));
1439         filemenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-cvs_update"),sigc::hide_return(sigc::mem_fun(*get_instance(), &studio::Instance::dialog_cvs_update))));
1440         filemenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-cvs_commit"),sigc::hide_return(sigc::mem_fun(*get_instance(), &studio::Instance::dialog_cvs_commit))));
1441
1442         filemenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1443         filemenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Import..."),Gtk::AccelKey('I',Gdk::CONTROL_MASK),sigc::hide_return(sigc::mem_fun(*this, &studio::CanvasView::image_import))));
1444         filemenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1445         filemenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Render"),Gtk::AccelKey("F9"),
1446                 sigc::mem_fun(render_settings,&studio::RenderSettings::present)
1447         ));
1448         filemenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Preview"),Gtk::AccelKey("F11"),
1449                 sigc::mem_fun(*this,&CanvasView::on_preview_option)
1450         ));     
1451         filemenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Sound File"),
1452                 sigc::mem_fun(*this,&CanvasView::on_audio_option)
1453         ));     
1454         
1455         filemenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1456         filemenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Options"),Gtk::AccelKey("F12"),
1457                 sigc::mem_fun(canvas_options,&studio::CanvasOptions::present)
1458         ));
1459         filemenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1460         filemenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-close"),sigc::hide_return(sigc::mem_fun(*this,&studio::CanvasView::close))));
1461
1462         editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-undo"),Gtk::AccelKey('Z',Gdk::CONTROL_MASK),SLOT_EVENT(EVENT_UNDO)));
1463         editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-redo"),Gtk::AccelKey('R',Gdk::CONTROL_MASK),SLOT_EVENT(EVENT_REDO)));
1464         editmenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1465         editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-cut"),NOT_IMPLEMENTED_SLOT));
1466         editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-copy"),NOT_IMPLEMENTED_SLOT));
1467         editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-paste"),NOT_IMPLEMENTED_SLOT));
1468         editmenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1469         editmenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Select All Ducks"),Gtk::AccelKey('E',Gdk::CONTROL_MASK),sigc::mem_fun(*work_area,&studio::WorkArea::select_all_ducks)));
1470         editmenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Unselect All Layers"),Gtk::AccelKey('D',Gdk::CONTROL_MASK),sigc::mem_fun(*this,&CanvasView::on_unselect_layers)));
1471         editmenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1472
1473         //editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-stop"),Gtk::AccelKey(GDK_Escape,static_cast<Gdk::ModifierType>(0)),sigc::hide_return(sigc::mem_fun(*this, &studio::CanvasView::stop))));
1474         //editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-refresh"),Gtk::AccelKey('k',Gdk::CONTROL_MASK),sigc::hide_return(sigc::mem_fun(*this, &studio::CanvasView::on_refresh_pressed))));
1475         editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-stop"),Gtk::AccelKey(GDK_Escape,static_cast<Gdk::ModifierType>(0)),SLOT_EVENT(EVENT_STOP)));
1476         editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-refresh"),Gtk::AccelKey('k',Gdk::CONTROL_MASK),SLOT_EVENT(EVENT_REFRESH)));
1477         editmenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1478         editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-rotoscope_bline"),
1479                 sigc::mem_fun(*this, &studio::CanvasView::do_rotoscope_bline)));
1480         editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-rotoscope_polygon"),
1481                 sigc::mem_fun(*this, &studio::CanvasView::do_rotoscope_poly)));
1482         editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-eyedrop"),
1483                 sigc::mem_fun(*this, &studio::CanvasView::do_eyedrop)));
1484         editmenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1485         editmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-properties"),Gtk::AccelKey("F8"),
1486                 sigc::mem_fun(canvas_properties,&studio::CanvasProperties::present)
1487         ));
1488
1489         build_new_layer_menu(newlayermenu);
1490         layermenu.items().push_back(Gtk::Menu_Helpers::MenuElem("New",newlayermenu));
1491
1492
1493         {
1494                 synfigapp::Action::ParamList param_list;
1495                 param_list.add("canvas",Canvas::Handle(get_canvas()));
1496                 param_list.add("canvas_interface",canvas_interface());
1497                 add_actions_to_menu(&canvasmenu, param_list,synfigapp::Action::CATEGORY_CANVAS);
1498         }
1499
1500         
1501         //canvasmenu.items().push_back(Gtk::Menu_Helpers::MenuElem("Keyframe Dialog",sigc::mem_fun(keyframe_dialog,&studio::Dialog_Keyframe::present)));
1502
1503         // Duck Mask Menu
1504         if(1)
1505                 {
1506                 duckmaskmenu.items().push_back(Gtk::Menu_Helpers::TearoffMenuElem());
1507
1508                 duckmaskmenu.items().push_back(Gtk::Menu_Helpers::CheckMenuElem(_("Position Ducks"),Gtk::AccelKey('1',Gdk::MOD1_MASK)));
1509                 duck_mask_position=static_cast<Gtk::CheckMenuItem*>(&duckmaskmenu.items().back());
1510                 duck_mask_position->set_active((bool)(work_area->get_type_mask()&Duck::TYPE_POSITION));
1511                 duck_mask_position->signal_toggled().connect(
1512                         sigc::bind(
1513                                 sigc::mem_fun(*this, &studio::CanvasView::toggle_duck_mask),
1514                                 Duck::TYPE_POSITION
1515                         )
1516                 );
1517
1518                 duckmaskmenu.items().push_back(Gtk::Menu_Helpers::CheckMenuElem(_("Vertex Ducks"),Gtk::AccelKey('2',Gdk::MOD1_MASK)));
1519                 duck_mask_vertex=static_cast<Gtk::CheckMenuItem*>(&duckmaskmenu.items().back());
1520                 duck_mask_vertex->set_active((bool)(work_area->get_type_mask()&Duck::TYPE_VERTEX));
1521                 duck_mask_vertex->signal_toggled().connect(
1522                         sigc::bind(
1523                                 sigc::mem_fun(*this, &studio::CanvasView::toggle_duck_mask),
1524                                 Duck::TYPE_VERTEX
1525                         )
1526                 );
1527
1528                 duckmaskmenu.items().push_back(Gtk::Menu_Helpers::CheckMenuElem(_("Tangent Ducks"),Gtk::AccelKey('3',Gdk::MOD1_MASK)));
1529                 duck_mask_tangent=static_cast<Gtk::CheckMenuItem*>(&duckmaskmenu.items().back());
1530                 duck_mask_tangent->set_active((bool)(work_area->get_type_mask()&Duck::TYPE_TANGENT));
1531                 duck_mask_tangent->signal_toggled().connect(
1532                         sigc::bind(
1533                                 sigc::mem_fun(*this, &studio::CanvasView::toggle_duck_mask),
1534                                 Duck::TYPE_TANGENT
1535                         )
1536                 );
1537
1538                 duckmaskmenu.items().push_back(Gtk::Menu_Helpers::CheckMenuElem(_("Radius Ducks"),Gtk::AccelKey('4',Gdk::MOD1_MASK)));
1539                 duck_mask_radius=static_cast<Gtk::CheckMenuItem*>(&duckmaskmenu.items().back());
1540                 duck_mask_radius->set_active((bool)(work_area->get_type_mask()&Duck::TYPE_RADIUS));
1541                 duck_mask_radius->signal_toggled().connect(
1542                         sigc::bind(
1543                                 sigc::mem_fun(*this, &studio::CanvasView::toggle_duck_mask),
1544                                 Duck::TYPE_RADIUS
1545                         )
1546                 );
1547
1548                 duckmaskmenu.items().push_back(Gtk::Menu_Helpers::CheckMenuElem(_("Width Ducks"),Gtk::AccelKey('5',Gdk::MOD1_MASK)));
1549                 duck_mask_width=static_cast<Gtk::CheckMenuItem*>(&duckmaskmenu.items().back());
1550                 duck_mask_width->set_active((bool)(work_area->get_type_mask()&Duck::TYPE_WIDTH));
1551                 duck_mask_width->signal_toggled().connect(
1552                         sigc::bind(
1553                                 sigc::mem_fun(*this, &studio::CanvasView::toggle_duck_mask),
1554                                 Duck::TYPE_WIDTH
1555                         )
1556                 );
1557
1558                 duckmaskmenu.items().push_back(Gtk::Menu_Helpers::CheckMenuElem(_("Angle Ducks"),Gtk::AccelKey('6',Gdk::MOD1_MASK)));
1559                 duck_mask_angle=static_cast<Gtk::CheckMenuItem*>(&duckmaskmenu.items().back());
1560                 duck_mask_angle->set_active((bool)(work_area->get_type_mask()&Duck::TYPE_ANGLE));
1561                 duck_mask_angle->signal_toggled().connect(
1562                         sigc::bind(
1563                                 sigc::mem_fun(*this, &studio::CanvasView::toggle_duck_mask),
1564                                 Duck::TYPE_ANGLE
1565                         )
1566                 );
1567
1568                 viewmenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("_Mask Ducks"),duckmaskmenu));
1569         }
1570         
1571         // Preview Quality Menu
1572         if(1)
1573         {
1574                 qualitymenu.items().push_back(Gtk::Menu_Helpers::TearoffMenuElem());
1575                 int i;
1576                 qualitymenu.items().push_back(Gtk::Menu_Helpers::MenuElem(strprintf(_("Use Parametric Renderer"),0),
1577                         sigc::bind(
1578                                 sigc::mem_fun(*work_area, &studio::WorkArea::set_quality),
1579                                 0
1580                         )
1581                 ));
1582                 for(i=1;i<=10;i++)
1583                 {
1584                         qualitymenu.items().push_back(Gtk::Menu_Helpers::MenuElem(strprintf(_("Set Quality to %d"),i),Gtk::AccelKey('0'+(i%10),Gdk::CONTROL_MASK),
1585                                 sigc::bind(
1586                                         sigc::mem_fun(*work_area, &studio::WorkArea::set_quality),
1587                                         i
1588                                 )
1589                         ));
1590                 }
1591                 viewmenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Preview Quality"),qualitymenu));
1592         }
1593         
1594         viewmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-play"),
1595                 sigc::mem_fun(*this, &studio::CanvasView::play)));
1596         viewmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("Flipbook Dialog"),
1597                 sigc::mem_fun(*preview_dialog, &studio::Dialog_Preview::present)));
1598
1599         viewmenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1600
1601         viewmenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Toggle Grid Show"),Gtk::AccelKey('g',Gdk::CONTROL_MASK),
1602                 sigc::mem_fun(*work_area, &studio::WorkArea::toggle_grid)));
1603         viewmenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Toggle Grid Snap"),Gtk::AccelKey('l',Gdk::CONTROL_MASK),
1604                 sigc::mem_fun(*work_area, &studio::WorkArea::toggle_grid_snap)));
1605         viewmenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Toggle Guide Snap"),Gtk::AccelKey('k',Gdk::CONTROL_MASK),
1606                 sigc::mem_fun(*work_area, &studio::WorkArea::toggle_guide_snap)));
1607         viewmenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Toggle Low-Res"),Gtk::AccelKey('`',Gdk::CONTROL_MASK),
1608                 sigc::mem_fun(*work_area, &studio::WorkArea::toggle_low_resolution_flag)));
1609         
1610         viewmenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1611
1612         viewmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-zoom-in"),Gtk::AccelKey('=',static_cast<Gdk::ModifierType>(0)),
1613                 sigc::mem_fun(*work_area, &studio::WorkArea::zoom_in)));
1614         viewmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-zoom-out"),Gtk::AccelKey('-',static_cast<Gdk::ModifierType>(0)),
1615                 sigc::mem_fun(*work_area, &studio::WorkArea::zoom_out)));
1616         viewmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-zoom-fit"),
1617                 sigc::mem_fun(*work_area, &studio::WorkArea::zoom_fit)));
1618         viewmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-zoom-100"),Gtk::AccelKey('`',static_cast<Gdk::ModifierType>(0)),
1619                 sigc::mem_fun(*work_area, &studio::WorkArea::zoom_norm)));
1620         viewmenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1621
1622         viewmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-zoom-in"),Gtk::AccelKey('+',static_cast<Gdk::ModifierType>(0)),
1623                 sigc::mem_fun(*this, &studio::CanvasView::time_zoom_in)));
1624         viewmenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-zoom-out"),Gtk::AccelKey('_',static_cast<Gdk::ModifierType>(0)),
1625                 sigc::mem_fun(*this, &studio::CanvasView::time_zoom_out)));
1626
1627         viewmenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Jump to Next Keyframe"),Gtk::AccelKey(']',static_cast<Gdk::ModifierType>(0)),
1628                 sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::jump_to_next_keyframe)));
1629         viewmenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Jump to Prev Keyframe"),Gtk::AccelKey('[',static_cast<Gdk::ModifierType>(0)),
1630                 sigc::mem_fun(*canvas_interface().get(), &synfigapp::CanvasInterface::jump_to_prev_keyframe)));
1631
1632         mainmenu.items().push_back(Gtk::Menu_Helpers::TearoffMenuElem());
1633         mainmenu.items().push_back(Gtk::Menu_Helpers::MenuElem("_File",filemenu));
1634         mainmenu.items().push_back(Gtk::Menu_Helpers::MenuElem("_Edit",editmenu));
1635         mainmenu.items().push_back(Gtk::Menu_Helpers::MenuElem("_View",viewmenu));
1636         mainmenu.items().push_back(Gtk::Menu_Helpers::MenuElem("_Canvas",canvasmenu));
1637         mainmenu.items().push_back(Gtk::Menu_Helpers::MenuElem("_Layer",layermenu));
1638
1639         mainmenu.accelerate(*this);
1640         
1641         {
1642                 
1643                 trackmenu.items().push_back(Gtk::Menu_Helpers::MenuElem("New Waypoint",NOT_IMPLEMENTED_SLOT));
1644                 trackmenu.items().push_back(Gtk::Menu_Helpers::MenuElem("Delete Waypoint",NOT_IMPLEMENTED_SLOT));
1645                 trackmenu.items().push_back(Gtk::Menu_Helpers::MenuElem("Export",NOT_IMPLEMENTED_SLOT));
1646                 trackmenu.items().push_back(Gtk::Menu_Helpers::SeparatorElem());
1647                 trackmenu.items().push_back(Gtk::Menu_Helpers::MenuElem("Properties",NOT_IMPLEMENTED_SLOT));
1648         }
1649         mainmenu.show();
1650         filemenu.show();
1651         editmenu.show();
1652         canvasmenu.show();
1653         layermenu.show();
1654
1655         keyframemenu.items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-properties"),
1656                 sigc::mem_fun(*this,&studio::CanvasView::show_keyframe_dialog)
1657         ));
1658
1659
1660         get_accel_group()->unlock();
1661
1662         //Set the accelerator paths for all the menus
1663         filemenu.set_accel_path("<synfig>/File");
1664         editmenu.set_accel_path("<synfig>/Edit");
1665         layermenu.set_accel_path("<synfig>/Layer");
1666         //mainmenu.set_accel_path("<synfig-main>");
1667         canvasmenu.set_accel_path("<synfig>/Canvas");
1668         viewmenu.set_accel_path("<synfig>/View");
1669         duckmaskmenu.set_accel_path("<synfig>/DuckMask");
1670 #endif  
1671 }
1672
1673 void
1674 CanvasView::on_unselect_layers()
1675 {
1676         layer_tree->clear_selected_layers();
1677 }
1678
1679 void
1680 CanvasView::show_keyframe_dialog()
1681 {
1682         Glib::RefPtr<Gtk::TreeSelection> selection(keyframe_tree->get_selection());
1683         if(selection->get_selected())
1684         {
1685                 Gtk::TreeRow row(*selection->get_selected());
1686                 
1687                 Keyframe keyframe(row[keyframe_tree->model.keyframe]);
1688                 
1689                 keyframe_dialog.set_keyframe(keyframe);
1690                 keyframe_dialog.present();
1691         }
1692 }
1693
1694 void
1695 CanvasView::add_layer(synfig::String x)
1696 {
1697         Canvas::Handle canvas;
1698         
1699         synfigapp::SelectionManager::LayerList layer_list(get_selection_manager()->get_selected_layers());
1700         
1701         int target_depth(0);
1702         
1703         if(layer_list.empty())
1704         {
1705                 canvas=get_canvas();
1706         }
1707         else
1708         {
1709                 canvas=(*layer_list.begin())->get_canvas();
1710                 target_depth=canvas->get_depth(*layer_list.begin());
1711         }
1712
1713         
1714         Layer::Handle layer(canvas_interface()->add_layer_to(x,canvas,target_depth));
1715         if(layer)
1716         {
1717                 get_selection_manager()->clear_selected_layers();
1718                 get_selection_manager()->set_selected_layer(layer);             
1719         }
1720 }
1721
1722 void
1723 CanvasView::popup_layer_menu(synfig::Layer::Handle layer)
1724 {
1725         //Gtk::Menu* menu(manage(new Gtk::Menu));
1726         Gtk::Menu* menu(&parammenu);
1727         menu->items().clear();
1728         
1729         synfigapp::Action::ParamList param_list;
1730         param_list.add("time",canvas_interface()->get_time());
1731         param_list.add("canvas",Canvas::Handle(layer->get_canvas()));
1732         param_list.add("canvas_interface",canvas_interface());
1733         param_list.add("layer",layer);
1734         
1735         //Gtk::Menu *newlayers(manage(new Gtk::Menu()));
1736         //build_new_layer_menu(*newlayers);
1737         
1738         //parammenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("New Layer"),*newlayers));
1739
1740         if(layer->get_name()=="PasteCanvas")
1741         {
1742                 menu->items().push_back(Gtk::Menu_Helpers::MenuElem(_("Select All Children"),
1743                         sigc::bind(
1744                                 sigc::mem_fun(
1745                                         *layer_tree,
1746                                         &studio::LayerTree::select_all_children_layers
1747                                 ),
1748                                 layer
1749                         )
1750                 ));
1751         }
1752         
1753         add_actions_to_menu(menu, param_list,synfigapp::Action::CATEGORY_LAYER);
1754         
1755         menu->popup(3,gtk_get_current_event_time());
1756 }
1757
1758 void
1759 CanvasView::register_layer_type(synfig::Layer::Book::value_type &lyr,std::map<synfig::String,Gtk::Menu*>* category_map)
1760 {
1761 /*      if(lyr.second.category==_("Do Not Use"))
1762                 return;
1763         
1764         if(category_map->count(lyr.second.category)==0)
1765                 (*category_map)[lyr.second.category]=manage(new Gtk::Menu());
1766
1767         (*category_map)[lyr.second.category]->items().push_back(Gtk::Menu_Helpers::MenuElem(lyr.second.local_name,
1768                 sigc::hide_return(
1769                         sigc::bind(
1770                                 sigc::mem_fun(*this,&studio::CanvasView::add_layer),
1771                                 lyr.first
1772                         )
1773                 )
1774         ));
1775 */
1776 }
1777
1778 void
1779 CanvasView::build_new_layer_menu(Gtk::Menu &menu)
1780 {
1781 /*
1782         std::map<synfig::String,Gtk::Menu*> category_map;
1783
1784         std::for_each(
1785                 synfig::Layer::book().begin(),
1786                 synfig::Layer::book().end(),
1787                 sigc::bind(
1788                         sigc::mem_fun(
1789                                 *this,
1790                                 &studio::CanvasView::register_layer_type
1791                         ),
1792                         &category_map
1793                 )
1794         );
1795
1796         menu.items().clear();
1797         menu.items().push_back(Gtk::Menu_Helpers::TearoffMenuElem());
1798
1799         std::map<synfig::String,Gtk::Menu*>::iterator iter;
1800         for(iter=category_map.begin();iter!=category_map.end();++iter)
1801                 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(iter->first,*iter->second)); 
1802
1803         menu.show();
1804 */
1805 }
1806
1807 void
1808 CanvasView::popup_main_menu()
1809 {
1810         //mainmenu.popup(0,gtk_get_current_event_time());
1811         Gtk::Menu* menu = dynamic_cast<Gtk::Menu*>(App::ui_manager()->get_widget("/menu-main"));
1812         if(menu)
1813         {
1814                 //menu->set_accel_group(App::ui_manager()->get_accel_group());
1815                 //menu->accelerate(*this);
1816                 menu->popup(0,gtk_get_current_event_time());
1817         }
1818 }
1819
1820 void
1821 CanvasView::on_refresh_pressed()
1822 {
1823         rebuild_tables();
1824         rebuild_ducks();
1825         work_area->queue_render_preview();
1826 }
1827
1828 void
1829 CanvasView::workarea_layer_selected(synfig::Layer::Handle layer)
1830 {
1831         get_selection_manager()->clear_selected_layers();
1832         if(layer)
1833                 get_selection_manager()->set_selected_layer(layer);
1834 }
1835
1836
1837 void
1838 CanvasView::refresh_rend_desc()
1839 {
1840         current_time_widget->set_fps(get_canvas()->rend_desc().get_frame_rate());
1841
1842         
1843         //????
1844         //synfig::info("Canvasview: Refreshing render desc info");
1845         if(!get_time().is_equal(time_adjustment().get_value()))
1846         {
1847                 time_adjustment().set_value(get_time());
1848                 time_adjustment().value_changed();
1849         }
1850
1851         Time length(get_canvas()->rend_desc().get_time_end()-get_canvas()->rend_desc().get_time_start());
1852         if(length<DEFAULT_TIME_WINDOW_SIZE)
1853         {
1854                 time_window_adjustment().set_page_increment(length);
1855                 time_window_adjustment().set_page_size(length);
1856         }
1857         else
1858         {
1859                 time_window_adjustment().set_page_increment(DEFAULT_TIME_WINDOW_SIZE);
1860                 time_window_adjustment().set_page_size(DEFAULT_TIME_WINDOW_SIZE);
1861         }
1862
1863         //set the FPS of the timeslider
1864         timeslider->set_global_fps(get_canvas()->rend_desc().get_frame_rate());
1865         
1866         //set the beginning and ending time of the time slider
1867         Time begin_time=get_canvas()->rend_desc().get_time_start();
1868         Time end_time=get_canvas()->rend_desc().get_time_end();
1869
1870         // Setup the time_window adjustment
1871         time_window_adjustment().set_lower(begin_time);
1872         time_window_adjustment().set_upper(end_time);
1873         time_window_adjustment().set_step_increment(synfig::Time(1.0/get_canvas()->rend_desc().get_frame_rate()));
1874
1875         //Time length(get_canvas()->rend_desc().get_time_end()-get_canvas()->rend_desc().get_time_start());
1876         if(length < time_window_adjustment().get_page_size())
1877         {
1878                 time_window_adjustment().set_page_increment(length);
1879                 time_window_adjustment().set_page_size(length);
1880         }
1881         
1882         /*synfig::info("w: %p - [%.3f,%.3f] (%.3f,%.3f) child: %p\n",
1883                                 &time_window_adjustment_, time_window_adjustment_.get_lower(),
1884                                 time_window_adjustment_.get_upper(),time_window_adjustment_.get_value(),
1885                                 time_window_adjustment_.get_page_size(),time_window_adjustment_.get_child_adjustment()
1886         );*/
1887         
1888         time_window_adjustment().changed(); //only non-value stuff was changed
1889
1890         // Setup the time adjustment
1891         
1892         //NOTE THESE TWO SHOULD BE CHANGED BY THE changed() CALL ABOVE
1893         //time_adjustment().set_lower(time_window_adjustment().get_value());
1894         //time_adjustment().set_upper(time_window_adjustment().get_value()+time_window_adjustment().get_page_size());
1895         
1896 //      time_adjustment().set_lower(get_canvas()->rend_desc().get_time_start());
1897 //      time_adjustment().set_upper(get_canvas()->rend_desc().get_time_end());
1898         time_adjustment().set_step_increment(synfig::Time(1.0/get_canvas()->rend_desc().get_frame_rate()));
1899         time_adjustment().set_page_increment(synfig::Time(1.0));
1900         time_adjustment().set_page_size(0);
1901         
1902         time_adjustment().changed();
1903         
1904         /*synfig::info("w: %p - [%.3f,%.3f] (%.3f,%.3f) child: %p\n",
1905                                 &time_window_adjustment_, time_window_adjustment_.get_lower(),
1906                                 time_window_adjustment_.get_upper(),time_window_adjustment_.get_value(),
1907                                 time_window_adjustment_.get_page_size(),time_window_adjustment_.get_child_adjustment()
1908         );      */
1909         
1910         if(begin_time==end_time)
1911         {
1912                 hide_timebar();
1913         }
1914         else
1915         {
1916                 show_timebar();
1917         }
1918
1919         //clamp time to big bounds...
1920         if(time_adjustment().get_value() < begin_time)
1921         {
1922                 time_adjustment().set_value(begin_time);
1923                 time_adjustment().value_changed();
1924         }
1925         
1926         if(time_adjustment().get_value() > end_time)
1927         {
1928                 time_adjustment().set_value(end_time);
1929                 time_adjustment().value_changed();
1930         }
1931         
1932         /*synfig::info("Time stats: \n"
1933                                 "w: %p - [%.3f,%.3f] (%.3f,%.3f) child: %p\n"
1934                                 "t: %p - [%.3f,%.3f] %.3f",
1935                                 &time_window_adjustment_, time_window_adjustment_.get_lower(),
1936                                 time_window_adjustment_.get_upper(),time_window_adjustment_.get_value(),
1937                                 time_window_adjustment_.get_page_size(),time_window_adjustment_.get_child_adjustment(),
1938                                 &time_adjustment_,time_adjustment_.get_lower(),time_adjustment_.get_upper(),
1939                                 time_adjustment_.get_value()
1940         );*/
1941
1942         work_area->queue_render_preview();
1943 }
1944
1945
1946 bool
1947 CanvasView::close()
1948 {
1949         get_instance()->safe_close();
1950         return false;
1951 }
1952
1953 handle<CanvasView>
1954 CanvasView::create(loose_handle<Instance> instance,handle<Canvas> canvas)
1955 {
1956         etl::handle<studio::CanvasView> view(new CanvasView(instance,instance->synfigapp::Instance::find_canvas_interface(canvas)));
1957         instance->canvas_view_list().push_front(view);
1958         instance->signal_canvas_view_created()(view.get());
1959         return view;
1960 }
1961
1962 void
1963 CanvasView::update_title()
1964 {
1965         string title;
1966
1967         title+=etl::basename(get_instance()->get_file_name())
1968                 +" : ";
1969         if(get_canvas()->get_name().empty())
1970                 title+='"'+get_canvas()->get_id()+'"';
1971         else
1972                 title+='"'+get_canvas()->get_name()+'"';
1973
1974         if(get_instance()->synfigapp::Instance::get_action_count())
1975                 title+=_(" (Unsaved)");
1976
1977         if(get_instance()->synfigapp::Instance::in_repository())
1978         {
1979                 title+=" (CVS";
1980                 if(get_instance()->synfigapp::Instance::is_modified())
1981                         title+=_("-MODIFIED");
1982                 if(get_instance()->synfigapp::Instance::is_updated())
1983                         title+=_("-UPDATED");
1984                 title+=')';
1985         }
1986
1987         if(get_canvas()->is_root())
1988                 title+=_(" (Root)");
1989
1990         set_title(title);
1991 }
1992
1993
1994 void
1995 CanvasView::on_hide()
1996 {
1997         smach_.egress();
1998         Gtk::Window::on_hide();
1999 }
2000
2001 void
2002 CanvasView::present()
2003 {
2004         grab_focus();//on_focus_in_event(0);
2005         Gtk::Window::present();
2006 }
2007
2008 bool
2009 CanvasView::on_focus_in_event(GdkEventFocus*x)
2010 {
2011         if(studio::App::get_selected_canvas_view()!=this)
2012         {
2013                 if(studio::App::get_selected_canvas_view())
2014                 {
2015                         studio::App::get_selected_canvas_view()->get_smach().process_event(EVENT_YIELD_TOOL_OPTIONS);
2016                         App::ui_manager()->remove_action_group(App::get_selected_canvas_view()->action_group);
2017                 }
2018
2019                 get_smach().process_event(EVENT_REFRESH_TOOL_OPTIONS);
2020
2021                 studio::App::set_selected_canvas_view(this);
2022         
2023                 App::ui_manager()->insert_action_group(action_group);
2024         }
2025         
2026         // HACK ... Questionable...?
2027         if(x)
2028                 return Gtk::Window::on_focus_in_event(x);
2029         
2030         return true;
2031 }
2032
2033 bool
2034 CanvasView::on_focus_out_event(GdkEventFocus*x)
2035 {
2036         //App::ui_manager()->remove_action_group(action_group);
2037         //App::ui_manager()->ensure_update();
2038         return Gtk::Window::on_focus_out_event(x);
2039 }
2040
2041 void
2042 CanvasView::refresh_tables()
2043 {
2044 //      if(layer_tree_store_)layer_tree_store_->refresh();
2045 //      if(children_tree_store_)children_tree_store_->refresh();
2046 }
2047
2048 void
2049 CanvasView::rebuild_tables()
2050 {
2051 //      layer_tree_store_->rebuild();
2052 //      children_tree_store_->rebuild();
2053 }
2054
2055 void
2056 CanvasView::build_tables()
2057 {
2058 //      layer_tree_store_->rebuild();
2059 //      children_tree_store_->rebuild();
2060 }
2061
2062 void
2063 CanvasView::on_layer_toggle(synfig::Layer::Handle layer)
2064 {
2065         synfigapp::Action::Handle action(synfigapp::Action::create("layer_activate"));
2066         assert(action);
2067         
2068         if(!action)
2069                 return;
2070         
2071         action->set_param("canvas",Canvas::Handle(layer->get_canvas()));
2072         if(!action->set_param("canvas_interface",canvas_interface()))
2073 //      if(!action->set_param("canvas_interface",get_instance()->find_canvas_interface(layer->get_canvas())))
2074                 synfig::error("LayerActivate didn't like CanvasInterface...?");
2075         action->set_param("time",get_time());
2076         action->set_param("layer",layer);
2077         action->set_param("new_status",!layer->active());
2078
2079         assert(action->is_ready());
2080         
2081         canvas_interface()->get_instance()->perform_action(action);
2082 }
2083
2084
2085 void
2086 CanvasView::popup_param_menu(synfigapp::ValueDesc value_desc, float location)
2087 {
2088         parammenu.items().clear();
2089         get_instance()->make_param_menu(&parammenu,get_canvas(),value_desc,location);
2090         
2091         parammenu.popup(3,gtk_get_current_event_time());
2092 }
2093
2094 void
2095 CanvasView::add_actions_to_menu(Gtk::Menu *menu, const synfigapp::Action::ParamList &param_list,synfigapp::Action::Category category)const
2096 {
2097         get_instance()->add_actions_to_menu(menu, param_list, category);
2098 }
2099
2100 bool
2101 CanvasView::on_layer_user_click(int button, Gtk::TreeRow row, LayerTree::ColumnID column_id)
2102 {       
2103         switch(button)
2104         {
2105         case 3:
2106                 {
2107
2108                         Gtk::MenuItem* menu = dynamic_cast<Gtk::MenuItem*>(App::ui_manager()->get_widget("/menu-main/menu-layer"));
2109                         if(menu && menu->get_submenu())
2110                         {
2111                                 //menu->set_accel_group(App::ui_manager()->get_accel_group());
2112                                 //menu->accelerate(*this);
2113                                 menu->get_submenu()->popup(button,gtk_get_current_event_time());
2114                         }
2115
2116                         
2117                         #if 0
2118                         bool multiple_selected=true;
2119                         
2120                         if(layer_tree->get_selection()->count_selected_rows()<=1)
2121                                 multiple_selected=false;
2122
2123                         // If the clicked row is not selected, then unselect
2124                         // everything that isn't selected and select this row
2125                         if(multiple_selected && !layer_tree->get_selection()->is_selected(row))
2126                         {
2127                                 layer_tree->get_selection()->unselect_all();
2128                                 layer_tree->get_selection()->select(row);
2129                                 multiple_selected=false;
2130                         }
2131                         
2132                         if(column_id==COLUMNID_TIME_TRACK)
2133                                 return false;
2134                         
2135                         //synfigapp::ValueDesc value_desc(row[layer_param_tree_model.value_desc]);
2136                         //ValueNode::Handle value_node(row[layer_param_tree_model.value_node]);
2137                         //ValueNode::Handle parent_value_node;
2138                         //ValueBase value=row[layer_param_tree_model.value];
2139                         
2140                         //if(row.parent())
2141                         //{
2142                         //      parent_value_node=(*row.parent())[layer_tree_model.value_node];
2143                         //}
2144
2145                         {
2146                                 Layer::Handle layer(row[layer_tree_model.layer]);
2147                                 synfigapp::Action::ParamList param_list;
2148                                 param_list.add("time",canvas_interface()->get_time());
2149                                 param_list.add("canvas",Canvas::Handle(row[layer_tree_model.canvas]));
2150                                 param_list.add("canvas_interface",canvas_interface());
2151                                 if(!multiple_selected)
2152                                         param_list.add("layer",layer);
2153                                 else
2154                                 {
2155                                         synfigapp::SelectionManager::LayerList layer_list(get_selection_manager()->get_selected_layers());
2156                                         synfigapp::SelectionManager::LayerList::iterator iter;
2157                                 
2158                                         for(iter=layer_list.begin();iter!=layer_list.end();++iter)
2159                                                 param_list.add("layer",Layer::Handle(*iter));
2160                                 }
2161                                 
2162                                 parammenu.items().clear();
2163
2164                                 Gtk::Menu *newlayers(manage(new Gtk::Menu()));
2165                                 build_new_layer_menu(*newlayers);
2166                                 
2167                                 parammenu.items().push_back(Gtk::Menu_Helpers::MenuElem("New Layer",*newlayers));
2168                                 if(!multiple_selected && layer->get_name()=="PasteCanvas")
2169                                 {
2170                                         parammenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Select All Children"),
2171                                                 sigc::bind(
2172                                                         sigc::mem_fun(
2173                                                                 *layer_tree,
2174                                                                 &studio::LayerTree::select_all_children_layers
2175                                                         ),
2176                                                         layer
2177                                                 )
2178                                         ));
2179                                 }
2180                                 
2181                                 add_actions_to_menu(&parammenu, param_list,synfigapp::Action::CATEGORY_LAYER);
2182                                 parammenu.popup(button,gtk_get_current_event_time());
2183                                 return true;
2184                         }
2185 /*
2186                         else if(column_id==LayerTree::COLUMNID_TIME_TRACK && value_node && handle<synfig::ValueNode_Animated>::cast_dynamic(value_node))
2187                         {                                       
2188                                 // Right-click on time track with animated
2189 //                              trackmenu.popup(0,0);
2190                                 return true;
2191                         }
2192                         else
2193                         {
2194                                 if(!multiple_selected)
2195                                 {
2196                                         popup_param_menu(value_desc);
2197                                         return true;
2198                                 }
2199                                 else
2200                                 {
2201 #warning update me!
2202 #if 0
2203                                         parammenu.items().clear();
2204                                         parammenu.items().push_back(Gtk::Menu_Helpers::MenuElem("Connect",
2205                                                 hide_return(sigc::mem_fun(*canvas_interface().get(),&synfigapp::CanvasInterface::connect_selected_layer_params))
2206                                         ));
2207                                         parammenu.items().push_back(Gtk::Menu_Helpers::MenuElem("Disconnect",
2208                                                 hide_return(sigc::mem_fun(*canvas_interface().get(),&synfigapp::CanvasInterface::disconnect_selected_layer_params))
2209                                         ));
2210                                         parammenu.popup(0,0);
2211 #endif
2212                                 }
2213                                 return true;
2214                         }
2215                 */
2216 #endif
2217 }
2218                 return true;
2219                 break;
2220                 
2221         default:
2222                 return false;
2223                 break;          
2224         }
2225 }
2226
2227
2228
2229 bool
2230 CanvasView::on_children_user_click(int button, Gtk::TreeRow row, ChildrenTree::ColumnID column_id)
2231 {       
2232         switch(button)
2233         {
2234         case 3:
2235                 {
2236                         if(column_id==COLUMNID_TIME_TRACK)
2237                                 return false;
2238                         if(!(bool)row[children_tree_model.is_canvas])
2239                         {
2240                                 synfigapp::ValueDesc value_desc=row[children_tree_model.value_desc];
2241                                 assert(value_desc);
2242                                 popup_param_menu(value_desc);
2243                                 return true;
2244                         }
2245                 }
2246                 return true;
2247                 break;
2248                 
2249         default:
2250                 return false;
2251                 break;          
2252         }
2253 }
2254
2255 bool
2256 CanvasView::on_keyframe_tree_event(GdkEvent *event)
2257 {
2258     switch(event->type)
2259     {
2260         case GDK_BUTTON_PRESS:
2261                 switch(event->button.button)
2262                 {
2263                         case 3: 
2264                         {
2265                                 //keyframemenu.popup(event->button.button,gtk_get_current_event_time());
2266                                 return true;
2267                         }
2268                         break;
2269                 }
2270                 break;
2271         case GDK_MOTION_NOTIFY:
2272                 break;
2273         case GDK_BUTTON_RELEASE:
2274                 break;
2275         default:
2276                 break;
2277         }
2278         return false;
2279 }
2280
2281
2282 void
2283 CanvasView::refresh_time_window()
2284 {
2285         //THESE SHOULD AUTOMATICALLY BE TAKEN CARE OF
2286         //time_adjustment().set_lower(time_window_adjustment().get_value());
2287         //time_adjustment().set_upper(time_window_adjustment().get_value()+time_window_adjustment().get_page_size());
2288         
2289         time_adjustment().set_page_increment(1.0); // One second
2290         time_adjustment().set_page_size(0);
2291         
2292         if(get_canvas())
2293                 time_adjustment().set_step_increment(1.0/get_canvas()->rend_desc().get_frame_rate());
2294         time_adjustment().changed();
2295         
2296         //NOTE THIS SHOULD HOOK INTO THE CORRECT SIGNALS...
2297         if(children_tree)
2298                 children_tree->queue_draw();
2299 }
2300
2301 void
2302 CanvasView::on_time_changed()
2303 {
2304         Time time(get_time());
2305                 
2306         current_time_widget->set_value(time);
2307         try {
2308                 get_canvas()->keyframe_list().find(time);
2309                 current_time_widget->modify_text(Gtk::STATE_NORMAL,Gdk::Color("#FF0000"));
2310         }catch(...){
2311                 current_time_widget->modify_text(Gtk::STATE_NORMAL,Gdk::Color("#000000"));              
2312         }
2313         
2314         if(get_time() != time_adjustment().get_value())
2315         {
2316                 
2317                 //Recenters the window, causing it to jump (possibly undesirably... but whatever)
2318                 if(time < time_window_adjustment().get_value() ||
2319                         time > time_window_adjustment().get_value()+time_window_adjustment().get_page_size())
2320                 {
2321                         time_window_adjustment().set_value(
2322                                 time-time_window_adjustment().get_page_size()/2
2323                         );
2324                 }
2325                 time_adjustment().set_value(time);
2326                 time_adjustment().value_changed();
2327                 
2328                 // Shouldn't these trees just hook into
2329                 // the time changed signal...?
2330                 //YES THEY SHOULD...
2331                 if(layer_tree)layer_tree->queue_draw();
2332                 if(children_tree)children_tree->queue_draw();
2333         }
2334 }
2335
2336 void
2337 CanvasView::time_zoom_in()
2338 {
2339         time_window_adjustment().set_page_size(time_window_adjustment().get_page_size()*0.75);
2340         time_window_adjustment().changed();
2341         
2342         refresh_time_window();
2343 }
2344
2345 void
2346 CanvasView::time_zoom_out()
2347 {
2348         time_window_adjustment().set_page_size(time_window_adjustment().get_page_size()/0.75);
2349         time_window_adjustment().changed();
2350         
2351         refresh_time_window();
2352 }
2353
2354 void
2355 CanvasView::time_was_changed()
2356 {
2357         synfig::Time time((synfig::Time)(double)time_adjustment().get_value());
2358         set_time(time);
2359 }
2360
2361 void
2362 CanvasView::on_edited_value(synfigapp::ValueDesc value_desc,synfig::ValueBase new_value)
2363 {
2364         canvas_interface()->change_value(value_desc,new_value);
2365 }
2366
2367 /*
2368 void
2369 CanvasView::on_children_edited_value(const Glib::ustring&path_string,synfig::ValueBase value)
2370 {
2371         Gtk::TreePath path(path_string);
2372         
2373         const Gtk::TreeRow row = *(children_tree->get_model()->get_iter(path));
2374
2375         assert((bool)row[children_tree_model.is_value_node]);
2376
2377         synfigapp::ValueDesc value_desc=row[children_tree_model.value_desc];
2378         assert(value_desc);
2379
2380         on_edited_value(value_desc,value);
2381 }
2382 */
2383
2384 void
2385 CanvasView::on_id_changed()
2386 {
2387         update_title();
2388 }
2389
2390
2391 void
2392 CanvasView::on_mode_changed(synfigapp::CanvasInterface::Mode mode)
2393 {
2394         // If the aninimate flag was set in mode...
2395         if(mode&synfigapp::MODE_ANIMATE)
2396         {
2397                 Gtk::Image *icon;
2398                 icon=manage(new Gtk::Image(Gtk::StockID("gtk-no"),Gtk::ICON_SIZE_BUTTON));
2399                 animatebutton->remove();        
2400                 animatebutton->add(*icon);      
2401                 tooltips.set_tip(*animatebutton,_("In Animate Editing Mode"));
2402                 icon->set_padding(0,0);
2403                 icon->show();
2404         }
2405         else
2406         {
2407                 Gtk::Image *icon;
2408                 icon=manage(new Gtk::Image(Gtk::StockID("gtk-yes"),Gtk::ICON_SIZE_BUTTON));
2409                 animatebutton->remove();        
2410                 animatebutton->add(*icon);      
2411                 tooltips.set_tip(*animatebutton,_("Not in Animate Editing Mode"));
2412                 icon->set_padding(0,0);
2413                 icon->show();
2414         }
2415
2416         if((mode&synfigapp::MODE_ANIMATE_FUTURE) && (mode&synfigapp::MODE_ANIMATE_PAST))
2417         {
2418                 Gtk::Image *icon;
2419                 icon=manage(new Gtk::Image(Gtk::StockID("synfig-keyframe_lock_all"),Gtk::ICON_SIZE_BUTTON));
2420                 keyframebutton->remove();       
2421                 keyframebutton->add(*icon);     
2422                 tooltips.set_tip(*keyframebutton,_("All Keyframes Locked"));
2423                 icon->set_padding(0,0);
2424                 icon->show();
2425         }
2426         else if((mode&synfigapp::MODE_ANIMATE_FUTURE) && !(mode&synfigapp::MODE_ANIMATE_PAST))
2427         {
2428                 Gtk::Image *icon;
2429                 icon=manage(new Gtk::Image(Gtk::StockID("synfig-keyframe_lock_future"),Gtk::ICON_SIZE_BUTTON));
2430                 keyframebutton->remove();       
2431                 keyframebutton->add(*icon);     
2432                 tooltips.set_tip(*keyframebutton,_("Future Keyframes Locked"));
2433                 icon->set_padding(0,0);
2434                 icon->show();
2435         }
2436         else if(!(mode&synfigapp::MODE_ANIMATE_FUTURE) && (mode&synfigapp::MODE_ANIMATE_PAST))
2437         {
2438                 Gtk::Image *icon;
2439                 icon=manage(new Gtk::Image(Gtk::StockID("synfig-keyframe_lock_past"),Gtk::ICON_SIZE_BUTTON));
2440                 keyframebutton->remove();       
2441                 keyframebutton->add(*icon);     
2442                 tooltips.set_tip(*keyframebutton,_("Past Keyframes Locked"));
2443                 icon->set_padding(0,0);
2444                 icon->show();
2445         }
2446         else if(!(mode&synfigapp::MODE_ANIMATE_FUTURE) && !(mode&synfigapp::MODE_ANIMATE_PAST))
2447         {
2448                 Gtk::Image *icon;
2449                 icon=manage(new Gtk::Image(Gtk::StockID("synfig-keyframe_lock_none"),Gtk::ICON_SIZE_BUTTON));
2450                 keyframebutton->remove();       
2451                 keyframebutton->add(*icon);     
2452                 tooltips.set_tip(*keyframebutton,_("No Keyframes Locked"));
2453                 icon->set_padding(0,0);
2454                 icon->show();
2455         }
2456         
2457         work_area->queue_draw();
2458 }
2459
2460 void
2461 CanvasView::on_animate_button_pressed()
2462 {
2463         if(get_mode()&synfigapp::MODE_ANIMATE)
2464                 set_mode(get_mode()-synfigapp::MODE_ANIMATE);
2465         else
2466                 set_mode(get_mode()|synfigapp::MODE_ANIMATE);
2467 }
2468
2469 void
2470 CanvasView::on_keyframe_button_pressed()
2471 {
2472         synfigapp::CanvasInterface::Mode mode(get_mode());
2473         
2474         if((mode&synfigapp::MODE_ANIMATE_FUTURE) && (mode&synfigapp::MODE_ANIMATE_PAST))
2475         {
2476                 set_mode(get_mode()-synfigapp::MODE_ANIMATE_FUTURE);
2477         }
2478         else if(!(mode&synfigapp::MODE_ANIMATE_FUTURE) && (mode&synfigapp::MODE_ANIMATE_PAST))
2479         {
2480                 set_mode(get_mode()-synfigapp::MODE_ANIMATE_PAST|synfigapp::MODE_ANIMATE_FUTURE);
2481         }
2482         else if((mode&synfigapp::MODE_ANIMATE_FUTURE) && !(mode&synfigapp::MODE_ANIMATE_PAST))
2483         {
2484                 set_mode(get_mode()-synfigapp::MODE_ANIMATE_FUTURE);
2485         }
2486         else if(!(mode&synfigapp::MODE_ANIMATE_FUTURE) && !(mode&synfigapp::MODE_ANIMATE_PAST))
2487         {
2488                 set_mode(get_mode()|synfigapp::MODE_ANIMATE_FUTURE|synfigapp::MODE_ANIMATE_PAST);
2489         }
2490 }
2491
2492 bool
2493 CanvasView::duck_change_param(const Point &value,synfig::Layer::Handle layer, synfig::String param_name)
2494 {
2495         return canvas_interface()->change_value(synfigapp::ValueDesc(layer,param_name),value);
2496 }
2497
2498 bool
2499 CanvasView::on_duck_changed(const synfig::Point &value,const synfigapp::ValueDesc& value_desc)
2500 {
2501         switch(value_desc.get_value_type())
2502         {
2503         case ValueBase::TYPE_REAL:
2504                 return canvas_interface()->change_value(value_desc,value.mag());
2505                 break;
2506         case ValueBase::TYPE_ANGLE:
2507                 return canvas_interface()->change_value(value_desc,Angle::tan(value[1],value[0]));
2508                 break;
2509         default:
2510                 return canvas_interface()->change_value(value_desc,value);
2511                 break;
2512         }
2513
2514         return true;
2515 }
2516
2517 void
2518 CanvasView::selected_layer_color_set(Color color)
2519 {
2520         synfigapp::SelectionManager::LayerList selected_list(get_selection_manager()->get_selected_layers());
2521         synfigapp::SelectionManager::LayerList::iterator iter;
2522
2523         // Create the action group
2524         //synfigapp::PassiveGrouper group(canvas_interface()->get_instance(),_("Set Colors"));
2525
2526         Layer::Handle layer;
2527         for(iter=selected_list.begin();iter!=selected_list.end();++iter)
2528         {
2529                 if(*iter==layer)
2530                         continue;
2531                 layer=*iter;
2532                 on_edited_value(synfigapp::ValueDesc(layer,"color"),color);
2533         }
2534 }
2535
2536 void
2537 CanvasView::rebuild_ducks_layer_(synfig::TransformStack& transform_stack, Canvas::Handle canvas, std::set<synfig::Layer::Handle>& selected_list)
2538 {
2539         int transforms(0);
2540         String layer_name;
2541                 
2542 #define QUEUE_REBUILD_DUCKS             sigc::mem_fun(*this,&CanvasView::queue_rebuild_ducks)   
2543
2544         if(!canvas)
2545         {
2546                 synfig::warning("CanvasView::rebuild_ducks_layer_(): Layer doesn't have canvas set");
2547                 return;
2548         }
2549         for(Canvas::iterator iter(canvas->begin());iter!=canvas->end();++iter)
2550         {
2551                 Layer::Handle layer(*iter);
2552
2553                 if(selected_list.count(layer))
2554                 {
2555                         if(!curr_transform_stack_set)
2556                         {
2557                                 curr_transform_stack_set=true;
2558                                 curr_transform_stack=transform_stack;
2559                         }
2560
2561                         // This layer is currently selected.
2562                         duck_changed_connections.push_back(layer->signal_changed().connect(QUEUE_REBUILD_DUCKS));
2563                         
2564                         // do the bounding box thing
2565                         bbox|=transform_stack.perform(layer->get_bounding_rect());
2566                         
2567                         // Grab the layer's list pf parameters
2568                         Layer::ParamList paramlist(layer->get_param_list());
2569                 
2570                         // Grab the layer vocabulary
2571                         Layer::Vocab vocab=layer->get_param_vocab();
2572                         Layer::Vocab::iterator iter;
2573                                 
2574                         for(iter=vocab.begin();iter!=vocab.end();iter++)
2575                         {
2576                                 if(!iter->get_hidden() && !iter->get_invisible_duck())
2577                                 {
2578                                         synfigapp::ValueDesc value_desc(layer,iter->get_name());
2579                                         work_area->add_to_ducks(value_desc,this,transform_stack,&*iter);
2580                                         if(value_desc.is_value_node())
2581                                                 duck_changed_connections.push_back(value_desc.get_value_node()->signal_changed().connect(QUEUE_REBUILD_DUCKS));
2582                                 }
2583                                 if(iter->get_name()=="color")
2584                                 {
2585                                         /*
2586                                         if(!App::dialog_color->busy())
2587                                         {
2588                                                 App::dialog_color->reset();
2589                                                 App::dialog_color->set_color(layer->get_param("color").get(Color()));
2590                                                 App::dialog_color->signal_edited().connect(
2591                                                         sigc::mem_fun(
2592                                                                 *this,
2593                                                                 &studio::CanvasView::selected_layer_color_set
2594                                                         )
2595                                                 );              
2596                                         }
2597                                         */
2598                                 }
2599                         }
2600                 }
2601                 
2602                 layer_name=layer->get_name();
2603                 
2604                 if(layer->active())
2605                 {
2606                         Transform::Handle trans(layer->get_transform());
2607                         if(trans)
2608                         {
2609                                 transform_stack.push(trans);
2610                                 transforms++;
2611                         }
2612
2613 /*                      // Add transforms onto the stack
2614                         if(layer_name=="Translate")
2615                         {
2616                                 transform_stack.push(synfig::Transform_Translate(layer->get_param("origin").get(Vector())));
2617                                 transforms++;
2618                         }else
2619                         if(layer_name=="Zoom")
2620                         {
2621                                 Vector scale;
2622                                 scale[0]=scale[1]=exp(layer->get_param("amount").get(Real()));
2623                                 transform_stack.push(synfig::Transform_Scale(scale,layer->get_param("center").get(Vector())));
2624                                 transforms++;
2625                         }else
2626                         if(layer_name=="stretch")
2627                         {
2628                                 Vector scale(layer->get_param("amount").get(Vector()));
2629                                 transform_stack.push(synfig::Transform_Scale(scale,layer->get_param("center").get(Vector())));
2630                                 transforms++;
2631                         }else
2632                         if(layer_name=="Rotate")
2633                         {
2634                                 transform_stack.push(synfig::Transform_Rotate(layer->get_param("amount").get(Angle()),layer->get_param("origin").get(Vector())));
2635                                 transforms++;
2636                         }
2637 */
2638                 }
2639                 
2640                 // If this is a paste canvas layer, then we need to
2641                 // descend into it
2642                 if(layer_name=="PasteCanvas")
2643                 {
2644                         Vector scale;
2645                         scale[0]=scale[1]=exp(layer->get_param("zoom").get(Real()));
2646                         Vector origin(layer->get_param("origin").get(Vector()));
2647                         
2648                         Canvas::Handle child_canvas(layer->get_param("canvas").get(Canvas::Handle()));
2649                         
2650                         if(!scale.is_equal_to(Vector(1,1)))
2651                                 transform_stack.push(new Transform_Scale(scale,origin));
2652                         if(!scale.is_equal_to(Vector(0,0)))
2653                                 transform_stack.push(new Transform_Translate(origin));
2654                         
2655                         rebuild_ducks_layer_(transform_stack,child_canvas,selected_list);
2656                         
2657                         if(!scale.is_equal_to(Vector(0,0)))
2658                                 transform_stack.pop();
2659                         if(!scale.is_equal_to(Vector(1,1)))
2660                                 transform_stack.pop();
2661                 }
2662         }
2663         // Remove all of the transforms we have added
2664         while(transforms--) { transform_stack.pop(); }
2665
2666 #undef QUEUE_REBUILD_DUCKS
2667 }
2668
2669 void
2670 CanvasView::queue_rebuild_ducks()
2671 {
2672 #if 0
2673         if(rebuild_ducks_queued)
2674                 return;
2675 #else   
2676         if(rebuild_ducks_queued)
2677                 queue_rebuild_ducks_connection.disconnect();
2678 #endif
2679         
2680         queue_rebuild_ducks_connection=Glib::signal_timeout().connect(
2681                 sigc::bind_return(
2682                         sigc::mem_fun(*this,&CanvasView::rebuild_ducks),
2683                         false
2684                 ),
2685                 50
2686         );
2687         
2688         rebuild_ducks_queued=true;
2689 }
2690
2691 void
2692 CanvasView::rebuild_ducks()
2693 {       
2694         /*static int i=0;
2695         i++;
2696         if(i>30)
2697                 synfig::info("%d",i/(i-i));
2698         */
2699         
2700         rebuild_ducks_queued=false;
2701         //queue_rebuild_ducks_connection.disconnect();
2702         
2703         if(work_area->is_dragging())
2704         {
2705                 queue_rebuild_ducks();
2706                 return;
2707         }
2708         
2709         if(!duck_refresh_flag)
2710         {
2711                 duck_refresh_needed=true;
2712                 return;
2713         }
2714         
2715         bbox=Rect::zero();
2716         
2717         work_area->clear_ducks();
2718         work_area->set_time(get_time());
2719         get_canvas()->set_time(get_time());
2720         curr_transform_stack.clear();
2721         //curr_transform_stack.push(new Transform_Translate(Point(0,0)));
2722         curr_transform_stack_set=false;
2723
2724         for(;!duck_changed_connections.empty();duck_changed_connections.pop_back())duck_changed_connections.back().disconnect();
2725         
2726         //get_canvas()->set_time(get_time());
2727         bool not_empty(false);
2728         
2729         // First do the layers...       
2730         do{
2731                 synfigapp::SelectionManager::LayerList selected_list(get_selection_manager()->get_selected_layers());
2732                 std::set<synfig::Layer::Handle> layer_set(selected_list.begin(),selected_list.end());
2733                 
2734                 if(!layer_set.empty())
2735                         not_empty=true;
2736                 
2737                 synfig::TransformStack transform_stack;
2738                 
2739                 rebuild_ducks_layer_(transform_stack, get_canvas(), layer_set);
2740
2741         }while(0);
2742
2743         // Now do the children
2744         do{
2745                 synfigapp::SelectionManager::ChildrenList selected_list(get_selection_manager()->get_selected_children());
2746                 synfigapp::SelectionManager::ChildrenList::iterator iter;
2747                 synfig::TransformStack transform_stack;
2748         
2749                 if(selected_list.empty())
2750                 {
2751                         break;
2752                 }
2753                 else
2754                 {
2755                         not_empty=true;
2756                         for(iter=selected_list.begin();iter!=selected_list.end();++iter)
2757                         {
2758                                 work_area->add_to_ducks(*iter,this,transform_stack);
2759                         }
2760                 }
2761         }while(0);
2762         work_area->refresh_selected_ducks();
2763         work_area->queue_draw_preview();
2764 }
2765
2766 void
2767 CanvasView::on_dirty_preview()
2768 {
2769         if(!is_playing_)
2770         {
2771                 IsWorking is_working(*this);
2772
2773                 work_area->queue_render_preview();
2774         }
2775 }
2776
2777 void
2778 CanvasView::play()
2779 {
2780         assert(get_canvas());
2781         
2782         // If we are already busy, don't play!
2783         if(working_depth)return;
2784         
2785         // Set us up as working
2786         IsWorking is_working(*this);
2787         
2788         etl::clock timer;
2789         Time
2790                 time=work_area->get_time(),
2791                 endtime=get_canvas()->rend_desc().get_time_end();
2792
2793         // If we are already at the end of time, start over
2794         if(time==endtime)
2795                 time=get_canvas()->rend_desc().get_time_start();
2796         
2797         is_playing_=true;               
2798
2799         work_area->clear_ducks();
2800         
2801         for(timer.reset(); time + timer() < endtime;)
2802         {
2803                 //Clamp the time window so we can see the time value as it races across the horizon
2804                 bool timewindreset = false;
2805                 
2806                 while( time + timer() > Time(time_window_adjustment().get_sub_upper()) )
2807                 {
2808                         time_window_adjustment().set_value(
2809                                         min(
2810                                                 time_window_adjustment().get_value()+time_window_adjustment().get_page_size()/2,
2811                                                 time_window_adjustment().get_upper()-time_window_adjustment().get_page_size() )
2812                                 );
2813                         timewindreset = true;
2814                 }
2815                 
2816                 while( time + timer() < Time(time_window_adjustment().get_sub_lower()) )
2817                 {
2818                         time_window_adjustment().set_value(
2819                                 max(
2820                                         time_window_adjustment().get_value()-time_window_adjustment().get_page_size()/2,
2821                                         time_window_adjustment().get_lower())
2822                         );
2823                         
2824                         timewindreset = true;
2825                 }
2826                 
2827                 //we need to tell people that the value changed
2828                 if(timewindreset) time_window_adjustment().value_changed();
2829                 
2830                 //update actual time to next step
2831                 time_adjustment().set_value(time+timer());
2832                 time_adjustment().value_changed();
2833                 
2834                 if(!work_area->sync_render_preview())
2835                         break;
2836                 
2837                 studio::App::iteration(false);
2838                 
2839                 if(get_cancel_status())
2840                         return;
2841         }
2842         is_playing_=false;              
2843
2844         time_adjustment().set_value(endtime);
2845         time_adjustment().value_changed();
2846 }
2847
2848 void
2849 CanvasView::show_tables()
2850 {
2851 /*
2852         Smach::event_result x(process_event_key(EVENT_TABLES_SHOW));
2853         if(x==Smach::RESULT_OK || x==Smach::RESULT_ACCEPT)
2854         {
2855                 Gtk::IconSize iconsize=Gtk::IconSize::from_name("synfig-small_icon");
2856                 treetogglebutton->remove();
2857                 treetogglebutton->add(*manage(new Gtk::Image(Gtk::StockID("gtk-go-down"),iconsize)));   
2858                 treetogglebutton->show_all();
2859                 notebook->show();
2860         }
2861 */
2862 }
2863
2864 void
2865 CanvasView::hide_tables()
2866 {
2867 /*
2868         Smach::event_result x(process_event_key(EVENT_TABLES_HIDE));
2869         if(x==Smach::RESULT_OK || x==Smach::RESULT_ACCEPT)
2870         {
2871                 Gtk::IconSize iconsize=Gtk::IconSize::from_name("synfig-small_icon");
2872                 treetogglebutton->remove();
2873                 treetogglebutton->add(*manage(new Gtk::Image(Gtk::StockID("gtk-go-up"),iconsize)));     
2874                 treetogglebutton->show_all();
2875                 notebook->hide();
2876         }
2877 */
2878 }
2879
2880 bool
2881 CanvasView::tables_are_visible()
2882 {
2883 //      return notebook->is_visible();
2884         return false;
2885 }
2886
2887 void
2888 CanvasView::toggle_tables()
2889 {
2890 //      if(tables_are_visible())
2891 //              hide_tables();
2892 //      else
2893 //              show_tables();
2894 }
2895
2896 void
2897 CanvasView::do_rotoscope_bline()
2898 {
2899         smach_.enter(&state_bline);
2900 }
2901
2902 void
2903 CanvasView::do_rotoscope()
2904 {
2905         smach_.enter(&state_draw);
2906 }
2907
2908 void
2909 CanvasView::do_rotoscope_poly()
2910 {
2911         smach_.enter(&state_polygon);
2912 }
2913
2914 void
2915 CanvasView::do_eyedrop()
2916 {
2917         smach_.enter(&state_eyedrop);
2918 }
2919
2920 void
2921 CanvasView::show_timebar()
2922 {
2923         timebar->show();
2924         current_time_widget->show();
2925         
2926         //keyframe_tab_child->show();
2927         if(layer_tree)
2928                 layer_tree->set_show_timetrack(true);
2929         if(children_tree)
2930                 children_tree->set_show_timetrack(true);
2931 }
2932
2933 void
2934 CanvasView::hide_timebar()
2935 {
2936         timebar->hide();
2937         current_time_widget->hide();
2938         //keyframe_tab_child->hide();
2939         if(layer_tree)
2940                 layer_tree->set_show_timetrack(false);
2941         if(children_tree)
2942                 children_tree->set_show_timetrack(false);
2943 }
2944
2945
2946
2947 void
2948 CanvasView::on_waypoint_clicked(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint,int button)
2949 {
2950         waypoint_dialog.set_value_desc(value_desc);
2951         waypoint_dialog.set_waypoint(waypoint);
2952
2953         switch(button)
2954         {
2955         case -1:
2956                 waypoint_dialog.show();
2957                 break;
2958         case 2:
2959                 {
2960                         Gtk::Menu* waypoint_menu(manage(new Gtk::Menu()));
2961
2962                         waypoint_menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-jump-to"),
2963                                 sigc::bind(
2964                                         sigc::mem_fun(
2965                                                 *canvas_interface(),
2966                                                 &synfigapp::CanvasInterface::set_time
2967                                         ),
2968                                         waypoint.get_time()
2969                                 )
2970                         ));
2971
2972                         waypoint_menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("Edit Waypoint"),
2973                                 sigc::mem_fun(
2974                                         waypoint_dialog,
2975                                         &Gtk::Widget::show
2976                                 )
2977                         ));
2978                         
2979                         waypoint_menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-duplicate"),
2980                                 sigc::bind(
2981                                         sigc::bind(
2982                                                 sigc::mem_fun(
2983                                                         *canvas_interface(),
2984                                                         &synfigapp::CanvasInterface::waypoint_duplicate
2985                                                 ),
2986                                                 waypoint
2987                                         ),
2988                                         value_desc
2989                                 )
2990                         ));
2991                         waypoint_menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-delete"),
2992                                 sigc::bind(
2993                                         sigc::bind(
2994                                                 sigc::mem_fun(
2995                                                         *canvas_interface(),
2996                                                         &synfigapp::CanvasInterface::waypoint_remove
2997                                                 ),
2998                                                 waypoint
2999                                         ),
3000                                         value_desc
3001                                 )
3002                         ));
3003                         waypoint_menu->popup(button+1,gtk_get_current_event_time());
3004                 }
3005                 break;
3006         default:
3007                 break;
3008         }
3009 }
3010
3011 void
3012 CanvasView::on_waypoint_changed()
3013 {
3014         synfigapp::Action::ParamList param_list;
3015         param_list.add("canvas",get_canvas());
3016         param_list.add("canvas_interface",canvas_interface());
3017         param_list.add("value_node",waypoint_dialog.get_value_desc().get_value_node());
3018         param_list.add("waypoint",waypoint_dialog.get_waypoint());
3019 //      param_list.add("time",canvas_interface()->get_time());
3020
3021         get_instance()->process_action("waypoint_set_smart", param_list);
3022 }
3023
3024 void
3025 CanvasView::on_waypoint_delete()
3026 {
3027         synfigapp::Action::ParamList param_list;
3028         param_list.add("canvas",get_canvas());
3029         param_list.add("canvas_interface",canvas_interface());
3030         param_list.add("value_node",waypoint_dialog.get_value_desc().get_value_node());
3031         param_list.add("waypoint",waypoint_dialog.get_waypoint());
3032 //      param_list.add("time",canvas_interface()->get_time());
3033
3034         get_instance()->process_action("waypoint_remove", param_list);
3035 }
3036
3037 void
3038 CanvasView::on_drop_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& selection_data_, guint info, guint time)
3039 {
3040         // We will make this true once we have a solid drop
3041         bool success(false);
3042         //synfig::info("Droped data of type \"%s\"",selection_data.get_data_type());
3043         //synfig::info("Droped data of target \"%s\"",gdk_atom_name(selection_data->target));
3044         //synfig::info("selection=\"%s\"",gdk_atom_name(selection_data->selection));
3045         
3046         if ((selection_data_.get_length() >= 0) && (selection_data_.get_format() == 8))
3047         {
3048                 if(synfig::String(selection_data_.get_data_type())=="STRING")do
3049                 {
3050                         synfig::String selection_data((gchar *)(selection_data_.get_data()));
3051
3052                         Layer::Handle layer(synfig::Layer::create("Text"));
3053                         if(!layer)
3054                                 break;
3055                         if(!layer->set_param("text",ValueBase(selection_data)))
3056                                 break;
3057
3058                         synfigapp::Action::Handle       action(synfigapp::Action::create("layer_add"));
3059                 
3060                         assert(action);
3061                         if(!action)
3062                                 break;
3063                         
3064                         action->set_param("canvas",get_canvas());
3065                         action->set_param("canvas_interface",canvas_interface());
3066                         action->set_param("new",layer);
3067
3068                         if(!get_instance()->perform_action(action))
3069                                 break;
3070                         
3071                         // Ok, we have successfuly imported at least one item.
3072                         success=true;
3073                 } while(0); // END of "STRING"
3074                 
3075                 if(synfig::String(selection_data_.get_data_type())=="text/plain")
3076                 {
3077                         synfig::String selection_data((gchar *)(selection_data_.get_data()));
3078         
3079                         // For some reason, GTK hands us a list of URL's seperated
3080                         // by not only Carrage-Returns, but also Line-Feeds.
3081                         // Line-Feeds will mess us up. Remove all the line-feeds.
3082                         while(selection_data.find_first_of('\r')!=synfig::String::npos)
3083                                 selection_data.erase(selection_data.begin()+selection_data.find_first_of('\r'));
3084         
3085                         std::stringstream stream(selection_data);
3086         
3087                         //synfigapp::PassiveGrouper group(canvas_interface()->get_instance(),_("Insert Image"));
3088                         while(stream)
3089                         {
3090                                 synfig::String filename,URI;
3091                                 getline(stream,filename);
3092                                 
3093                                 // If we don't have a filename, move on.
3094                                 if(filename.empty())
3095                                         continue;
3096                                 
3097                                 // Make sure this URL is of the "file://" type.
3098                                 URI=String(filename.begin(),filename.begin()+sizeof("file://")-1);
3099                                 if(URI!="file://")
3100                                 {
3101                                         synfig::warning("Unknown URI (%s) in \"%s\"",URI.c_str(),filename.c_str());
3102                                         continue;
3103                                 }
3104                                 
3105                                 // Strip the "file://" part from the filename
3106                                 filename=synfig::String(filename.begin()+sizeof("file://")-1,filename.end());
3107
3108                                 String ext;
3109                                 try{ext=(String(filename.begin()+filename.find_last_of('.')+1,filename.end()));}catch(...){continue;}
3110         
3111                                 // If this is a SIF file, then we need to do things slightly differently
3112                                 if(ext=="sketch")
3113                                 {
3114                                         if(work_area->load_sketch(filename))
3115                                         {
3116                                                 success=true;
3117                                                 work_area->queue_draw();
3118                                         }
3119                                 }
3120                                 else
3121                                 {
3122                                         if(canvas_interface()->import(filename))
3123                                                 success=true;
3124                                 }
3125
3126                                 continue;
3127                         }
3128                 } // END of "text/plain"
3129         }
3130         else
3131                 ui_interface_->error("Drop failed: bad selection data");
3132
3133         // Finish the drag
3134         context->drag_finish(success, false, time);
3135 }
3136
3137 void
3138 CanvasView::on_keyframe_add_pressed()
3139 {
3140         synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_add"));
3141
3142         if(!action)
3143         {
3144                 ui_interface_->error("I am unable to find the appropriate action");
3145                 return;
3146         }               
3147         
3148         action->set_param("canvas",get_canvas());
3149         action->set_param("canvas_interface",canvas_interface());
3150         action->set_param("keyframe",Keyframe(get_time()));
3151
3152         canvas_interface()->get_instance()->perform_action(action);
3153 }
3154
3155 void
3156 CanvasView::on_keyframe_duplicate_pressed()
3157 {
3158         const KeyframeTreeStore::Model model;
3159         const Gtk::TreeRow row(*keyframe_tree->get_selection()->get_selected());
3160         Keyframe keyframe;
3161         if(!row)
3162         {
3163                 ui_interface_->error("I am unable to duplicate the keyframe");
3164                 return;
3165         }               
3166         keyframe=row[model.keyframe];
3167
3168         synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_duplicate"));
3169
3170         if(!action)
3171         {
3172                 ui_interface_->error("I am unable to find the appropriate action");
3173                 return;
3174         }               
3175         
3176         action->set_param("canvas",get_canvas());
3177         action->set_param("canvas_interface",canvas_interface());
3178         action->set_param("keyframe",keyframe);
3179         action->set_param("time",get_time());
3180
3181         canvas_interface()->get_instance()->perform_action(action);
3182 }
3183
3184 void
3185 CanvasView::on_keyframe_remove_pressed()
3186 {
3187         const KeyframeTreeStore::Model model;
3188         const Gtk::TreeRow row(*keyframe_tree->get_selection()->get_selected());
3189         Keyframe keyframe;
3190         if(!row)
3191         {
3192                 ui_interface_->error("I am unable to remove the keyframe");
3193                 return;
3194         }               
3195         keyframe=row[model.keyframe];
3196
3197         synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_remove"));
3198
3199         if(!action)
3200         {
3201                 ui_interface_->error("I am unable to find the appropriate action");
3202                 return;
3203         }               
3204         
3205         action->set_param("canvas",get_canvas());
3206         action->set_param("canvas_interface",canvas_interface());
3207         action->set_param("keyframe",keyframe);
3208
3209         canvas_interface()->get_instance()->perform_action(action);
3210 }
3211
3212
3213 void
3214 CanvasView::toggle_duck_mask(Duckmatic::Type type)
3215 {
3216         bool is_currently_on(work_area->get_type_mask()&type);
3217
3218         switch(type)
3219         {
3220         case Duck::TYPE_POSITION:
3221                 if(duck_mask_position)
3222                         duck_mask_position->set_active(!is_currently_on);
3223                 break;
3224         
3225         case Duck::TYPE_VERTEX:
3226                 if(duck_mask_vertex)
3227                         duck_mask_vertex->set_active(!is_currently_on);
3228                 break;
3229         
3230         case Duck::TYPE_TANGENT:
3231                 if(duck_mask_tangent)
3232                         duck_mask_tangent->set_active(!is_currently_on);
3233                 break;
3234         
3235         case Duck::TYPE_RADIUS:
3236                 if(duck_mask_radius)
3237                         duck_mask_radius->set_active(!is_currently_on);
3238                 break;
3239         
3240         case Duck::TYPE_WIDTH:
3241                 if(duck_mask_width)
3242                         duck_mask_width->set_active(!is_currently_on);
3243                 break;
3244
3245         case Duck::TYPE_ANGLE:
3246                 if(duck_mask_angle)
3247                         duck_mask_angle->set_active(!is_currently_on);
3248                 break;
3249
3250         default:
3251                 synfig::warning("CanvasView::toggle_duck_mask():Unknown duck type!");
3252                 break;
3253         }
3254
3255         if(is_currently_on)
3256                 work_area->set_type_mask(work_area->get_type_mask()-type);
3257         else
3258                 work_area->set_type_mask(work_area->get_type_mask()|type);
3259         
3260         work_area->queue_draw();
3261 }
3262
3263
3264 void
3265 CanvasView::image_import()
3266 {
3267         String filename(dirname(get_canvas()->get_file_name()));
3268         if(App::dialog_open_file(_("Import Image"), filename))
3269                 canvas_interface()->import(filename);
3270 }
3271
3272 Smach::event_result
3273 CanvasView::process_event_key(EventKey x)
3274 {
3275         return smach_.process_event(x);
3276 }
3277
3278 void
3279 CanvasView::on_input_device_changed(GdkDevice* device)
3280 {
3281         if(!device)
3282         {
3283                 get_smach().egress();
3284         }
3285         assert(device);
3286         
3287         synfigapp::InputDevice::Handle input_device;
3288         input_device=synfigapp::Main::select_input_device(device->name);
3289         App::toolbox->change_state(input_device->get_state());
3290         process_event_key(EVENT_INPUT_DEVICE_CHANGED);
3291 }
3292
3293 void
3294 CanvasView::on_preview_option()
3295 {
3296         Dialog_PreviewOptions *po = dynamic_cast<Dialog_PreviewOptions *>(get_ext_widget("prevoptions"));
3297         
3298         Canvas::Handle  canv = get_canvas();
3299         
3300         if(canv)
3301         {
3302                 RendDesc &r = canv->rend_desc();
3303                 if(r.get_frame_rate())
3304                 {
3305                         float rate = 1/r.get_frame_rate();
3306                         float beg = r.get_time_start() + r.get_frame_start()*rate;
3307                         float end = r.get_time_start() + r.get_frame_end()*rate;
3308
3309                         if(!po)
3310                         {
3311                                 po = new Dialog_PreviewOptions;
3312                                 po->set_zoom(work_area->get_zoom()/2);
3313                                 po->set_fps(r.get_frame_rate()/2);
3314                                 po->set_begintime(beg);
3315                                 po->set_begin_override(false);
3316                                 po->set_endtime(end);
3317                                 po->set_end_override(false);
3318                                 
3319                                 set_ext_widget("prevoptions",po);
3320                         }
3321                         /*po->set_zoom(work_area->get_zoom()/2);
3322                         po->set_fps(r.get_frame_rate()/2);
3323                         po->set_begintime(beg);
3324                         po->set_begin_override(false);
3325                         po->set_endtime(end);
3326                         po->set_end_override(false);*/
3327                         
3328                         po->set_global_fps(r.get_frame_rate());
3329                         po->signal_finish().connect(sigc::mem_fun(*this,&CanvasView::on_preview_create));
3330                         po->present();
3331                 }
3332         }
3333 }
3334
3335 void
3336 CanvasView::on_preview_create(const PreviewInfo &info)
3337 {       
3338         //set all the options
3339         etl::handle<Preview>    prev = new Preview;
3340         
3341         prev->set_canvasview(this);
3342         prev->set_zoom(info.zoom);
3343         prev->set_fps(info.fps);
3344         prev->set_overbegin(info.overbegin);
3345         prev->set_begintime(info.begintime);
3346         prev->set_overend(info.overend);
3347         prev->set_endtime(info.endtime);
3348         prev->set_quality(work_area->get_quality());
3349
3350         //render it out...
3351         prev->render();
3352         
3353         Dialog_Preview *pd = preview_dialog.get();
3354         assert(pd);
3355         
3356         pd->set_preview(prev.get());
3357         pd->present();
3358 }
3359
3360 void
3361 CanvasView::on_audio_option()
3362 {
3363         synfig::warning("Launching Audio Options");
3364         sound_dialog->set_global_fps(get_canvas()->rend_desc().get_frame_rate());
3365         sound_dialog->present();
3366 }
3367
3368 void
3369 CanvasView::on_audio_file_change(const std::string &f)
3370 {       
3371         //save in meta data - always even when not valid...
3372         canvas_interface()->set_meta_data("audiofile",f);
3373 }
3374
3375 void
3376 CanvasView::on_audio_offset_change(const Time &t)
3377 {
3378         canvas_interface()->set_meta_data("audiooffset",t.get_string());
3379 }
3380
3381 void
3382 CanvasView::on_audio_file_notify()
3383 {
3384         std::string file(get_canvas()->get_meta_data("audiofile"));
3385         if(!file.c_str()) return;
3386         
3387         if(!audio->load(file,dirname(get_canvas()->get_file_name())+string("/")))
3388         {
3389                 if(file != "") synfig::warning("Could not load the file: %s", file.c_str());
3390                 get_canvas()->erase_meta_data("audiofile");
3391                 disp_audio->hide();
3392                 disp_audio->set_profile(etl::handle<AudioProfile>());
3393         }else
3394         {
3395                 //save in canvasview            
3396                 synfig::warning("Getting the profile of the music stuff");
3397                 
3398                 //profile specific stuff for the preview widget
3399                 //similar for other attachments
3400                 Dialog_Preview *pd = preview_dialog.get();
3401                 pd->get_widget().set_audio(audio);
3402                 
3403                 handle<AudioProfile>    prof = audio->get_profile();
3404                 
3405                 if(!prof)
3406                 {
3407                         synfig::warning("Agh, I couldn't build the profile captain!");                  
3408                 }
3409                 pd->get_widget().set_audioprofile(prof);
3410                 
3411                 disp_audio->set_profile(audio->get_profile());
3412                 disp_audio->show();
3413                 
3414                 synfig::warning("successfully set the profiles and stuff");
3415         }
3416         disp_audio->queue_draw();
3417 }
3418
3419 void
3420 CanvasView::on_audio_offset_notify()
3421 {
3422         Time t(get_canvas()->get_meta_data("audiooffset"),get_canvas()->rend_desc().get_frame_rate());
3423         audio->set_offset(t);
3424         sound_dialog->set_offset(t);
3425         disp_audio->queue_draw();
3426         
3427         synfig::info("CanvasView::on_audio_offset_notify(): offset time set to %s",t.get_string(get_canvas()->rend_desc().get_frame_rate()).c_str());
3428 }
3429
3430 void
3431 CanvasView::play_audio(float t)
3432 {
3433         if(audio.get())
3434         {
3435                 synfig::info("Playing audio at %f s",t);
3436                 audio->play(t);
3437         }               
3438 }
3439
3440 void
3441 CanvasView::stop_audio()
3442 {
3443         if(audio.get())
3444         {
3445                 audio->stop();
3446         }
3447 }
3448
3449 bool
3450 CanvasView::on_audio_scrub()
3451 {
3452         disp_audio->draw();
3453         return true;
3454 }
3455
3456
3457
3458 Glib::RefPtr<Glib::ObjectBase>
3459 CanvasView::get_ref_obj(const synfig::String& x)
3460 {
3461         return ref_obj_book_[x];
3462 }
3463
3464 Glib::RefPtr<const Glib::ObjectBase>
3465 CanvasView::get_ref_obj(const synfig::String& x)const
3466 {
3467         return ref_obj_book_.find(x)->second;
3468 }
3469
3470 void
3471 CanvasView::set_ref_obj(const synfig::String& x, Glib::RefPtr<Glib::ObjectBase> y)
3472 {
3473         ref_obj_book_[x]=y;
3474 }
3475
3476 Glib::RefPtr<Gtk::TreeModel>
3477 CanvasView::get_tree_model(const synfig::String& x)
3478 {
3479         return Glib::RefPtr<Gtk::TreeModel>::cast_dynamic(ref_obj_book_["_tree_model_"+x]);
3480 }
3481
3482 Glib::RefPtr<const Gtk::TreeModel>
3483 CanvasView::get_tree_model(const synfig::String& x)const
3484 {
3485         return Glib::RefPtr<Gtk::TreeModel>::cast_dynamic(ref_obj_book_.find("_tree_model_"+x)->second);
3486 }
3487
3488 void
3489 CanvasView::set_tree_model(const synfig::String& x, Glib::RefPtr<Gtk::TreeModel> y)
3490 {
3491         ref_obj_book_["_tree_model_"+x]=Glib::RefPtr<Glib::ObjectBase>::cast_static(y);
3492 }
3493
3494 Gtk::Widget*
3495 CanvasView::get_ext_widget(const synfig::String& x)
3496 {
3497         return ext_widget_book_[x];
3498 }
3499
3500 void
3501 CanvasView::set_ext_widget(const synfig::String& x, Gtk::Widget* y)
3502 {
3503         ext_widget_book_[x]=y;
3504         if(x=="layers_cmp")
3505         {
3506                 layer_tree=dynamic_cast<LayerTree*>(y);
3507
3508                 layer_tree->get_selection()->signal_changed().connect(SLOT_EVENT(EVENT_LAYER_SELECTION_CHANGED));
3509                 layer_tree->get_selection()->signal_changed().connect(SLOT_EVENT(EVENT_REFRESH_DUCKS));
3510                 layer_tree->signal_layer_user_click().connect(sigc::mem_fun(*this, &studio::CanvasView::on_layer_user_click));
3511                 layer_tree->signal_param_user_click().connect(sigc::mem_fun(*this, &studio::CanvasView::on_children_user_click));
3512                 layer_tree->signal_waypoint_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_waypoint_clicked));
3513         }
3514         if(x=="children")
3515         {
3516                 children_tree=dynamic_cast<ChildrenTree*>(y);
3517                 if(children_tree)children_tree->signal_user_click().connect(sigc::mem_fun(*this, &studio::CanvasView::on_children_user_click));
3518                 if(children_tree)children_tree->signal_waypoint_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_waypoint_clicked));
3519                 if(children_tree)children_tree->get_selection()->signal_changed().connect(SLOT_EVENT(EVENT_REFRESH_DUCKS));
3520         }
3521         if(x=="keyframes")
3522                 keyframe_tree=dynamic_cast<KeyframeTree*>(y);
3523 }
3524
3525 static bool _close_instance(etl::handle<Instance> instance)
3526 {
3527         etl::handle<Instance> argh(instance);
3528         instance->safe_close();
3529         synfig::info("closed");
3530         return false;
3531 }
3532
3533 bool
3534 CanvasView::on_delete_event(GdkEventAny* event)
3535 {
3536         if(get_instance()->get_visible_canvases()==1)
3537         {
3538                 // Schedule a close to occur in a few moments
3539                 Glib::signal_timeout().connect(
3540                         sigc::bind(
3541                                 sigc::ptr_fun(_close_instance),
3542                                 (etl::handle<Instance>)get_instance()
3543                         )
3544                         ,250
3545                 );
3546         }
3547         if(event)
3548                 return Gtk::Window::on_delete_event(event);
3549         
3550         return true;
3551 }