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