Synchronise toggle buttons with keyboard and menu actions.
[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         bool toggling_ducks_;
271
272
273         //! Shows current time and allows edition
274         Widget_Time *current_time_widget;
275         void on_current_time_widget_changed();
276
277         //! Time slider class. Same than the Time track panel
278         std::auto_ptr<Widget_Timeslider> timeslider;
279
280         std::list<sigc::connection> duck_changed_connections;
281
282 /*      DEBUGPOINT_CLASS(8);
283
284         Gtk::Menu duckmaskmenu;
285         DEBUGPOINT_CLASS(77);
286         Gtk::Menu qualitymenu;
287         DEBUGPOINT_CLASS(6);
288
289         Gtk::Menu filemenu;
290         DEBUGPOINT_CLASS(777);
291         Gtk::Menu editmenu;
292         DEBUGPOINT_CLASS(71);
293         Gtk::Menu canvasmenu;
294         DEBUGPOINT_CLASS(73);
295 public:
296         Gtk::Menu layermenu;
297 private:
298         DEBUGPOINT_CLASS(74);
299         Gtk::Menu newlayermenu;
300         DEBUGPOINT_CLASS(76);
301         Gtk::Menu viewmenu;
302
303         DEBUGPOINT_CLASS(99);
304         Gtk::Menu keyframemenu;
305
306         Gtk::Menu parammenu;
307         DEBUGPOINT_CLASS(9);
308         Gtk::Menu trackmenu;
309         DEBUGPOINT_CLASS(7);
310
311         Gtk::CheckMenuItem* duck_mask_position;
312         Gtk::CheckMenuItem* duck_mask_vertex;
313         Gtk::CheckMenuItem* duck_mask_tangent;
314         Gtk::CheckMenuItem* duck_mask_radius;
315         Gtk::CheckMenuItem* duck_mask_width;
316         Gtk::CheckMenuItem* duck_mask_angle;
317 */
318         //! Menu members
319         Gtk::Menu parammenu;
320
321         Glib::RefPtr<Gtk::ToggleAction> grid_snap_toggle;
322         Glib::RefPtr<Gtk::ToggleAction> grid_show_toggle;
323
324         Gtk::RadioButtonGroup quality_group;
325         Gtk::RadioButtonGroup low_res_pixel_size_group;
326
327         Glib::RefPtr<Gtk::ActionGroup> action_group;
328
329         etl::handle<synfigapp::UIInterface> ui_interface_;
330         etl::handle<synfigapp::SelectionManager> selection_manager_;
331
332         bool is_playing_;
333
334         sigc::signal<void> signal_deleted_;
335
336         bool rebuild_ducks_queued;
337         sigc::connection queue_rebuild_ducks_connection;
338
339         /*
340  -- ** -- P U B L I C   D A T A -----------------------------------------------
341         */
342
343 public:
344         void queue_rebuild_ducks();
345         sigc::signal<void>& signal_deleted() { return signal_deleted_; }
346
347         Gtk::Menu mainmenu;
348
349         bool duck_refresh_flag;
350         bool duck_refresh_needed;
351
352         //! This is for the IsWorking class.
353         int working_depth;
354
355         bool cancel;
356
357         /*
358  -- ** -- D I A L O G S -------------------------------------------------------
359         */
360
361 public:
362
363         CanvasProperties canvas_properties;
364         CanvasOptions canvas_options;
365         RenderSettings render_settings;
366         Dialog_Waypoint waypoint_dialog;
367         Dialog_Keyframe keyframe_dialog;
368
369         std::auto_ptr<Dialog_Preview>                   preview_dialog;
370         //std::auto_ptr<Dialog_PreviewOptions>  previewoption_dialog;
371         std::auto_ptr<Dialog_SoundSelect>               sound_dialog;
372
373         /*
374  -- ** -- P R I V A T E   M E T H O D S ---------------------------------------
375         */
376
377 private:
378
379         // Constructor is private to force the use of the "create()" constructor
380         CanvasView(etl::loose_handle<Instance> instance,etl::handle<synfigapp::CanvasInterface> canvas_interface);
381
382         //! Constructor Helper
383         // Gtk::Widget* create_layer_tree();
384
385         //! Constructor Helper
386         // Gtk::Widget* create_children_tree();
387
388         //! Constructor Helper
389         // Gtk::Widget* create_keyframe_tree();
390
391         //! Constructor Helper
392         Gtk::Widget* create_status_bar();
393
394         //! Constructor Helper - Initializes all of the menus
395         void init_menus();
396
397         bool duck_change_param(const synfig::Point &value,synfig::Layer::Handle layer, synfig::String param_name);
398
399         void refresh_time_window();
400
401         void time_was_changed();
402
403         void refresh_rend_desc();
404
405         void toggle_duck_mask(Duckmatic::Type type);
406
407         Gtk::Widget *create_work_area();
408
409         Gtk::Widget *create_time_bar();
410
411         //! Pop up menu for the bezier (bline, draw) tool (?)
412         void popup_param_menu_bezier(float location, synfigapp::ValueDesc value_desc)
413         { popup_param_menu(value_desc,location,true); }
414
415         //! Pop up menu for the tools but not the bezier ones.
416         void popup_param_menu(synfigapp::ValueDesc value_desc, float location=0, bool bezier=false);
417
418         void workarea_layer_selected(synfig::Layer::Handle layer);
419
420         void selected_layer_color_set(synfig::Color color);
421
422         void register_layer_type(synfig::Layer::Book::value_type &lyr,std::map<synfig::String,Gtk::Menu*>*);
423
424         //! Rebuilds the "new layer" menu
425         void build_new_layer_menu(Gtk::Menu &menu);
426
427         void rebuild_ducks_layer_(synfig::TransformStack& transform_stack, synfig::Canvas::Handle canvas, std::set<synfig::Layer::Handle>& selected_list);
428
429         void decrease_low_res_pixel_size();
430         void increase_low_res_pixel_size();
431
432         /*
433  -- ** -- P U B L I C   M E T H O D S -----------------------------------------
434         */
435
436 public:
437         const synfig::TransformStack& get_curr_transform_stack()const { return curr_transform_stack; }
438
439         const synfig::Rect& get_bbox()const { return bbox; }
440
441         Glib::RefPtr<Glib::ObjectBase> get_ref_obj(const synfig::String& x);
442         Glib::RefPtr<const Glib::ObjectBase> get_ref_obj(const synfig::String& x)const;
443         void set_ref_obj(const synfig::String& x, Glib::RefPtr<Glib::ObjectBase> y);
444
445         Glib::RefPtr<Gtk::TreeModel> get_tree_model(const synfig::String& x);
446         Glib::RefPtr<const Gtk::TreeModel> get_tree_model(const synfig::String& x)const;
447         void set_tree_model(const synfig::String& x, Glib::RefPtr<Gtk::TreeModel> y);
448
449         Gtk::Widget* get_ext_widget(const synfig::String& x);
450         void set_ext_widget(const synfig::String& x, Gtk::Widget* y);
451
452         //std::map<synfig::String,Gtk::Widget*>& tree_view_book() { return tree_view_book_; }
453         //std::map<synfig::String,Gtk::Widget*>& ext_widget_book() { return tree_view_book_; }
454
455         void popup_main_menu();
456
457         Smach& get_smach() { return smach_; }
458
459         const Smach& get_smach()const { return smach_; }
460
461         Smach::event_result process_event_key(EventKey x);
462
463         void popup_layer_menu(synfig::Layer::Handle layer);
464
465         virtual ~CanvasView();
466
467         void set_mode(Mode x) { canvas_interface()->set_mode(x); }
468
469         Mode get_mode()const { return canvas_interface()->get_mode(); }
470
471         Gtk::Adjustment &time_adjustment() { return time_adjustment_; }
472
473         const Gtk::Adjustment &time_adjustment()const { return time_adjustment_; }
474
475         studio::Adjust_Window &time_window_adjustment() { return time_window_adjustment_; }
476
477         const studio::Adjust_Window &time_window_adjustment()const { return time_window_adjustment_; }
478
479         etl::handle<synfigapp::UIInterface> get_ui_interface() { return ui_interface_;}
480
481         etl::handle<synfigapp::SelectionManager> get_selection_manager() { return selection_manager_; }
482
483         Glib::RefPtr<Gtk::TreeModel> layer_tree_store() { return get_tree_model("layers"); }
484
485         Glib::RefPtr<const Gtk::TreeModel> layer_tree_store()const { return get_tree_model("layers"); }
486
487         Glib::RefPtr<Gtk::TreeModel> children_tree_store() { return get_tree_model("children"); }
488
489         Glib::RefPtr<const Gtk::TreeModel> children_tree_store()const { return get_tree_model("children"); }
490
491         Glib::RefPtr<Gtk::TreeModel> keyframe_tree_store() { return get_tree_model("keyframes"); }
492
493         Glib::RefPtr<const Gtk::TreeModel> keyframe_tree_store()const { return get_tree_model("keyframes"); }
494
495         void set_time(synfig::Time t) { canvas_interface_->set_time(t); }
496
497         synfig::Time get_time() { return canvas_interface_->get_time(); }
498
499         etl::handle<synfig::Canvas> get_canvas()const { return canvas_interface_->get_canvas(); }
500
501         etl::handle<Instance> get_instance()const { return instance_; }
502
503         etl::handle<synfigapp::CanvasInterface> canvas_interface() { return canvas_interface_; }
504
505         etl::handle<const synfigapp::CanvasInterface> canvas_interface()const { return canvas_interface_; }
506
507         void add_actions_to_menu(Gtk::Menu *menu,   const synfigapp::Action::ParamList &param_list, synfigapp::Action::Category category=synfigapp::Action::CATEGORY_ALL)const;
508
509         //! Updates the title of the window
510         void update_title();
511
512         //! Closes this document
513         bool close_instance();
514
515         //! Closes this canvas view
516         bool close_view();
517
518         //!     Stops the currently executing action
519         /*! \see get_cancel_status(), reset_cancel_status(), IsWorking */
520         void stop() { cancel=true; }
521
522         //! Returns the cancel status
523         /*! \see stop(), reset_cancel_status(), IsWorking */
524         bool get_cancel_status()const { return cancel; }
525
526         //! Resets the cancel status
527         /*! \see stop(), get_cancel_status(), IsWorking */
528         void reset_cancel_status() { cancel=false; }
529
530         void new_child_canvas();
531
532         //! Rebuilds layer_tree_store_ from the Canvas. Maintains selected items.
533         void rebuild_tables();
534
535         //! Builds layer_tree_store_ from the Canvas. Does not maintain selected items.
536         void build_tables();
537
538         //! Refreshes the data for the tables
539         void refresh_tables();
540
541         //void rebuild_layer_table();
542         //void build_layer_table();
543         //void refresh_layer_table();
544
545 //      void rebuild_canvas_table();
546 //      void build_canvas_table();
547 //      void refresh_canvas_table();
548
549 //      void rebuild_valuenode_table();
550 //      void build_valuenode_table();
551 //      void refresh_valuenode_table();
552
553         //! \writeme
554         void rebuild_ducks();
555
556         //bool add_to_ducks(synfigapp::ValueDesc value_desc, synfig::ParamDesc *param_desc=NULL);
557
558         //! Starts "playing" the animation in real-time
559         void play();
560
561         //! Shows the tables (Layer/Children)
562         void show_tables();
563
564         //! Hides the tables (Layer/Children)
565         void hide_tables();
566
567         //! Toggles the tables
568         void toggle_tables();
569
570         //! Gets the table status
571         bool tables_are_visible();
572
573         //! Shows the time bar
574         void show_timebar();
575
576         //! Hides the time bar
577         void hide_timebar();
578
579         //t Enables or disables interaction with the timebar
580         void set_sensitive_timebar(bool sensitive);
581
582         void time_zoom_in();
583         void time_zoom_out();
584
585         void add_layer(synfig::String x);
586
587         void show_keyframe_dialog();
588
589         void play_audio(float t);
590         void stop_audio();
591
592         void image_import();
593
594         void on_waypoint_clicked_canvasview(synfigapp::ValueDesc,std::set<synfig::Waypoint,std::less<synfig::UniqueID> >, int button);
595
596         void preview_option() {on_preview_option();}
597
598         void present();
599
600         bool is_playing() { return is_playing_; }
601
602         /*
603  -- ** -- S I G N A L   T E R M I N A L S -------------------------------------
604         */
605
606 private:
607
608         void on_select_layers();
609         void on_unselect_layers();
610
611         void on_input_device_changed(GdkDevice*);
612
613         virtual void on_hide();
614
615         virtual bool on_focus_in_event(GdkEventFocus*);
616         virtual bool on_focus_out_event(GdkEventFocus*);
617
618         //bool on_children_tree_event(GdkEvent *event);
619
620         bool on_keyframe_tree_event(GdkEvent *event);
621
622         //void on_children_edited_value(const Glib::ustring&path_string,synfig::ValueBase value);
623
624         void on_dirty_preview();
625
626         bool on_children_user_click(int, Gtk::TreeRow, ChildrenTree::ColumnID);
627
628         bool on_layer_user_click(int, Gtk::TreeRow, LayerTree::ColumnID);
629
630 //      void on_layer_toggle(const Glib::ustring& path_string, Gtk::TreeModelColumn<bool> column);
631
632         void on_mode_changed(synfigapp::CanvasInterface::Mode mode);
633
634 //      void on_layer_waypoint_clicked(const Glib::ustring &, synfig::ValueNode_Animated::WaypointList::iterator);
635
636         //void on_children_waypoint_clicked(const Glib::ustring &, synfig::ValueNode_Animated::WaypointList::iterator);
637
638         void on_waypoint_changed();
639
640         void on_waypoint_delete();
641
642         void on_refresh_pressed();
643
644         void on_id_changed();
645
646         void on_time_changed();
647
648         /*
649         void on_layer_raise_pressed();
650         void on_layer_lower_pressed();
651         void on_layer_duplicate_pressed();
652         void on_layer_delete_pressed();
653         */
654
655         void on_keyframe_add_pressed();
656
657         void on_keyframe_duplicate_pressed();
658
659         void on_keyframe_remove_pressed();
660
661         void on_animate_button_pressed();
662
663         void on_keyframe_button_pressed();
664
665         void on_preview_option();
666         void on_preview_create(const PreviewInfo &);
667
668         void on_audio_option();
669         void on_audio_file_change(const std::string &f);
670         void on_audio_offset_change(const synfig::Time &t);
671
672         void on_audio_file_notify();
673         void on_audio_offset_notify();
674
675         bool on_duck_changed(const synfig::Point &value,const synfigapp::ValueDesc& value_desc);
676         bool on_duck_angle_changed(const synfig::Angle &rotation,const synfigapp::ValueDesc& value_desc);
677
678         void on_layer_toggle(synfig::Layer::Handle);
679
680         void on_edited_value(synfigapp::ValueDesc,synfig::ValueBase);
681
682         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);
683
684         //void on_audio_play();
685         bool on_audio_scrub();
686
687         void on_play_stop_pressed();
688
689 protected:
690         bool close_instance_when_safe();
691         bool on_delete_event(GdkEventAny* event);
692
693         /*
694  -- ** -- S T A T I C   P U B L I C   M E T H O D S ---------------------------
695         */
696
697 public:
698
699         static etl::handle<studio::CanvasView> create(etl::loose_handle<Instance> instance,etl::handle<synfig::Canvas> canvas);
700         static std::list<int>& get_pixel_sizes();
701
702 }; // END of class CanvasView
703
704 }; // END of namespace studio
705
706 /* === E N D =============================================================== */
707
708 #endif