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