Added "--with-userdir" to configure script to change the default user settings directory
[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", _("Mask "#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         hide();
1950         // I think this is bad..., removing
1951         //      get_instance()->canvas_view_list().erase(std::find(get_instance()->canvas_view_list().begin(),get_instance()->canvas_view_list().end(),this));
1952         return false;
1953 }
1954
1955 handle<CanvasView>
1956 CanvasView::create(loose_handle<Instance> instance,handle<Canvas> canvas)
1957 {
1958         etl::handle<studio::CanvasView> view(new CanvasView(instance,instance->synfigapp::Instance::find_canvas_interface(canvas)));
1959         instance->canvas_view_list().push_front(view);
1960         instance->signal_canvas_view_created()(view.get());
1961         return view;
1962 }
1963
1964 void
1965 CanvasView::update_title()
1966 {
1967         string title;
1968
1969         title+=etl::basename(get_instance()->get_file_name())
1970                 +" : ";
1971         if(get_canvas()->get_name().empty())
1972                 title+='"'+get_canvas()->get_id()+'"';
1973         else
1974                 title+='"'+get_canvas()->get_name()+'"';
1975
1976         if(get_instance()->synfigapp::Instance::get_action_count())
1977                 title+=_(" (Unsaved)");
1978
1979         if(get_instance()->synfigapp::Instance::in_repository())
1980         {
1981                 title+=" (CVS";
1982                 if(get_instance()->synfigapp::Instance::is_modified())
1983                         title+=_("-MODIFIED");
1984                 if(get_instance()->synfigapp::Instance::is_updated())
1985                         title+=_("-UPDATED");
1986                 title+=')';
1987         }
1988
1989         if(get_canvas()->is_root())
1990                 title+=_(" (Root)");
1991
1992         set_title(title);
1993 }
1994
1995
1996 void
1997 CanvasView::on_hide()
1998 {
1999         smach_.egress();
2000         Gtk::Window::on_hide();
2001 }
2002
2003 void
2004 CanvasView::present()
2005 {
2006         grab_focus();//on_focus_in_event(0);
2007         Gtk::Window::present();
2008 }
2009
2010 bool
2011 CanvasView::on_focus_in_event(GdkEventFocus*x)
2012 {
2013         if(studio::App::get_selected_canvas_view()!=this)
2014         {
2015                 if(studio::App::get_selected_canvas_view())
2016                 {
2017                         studio::App::get_selected_canvas_view()->get_smach().process_event(EVENT_YIELD_TOOL_OPTIONS);
2018                         App::ui_manager()->remove_action_group(App::get_selected_canvas_view()->action_group);
2019                 }
2020
2021                 get_smach().process_event(EVENT_REFRESH_TOOL_OPTIONS);
2022
2023                 studio::App::set_selected_canvas_view(this);
2024         
2025                 App::ui_manager()->insert_action_group(action_group);
2026         }
2027         
2028         // HACK ... Questionable...?
2029         if(x)
2030                 return Gtk::Window::on_focus_in_event(x);
2031         
2032         return true;
2033 }
2034
2035 bool
2036 CanvasView::on_focus_out_event(GdkEventFocus*x)
2037 {
2038         //App::ui_manager()->remove_action_group(action_group);
2039         //App::ui_manager()->ensure_update();
2040         return Gtk::Window::on_focus_out_event(x);
2041 }
2042
2043 void
2044 CanvasView::refresh_tables()
2045 {
2046 //      if(layer_tree_store_)layer_tree_store_->refresh();
2047 //      if(children_tree_store_)children_tree_store_->refresh();
2048 }
2049
2050 void
2051 CanvasView::rebuild_tables()
2052 {
2053 //      layer_tree_store_->rebuild();
2054 //      children_tree_store_->rebuild();
2055 }
2056
2057 void
2058 CanvasView::build_tables()
2059 {
2060 //      layer_tree_store_->rebuild();
2061 //      children_tree_store_->rebuild();
2062 }
2063
2064 void
2065 CanvasView::on_layer_toggle(synfig::Layer::Handle layer)
2066 {
2067         synfigapp::Action::Handle action(synfigapp::Action::create("layer_activate"));
2068         assert(action);
2069         
2070         if(!action)
2071                 return;
2072         
2073         action->set_param("canvas",Canvas::Handle(layer->get_canvas()));
2074         if(!action->set_param("canvas_interface",canvas_interface()))
2075 //      if(!action->set_param("canvas_interface",get_instance()->find_canvas_interface(layer->get_canvas())))
2076                 synfig::error("LayerActivate didn't like CanvasInterface...?");
2077         action->set_param("time",get_time());
2078         action->set_param("layer",layer);
2079         action->set_param("new_status",!layer->active());
2080
2081         assert(action->is_ready());
2082         
2083         canvas_interface()->get_instance()->perform_action(action);
2084 }
2085
2086
2087 void
2088 CanvasView::popup_param_menu(synfigapp::ValueDesc value_desc, float location)
2089 {
2090         parammenu.items().clear();
2091         get_instance()->make_param_menu(&parammenu,get_canvas(),value_desc,location);
2092         
2093         parammenu.popup(3,gtk_get_current_event_time());
2094 }
2095
2096 void
2097 CanvasView::add_actions_to_menu(Gtk::Menu *menu, const synfigapp::Action::ParamList &param_list,synfigapp::Action::Category category)const
2098 {
2099         get_instance()->add_actions_to_menu(menu, param_list, category);
2100 }
2101
2102 bool
2103 CanvasView::on_layer_user_click(int button, Gtk::TreeRow row, LayerTree::ColumnID column_id)
2104 {       
2105         switch(button)
2106         {
2107         case 3:
2108                 {
2109
2110                         Gtk::MenuItem* menu = dynamic_cast<Gtk::MenuItem*>(App::ui_manager()->get_widget("/menu-main/menu-layer"));
2111                         if(menu && menu->get_submenu())
2112                         {
2113                                 //menu->set_accel_group(App::ui_manager()->get_accel_group());
2114                                 //menu->accelerate(*this);
2115                                 menu->get_submenu()->popup(button,gtk_get_current_event_time());
2116                         }
2117
2118                         
2119                         #if 0
2120                         bool multiple_selected=true;
2121                         
2122                         if(layer_tree->get_selection()->count_selected_rows()<=1)
2123                                 multiple_selected=false;
2124
2125                         // If the clicked row is not selected, then unselect
2126                         // everything that isn't selected and select this row
2127                         if(multiple_selected && !layer_tree->get_selection()->is_selected(row))
2128                         {
2129                                 layer_tree->get_selection()->unselect_all();
2130                                 layer_tree->get_selection()->select(row);
2131                                 multiple_selected=false;
2132                         }
2133                         
2134                         if(column_id==COLUMNID_TIME_TRACK)
2135                                 return false;
2136                         
2137                         //synfigapp::ValueDesc value_desc(row[layer_param_tree_model.value_desc]);
2138                         //ValueNode::Handle value_node(row[layer_param_tree_model.value_node]);
2139                         //ValueNode::Handle parent_value_node;
2140                         //ValueBase value=row[layer_param_tree_model.value];
2141                         
2142                         //if(row.parent())
2143                         //{
2144                         //      parent_value_node=(*row.parent())[layer_tree_model.value_node];
2145                         //}
2146
2147                         {
2148                                 Layer::Handle layer(row[layer_tree_model.layer]);
2149                                 synfigapp::Action::ParamList param_list;
2150                                 param_list.add("time",canvas_interface()->get_time());
2151                                 param_list.add("canvas",Canvas::Handle(row[layer_tree_model.canvas]));
2152                                 param_list.add("canvas_interface",canvas_interface());
2153                                 if(!multiple_selected)
2154                                         param_list.add("layer",layer);
2155                                 else
2156                                 {
2157                                         synfigapp::SelectionManager::LayerList layer_list(get_selection_manager()->get_selected_layers());
2158                                         synfigapp::SelectionManager::LayerList::iterator iter;
2159                                 
2160                                         for(iter=layer_list.begin();iter!=layer_list.end();++iter)
2161                                                 param_list.add("layer",Layer::Handle(*iter));
2162                                 }
2163                                 
2164                                 parammenu.items().clear();
2165
2166                                 Gtk::Menu *newlayers(manage(new Gtk::Menu()));
2167                                 build_new_layer_menu(*newlayers);
2168                                 
2169                                 parammenu.items().push_back(Gtk::Menu_Helpers::MenuElem("New Layer",*newlayers));
2170                                 if(!multiple_selected && layer->get_name()=="PasteCanvas")
2171                                 {
2172                                         parammenu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Select All Children"),
2173                                                 sigc::bind(
2174                                                         sigc::mem_fun(
2175                                                                 *layer_tree,
2176                                                                 &studio::LayerTree::select_all_children_layers
2177                                                         ),
2178                                                         layer
2179                                                 )
2180                                         ));
2181                                 }
2182                                 
2183                                 add_actions_to_menu(&parammenu, param_list,synfigapp::Action::CATEGORY_LAYER);
2184                                 parammenu.popup(button,gtk_get_current_event_time());
2185                                 return true;
2186                         }
2187 /*
2188                         else if(column_id==LayerTree::COLUMNID_TIME_TRACK && value_node && handle<synfig::ValueNode_Animated>::cast_dynamic(value_node))
2189                         {                                       
2190                                 // Right-click on time track with animated
2191 //                              trackmenu.popup(0,0);
2192                                 return true;
2193                         }
2194                         else
2195                         {
2196                                 if(!multiple_selected)
2197                                 {
2198                                         popup_param_menu(value_desc);
2199                                         return true;
2200                                 }
2201                                 else
2202                                 {
2203 #warning update me!
2204 #if 0
2205                                         parammenu.items().clear();
2206                                         parammenu.items().push_back(Gtk::Menu_Helpers::MenuElem("Connect",
2207                                                 hide_return(sigc::mem_fun(*canvas_interface().get(),&synfigapp::CanvasInterface::connect_selected_layer_params))
2208                                         ));
2209                                         parammenu.items().push_back(Gtk::Menu_Helpers::MenuElem("Disconnect",
2210                                                 hide_return(sigc::mem_fun(*canvas_interface().get(),&synfigapp::CanvasInterface::disconnect_selected_layer_params))
2211                                         ));
2212                                         parammenu.popup(0,0);
2213 #endif
2214                                 }
2215                                 return true;
2216                         }
2217                 */
2218 #endif
2219 }
2220                 return true;
2221                 break;
2222                 
2223         default:
2224                 return false;
2225                 break;          
2226         }
2227 }
2228
2229
2230
2231 bool
2232 CanvasView::on_children_user_click(int button, Gtk::TreeRow row, ChildrenTree::ColumnID column_id)
2233 {       
2234         switch(button)
2235         {
2236         case 3:
2237                 {
2238                         if(column_id==COLUMNID_TIME_TRACK)
2239                                 return false;
2240                         if(!(bool)row[children_tree_model.is_canvas])
2241                         {
2242                                 synfigapp::ValueDesc value_desc=row[children_tree_model.value_desc];
2243                                 assert(value_desc);
2244                                 popup_param_menu(value_desc);
2245                                 return true;
2246                         }
2247                 }
2248                 return true;
2249                 break;
2250                 
2251         default:
2252                 return false;
2253                 break;          
2254         }
2255 }
2256
2257 bool
2258 CanvasView::on_keyframe_tree_event(GdkEvent *event)
2259 {
2260     switch(event->type)
2261     {
2262         case GDK_BUTTON_PRESS:
2263                 switch(event->button.button)
2264                 {
2265                         case 3: 
2266                         {
2267                                 //keyframemenu.popup(event->button.button,gtk_get_current_event_time());
2268                                 return true;
2269                         }
2270                         break;
2271                 }
2272                 break;
2273         case GDK_MOTION_NOTIFY:
2274                 break;
2275         case GDK_BUTTON_RELEASE:
2276                 break;
2277         default:
2278                 break;
2279         }
2280         return false;
2281 }
2282
2283
2284 void
2285 CanvasView::refresh_time_window()
2286 {
2287         //THESE SHOULD AUTOMATICALLY BE TAKEN CARE OF
2288         //time_adjustment().set_lower(time_window_adjustment().get_value());
2289         //time_adjustment().set_upper(time_window_adjustment().get_value()+time_window_adjustment().get_page_size());
2290         
2291         time_adjustment().set_page_increment(1.0); // One second
2292         time_adjustment().set_page_size(0);
2293         
2294         if(get_canvas())
2295                 time_adjustment().set_step_increment(1.0/get_canvas()->rend_desc().get_frame_rate());
2296         time_adjustment().changed();
2297         
2298         //NOTE THIS SHOULD HOOK INTO THE CORRECT SIGNALS...
2299         if(children_tree)
2300                 children_tree->queue_draw();
2301 }
2302
2303 void
2304 CanvasView::on_time_changed()
2305 {
2306         Time time(get_time());
2307                 
2308         current_time_widget->set_value(time);
2309         try {
2310                 get_canvas()->keyframe_list().find(time);
2311                 current_time_widget->modify_text(Gtk::STATE_NORMAL,Gdk::Color("#FF0000"));
2312         }catch(...){
2313                 current_time_widget->modify_text(Gtk::STATE_NORMAL,Gdk::Color("#000000"));              
2314         }
2315         
2316         if(get_time() != time_adjustment().get_value())
2317         {
2318                 
2319                 //Recenters the window, causing it to jump (possibly undesirably... but whatever)
2320                 if(time < time_window_adjustment().get_value() ||
2321                         time > time_window_adjustment().get_value()+time_window_adjustment().get_page_size())
2322                 {
2323                         time_window_adjustment().set_value(
2324                                 time-time_window_adjustment().get_page_size()/2
2325                         );
2326                 }
2327                 time_adjustment().set_value(time);
2328                 time_adjustment().value_changed();
2329                 
2330                 // Shouldn't these trees just hook into
2331                 // the time changed signal...?
2332                 //YES THEY SHOULD...
2333                 if(layer_tree)layer_tree->queue_draw();
2334                 if(children_tree)children_tree->queue_draw();
2335         }
2336 }
2337
2338 void
2339 CanvasView::time_zoom_in()
2340 {
2341         time_window_adjustment().set_page_size(time_window_adjustment().get_page_size()*0.75);
2342         time_window_adjustment().changed();
2343         
2344         refresh_time_window();
2345 }
2346
2347 void
2348 CanvasView::time_zoom_out()
2349 {
2350         time_window_adjustment().set_page_size(time_window_adjustment().get_page_size()/0.75);
2351         time_window_adjustment().changed();
2352         
2353         refresh_time_window();
2354 }
2355
2356 void
2357 CanvasView::time_was_changed()
2358 {
2359         synfig::Time time((synfig::Time)(double)time_adjustment().get_value());
2360         set_time(time);
2361 }
2362
2363 void
2364 CanvasView::on_edited_value(synfigapp::ValueDesc value_desc,synfig::ValueBase new_value)
2365 {
2366         canvas_interface()->change_value(value_desc,new_value);
2367 }
2368
2369 /*
2370 void
2371 CanvasView::on_children_edited_value(const Glib::ustring&path_string,synfig::ValueBase value)
2372 {
2373         Gtk::TreePath path(path_string);
2374         
2375         const Gtk::TreeRow row = *(children_tree->get_model()->get_iter(path));
2376
2377         assert((bool)row[children_tree_model.is_value_node]);
2378
2379         synfigapp::ValueDesc value_desc=row[children_tree_model.value_desc];
2380         assert(value_desc);
2381
2382         on_edited_value(value_desc,value);
2383 }
2384 */
2385
2386 void
2387 CanvasView::on_id_changed()
2388 {
2389         update_title();
2390 }
2391
2392
2393 void
2394 CanvasView::on_mode_changed(synfigapp::CanvasInterface::Mode mode)
2395 {
2396         // If the aninimate flag was set in mode...
2397         if(mode&synfigapp::MODE_ANIMATE)
2398         {
2399                 Gtk::Image *icon;
2400                 icon=manage(new Gtk::Image(Gtk::StockID("gtk-no"),Gtk::ICON_SIZE_BUTTON));
2401                 animatebutton->remove();        
2402                 animatebutton->add(*icon);      
2403                 tooltips.set_tip(*animatebutton,_("In Animate Editing Mode"));
2404                 icon->set_padding(0,0);
2405                 icon->show();
2406         }
2407         else
2408         {
2409                 Gtk::Image *icon;
2410                 icon=manage(new Gtk::Image(Gtk::StockID("gtk-yes"),Gtk::ICON_SIZE_BUTTON));
2411                 animatebutton->remove();        
2412                 animatebutton->add(*icon);      
2413                 tooltips.set_tip(*animatebutton,_("Not in Animate Editing Mode"));
2414                 icon->set_padding(0,0);
2415                 icon->show();
2416         }
2417
2418         if((mode&synfigapp::MODE_ANIMATE_FUTURE) && (mode&synfigapp::MODE_ANIMATE_PAST))
2419         {
2420                 Gtk::Image *icon;
2421                 icon=manage(new Gtk::Image(Gtk::StockID("synfig-keyframe_lock_all"),Gtk::ICON_SIZE_BUTTON));
2422                 keyframebutton->remove();       
2423                 keyframebutton->add(*icon);     
2424                 tooltips.set_tip(*keyframebutton,_("All Keyframes Locked"));
2425                 icon->set_padding(0,0);
2426                 icon->show();
2427         }
2428         else if((mode&synfigapp::MODE_ANIMATE_FUTURE) && !(mode&synfigapp::MODE_ANIMATE_PAST))
2429         {
2430                 Gtk::Image *icon;
2431                 icon=manage(new Gtk::Image(Gtk::StockID("synfig-keyframe_lock_future"),Gtk::ICON_SIZE_BUTTON));
2432                 keyframebutton->remove();       
2433                 keyframebutton->add(*icon);     
2434                 tooltips.set_tip(*keyframebutton,_("Future Keyframes Locked"));
2435                 icon->set_padding(0,0);
2436                 icon->show();
2437         }
2438         else if(!(mode&synfigapp::MODE_ANIMATE_FUTURE) && (mode&synfigapp::MODE_ANIMATE_PAST))
2439         {
2440                 Gtk::Image *icon;
2441                 icon=manage(new Gtk::Image(Gtk::StockID("synfig-keyframe_lock_past"),Gtk::ICON_SIZE_BUTTON));
2442                 keyframebutton->remove();       
2443                 keyframebutton->add(*icon);     
2444                 tooltips.set_tip(*keyframebutton,_("Past Keyframes Locked"));
2445                 icon->set_padding(0,0);
2446                 icon->show();
2447         }
2448         else if(!(mode&synfigapp::MODE_ANIMATE_FUTURE) && !(mode&synfigapp::MODE_ANIMATE_PAST))
2449         {
2450                 Gtk::Image *icon;
2451                 icon=manage(new Gtk::Image(Gtk::StockID("synfig-keyframe_lock_none"),Gtk::ICON_SIZE_BUTTON));
2452                 keyframebutton->remove();       
2453                 keyframebutton->add(*icon);     
2454                 tooltips.set_tip(*keyframebutton,_("No Keyframes Locked"));
2455                 icon->set_padding(0,0);
2456                 icon->show();
2457         }
2458         
2459         work_area->queue_draw();
2460 }
2461
2462 void
2463 CanvasView::on_animate_button_pressed()
2464 {
2465         if(get_mode()&synfigapp::MODE_ANIMATE)
2466                 set_mode(get_mode()-synfigapp::MODE_ANIMATE);
2467         else
2468                 set_mode(get_mode()|synfigapp::MODE_ANIMATE);
2469 }
2470
2471 void
2472 CanvasView::on_keyframe_button_pressed()
2473 {
2474         synfigapp::CanvasInterface::Mode mode(get_mode());
2475         
2476         if((mode&synfigapp::MODE_ANIMATE_FUTURE) && (mode&synfigapp::MODE_ANIMATE_PAST))
2477         {
2478                 set_mode(get_mode()-synfigapp::MODE_ANIMATE_FUTURE);
2479         }
2480         else if(!(mode&synfigapp::MODE_ANIMATE_FUTURE) && (mode&synfigapp::MODE_ANIMATE_PAST))
2481         {
2482                 set_mode(get_mode()-synfigapp::MODE_ANIMATE_PAST|synfigapp::MODE_ANIMATE_FUTURE);
2483         }
2484         else if((mode&synfigapp::MODE_ANIMATE_FUTURE) && !(mode&synfigapp::MODE_ANIMATE_PAST))
2485         {
2486                 set_mode(get_mode()-synfigapp::MODE_ANIMATE_FUTURE);
2487         }
2488         else if(!(mode&synfigapp::MODE_ANIMATE_FUTURE) && !(mode&synfigapp::MODE_ANIMATE_PAST))
2489         {
2490                 set_mode(get_mode()|synfigapp::MODE_ANIMATE_FUTURE|synfigapp::MODE_ANIMATE_PAST);
2491         }
2492 }
2493
2494 bool
2495 CanvasView::duck_change_param(const Point &value,synfig::Layer::Handle layer, synfig::String param_name)
2496 {
2497         return canvas_interface()->change_value(synfigapp::ValueDesc(layer,param_name),value);
2498 }
2499
2500 bool
2501 CanvasView::on_duck_changed(const synfig::Point &value,const synfigapp::ValueDesc& value_desc)
2502 {
2503         switch(value_desc.get_value_type())
2504         {
2505         case ValueBase::TYPE_REAL:
2506                 return canvas_interface()->change_value(value_desc,value.mag());
2507                 break;
2508         case ValueBase::TYPE_ANGLE:
2509                 return canvas_interface()->change_value(value_desc,Angle::tan(value[1],value[0]));
2510                 break;
2511         default:
2512                 return canvas_interface()->change_value(value_desc,value);
2513                 break;
2514         }
2515
2516         return true;
2517 }
2518
2519 void
2520 CanvasView::selected_layer_color_set(Color color)
2521 {
2522         synfigapp::SelectionManager::LayerList selected_list(get_selection_manager()->get_selected_layers());
2523         synfigapp::SelectionManager::LayerList::iterator iter;
2524
2525         // Create the action group
2526         //synfigapp::PassiveGrouper group(canvas_interface()->get_instance(),_("Set Colors"));
2527
2528         Layer::Handle layer;
2529         for(iter=selected_list.begin();iter!=selected_list.end();++iter)
2530         {
2531                 if(*iter==layer)
2532                         continue;
2533                 layer=*iter;
2534                 on_edited_value(synfigapp::ValueDesc(layer,"color"),color);
2535         }
2536 }
2537
2538 void
2539 CanvasView::rebuild_ducks_layer_(synfig::TransformStack& transform_stack, Canvas::Handle canvas, std::set<synfig::Layer::Handle>& selected_list)
2540 {
2541         int transforms(0);
2542         String layer_name;
2543                 
2544 #define QUEUE_REBUILD_DUCKS             sigc::mem_fun(*this,&CanvasView::queue_rebuild_ducks)   
2545
2546         if(!canvas)
2547         {
2548                 synfig::warning("CanvasView::rebuild_ducks_layer_(): Layer doesn't have canvas set");
2549                 return;
2550         }
2551         for(Canvas::iterator iter(canvas->begin());iter!=canvas->end();++iter)
2552         {
2553                 Layer::Handle layer(*iter);
2554
2555                 if(selected_list.count(layer))
2556                 {
2557                         if(!curr_transform_stack_set)
2558                         {
2559                                 curr_transform_stack_set=true;
2560                                 curr_transform_stack=transform_stack;
2561                         }
2562
2563                         // This layer is currently selected.
2564                         duck_changed_connections.push_back(layer->signal_changed().connect(QUEUE_REBUILD_DUCKS));
2565                         
2566                         // do the bounding box thing
2567                         bbox|=transform_stack.perform(layer->get_bounding_rect());
2568                         
2569                         // Grab the layer's list pf parameters
2570                         Layer::ParamList paramlist(layer->get_param_list());
2571                 
2572                         // Grab the layer vocabulary
2573                         Layer::Vocab vocab=layer->get_param_vocab();
2574                         Layer::Vocab::iterator iter;
2575                                 
2576                         for(iter=vocab.begin();iter!=vocab.end();iter++)
2577                         {
2578                                 if(!iter->get_hidden() && !iter->get_invisible_duck())
2579                                 {
2580                                         synfigapp::ValueDesc value_desc(layer,iter->get_name());
2581                                         work_area->add_to_ducks(value_desc,this,transform_stack,&*iter);
2582                                         if(value_desc.is_value_node())
2583                                                 duck_changed_connections.push_back(value_desc.get_value_node()->signal_changed().connect(QUEUE_REBUILD_DUCKS));
2584                                 }
2585                                 if(iter->get_name()=="color")
2586                                 {
2587                                         /*
2588                                         if(!App::dialog_color->busy())
2589                                         {
2590                                                 App::dialog_color->reset();
2591                                                 App::dialog_color->set_color(layer->get_param("color").get(Color()));
2592                                                 App::dialog_color->signal_edited().connect(
2593                                                         sigc::mem_fun(
2594                                                                 *this,
2595                                                                 &studio::CanvasView::selected_layer_color_set
2596                                                         )
2597                                                 );              
2598                                         }
2599                                         */
2600                                 }
2601                         }
2602                 }
2603                 
2604                 layer_name=layer->get_name();
2605                 
2606                 if(layer->active())
2607                 {
2608                         Transform::Handle trans(layer->get_transform());
2609                         if(trans)
2610                         {
2611                                 transform_stack.push(trans);
2612                                 transforms++;
2613                         }
2614
2615 /*                      // Add transforms onto the stack
2616                         if(layer_name=="Translate")
2617                         {
2618                                 transform_stack.push(synfig::Transform_Translate(layer->get_param("origin").get(Vector())));
2619                                 transforms++;
2620                         }else
2621                         if(layer_name=="Zoom")
2622                         {
2623                                 Vector scale;
2624                                 scale[0]=scale[1]=exp(layer->get_param("amount").get(Real()));
2625                                 transform_stack.push(synfig::Transform_Scale(scale,layer->get_param("center").get(Vector())));
2626                                 transforms++;
2627                         }else
2628                         if(layer_name=="stretch")
2629                         {
2630                                 Vector scale(layer->get_param("amount").get(Vector()));
2631                                 transform_stack.push(synfig::Transform_Scale(scale,layer->get_param("center").get(Vector())));
2632                                 transforms++;
2633                         }else
2634                         if(layer_name=="Rotate")
2635                         {
2636                                 transform_stack.push(synfig::Transform_Rotate(layer->get_param("amount").get(Angle()),layer->get_param("origin").get(Vector())));
2637                                 transforms++;
2638                         }
2639 */
2640                 }
2641                 
2642                 // If this is a paste canvas layer, then we need to
2643                 // descend into it
2644                 if(layer_name=="PasteCanvas")
2645                 {
2646                         Vector scale;
2647                         scale[0]=scale[1]=exp(layer->get_param("zoom").get(Real()));
2648                         Vector origin(layer->get_param("origin").get(Vector()));
2649                         
2650                         Canvas::Handle child_canvas(layer->get_param("canvas").get(Canvas::Handle()));
2651                         
2652                         if(!scale.is_equal_to(Vector(1,1)))
2653                                 transform_stack.push(new Transform_Scale(scale,origin));
2654                         if(!scale.is_equal_to(Vector(0,0)))
2655                                 transform_stack.push(new Transform_Translate(origin));
2656                         
2657                         rebuild_ducks_layer_(transform_stack,child_canvas,selected_list);
2658                         
2659                         if(!scale.is_equal_to(Vector(0,0)))
2660                                 transform_stack.pop();
2661                         if(!scale.is_equal_to(Vector(1,1)))
2662                                 transform_stack.pop();
2663                 }
2664         }
2665         // Remove all of the transforms we have added
2666         while(transforms--) { transform_stack.pop(); }
2667
2668 #undef QUEUE_REBUILD_DUCKS
2669 }
2670
2671 void
2672 CanvasView::queue_rebuild_ducks()
2673 {
2674 #if 0
2675         if(rebuild_ducks_queued)
2676                 return;
2677 #else   
2678         if(rebuild_ducks_queued)
2679                 queue_rebuild_ducks_connection.disconnect();
2680 #endif
2681         
2682         queue_rebuild_ducks_connection=Glib::signal_timeout().connect(
2683                 sigc::bind_return(
2684                         sigc::mem_fun(*this,&CanvasView::rebuild_ducks),
2685                         false
2686                 ),
2687                 50
2688         );
2689         
2690         rebuild_ducks_queued=true;
2691 }
2692
2693 void
2694 CanvasView::rebuild_ducks()
2695 {       
2696         /*static int i=0;
2697         i++;
2698         if(i>30)
2699                 synfig::info("%d",i/(i-i));
2700         */
2701         
2702         rebuild_ducks_queued=false;
2703         //queue_rebuild_ducks_connection.disconnect();
2704         
2705         if(work_area->is_dragging())
2706         {
2707                 queue_rebuild_ducks();
2708                 return;
2709         }
2710         
2711         if(!duck_refresh_flag)
2712         {
2713                 duck_refresh_needed=true;
2714                 return;
2715         }
2716         
2717         bbox=Rect::zero();
2718         
2719         work_area->clear_ducks();
2720         work_area->set_time(get_time());
2721         get_canvas()->set_time(get_time());
2722         curr_transform_stack.clear();
2723         //curr_transform_stack.push(new Transform_Translate(Point(0,0)));
2724         curr_transform_stack_set=false;
2725
2726         for(;!duck_changed_connections.empty();duck_changed_connections.pop_back())duck_changed_connections.back().disconnect();
2727         
2728         //get_canvas()->set_time(get_time());
2729         bool not_empty(false);
2730         
2731         // First do the layers...       
2732         do{
2733                 synfigapp::SelectionManager::LayerList selected_list(get_selection_manager()->get_selected_layers());
2734                 std::set<synfig::Layer::Handle> layer_set(selected_list.begin(),selected_list.end());
2735                 
2736                 if(!layer_set.empty())
2737                         not_empty=true;
2738                 
2739                 synfig::TransformStack transform_stack;
2740                 
2741                 rebuild_ducks_layer_(transform_stack, get_canvas(), layer_set);
2742
2743         }while(0);
2744
2745         // Now do the children
2746         do{
2747                 synfigapp::SelectionManager::ChildrenList selected_list(get_selection_manager()->get_selected_children());
2748                 synfigapp::SelectionManager::ChildrenList::iterator iter;
2749                 synfig::TransformStack transform_stack;
2750         
2751                 if(selected_list.empty())
2752                 {
2753                         break;
2754                 }
2755                 else
2756                 {
2757                         not_empty=true;
2758                         for(iter=selected_list.begin();iter!=selected_list.end();++iter)
2759                         {
2760                                 work_area->add_to_ducks(*iter,this,transform_stack);
2761                         }
2762                 }
2763         }while(0);
2764         work_area->refresh_selected_ducks();
2765         work_area->queue_draw_preview();
2766 }
2767
2768 void
2769 CanvasView::on_dirty_preview()
2770 {
2771         if(!is_playing_)
2772         {
2773                 IsWorking is_working(*this);
2774
2775                 work_area->queue_render_preview();
2776         }
2777 }
2778
2779 void
2780 CanvasView::play()
2781 {
2782         assert(get_canvas());
2783         
2784         // If we are already busy, don't play!
2785         if(working_depth)return;
2786         
2787         // Set us up as working
2788         IsWorking is_working(*this);
2789         
2790         etl::clock timer;
2791         Time
2792                 time=work_area->get_time(),
2793                 endtime=get_canvas()->rend_desc().get_time_end();
2794
2795         // If we are already at the end of time, start over
2796         if(time==endtime)
2797                 time=get_canvas()->rend_desc().get_time_start();
2798         
2799         is_playing_=true;               
2800
2801         work_area->clear_ducks();
2802         
2803         for(timer.reset(); time + timer() < endtime;)
2804         {
2805                 //Clamp the time window so we can see the time value as it races across the horizon
2806                 bool timewindreset = false;
2807                 
2808                 while( time + timer() > Time(time_window_adjustment().get_sub_upper()) )
2809                 {
2810                         time_window_adjustment().set_value(
2811                                         min(
2812                                                 time_window_adjustment().get_value()+time_window_adjustment().get_page_size()/2,
2813                                                 time_window_adjustment().get_upper()-time_window_adjustment().get_page_size() )
2814                                 );
2815                         timewindreset = true;
2816                 }
2817                 
2818                 while( time + timer() < Time(time_window_adjustment().get_sub_lower()) )
2819                 {
2820                         time_window_adjustment().set_value(
2821                                 max(
2822                                         time_window_adjustment().get_value()-time_window_adjustment().get_page_size()/2,
2823                                         time_window_adjustment().get_lower())
2824                         );
2825                         
2826                         timewindreset = true;
2827                 }
2828                 
2829                 //we need to tell people that the value changed
2830                 if(timewindreset) time_window_adjustment().value_changed();
2831                 
2832                 //update actual time to next step
2833                 time_adjustment().set_value(time+timer());
2834                 time_adjustment().value_changed();
2835                 
2836                 if(!work_area->sync_render_preview())
2837                         break;
2838                 
2839                 studio::App::iteration(false);
2840                 
2841                 if(get_cancel_status())
2842                         return;
2843         }
2844         is_playing_=false;              
2845
2846         time_adjustment().set_value(endtime);
2847         time_adjustment().value_changed();
2848 }
2849
2850 void
2851 CanvasView::show_tables()
2852 {
2853 /*
2854         Smach::event_result x(process_event_key(EVENT_TABLES_SHOW));
2855         if(x==Smach::RESULT_OK || x==Smach::RESULT_ACCEPT)
2856         {
2857                 Gtk::IconSize iconsize=Gtk::IconSize::from_name("synfig-small_icon");
2858                 treetogglebutton->remove();
2859                 treetogglebutton->add(*manage(new Gtk::Image(Gtk::StockID("gtk-go-down"),iconsize)));   
2860                 treetogglebutton->show_all();
2861                 notebook->show();
2862         }
2863 */
2864 }
2865
2866 void
2867 CanvasView::hide_tables()
2868 {
2869 /*
2870         Smach::event_result x(process_event_key(EVENT_TABLES_HIDE));
2871         if(x==Smach::RESULT_OK || x==Smach::RESULT_ACCEPT)
2872         {
2873                 Gtk::IconSize iconsize=Gtk::IconSize::from_name("synfig-small_icon");
2874                 treetogglebutton->remove();
2875                 treetogglebutton->add(*manage(new Gtk::Image(Gtk::StockID("gtk-go-up"),iconsize)));     
2876                 treetogglebutton->show_all();
2877                 notebook->hide();
2878         }
2879 */
2880 }
2881
2882 bool
2883 CanvasView::tables_are_visible()
2884 {
2885 //      return notebook->is_visible();
2886         return false;
2887 }
2888
2889 void
2890 CanvasView::toggle_tables()
2891 {
2892 //      if(tables_are_visible())
2893 //              hide_tables();
2894 //      else
2895 //              show_tables();
2896 }
2897
2898 void
2899 CanvasView::do_rotoscope_bline()
2900 {
2901         smach_.enter(&state_bline);
2902 }
2903
2904 void
2905 CanvasView::do_rotoscope()
2906 {
2907         smach_.enter(&state_draw);
2908 }
2909
2910 void
2911 CanvasView::do_rotoscope_poly()
2912 {
2913         smach_.enter(&state_polygon);
2914 }
2915
2916 void
2917 CanvasView::do_eyedrop()
2918 {
2919         smach_.enter(&state_eyedrop);
2920 }
2921
2922 void
2923 CanvasView::show_timebar()
2924 {
2925         timebar->show();
2926         current_time_widget->show();
2927         
2928         //keyframe_tab_child->show();
2929         if(layer_tree)
2930                 layer_tree->set_show_timetrack(true);
2931         if(children_tree)
2932                 children_tree->set_show_timetrack(true);
2933 }
2934
2935 void
2936 CanvasView::hide_timebar()
2937 {
2938         timebar->hide();
2939         current_time_widget->hide();
2940         //keyframe_tab_child->hide();
2941         if(layer_tree)
2942                 layer_tree->set_show_timetrack(false);
2943         if(children_tree)
2944                 children_tree->set_show_timetrack(false);
2945 }
2946
2947
2948
2949 void
2950 CanvasView::on_waypoint_clicked(synfigapp::ValueDesc value_desc,synfig::Waypoint waypoint,int button)
2951 {
2952         waypoint_dialog.set_value_desc(value_desc);
2953         waypoint_dialog.set_waypoint(waypoint);
2954
2955         switch(button)
2956         {
2957         case -1:
2958                 waypoint_dialog.show();
2959                 break;
2960         case 2:
2961                 {
2962                         Gtk::Menu* waypoint_menu(manage(new Gtk::Menu()));
2963
2964                         waypoint_menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-jump-to"),
2965                                 sigc::bind(
2966                                         sigc::mem_fun(
2967                                                 *canvas_interface(),
2968                                                 &synfigapp::CanvasInterface::set_time
2969                                         ),
2970                                         waypoint.get_time()
2971                                 )
2972                         ));
2973
2974                         waypoint_menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("Edit Waypoint"),
2975                                 sigc::mem_fun(
2976                                         waypoint_dialog,
2977                                         &Gtk::Widget::show
2978                                 )
2979                         ));
2980                         
2981                         waypoint_menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("synfig-duplicate"),
2982                                 sigc::bind(
2983                                         sigc::bind(
2984                                                 sigc::mem_fun(
2985                                                         *canvas_interface(),
2986                                                         &synfigapp::CanvasInterface::waypoint_duplicate
2987                                                 ),
2988                                                 waypoint
2989                                         ),
2990                                         value_desc
2991                                 )
2992                         ));
2993                         waypoint_menu->items().push_back(Gtk::Menu_Helpers::StockMenuElem(Gtk::StockID("gtk-delete"),
2994                                 sigc::bind(
2995                                         sigc::bind(
2996                                                 sigc::mem_fun(
2997                                                         *canvas_interface(),
2998                                                         &synfigapp::CanvasInterface::waypoint_remove
2999                                                 ),
3000                                                 waypoint
3001                                         ),
3002                                         value_desc
3003                                 )
3004                         ));
3005                         waypoint_menu->popup(button+1,gtk_get_current_event_time());
3006                 }
3007                 break;
3008         default:
3009                 break;
3010         }
3011 }
3012
3013 void
3014 CanvasView::on_waypoint_changed()
3015 {
3016         synfigapp::Action::ParamList param_list;
3017         param_list.add("canvas",get_canvas());
3018         param_list.add("canvas_interface",canvas_interface());
3019         param_list.add("value_node",waypoint_dialog.get_value_desc().get_value_node());
3020         param_list.add("waypoint",waypoint_dialog.get_waypoint());
3021 //      param_list.add("time",canvas_interface()->get_time());
3022
3023         get_instance()->process_action("waypoint_set_smart", param_list);
3024 }
3025
3026 void
3027 CanvasView::on_waypoint_delete()
3028 {
3029         synfigapp::Action::ParamList param_list;
3030         param_list.add("canvas",get_canvas());
3031         param_list.add("canvas_interface",canvas_interface());
3032         param_list.add("value_node",waypoint_dialog.get_value_desc().get_value_node());
3033         param_list.add("waypoint",waypoint_dialog.get_waypoint());
3034 //      param_list.add("time",canvas_interface()->get_time());
3035
3036         get_instance()->process_action("waypoint_remove", param_list);
3037 }
3038
3039 void
3040 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)
3041 {
3042         // We will make this true once we have a solid drop
3043         bool success(false);
3044         //synfig::info("Droped data of type \"%s\"",selection_data.get_data_type());
3045         //synfig::info("Droped data of target \"%s\"",gdk_atom_name(selection_data->target));
3046         //synfig::info("selection=\"%s\"",gdk_atom_name(selection_data->selection));
3047         
3048         if ((selection_data_.get_length() >= 0) && (selection_data_.get_format() == 8))
3049         {
3050                 if(synfig::String(selection_data_.get_data_type())=="STRING")do
3051                 {
3052                         synfig::String selection_data((gchar *)(selection_data_.get_data()));
3053
3054                         Layer::Handle layer(synfig::Layer::create("Text"));
3055                         if(!layer)
3056                                 break;
3057                         if(!layer->set_param("text",ValueBase(selection_data)))
3058                                 break;
3059
3060                         synfigapp::Action::Handle       action(synfigapp::Action::create("layer_add"));
3061                 
3062                         assert(action);
3063                         if(!action)
3064                                 break;
3065                         
3066                         action->set_param("canvas",get_canvas());
3067                         action->set_param("canvas_interface",canvas_interface());
3068                         action->set_param("new",layer);
3069
3070                         if(!get_instance()->perform_action(action))
3071                                 break;
3072                         
3073                         // Ok, we have successfuly imported at least one item.
3074                         success=true;
3075                 } while(0); // END of "STRING"
3076                 
3077                 if(synfig::String(selection_data_.get_data_type())=="text/plain")
3078                 {
3079                         synfig::String selection_data((gchar *)(selection_data_.get_data()));
3080         
3081                         // For some reason, GTK hands us a list of URL's seperated
3082                         // by not only Carrage-Returns, but also Line-Feeds.
3083                         // Line-Feeds will mess us up. Remove all the line-feeds.
3084                         while(selection_data.find_first_of('\r')!=synfig::String::npos)
3085                                 selection_data.erase(selection_data.begin()+selection_data.find_first_of('\r'));
3086         
3087                         std::stringstream stream(selection_data);
3088         
3089                         //synfigapp::PassiveGrouper group(canvas_interface()->get_instance(),_("Insert Image"));
3090                         while(stream)
3091                         {
3092                                 synfig::String filename,URI;
3093                                 getline(stream,filename);
3094                                 
3095                                 // If we don't have a filename, move on.
3096                                 if(filename.empty())
3097                                         continue;
3098                                 
3099                                 // Make sure this URL is of the "file://" type.
3100                                 URI=String(filename.begin(),filename.begin()+sizeof("file://")-1);
3101                                 if(URI!="file://")
3102                                 {
3103                                         synfig::warning("Unknown URI (%s) in \"%s\"",URI.c_str(),filename.c_str());
3104                                         continue;
3105                                 }
3106                                 
3107                                 // Strip the "file://" part from the filename
3108                                 filename=synfig::String(filename.begin()+sizeof("file://")-1,filename.end());
3109
3110                                 String ext;
3111                                 try{ext=(String(filename.begin()+filename.find_last_of('.')+1,filename.end()));}catch(...){continue;}
3112         
3113                                 // If this is a SIF file, then we need to do things slightly differently
3114                                 if(ext=="sketch")
3115                                 {
3116                                         if(work_area->load_sketch(filename))
3117                                         {
3118                                                 success=true;
3119                                                 work_area->queue_draw();
3120                                         }
3121                                 }
3122                                 else
3123                                 {
3124                                         if(canvas_interface()->import(filename))
3125                                                 success=true;
3126                                 }
3127
3128                                 continue;
3129                         }
3130                 } // END of "text/plain"
3131         }
3132         else
3133                 ui_interface_->error("Drop failed: bad selection data");
3134
3135         // Finish the drag
3136         context->drag_finish(success, false, time);
3137 }
3138
3139 void
3140 CanvasView::on_keyframe_add_pressed()
3141 {
3142         synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_add"));
3143
3144         if(!action)
3145         {
3146                 ui_interface_->error("I am unable to find the appropriate action");
3147                 return;
3148         }               
3149         
3150         action->set_param("canvas",get_canvas());
3151         action->set_param("canvas_interface",canvas_interface());
3152         action->set_param("keyframe",Keyframe(get_time()));
3153
3154         canvas_interface()->get_instance()->perform_action(action);
3155 }
3156
3157 void
3158 CanvasView::on_keyframe_duplicate_pressed()
3159 {
3160         const KeyframeTreeStore::Model model;
3161         const Gtk::TreeRow row(*keyframe_tree->get_selection()->get_selected());
3162         Keyframe keyframe;
3163         if(!row)
3164         {
3165                 ui_interface_->error("I am unable to duplicate the keyframe");
3166                 return;
3167         }               
3168         keyframe=row[model.keyframe];
3169
3170         synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_duplicate"));
3171
3172         if(!action)
3173         {
3174                 ui_interface_->error("I am unable to find the appropriate action");
3175                 return;
3176         }               
3177         
3178         action->set_param("canvas",get_canvas());
3179         action->set_param("canvas_interface",canvas_interface());
3180         action->set_param("keyframe",keyframe);
3181         action->set_param("time",get_time());
3182
3183         canvas_interface()->get_instance()->perform_action(action);
3184 }
3185
3186 void
3187 CanvasView::on_keyframe_remove_pressed()
3188 {
3189         const KeyframeTreeStore::Model model;
3190         const Gtk::TreeRow row(*keyframe_tree->get_selection()->get_selected());
3191         Keyframe keyframe;
3192         if(!row)
3193         {
3194                 ui_interface_->error("I am unable to remove the keyframe");
3195                 return;
3196         }               
3197         keyframe=row[model.keyframe];
3198
3199         synfigapp::Action::Handle action(synfigapp::Action::create("keyframe_remove"));
3200
3201         if(!action)
3202         {
3203                 ui_interface_->error("I am unable to find the appropriate action");
3204                 return;
3205         }               
3206         
3207         action->set_param("canvas",get_canvas());
3208         action->set_param("canvas_interface",canvas_interface());
3209         action->set_param("keyframe",keyframe);
3210
3211         canvas_interface()->get_instance()->perform_action(action);
3212 }
3213
3214
3215 void
3216 CanvasView::toggle_duck_mask(Duckmatic::Type type)
3217 {
3218         bool is_currently_on(work_area->get_type_mask()&type);
3219
3220         switch(type)
3221         {
3222         case Duck::TYPE_POSITION:
3223                 if(duck_mask_position)
3224                         duck_mask_position->set_active(!is_currently_on);
3225                 break;
3226         
3227         case Duck::TYPE_VERTEX:
3228                 if(duck_mask_vertex)
3229                         duck_mask_vertex->set_active(!is_currently_on);
3230                 break;
3231         
3232         case Duck::TYPE_TANGENT:
3233                 if(duck_mask_tangent)
3234                         duck_mask_tangent->set_active(!is_currently_on);
3235                 break;
3236         
3237         case Duck::TYPE_RADIUS:
3238                 if(duck_mask_radius)
3239                         duck_mask_radius->set_active(!is_currently_on);
3240                 break;
3241         
3242         case Duck::TYPE_WIDTH:
3243                 if(duck_mask_width)
3244                         duck_mask_width->set_active(!is_currently_on);
3245                 break;
3246
3247         case Duck::TYPE_ANGLE:
3248                 if(duck_mask_angle)
3249                         duck_mask_angle->set_active(!is_currently_on);
3250                 break;
3251
3252         default:
3253                 synfig::warning("CanvasView::toggle_duck_mask():Unknown duck type!");
3254                 break;
3255         }
3256
3257         if(is_currently_on)
3258                 work_area->set_type_mask(work_area->get_type_mask()-type);
3259         else
3260                 work_area->set_type_mask(work_area->get_type_mask()|type);
3261         
3262         work_area->queue_draw();
3263 }
3264
3265
3266 void
3267 CanvasView::image_import()
3268 {
3269         String filename(dirname(get_canvas()->get_file_name()));
3270         if(App::dialog_open_file(_("Import Image"), filename))
3271                 canvas_interface()->import(filename);
3272 }
3273
3274 Smach::event_result
3275 CanvasView::process_event_key(EventKey x)
3276 {
3277         return smach_.process_event(x);
3278 }
3279
3280 void
3281 CanvasView::on_input_device_changed(GdkDevice* device)
3282 {
3283         if(!device)
3284         {
3285                 get_smach().egress();
3286         }
3287         assert(device);
3288         
3289         synfigapp::InputDevice::Handle input_device;
3290         input_device=synfigapp::Main::select_input_device(device->name);
3291         App::toolbox->change_state(input_device->get_state());
3292         process_event_key(EVENT_INPUT_DEVICE_CHANGED);
3293 }
3294
3295 void
3296 CanvasView::on_preview_option()
3297 {
3298         Dialog_PreviewOptions *po = dynamic_cast<Dialog_PreviewOptions *>(get_ext_widget("prevoptions"));
3299         
3300         Canvas::Handle  canv = get_canvas();
3301         
3302         if(canv)
3303         {
3304                 RendDesc &r = canv->rend_desc();
3305                 if(r.get_frame_rate())
3306                 {
3307                         float rate = 1/r.get_frame_rate();
3308                         float beg = r.get_time_start() + r.get_frame_start()*rate;
3309                         float end = r.get_time_start() + r.get_frame_end()*rate;
3310
3311                         if(!po)
3312                         {
3313                                 po = new Dialog_PreviewOptions;
3314                                 po->set_zoom(work_area->get_zoom()/2);
3315                                 po->set_fps(r.get_frame_rate()/2);
3316                                 po->set_begintime(beg);
3317                                 po->set_begin_override(false);
3318                                 po->set_endtime(end);
3319                                 po->set_end_override(false);
3320                                 
3321                                 set_ext_widget("prevoptions",po);
3322                         }
3323                         /*po->set_zoom(work_area->get_zoom()/2);
3324                         po->set_fps(r.get_frame_rate()/2);
3325                         po->set_begintime(beg);
3326                         po->set_begin_override(false);
3327                         po->set_endtime(end);
3328                         po->set_end_override(false);*/
3329                         
3330                         po->set_global_fps(r.get_frame_rate());
3331                         po->signal_finish().connect(sigc::mem_fun(*this,&CanvasView::on_preview_create));
3332                         po->present();
3333                 }
3334         }
3335 }
3336
3337 void
3338 CanvasView::on_preview_create(const PreviewInfo &info)
3339 {       
3340         //set all the options
3341         etl::handle<Preview>    prev = new Preview;
3342         
3343         prev->set_canvasview(this);
3344         prev->set_zoom(info.zoom);
3345         prev->set_fps(info.fps);
3346         prev->set_overbegin(info.overbegin);
3347         prev->set_begintime(info.begintime);
3348         prev->set_overend(info.overend);
3349         prev->set_endtime(info.endtime);
3350         prev->set_quality(work_area->get_quality());
3351
3352         //render it out...
3353         prev->render();
3354         
3355         Dialog_Preview *pd = preview_dialog.get();
3356         assert(pd);
3357         
3358         pd->set_preview(prev.get());
3359         pd->present();
3360 }
3361
3362 void
3363 CanvasView::on_audio_option()
3364 {
3365         synfig::warning("Launching Audio Options");
3366         sound_dialog->set_global_fps(get_canvas()->rend_desc().get_frame_rate());
3367         sound_dialog->present();
3368 }
3369
3370 void
3371 CanvasView::on_audio_file_change(const std::string &f)
3372 {       
3373         //save in meta data - always even when not valid...
3374         canvas_interface()->set_meta_data("audiofile",f);
3375 }
3376
3377 void
3378 CanvasView::on_audio_offset_change(const Time &t)
3379 {
3380         canvas_interface()->set_meta_data("audiooffset",t.get_string());
3381 }
3382
3383 void
3384 CanvasView::on_audio_file_notify()
3385 {
3386         std::string file(get_canvas()->get_meta_data("audiofile"));
3387         if(!file.c_str()) return;
3388         
3389         if(!audio->load(file,dirname(get_canvas()->get_file_name())+string("/")))
3390         {
3391                 if(file != "") synfig::warning("Could not load the file: %s", file.c_str());
3392                 get_canvas()->erase_meta_data("audiofile");
3393                 disp_audio->hide();
3394                 disp_audio->set_profile(etl::handle<AudioProfile>());
3395         }else
3396         {
3397                 //save in canvasview            
3398                 synfig::warning("Getting the profile of the music stuff");
3399                 
3400                 //profile specific stuff for the preview widget
3401                 //similar for other attachments
3402                 Dialog_Preview *pd = preview_dialog.get();
3403                 pd->get_widget().set_audio(audio);
3404                 
3405                 handle<AudioProfile>    prof = audio->get_profile();
3406                 
3407                 if(!prof)
3408                 {
3409                         synfig::warning("Agh, I couldn't build the profile captain!");                  
3410                 }
3411                 pd->get_widget().set_audioprofile(prof);
3412                 
3413                 disp_audio->set_profile(audio->get_profile());
3414                 disp_audio->show();
3415                 
3416                 synfig::warning("successfully set the profiles and stuff");
3417         }
3418         disp_audio->queue_draw();
3419 }
3420
3421 void
3422 CanvasView::on_audio_offset_notify()
3423 {
3424         Time t(get_canvas()->get_meta_data("audiooffset"),get_canvas()->rend_desc().get_frame_rate());
3425         audio->set_offset(t);
3426         sound_dialog->set_offset(t);
3427         disp_audio->queue_draw();
3428         
3429         synfig::info("CanvasView::on_audio_offset_notify(): offset time set to %s",t.get_string(get_canvas()->rend_desc().get_frame_rate()).c_str());
3430 }
3431
3432 void
3433 CanvasView::play_audio(float t)
3434 {
3435         if(audio.get())
3436         {
3437                 synfig::info("Playing audio at %f s",t);
3438                 audio->play(t);
3439         }               
3440 }
3441
3442 void
3443 CanvasView::stop_audio()
3444 {
3445         if(audio.get())
3446         {
3447                 audio->stop();
3448         }
3449 }
3450
3451 bool
3452 CanvasView::on_audio_scrub()
3453 {
3454         disp_audio->draw();
3455         return true;
3456 }
3457
3458
3459
3460 Glib::RefPtr<Glib::ObjectBase>
3461 CanvasView::get_ref_obj(const synfig::String& x)
3462 {
3463         return ref_obj_book_[x];
3464 }
3465
3466 Glib::RefPtr<const Glib::ObjectBase>
3467 CanvasView::get_ref_obj(const synfig::String& x)const
3468 {
3469         return ref_obj_book_.find(x)->second;
3470 }
3471
3472 void
3473 CanvasView::set_ref_obj(const synfig::String& x, Glib::RefPtr<Glib::ObjectBase> y)
3474 {
3475         ref_obj_book_[x]=y;
3476 }
3477
3478 Glib::RefPtr<Gtk::TreeModel>
3479 CanvasView::get_tree_model(const synfig::String& x)
3480 {
3481         return Glib::RefPtr<Gtk::TreeModel>::cast_dynamic(ref_obj_book_["_tree_model_"+x]);
3482 }
3483
3484 Glib::RefPtr<const Gtk::TreeModel>
3485 CanvasView::get_tree_model(const synfig::String& x)const
3486 {
3487         return Glib::RefPtr<Gtk::TreeModel>::cast_dynamic(ref_obj_book_.find("_tree_model_"+x)->second);
3488 }
3489
3490 void
3491 CanvasView::set_tree_model(const synfig::String& x, Glib::RefPtr<Gtk::TreeModel> y)
3492 {
3493         ref_obj_book_["_tree_model_"+x]=Glib::RefPtr<Glib::ObjectBase>::cast_static(y);
3494 }
3495
3496 Gtk::Widget*
3497 CanvasView::get_ext_widget(const synfig::String& x)
3498 {
3499         return ext_widget_book_[x];
3500 }
3501
3502 void
3503 CanvasView::set_ext_widget(const synfig::String& x, Gtk::Widget* y)
3504 {
3505         ext_widget_book_[x]=y;
3506         if(x=="layers_cmp")
3507         {
3508                 layer_tree=dynamic_cast<LayerTree*>(y);
3509
3510                 layer_tree->get_selection()->signal_changed().connect(SLOT_EVENT(EVENT_LAYER_SELECTION_CHANGED));
3511                 layer_tree->get_selection()->signal_changed().connect(SLOT_EVENT(EVENT_REFRESH_DUCKS));
3512                 layer_tree->signal_layer_user_click().connect(sigc::mem_fun(*this, &studio::CanvasView::on_layer_user_click));
3513                 layer_tree->signal_param_user_click().connect(sigc::mem_fun(*this, &studio::CanvasView::on_children_user_click));
3514                 layer_tree->signal_waypoint_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_waypoint_clicked));
3515         }
3516         if(x=="children")
3517         {
3518                 children_tree=dynamic_cast<ChildrenTree*>(y);
3519                 if(children_tree)children_tree->signal_user_click().connect(sigc::mem_fun(*this, &studio::CanvasView::on_children_user_click));
3520                 if(children_tree)children_tree->signal_waypoint_clicked().connect(sigc::mem_fun(*this, &studio::CanvasView::on_waypoint_clicked));
3521                 if(children_tree)children_tree->get_selection()->signal_changed().connect(SLOT_EVENT(EVENT_REFRESH_DUCKS));
3522         }
3523         if(x=="keyframes")
3524                 keyframe_tree=dynamic_cast<KeyframeTree*>(y);
3525 }
3526
3527 static bool _close_instance(etl::handle<Instance> instance)
3528 {
3529         etl::handle<Instance> argh(instance);
3530         instance->safe_close();
3531         synfig::info("closed");
3532         return false;
3533 }
3534
3535 bool
3536 CanvasView::on_delete_event(GdkEventAny* event)
3537 {
3538         if(get_instance()->get_visible_canvases()==1)
3539         {
3540                 // Schedule a close to occur in a few moments
3541                 Glib::signal_timeout().connect(
3542                         sigc::bind(
3543                                 sigc::ptr_fun(_close_instance),
3544                                 (etl::handle<Instance>)get_instance()
3545                         )
3546                         ,250
3547                 );
3548         }
3549         if(event)
3550                 return Gtk::Window::on_delete_event(event);
3551         
3552         return true;
3553 }