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