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