Implements PXEGeek's http://wiki.synfig.com/Wish_list entry: "Optionally display...
[synfig.git] / synfig-studio / trunk / src / gtkmm / renderer_canvas.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file renderer_canvas.cpp
3 **      \brief Template File
4 **
5 **      $Id$
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 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "renderer_canvas.h"
33 #include "workarea.h"
34 #include <ETL/misc>
35
36 #endif
37
38 /* === U S I N G =========================================================== */
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43 using namespace studio;
44
45 /* === M A C R O S ========================================================= */
46
47 /* === G L O B A L S ======================================================= */
48
49 /* === P R O C E D U R E S ================================================= */
50
51 /* === M E T H O D S ======================================================= */
52
53 Renderer_Canvas::~Renderer_Canvas()
54 {
55 }
56 std::vector< std::pair<Glib::RefPtr<Gdk::Pixbuf>,int> >&
57 Renderer_Canvas::get_tile_book()
58 {
59         return get_work_area()->get_tile_book();
60 }
61
62 bool
63 Renderer_Canvas::get_full_frame()const
64 {
65         return get_work_area()->get_full_frame();
66 }
67
68 int Renderer_Canvas::get_refreshes()const
69 {
70         return get_work_area()->get_refreshes();
71 }
72
73 bool
74 Renderer_Canvas::get_canceled()const
75 {
76         return get_work_area()->get_canceled();
77 }
78
79 bool
80 Renderer_Canvas::get_queued()const
81 {
82         return get_work_area()->get_queued();
83 }
84
85 bool
86 Renderer_Canvas::get_rendering()const
87 {
88         return get_work_area()->get_rendering();
89 }
90
91 void
92 Renderer_Canvas::render_vfunc(
93         const Glib::RefPtr<Gdk::Drawable>& drawable,
94         const Gdk::Rectangle& expose_area
95 )
96 {
97         assert(get_work_area());
98         if(!get_work_area())
99                 return;
100
101 //      const synfig::RendDesc &rend_desc(get_work_area()->get_canvas()->rend_desc());
102
103         const synfig::Vector focus_point(get_work_area()->get_focus_point());
104
105         std::vector< std::pair<Glib::RefPtr<Gdk::Pixbuf>,int> >& tile_book(get_tile_book());
106
107         int drawable_w,drawable_h;
108         drawable->get_size(drawable_w,drawable_h);
109
110         // Calculate the window coordinates of the top-left
111         // corner of the canvas.
112         const synfig::Vector::value_type
113                 x(focus_point[0]/get_pw()+drawable_w/2-get_w()/2),
114                 y(focus_point[1]/get_ph()+drawable_h/2-get_h()/2);
115
116         /*const synfig::Vector::value_type window_startx(window_tl[0]);
117         const synfig::Vector::value_type window_endx(window_br[0]);
118         const synfig::Vector::value_type window_starty(window_tl[1]);
119         const synfig::Vector::value_type window_endy(window_br[1]);
120         */
121         const int
122                 tile_w(get_work_area()->get_tile_w()),
123                 tile_h(get_work_area()->get_tile_h());
124
125         const int
126                 w(get_w()),
127                 h(get_h());
128
129         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(drawable));
130
131         if(!tile_book.empty())
132         if(get_full_frame())
133         {
134                 if(tile_book[0].first)
135                 {
136                         drawable->draw_pixbuf(
137                                 gc, //GC
138                                 tile_book[0].first, //pixbuf
139                                 0, 0,   // Source X and Y
140                                 round_to_int(x),round_to_int(y),        // Dest X and Y
141                                 -1,-1,  // Width and Height
142                                 Gdk::RGB_DITHER_MAX,            // RgbDither
143                                 2, 2 // Dither offset X and Y
144                         );
145                 }
146                 if(tile_book[0].second!=get_refreshes() && get_canceled()==false && get_rendering()==false && get_queued()==false)
147                         get_work_area()->async_update_preview();
148         }
149         else
150         {
151                 const int width_in_tiles(w/tile_w+(w%tile_w?1:0));
152                 const int height_in_tiles(h/tile_h+(h%tile_h?1:0));
153
154                 int u(0),v(0),tx,ty;
155                 int u1(0),v1(0),u2(width_in_tiles), v2(height_in_tiles);
156
157                 bool needs_refresh(false);
158
159                 u1=int(-x/tile_w);
160                 v1=int(-y/tile_h);
161                 u2=int((-x+drawable_w)/tile_w+1);
162                 v2=int((-y+drawable_h)/tile_h+1);
163                 if(u2>width_in_tiles)u2=width_in_tiles;
164                 if(v2>height_in_tiles)v2=height_in_tiles;
165                 if(u1<0)u1=0;
166                 if(v1<0)v1=0;
167
168                 for(v=v1;v<v2;v++)
169                 {
170                         for(u=u1;u<u2;u++)
171                         {
172                                 int index=v*width_in_tiles+u;
173                                 if(tile_book.size()>index && tile_book[index].first)
174                                 {
175                                         tx=u*tile_w;
176                                         ty=v*tile_w;
177
178                                         drawable->draw_pixbuf(
179                                                 gc, //GC
180                                                 tile_book[index].first, //pixbuf
181                                                 0, 0,   // Source X and Y
182                                                 round_to_int(x)+tx,round_to_int(y)+ty,  // Dest X and Y
183                                                 -1,-1,  // Width and Height
184                                                 Gdk::RGB_DITHER_MAX,            // RgbDither
185                                                 2, 2 // Dither offset X and Y
186                                         );
187                                 }
188                                 if(tile_book[index].second!=get_refreshes())
189                                         needs_refresh=true;
190                         }
191                 }
192                 if(needs_refresh==true && get_canceled()==false && get_rendering()==false && get_queued()==false)
193                 {
194                         //queue_render_preview();
195                         get_work_area()->async_update_preview();
196                         //update_preview();
197                         //return true;
198                 }
199
200         }
201
202         // Draw the border around the rendered region
203         {
204                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
205                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
206                 drawable->draw_rectangle(
207                         gc,
208                         false,  // Fill?
209                         round_to_int(x),round_to_int(y),        // x,y
210                         w,h     //w,h
211                 );
212         }
213 }