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