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