more updates
[synfig.git] / synfig-studio / trunk / src / gtkmm / workarea.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file workarea.h
3 **      \brief Template Header
4 **
5 **      $Id: workarea.h,v 1.1.1.1 2005/01/07 03:34:37 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === S T A R T =========================================================== */
24
25 #ifndef __SYNFIG_GTKMM_WORKAREA_H
26 #define __SYNFIG_GTKMM_WORKAREA_H
27
28 /* === H E A D E R S ======================================================= */
29
30 #include <list>
31 #include <map>
32 #include <set>
33
34 #include <ETL/smart_ptr>
35 #include <ETL/handle>
36
37 #include <gtkmm/drawingarea.h>
38 #include <gtkmm/table.h>
39 #include <gtkmm/adjustment.h>
40 #include <gtkmm/ruler.h>
41 #include <gtkmm/image.h>
42 #include <gdkmm/pixbuf.h>
43 #include <gdkmm/cursor.h>
44 #include <gdkmm/device.h>
45
46 #include <synfig/time.h>
47 #include <synfig/vector.h>
48 #include <synfig/general.h>
49 #include <synfig/renddesc.h>
50 #include <synfig/canvas.h>
51
52 #include "zoomdial.h"
53 #include "duckmatic.h"
54 #include "instance.h"
55
56 /* === M A C R O S ========================================================= */
57
58 /* === T Y P E D E F S ===================================================== */
59
60 /* === C L A S S E S & S T R U C T S ======================================= */
61
62 /*
63 namespace etl {
64
65 template <typename T_, typename C_=std::less<T_,T_> >
66 class dereferenced_compare
67 {
68 public:
69         typedef etl::loose_handle<T_> first_argument_type;
70         typedef etl::loose_handle<T_> second_argument_type;
71         typedef bool result_type;
72         
73 }
74 };
75 */
76
77 class WorkAreaTarget;
78 class WorkAreaTarget_Full;
79 namespace synfigapp { class CanvasInterface; };
80
81 namespace synfig { class Layer; };
82 namespace Gtk { class Frame; };
83
84 namespace studio
85 {
86 class Instance;
87 class CanvasView;
88 class WorkArea;
89 class WorkAreaRenderer;
90 class AsyncRenderer;
91 class DirtyTrap
92 {
93         friend class WorkArea;
94         WorkArea *work_area;
95 public:
96         DirtyTrap(WorkArea *work_area);
97         ~DirtyTrap();
98 };
99
100
101 class WorkArea : public Gtk::Table, public Duckmatic
102 {
103         friend class WorkAreaTarget;
104         friend class WorkAreaTarget_Full;
105         friend class DirtyTrap;
106         friend class WorkAreaRenderer;
107                 
108         /*
109  -- ** -- P U B L I C   T Y P E S ---------------------------------------------
110         */
111         
112 public:
113
114         void insert_renderer(const etl::handle<WorkAreaRenderer> &x);
115         void insert_renderer(const etl::handle<WorkAreaRenderer> &x,int priority);
116         void erase_renderer(const etl::handle<WorkAreaRenderer> &x);
117         void resort_render_set();
118
119         enum DragMode
120         {
121                 DRAG_NONE=0,
122                 DRAG_WINDOW,
123                 DRAG_DUCK,
124                 DRAG_GUIDE,
125                 DRAG_BOX
126         };
127
128         /*
129  -- ** -- P R I V A T E   D A T A ---------------------------------------------
130         */
131
132 private:
133
134         std::set<etl::handle<WorkAreaRenderer> > renderer_set_;
135
136         etl::handle<studio::AsyncRenderer> async_renderer;
137
138
139         etl::loose_handle<synfigapp::CanvasInterface> canvas_interface;
140         etl::handle<synfig::Canvas> canvas;
141         etl::loose_handle<studio::Instance> instance;
142         etl::loose_handle<studio::CanvasView> canvas_view;
143
144         // Widgets
145         Gtk::DrawingArea *drawing_area;
146         Gtk::Adjustment scrollx_adjustment;
147         Gtk::Adjustment scrolly_adjustment;
148         Gtk::VRuler *vruler;
149         Gtk::HRuler *hruler;
150         Gtk::Button *menubutton;
151         Gtk::Frame *drawing_frame;
152
153         GdkDevice* curr_input_device;
154         
155         // Bleh!
156         int     w;                                              //!< Width of the image (in pixels)
157         int     h;                                              //!< Height of the image (in pixels)
158         synfig::Real    canvaswidth;    //!< Width of the canvas
159         synfig::Real    canvasheight;   //!< Height of the canvas
160         synfig::Real    pw;                             //!< The width of a pixel
161         synfig::Real    ph;                             //!< The height of a pixel
162         float zoom;                                     //!< Zoom factor
163         float prev_zoom;                        //!< Previous Zoom factor
164         synfig::Point window_tl;                //!< The (theoretical) top-left corner of the view window
165         synfig::Point window_br;                //!< The (theoretical) bottom-right corner of the view window
166
167         guint32 last_event_time;
168
169         int bpp;
170         //unsigned char *buffer;
171         
172         //! ???
173         synfig::ProgressCallback *progresscallback;
174
175         //! ???
176         synfig::RendDesc desc;
177         
178         //! This flag is set if the user is dragging the video window
179         /*! \see drag_point */
180         DragMode dragging;
181                 
182         etl::handle<Duckmatic::Duck> clicked_duck;
183         etl::handle<Duckmatic::Duck> hover_duck;
184
185         //! When dragging the viewport, this is set to the origin of the drag
186         synfig::Point drag_point;
187
188         synfig::Point curr_point;
189
190         //! ???
191         synfig::Point previous_focus;
192
193         //! This flag is set if the grid should be drawn
194         bool show_grid;
195
196         //! This flag is set if the guides should be drawn
197         bool show_guides;
198
199         bool low_resolution;
200         
201         bool meta_data_lock;
202
203         //! This flag is set if the entire frame is rendered rather than using tiles    
204         bool full_frame;
205         
206         //Glib::RefPtr<Gdk::Pixbuf> pix_buf;
207         
208         //! This vector holds all of the tiles for this image
209         std::vector< std::pair<Glib::RefPtr<Gdk::Pixbuf>,int> > tile_book;
210
211         //! This integer describes the total times that the work are has been refreshed
212         int refreshes;
213         
214         //! This list holds the queue of tiles that need to be rendered
215         //std::list<int> tile_queue;
216         
217         int tile_w, tile_h;
218
219         gint render_idle_func_id;
220
221         //! The coordinates of the focus the last time a part of the screen was refreshed
222         synfig::Point last_focus_point;
223
224         bool canceled_;
225         
226         int quality;
227
228         bool dirty_trap_enabled;
229         
230         int dirty_trap_queued;
231         
232         
233         bool onion_skin;
234                 
235         etl::loose_handle<synfig::ValueNode> selected_value_node_;
236
237
238         /*
239  -- ** -- P U B L I C   D A T A -----------------------------------------------
240         */
241
242 public:
243
244         const etl::loose_handle<synfig::ValueNode>& get_selected_value_node() { return  selected_value_node_; }
245         const synfig::Point& get_drag_point()const { return drag_point; }
246         std::vector< std::pair<Glib::RefPtr<Gdk::Pixbuf>,int> >& get_tile_book(){ return tile_book; }
247         int get_refreshes()const { return refreshes; }
248         bool get_canceled()const { return canceled_; }
249         bool get_queued()const { return queued; }
250         bool get_rendering()const { return rendering; }
251         bool get_full_frame()const { return full_frame; }
252         //int get_w()const { return w; }
253         //int get_h()const { return h; }
254
255         int get_tile_w()const { return tile_w; }
256         int get_tile_h()const { return tile_h; }
257         
258         bool solid_lines;
259         bool rendering;
260         bool dirty;
261         bool queued;
262         bool cancel;
263         bool allow_layer_clicks;
264         bool allow_duck_clicks;
265
266         GuideList::iterator curr_guide;
267         bool curr_guide_is_x;
268
269         /*
270  -- ** -- P R I V A T E   M E T H O D S ---------------------------------------
271         */
272
273 private:
274
275         //unsigned char *get_buffer() { return buffer; }
276         bool set_wh(int w, int h,int chan=3);
277
278         int next_unrendered_tile(int refreshes)const;
279         int next_unrendered_tile()const { return next_unrendered_tile(refreshes); }
280
281         /*
282  -- ** -- S I G N A L S -------------------------------------------------------
283         */
284
285 private:
286
287         sigc::signal<void,GdkDevice* > signal_input_device_changed_;
288
289         //! One signal per button
290         sigc::signal<void,synfig::Point> signal_user_click_[5];
291
292         sigc::signal<void> signal_popup_menu_;
293
294         sigc::signal<void> signal_cursor_moved_;
295         sigc::signal<void> signal_rendering_;
296
297         sigc::signal<void> signal_onion_skin_changed_;
298
299         //! Signal for when the user clicks on a layer
300         sigc::signal<void, etl::handle<synfig::Layer> > signal_layer_selected_;
301
302         sigc::signal<void> signal_view_window_changed_;
303
304 public:
305
306         sigc::signal<void>& signal_onion_skin_changed() { return signal_onion_skin_changed_; }
307
308         sigc::signal<void>& signal_rendering() { return signal_rendering_; }
309
310         sigc::signal<void>& signal_cursor_moved() { return signal_cursor_moved_; }
311
312         sigc::signal<void>& signal_view_window_changed() { return signal_view_window_changed_; }
313         void view_window_changed() { signal_view_window_changed()(); }
314
315         sigc::signal<void,GdkDevice* >& signal_input_device_changed() { return signal_input_device_changed_; }
316
317         sigc::signal<void> &signal_popup_menu() { return signal_popup_menu_; }
318
319         //! One signal per button (5 buttons)
320         sigc::signal<void,synfig::Point> &signal_user_click(int button=0){ return signal_user_click_[button]; }
321
322         sigc::signal<void, etl::handle<synfig::Layer> >& signal_layer_selected() { return signal_layer_selected_; }
323
324         /*
325  -- ** -- P U B L I C   M E T H O D S -----------------------------------------
326         */
327
328 public:
329         void set_onion_skin(bool x);
330         bool get_onion_skin()const;
331         void toggle_onion_skin() { set_onion_skin(!get_onion_skin()); }
332
333         void set_selected_value_node(etl::loose_handle<synfig::ValueNode> x);
334
335         bool is_dragging() { return dragging!=DRAG_NONE; }
336
337         DragMode get_dragging_mode() { return dragging; }
338
339         WorkArea(etl::loose_handle<synfigapp::CanvasInterface> canvas_interface);
340         virtual ~WorkArea();
341
342         void set_cursor(const Gdk::Cursor& x);
343         void set_cursor(Gdk::CursorType x);
344
345         const synfig::Point& get_cursor_pos()const { return curr_point; }
346
347         Gtk::Adjustment *get_scrollx_adjustment() { return &scrollx_adjustment; }
348         Gtk::Adjustment *get_scrolly_adjustment() { return &scrolly_adjustment; }
349         const Gtk::Adjustment *get_scrollx_adjustment()const { return &scrollx_adjustment; }
350         const Gtk::Adjustment *get_scrolly_adjustment()const { return &scrolly_adjustment; }
351
352         void set_instance(etl::loose_handle<studio::Instance> x) { instance=x; }
353         void set_canvas(etl::handle<synfig::Canvas> x) { canvas=x; }
354         void set_canvas_view(etl::loose_handle<studio::CanvasView> x) { canvas_view=x; }
355         etl::handle<synfig::Canvas> get_canvas()const { return canvas; }
356         etl::handle<studio::Instance> get_instance()const { return instance; }
357         etl::loose_handle<studio::CanvasView> get_canvas_view()const { return canvas_view; }
358
359         void refresh_dimension_info();
360         
361         //! Enables showing of the grid
362         void enable_grid();
363         
364         //! Disables showing of the grid
365         void disable_grid();
366         
367         //! Toggles the showing of the grid
368         void toggle_grid();
369         
370         //! Returns the state of the show_grid flag
371         bool grid_status()const { return show_grid; }
372
373         void toggle_grid_snap() { Duckmatic::toggle_grid_snap(); }
374
375         bool get_show_guides()const { return show_guides; }
376         void set_show_guides(bool x);
377         void toggle_show_guides() { set_show_guides(!get_show_guides()); }
378                 
379         bool get_low_resolution_flag()const { return low_resolution; }
380         void set_low_resolution_flag(bool x);
381         void toggle_low_resolution_flag();
382         
383         //! ???
384         void queue_scroll();
385         
386         //! Sets the size of the grid
387         void set_grid_size(const synfig::Vector &s);
388                 
389         //! ??
390         void popup_menu();
391         
392         int get_quality()const { return quality; }
393
394         void set_quality(int x);
395         
396
397         int get_w()const { return w; }
398         int get_h()const { return h; }
399         int get_bpp()const { return bpp; }
400
401         //! ??
402         const synfig::RendDesc &get_rend_desc()const { return desc; }
403         
404         //! ??
405         void set_rend_desc(const synfig::RendDesc &x) { desc=x; }
406         
407         //! Converts screen coords (ie: pixels) to composition coordinates
408         synfig::Point screen_to_comp_coords(synfig::Point pos)const;
409
410         //! Converts composition coordinates to screen coords (ie: pixels)
411         synfig::Point comp_to_screen_coords(synfig::Point pos)const;
412
413         float get_pw()const { return pw; }
414         float get_ph()const { return ph; }
415         
416         const synfig::Point &get_window_tl()const { return window_tl; }
417         const synfig::Point &get_window_br()const { return window_br; }
418
419
420         bool async_update_preview();
421         void async_update_finished();
422         void async_render_preview(synfig::Time time);
423         void async_render_preview();
424         
425         bool sync_update_preview();
426         bool sync_render_preview(synfig::Time time);
427         bool sync_render_preview();
428         void sync_render_preview_hook();
429
430         void queue_render_preview();
431         
432         
433         void queue_draw_preview();
434         
435         void zoom_in();
436         void zoom_out();
437         void zoom_fit();
438         void zoom_norm();
439         float get_zoom()const { return zoom; }
440         
441         void set_zoom(float z);
442
443
444         void set_progress_callback(synfig::ProgressCallback *x) { progresscallback=x; }
445         synfig::ProgressCallback *get_progress_callback() { return progresscallback; }
446
447         void set_focus_point(const synfig::Point &x);
448
449         synfig::Point get_focus_point()const;
450
451         void done_rendering();
452         
453         bool refresh(GdkEventExpose*bleh=NULL);
454         
455         void reset_cursor();
456         void refresh_cursor();
457
458         void save_meta_data();
459         void load_meta_data();
460         
461         /*
462  -- ** -- S I G N A L   T E R M I N A L S -------------------------------------
463         */
464
465 private:
466         bool on_key_press_event(GdkEventKey* event);
467         bool on_drawing_area_event(GdkEvent* event);
468         bool on_hruler_event(GdkEvent* event);
469         bool on_vruler_event(GdkEvent* event);
470
471         /*
472  -- ** -- S T A T I C   P U B L I C   M E T H O D S ---------------------------
473         */
474
475 public:
476
477         /*
478  -- ** -- S T A T I C   P R I V A T E   M E T H O D S -------------------------
479         */
480
481 private:
482
483         static gboolean __render_preview(gpointer data);
484
485 }; // END of class WorkArea
486
487 }; // END of namespace studio
488
489 /* === E N D =============================================================== */
490
491 #endif