grab stable branch
[synfig.git] / synfig-studio / tags / stable / src / gtkmm / preview.h
1 /* === S I N F G =========================================================== */
2 /*!     \file preview.h
3 **      \brief Previews an animation
4 **
5 **      $Id: preview.h,v 1.2 2005/01/10 08:13:44 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === S T A R T =========================================================== */
23
24 #ifndef __SINFG_PREVIEW_H
25 #define __SINFG_PREVIEW_H
26
27 /* === H E A D E R S ======================================================= */
28 #include <gtkmm/drawingarea.h>
29 #include <gtkmm/table.h>
30 #include <gtkmm/adjustment.h>
31 #include <gtkmm/image.h>
32 #include <gdkmm/pixbuf.h>
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/scrollbar.h>
35 #include <gtkmm/checkbutton.h>
36 #include <gtkmm/canvasview.h>
37
38 #include <sinfg/time.h>
39 #include <sinfg/vector.h>
40 #include <sinfg/general.h>
41 #include <sinfg/renddesc.h>
42 #include <sinfg/canvas.h>
43
44 #include "widget_sound.h"
45
46 #include <ETL/handle>
47 #include <ETL/clock>
48
49 #include <vector>
50
51 /* === M A C R O S ========================================================= */
52
53 /* === T Y P E D E F S ===================================================== */
54
55 /* === C L A S S E S & S T R U C T S ======================================= */
56
57 namespace studio {
58 class AsyncRenderer;
59         
60 class Preview : public sigc::trackable, public etl::shared_object
61 {
62 public:
63         struct FlipbookElem
64         {
65                 float                                           t;
66                 Glib::RefPtr<Gdk::Pixbuf>       buf; //at whatever resolution they are rendered at (resized at run time)
67         };
68
69         etl::handle<studio::AsyncRenderer>      renderer;
70         
71         sigc::signal<void, Preview *>   signal_destroyed_;      //so things can reference us without fear
72         
73         typedef std::vector<FlipbookElem>        FlipBook;
74 private:
75         
76         FlipBook                        frames;
77         
78         studio::CanvasView::LooseHandle canvasview;
79
80         //sinfg::RendDesc               description; //for rendering the preview...
81         float   zoom,fps;
82         float   begintime,endtime;
83         bool    overbegin,overend;
84         int             quality;
85
86         float   global_fps;
87
88         //expose the frame information etc.
89         class Preview_Target;
90         void frame_finish(const Preview_Target *);
91         
92         sigc::signal0<void>     sig_changed;
93         
94 public:
95                 
96         Preview(const studio::CanvasView::LooseHandle &h = studio::CanvasView::LooseHandle(), 
97                                 float zoom = 0.5f, float fps = 15);
98         ~Preview();
99         
100         float   get_zoom() const {return zoom;}
101         void    set_zoom(float z){zoom = z;}
102         
103         float   get_fps() const {return fps;}
104         void    set_fps(float f){fps = f;}
105         
106         float   get_global_fps() const {return global_fps;}
107         void    set_global_fps(float f){global_fps = f;}
108         
109         float   get_begintime() const   
110         {
111                 if(overbegin)
112                         return begintime;
113                 else if(canvasview)
114                         return get_canvas()->rend_desc().get_time_start();
115                 else return -1;
116         }
117         
118         float   get_endtime() const
119         {
120                 if(overend)
121                         return endtime;
122                 else if(canvasview)
123                         return get_canvas()->rend_desc().get_time_end();
124                 else return -1;
125         }
126         
127         void    set_begintime(float t)  {begintime = t;}
128         void    set_endtime(float t)    {endtime = t;}
129         
130         bool get_overbegin() const {return overbegin;}
131         void set_overbegin(bool b) {overbegin = b;}
132         
133         bool get_overend() const {return overend;}
134         void set_overend(bool b) {overend = b;}
135         
136         int             get_quality() const {return quality;}
137         void    set_quality(int i)      {quality = i;}
138         
139         sinfg::Canvas::Handle   get_canvas() const {return canvasview->get_canvas();}
140         studio::CanvasView::Handle      get_canvasview() const {return canvasview;}
141         
142         void set_canvasview(const studio::CanvasView::LooseHandle &h);
143         
144         //signal interface
145         sigc::signal<void, Preview *> & signal_destroyed() { return signal_destroyed_; }
146         //sigc::signal<void, const sinfg::RendDesc &>   &signal_desc_change() {return signal_desc_change_;}
147         
148         //functions for exposing iterators through the preview
149         FlipBook::iterator      begin()         {return frames.begin();}
150         FlipBook::iterator      end()           {return frames.end();}
151         
152         FlipBook::const_iterator        begin() const {return frames.begin();}
153         FlipBook::const_iterator        end() const       {return frames.end();}
154         
155         void clear() {frames.clear();}
156         
157         unsigned int                            numframes() const  {return frames.size();}
158         
159         void render();
160         
161         sigc::signal0<void>     &signal_changed() { return sig_changed; }
162 };
163
164 class Widget_Preview : public Gtk::Table
165 {
166         Gtk::DrawingArea        draw_area;
167         Gtk::Adjustment         adj_time_scrub; //the adjustment for the managed scrollbar
168         Gtk::HScrollbar         scr_time_scrub;
169         Gtk::ToggleButton       b_loop;
170                 
171         //Glib::RefPtr<Gdk::GC>         gc_area;
172         Glib::RefPtr<Gdk::Pixbuf>       currentbuf;
173         int                                                     currentindex;
174         //double                                                timeupdate;
175         double                                          timedisp;
176         double                                          audiotime;
177         
178         //sound stuff
179         etl::handle<AudioContainer>     audio;
180         sigc::connection        scrstartcon;
181         sigc::connection        scrstopcon;
182         sigc::connection        scrubcon;
183         
184         //preview encapsulation
185         etl::handle<Preview>    preview;
186         sigc::connection                prevchanged;
187         
188         Widget_Sound                    disp_sound;
189         Gtk::Adjustment                 adj_sound;
190         
191         Gtk::Label                              l_lasttime;
192         
193         //only for internal stuff, doesn't set anything
194         bool    playing;
195         bool    singleframe;
196         
197         //for accurate time tracking
198         etl::clock      timer;
199         
200         //int           curindex; //for later   
201         SigC::Connection        timecon;
202         
203         void slider_move(); //later to be a time_slider that's cooler
204         bool play_update();
205         void play_stop();
206         //bool play_frameupdate();
207         void update();
208                 
209         void scrub_updated(double t);
210         
211         void repreview();
212         
213         void whenupdated();
214         
215         void eraseall();
216         
217         bool scroll_move_event(GdkEvent *);
218         void disconnect_preview(Preview *);
219         
220         bool redraw(GdkEventExpose *heh = 0);
221         void preview_draw();
222         
223         sigc::signal<void,float>        signal_play_;
224         sigc::signal<void>                      signal_stop_;
225         sigc::signal<void,float>        signal_seek_;
226         
227 public:
228         
229         Widget_Preview();
230         ~Widget_Preview();
231         
232         //sets a signal to identify disconnection (so we don't hold onto it)...
233         void set_preview(etl::handle<Preview> prev);
234         void set_audioprofile(etl::handle<AudioProfile> p);
235         void set_audio(etl::handle<AudioContainer> a);
236
237         void clear();
238
239         void play();
240         void stop();
241         void seek(float t);
242
243         void stoprender();
244
245         sigc::signal<void,float>        &signal_play() {return signal_play_;}
246         sigc::signal<void>      &signal_stop() {return signal_stop_;}
247         sigc::signal<void,float>        &signal_seek() {return signal_seek_;}
248
249         bool get_loop_flag() const {return b_loop.get_active();}
250         void set_loop_flag(bool b) {return b_loop.set_active(b);}
251 };
252         
253 }; // END of namespace studio
254
255 /* === E N D =============================================================== */
256
257 #endif