ea3fae71a91c25878289e363a725e041093c9d19
[synfig.git] / synfig-studio / trunk / src / gtkmm / canvasview.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file canvasview.h
3 **      \brief Template Header
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 /* === S T A R T =========================================================== */
25
26 #ifndef __SYNFIG_STUDIO_GTKMM_CANVASVIEW_H
27 #define __SYNFIG_STUDIO_GTKMM_CANVASVIEW_H
28
29 /* === H E A D E R S ======================================================= */
30
31 #include <gtkmm/window.h>
32 #include <gtkmm/image.h>
33 #include <gtkmm/tooltips.h>
34 #include <gtkmm/table.h>
35 #include <gtkmm/statusbar.h>
36 #include <gtkmm/progressbar.h>
37 #include <gtkmm/button.h>
38 #include <gtkmm/menu.h>
39 #include <gtkmm/treeview.h>
40 #include <gtkmm/treestore.h>
41 #include <gtkmm/scrolledwindow.h>
42 #include <gtkmm/notebook.h>
43 #include <gdkmm/device.h>
44
45 #include <synfigapp/canvasinterface.h>
46 #include <synfigapp/selectionmanager.h>
47
48 #include <synfig/canvas.h>
49 #include <synfig/string.h>
50 #include <synfig/time.h>
51
52 #include "instance.h"
53 #include "canvasproperties.h"
54 #include "canvasoptions.h"
55 #include "render.h"
56 #include "cellrenderer_timetrack.h"
57 #include "app.h"
58
59 #include "layertreestore.h"
60 #include "layertree.h"
61 #include "childrentreestore.h"
62 #include "childrentree.h"
63 #include "keyframetreestore.h"
64 #include "keyframetree.h"
65
66 #include "dialog_waypoint.h"
67 #include "dialog_keyframe.h"
68 #include "framedial.h"
69 #include "toggleducksdial.h"
70 #include "resolutiondial.h"
71
72 #include "duckmatic.h"
73 #include <gtkmm/scale.h>
74
75 #include <gtkmm/uimanager.h>
76
77 #include "smach.h"
78
79 #include <memory>
80 #include <set>
81 #include <map>
82 #include <gtkmm/toggleaction.h>
83 #include <gtkmm/radioaction.h>
84 #include <synfig/rect.h>
85
86 #include "adjust_window.h"
87
88 #include <synfig/transform.h>
89
90 /* === M A C R O S ========================================================= */
91
92 #ifndef DEBUGPOINT_CLASS
93 #if     _DEBUG
94 #define DEBUGPOINT_CLASS(x)             struct debugpointclass_ ## x { debugpointclass_ ## x () { DEBUGPOINT(); } ~debugpointclass_ ## x () { DEBUGPOINT(); } } badfthguae_ ## x ;
95 #else
96 #define DEBUGPOINT_CLASS(x)
97 #endif
98 #endif
99
100 /* === T Y P E D E F S ===================================================== */
101
102 /* === C L A S S E S & S T R U C T S ======================================= */
103
104 namespace synfig {
105         class TransformStack;
106 }
107
108 namespace studio {
109
110 class CanvasViewUIInterface;
111 class CanvasViewSelectionManager;
112
113 class CellRenderer_TimeTrack;
114 class CellRenderer_ValueBase;
115 class UniversalScrubber;
116 class WorkArea;
117
118 class Duckmatic;
119
120 class Preview;
121 struct PreviewInfo;
122 class AudioContainer;
123
124 class Widget_Sound;
125 class Widget_Timeslider;
126 class Widget_Time;
127
128 class Dialog_SoundSelect;
129 class Dialog_Preview;
130
131 class Dock_Layers;
132 class Dock_Children;
133 class Dock_Keyframes;
134
135 class CanvasView : public Gtk::Window, public etl::shared_object
136 {
137         friend class UniversalScrubber;
138         friend class Dock_Layers;
139         friend class Dock_Children;
140         friend class Dock_Keyframes;
141
142         friend class CanvasViewUIInterface;
143         friend class CanvasViewSelectionManager;
144
145         friend class Duckmatic;
146
147         /*
148  -- ** -- P U B L I C   T Y P E S ---------------------------------------------
149         */
150
151 public:
152
153         typedef etl::handle<CanvasView> Handle;
154
155         typedef etl::handle<const CanvasView> ConstHandle;
156
157         typedef etl::loose_handle<CanvasView> LooseHandle;
158
159         typedef LayerTreeStore::Model LayerTreeModel;
160
161         typedef ChildrenTreeStore::Model ChildrenTreeModel;
162
163         //! Create an instance of this class whenever doing a longer task.
164         /*! Make sure that you check the bool value of this class occasionally
165         **      to make sure the action has not been canceled. */
166         class IsWorking
167         {
168                 CanvasView &canvas_view_;
169
170         public:
171                 IsWorking(CanvasView &canvas_view_);
172                 ~IsWorking();
173                 operator bool()const;
174         };
175         friend class IsWorking;
176
177         typedef synfigapp::CanvasInterface::Mode Mode;
178
179         void set_grid_snap_toggle(bool flag) { grid_snap_toggle->set_active(flag); }
180         void set_grid_show_toggle(bool flag) { grid_show_toggle->set_active(flag); }
181
182         /*
183  -- ** -- P R I V A T E   D A T A ---------------------------------------------
184         */
185
186 public:
187         std::auto_ptr<WorkArea> work_area;
188
189         WorkArea* get_work_area() { return work_area.get(); }
190 private:
191
192         synfig::TransformStack curr_transform_stack;
193         bool curr_transform_stack_set;
194
195         synfig::Rect bbox;
196
197         // DEBUGPOINT_CLASS(1);
198
199         //! State Machine
200         Smach smach_;
201
202         // DEBUGPOINT_CLASS(2);
203
204         etl::loose_handle<Instance> instance_;
205         etl::handle<synfigapp::CanvasInterface> canvas_interface_;
206
207         // DEBUGPOINT_CLASS(3);
208
209         //! Sound and information to play it
210         etl::handle<AudioContainer>             audio;
211         studio::Widget_Sound                    *disp_audio; //should this be put into thing too?
212
213         sigc::connection                                playcon;
214         sigc::connection                                stopcon;
215
216         std::auto_ptr<UniversalScrubber> universal_scrubber;
217
218         //! Tooltip controller
219         Gtk::Tooltips tooltips;
220
221         // DEBUGPOINT_CLASS(4);
222
223         //! TreeModel for the layers
224         LayerTreeModel layer_tree_model;
225
226         //! TreeModel for the the children
227         ChildrenTreeModel children_tree_model;
228
229         //Glib::RefPtr<LayerTreeStore> layer_tree_store_;
230
231         //Glib::RefPtr<ChildrenTreeStore> children_tree_store_;
232
233         //Glib::RefPtr<KeyframeTreeStore> keyframe_tree_store_;
234
235         // DEBUGPOINT_CLASS(5);
236
237         //std::map<synfig::String,Glib::RefPtr<Gtk::TreeModel> > tree_model_book_;
238         std::map<synfig::String,Glib::RefPtr<Glib::ObjectBase> > ref_obj_book_;
239         std::map<synfig::String,Gtk::Widget*> ext_widget_book_;
240
241         //! The time adjustment's scope is defined by the time_window adjustment
242         Gtk::Adjustment time_adjustment_;
243
244         //! The time_window adjustment governs the position of the time window on the whole time line
245         //Gtk::Adjustment time_window_adjustment_;
246         studio::Adjust_Window time_window_adjustment_;
247
248         LayerTree *layer_tree;
249
250         ChildrenTree *children_tree;
251
252         KeyframeTree *keyframe_tree;
253
254         Gtk::Widget *keyframe_tab_child;
255
256         Gtk::ProgressBar *progressbar;
257         Gtk::Statusbar *statusbar;
258
259         Gtk::TreeRow children_canvas_row;
260         Gtk::TreeRow children_valuenode_row;
261
262         Gtk::Button *stopbutton;
263         Gtk::Button *refreshbutton;
264         Gtk::Button *treetogglebutton;  // not used
265         Gtk::Notebook *notebook; // not used
266         Gtk::Table *timebar;
267         Gtk::Table *displaybar;
268         Gtk::Button *animatebutton;
269         Gtk::Button *keyframebutton;
270         FrameDial *framedial;
271         ToggleDucksDial *toggleducksdial;
272         bool toggling_ducks_;
273         ResolutionDial *resolutiondial;
274         bool changing_resolution_;
275
276
277         //! Shows current time and allows edition
278         Widget_Time *current_time_widget;
279         void on_current_time_widget_changed();
280
281         //! Time slider class. Same than the Time track panel
282         std::auto_ptr<Widget_Timeslider> timeslider;
283
284         std::list<sigc::connection> duck_changed_connections;
285
286 /*      DEBUGPOINT_CLASS(8);
287
288         Gtk::Menu duckmaskmenu;
289         DEBUGPOINT_CLASS(77);
290         Gtk::Menu qualitymenu;
291         DEBUGPOINT_CLASS(6);
292
293         Gtk::Menu filemenu;
294         DEBUGPOINT_CLASS(777);
295         Gtk::Menu editmenu;
296         DEBUGPOINT_CLASS(71);
297         Gtk::Menu canvasmenu;
298         DEBUGPOINT_CLASS(73);
299 public:
300         Gtk::Menu layermenu;
301 private:
302         DEBUGPOINT_CLASS(74);
303         Gtk::Menu newlayermenu;
304         DEBUGPOINT_CLASS(76);
305         Gtk::Menu viewmenu;
306
307         DEBUGPOINT_CLASS(99);
308         Gtk::Menu keyframemenu;
309
310         Gtk::Menu parammenu;
311         DEBUGPOINT_CLASS(9);
312         Gtk::Menu trackmenu;
313         DEBUGPOINT_CLASS(7);
314
315         Gtk::CheckMenuItem* duck_mask_position;
316         Gtk::CheckMenuItem* duck_mask_vertex;
317         Gtk::CheckMenuItem* duck_mask_tangent;
318         Gtk::CheckMenuItem* duck_mask_radius;
319         Gtk::CheckMenuItem* duck_mask_width;
320         Gtk::CheckMenuItem* duck_mask_angle;
321 */
322         //! Menu members
323         Gtk::Menu parammenu;
324
325         Glib::RefPtr<Gtk::ToggleAction> grid_snap_toggle;
326         Glib::RefPtr<Gtk::ToggleAction> grid_show_toggle;
327
328         Gtk::RadioButtonGroup quality_group;
329         Gtk::RadioButtonGroup low_res_pixel_size_group;
330
331         Glib::RefPtr<Gtk::ActionGroup> action_group;
332
333         etl::handle<synfigapp::UIInterface> ui_interface_;
334         etl::handle<synfigapp::SelectionManager> selection_manager_;
335
336         bool is_playing_;
337
338         sigc::signal<void> signal_deleted_;
339
340         bool rebuild_ducks_queued;
341         sigc::connection queue_rebuild_ducks_connection;
342
343         /*
344  -- ** -- P U B L I C   D A T A -----------------------------------------------
345         */
346
347 public:
348         void queue_rebuild_ducks();
349         sigc::signal<void>& signal_deleted() { return signal_deleted_; }
350
351         Gtk::Menu mainmenu;
352
353         bool duck_refresh_flag;
354         bool duck_refresh_needed;
355
356         //! This is for the IsWorking class.
357         int working_depth;
358
359         bool cancel;
360
361         /*
362  -- ** -- D I A L O G S -------------------------------------------------------
363         */
364
365 public:
366
367         CanvasProperties canvas_properties;
368         CanvasOptions canvas_options;
369         RenderSettings render_settings;
370         Dialog_Waypoint waypoint_dialog;
371         Dialog_Keyframe keyframe_dialog;
372
373         std::auto_ptr<Dialog_Preview>                   preview_dialog;
374         //std::auto_ptr<Dialog_PreviewOptions>  previewoption_dialog;
375         std::auto_ptr<Dialog_SoundSelect>               sound_dialog;
376
377         /*
378  -- ** -- P R I V A T E   M E T H O D S ---------------------------------------
379         */
380
381 private:
382
383         // Constructor is private to force the use of the "create()" constructor
384         CanvasView(etl::loose_handle<Instance> instance,etl::handle<synfigapp::CanvasInterface> canvas_interface);
385
386         //! Constructor Helper
387         // Gtk::Widget* create_layer_tree();
388
389         //! Constructor Helper
390         // Gtk::Widget* create_children_tree();
391
392         //! Constructor Helper
393         // Gtk::Widget* create_keyframe_tree();
394
395         //! Constructor Helper
396         Gtk::Widget* create_status_bar();
397
398         //! Constructor Helper - Initializes all of the menus
399         void init_menus();
400
401         bool duck_change_param(const synfig::Point &value,synfig::Layer::Handle layer, synfig::String param_name);
402
403         void refresh_time_window();
404
405         void time_was_changed();
406
407         void refresh_rend_desc();
408
409         void toggle_duck_mask(Duckmatic::Type type);
410
411         Gtk::Widget *create_work_area();
412
413         Gtk::Widget *create_time_bar();
414
415         Gtk::Widget *create_display_bar();
416
417         //! Pop up menu for the bezier (bline, draw) tool (?)
418         void popup_param_menu_bezier(float location, synfigapp::ValueDesc value_desc)
419         { popup_param_menu(value_desc,location,true); }
420
421         //! Pop up menu for the tools but not the bezier ones.
422         void popup_param_menu(synfigapp::ValueDesc value_desc, float location=0, bool bezier=false);
423
424         void workarea_layer_selected(synfig::Layer::Handle layer);
425
426         void selected_layer_color_set(synfig::Color color);
427
428         void register_layer_type(synfig::Layer::Book::value_type &lyr,std::map<synfig::String,Gtk::Menu*>*);
429
430         //! Rebuilds the "new layer" menu
431         void build_new_layer_menu(Gtk::Menu &menu);
432
433         void rebuild_ducks_layer_(synfig::TransformStack& transform_stack, synfig::Canvas::Handle canvas, std::set<synfig::Layer::Handle>& selected_list);
434
435         void decrease_low_res_pixel_size();
436         void increase_low_res_pixel_size();
437         void toggle_low_res_pixel_flag();
438
439         /*
440  -- ** -- P U B L I C   M E T H O D S -----------------------------------------
441         */
442
443 public:
444         const synfig::TransformStack& get_curr_transform_stack()const { return curr_transform_stack; }
445
446         const synfig::Rect& get_bbox()const { return bbox; }
447
448         Glib::RefPtr<Glib::ObjectBase> get_ref_obj(const synfig::String& x);
449         Glib::RefPtr<const Glib::ObjectBase> get_ref_obj(const synfig::String& x)const;
450         void set_ref_obj(const synfig::String& x, Glib::RefPtr<Glib::ObjectBase> y);
451
452         Glib::RefPtr<Gtk::TreeModel> get_tree_model(const synfig::String& x);
453         Glib::RefPtr<const Gtk::TreeModel> get_tree_model(const synfig::String& x)const;
454         void set_tree_model(const synfig::String& x, Glib::RefPtr<Gtk::TreeModel> y);
455
456         Gtk::Widget* get_ext_widget(const synfig::String& x);
457         void set_ext_widget(const synfig::String& x, Gtk::Widget* y);
458
459         //std::map<synfig::String,Gtk::Widget*>& tree_view_book() { return tree_view_book_; }
460         //std::map<synfig::String,Gtk::Widget*>& ext_widget_book() { return tree_view_book_; }
461
462         void popup_main_menu();
463
464         Smach& get_smach() { return smach_; }
465
466         const Smach& get_smach()const { return smach_; }
467
468         Smach::event_result process_event_key(EventKey x);
469
470         void popup_layer_menu(synfig::Layer::Handle layer);
471
472         virtual ~CanvasView();
473
474         void set_mode(Mode x) { canvas_interface()->set_mode(x); }
475
476         Mode get_mode()const { return canvas_interface()->get_mode(); }
477
478         Gtk::Adjustment &time_adjustment() { return time_adjustment_; }
479
480         const Gtk::Adjustment &time_adjustment()const { return time_adjustment_; }
481
482         studio::Adjust_Window &time_window_adjustment() { return time_window_adjustment_; }
483
484         const studio::Adjust_Window &time_window_adjustment()const { return time_window_adjustment_; }
485
486         etl::handle<synfigapp::UIInterface> get_ui_interface() { return ui_interface_;}
487
488         etl::handle<synfigapp::SelectionManager> get_selection_manager() { return selection_manager_; }
489
490         Glib::RefPtr<Gtk::TreeModel> layer_tree_store() { return get_tree_model("layers"); }
491
492         Glib::RefPtr<const Gtk::TreeModel> layer_tree_store()const { return get_tree_model("layers"); }
493
494         Glib::RefPtr<Gtk::TreeModel> children_tree_store() { return get_tree_model("children"); }
495
496         Glib::RefPtr<const Gtk::TreeModel> children_tree_store()const { return get_tree_model("children"); }
497
498         Glib::RefPtr<Gtk::TreeModel> keyframe_tree_store() { return get_tree_model("keyframes"); }
499
500         Glib::RefPtr<const Gtk::TreeModel> keyframe_tree_store()const { return get_tree_model("keyframes"); }
501
502         void set_time(synfig::Time t) { canvas_interface_->set_time(t); }
503
504         synfig::Time get_time() { return canvas_interface_->get_time(); }
505
506         etl::handle<synfig::Canvas> get_canvas()const { return canvas_interface_->get_canvas(); }
507
508         etl::handle<Instance> get_instance()const { return instance_; }
509
510         etl::handle<synfigapp::CanvasInterface> canvas_interface() { return canvas_interface_; }
511
512         etl::handle<const synfigapp::CanvasInterface> canvas_interface()const { return canvas_interface_; }
513
514         void add_actions_to_menu(Gtk::Menu *menu,   const synfigapp::Action::ParamList &param_list, synfigapp::Action::Category category=synfigapp::Action::CATEGORY_ALL)const;
515
516         //! Updates the title of the window
517         void update_title();
518
519         //! Closes this document
520         bool close_instance();
521
522         //! Closes this canvas view
523         bool close_view();
524
525         //!     Stops the currently executing action
526         /*! \see get_cancel_status(), reset_cancel_status(), IsWorking */
527         void stop() { cancel=true; }
528
529         //! Returns the cancel status
530         /*! \see stop(), reset_cancel_status(), IsWorking */
531         bool get_cancel_status()const { return cancel; }
532
533         //! Resets the cancel status
534         /*! \see stop(), get_cancel_status(), IsWorking */
535         void reset_cancel_status() { cancel=false; }
536
537         void new_child_canvas();
538
539         //! Rebuilds layer_tree_store_ from the Canvas. Maintains selected items.
540         void rebuild_tables();
541
542         //! Builds layer_tree_store_ from the Canvas. Does not maintain selected items.
543         void build_tables();
544
545         //! Refreshes the data for the tables
546         void refresh_tables();
547
548         //void rebuild_layer_table();
549         //void build_layer_table();
550         //void refresh_layer_table();
551
552 //      void rebuild_canvas_table();
553 //      void build_canvas_table();
554 //      void refresh_canvas_table();
555
556 //      void rebuild_valuenode_table();
557 //      void build_valuenode_table();
558 //      void refresh_valuenode_table();
559
560         //! \writeme
561         void rebuild_ducks();
562
563         //bool add_to_ducks(synfigapp::ValueDesc value_desc, synfig::ParamDesc *param_desc=NULL);
564
565         //! Starts "playing" the animation in real-time
566         void play();
567
568         //! Shows the tables (Layer/Children)
569         void show_tables();
570
571         //! Hides the tables (Layer/Children)
572         void hide_tables();
573
574         //! Toggles the tables
575         void toggle_tables();
576
577         //! Gets the table status
578         bool tables_are_visible();
579
580         //! Shows the time bar
581         void show_timebar();
582
583         //! Hides the time bar
584         void hide_timebar();
585
586         //t Enables or disables interaction with the timebar
587         void set_sensitive_timebar(bool sensitive);
588
589         void time_zoom_in();
590         void time_zoom_out();
591
592         void add_layer(synfig::String x);
593
594         void show_keyframe_dialog();
595
596         void play_audio(float t);
597         void stop_audio();
598
599         void image_import();
600
601         void on_waypoint_clicked_canvasview(synfigapp::ValueDesc,std::set<synfig::Waypoint,std::less<synfig::UniqueID> >, int button);
602
603         void preview_option() {on_preview_option();}
604
605         void present();
606
607         bool is_playing() { return is_playing_; }
608
609         /*
610  -- ** -- S I G N A L   T E R M I N A L S -------------------------------------
611         */
612
613 private:
614
615         void on_select_layers();
616         void on_unselect_layers();
617
618         void on_input_device_changed(GdkDevice*);
619
620         virtual void on_hide();
621
622         virtual bool on_focus_in_event(GdkEventFocus*);
623         virtual bool on_focus_out_event(GdkEventFocus*);
624
625         //bool on_children_tree_event(GdkEvent *event);
626
627         bool on_keyframe_tree_event(GdkEvent *event);
628
629         //void on_children_edited_value(const Glib::ustring&path_string,synfig::ValueBase value);
630
631         void on_dirty_preview();
632
633         bool on_children_user_click(int, Gtk::TreeRow, ChildrenTree::ColumnID);
634
635         bool on_layer_user_click(int, Gtk::TreeRow, LayerTree::ColumnID);
636
637 //      void on_layer_toggle(const Glib::ustring& path_string, Gtk::TreeModelColumn<bool> column);
638
639         void on_mode_changed(synfigapp::CanvasInterface::Mode mode);
640
641 //      void on_layer_waypoint_clicked(const Glib::ustring &, synfig::ValueNode_Animated::WaypointList::iterator);
642
643         //void on_children_waypoint_clicked(const Glib::ustring &, synfig::ValueNode_Animated::WaypointList::iterator);
644
645         void on_waypoint_changed();
646
647         void on_waypoint_delete();
648
649         void on_refresh_pressed();
650
651         void on_id_changed();
652
653         void on_time_changed();
654
655         /*
656         void on_layer_raise_pressed();
657         void on_layer_lower_pressed();
658         void on_layer_duplicate_pressed();
659         void on_layer_delete_pressed();
660         */
661
662         void on_keyframe_add_pressed();
663
664         void on_keyframe_duplicate_pressed();
665
666         void on_keyframe_remove_pressed();
667
668         void on_animate_button_pressed();
669
670         void on_keyframe_button_pressed();
671
672         void on_preview_option();
673         void on_preview_create(const PreviewInfo &);
674
675         void on_audio_option();
676         void on_audio_file_change(const std::string &f);
677         void on_audio_offset_change(const synfig::Time &t);
678
679         void on_audio_file_notify();
680         void on_audio_offset_notify();
681
682         bool on_duck_changed(const synfig::Point &value,const synfigapp::ValueDesc& value_desc);
683         bool on_duck_angle_changed(const synfig::Angle &rotation,const synfigapp::ValueDesc& value_desc);
684
685         void on_layer_toggle(synfig::Layer::Handle);
686
687         void on_edited_value(synfigapp::ValueDesc,synfig::ValueBase);
688
689         void on_drop_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const Gtk::SelectionData& selection_data, guint info, guint time);
690
691         //void on_audio_play();
692         bool on_audio_scrub();
693
694         void on_play_stop_pressed();
695
696 protected:
697         bool close_instance_when_safe();
698         bool on_delete_event(GdkEventAny* event);
699
700         /*
701  -- ** -- S T A T I C   P U B L I C   M E T H O D S ---------------------------
702         */
703
704 public:
705
706         static etl::handle<studio::CanvasView> create(etl::loose_handle<Instance> instance,etl::handle<synfig::Canvas> canvas);
707         static std::list<int>& get_pixel_sizes();
708
709 }; // END of class CanvasView
710
711 }; // END of namespace studio
712
713 /* === E N D =============================================================== */
714
715 #endif