Implements PXEGeek's http://wiki.synfig.com/Wish_list entry: "Optionally display...
[synfig.git] / synfig-studio / trunk / src / gtkmm / renderer_grid.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file renderer_grid.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_grid.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_Grid::~Renderer_Grid()
54 {
55 }
56
57 bool
58 Renderer_Grid::get_enabled_vfunc()const
59 {
60         return get_work_area()->grid_status();
61 }
62
63 synfig::Vector
64 Renderer_Grid::get_grid_size()const
65 {
66         return get_work_area()->get_grid_size();
67 }
68
69 void
70 Renderer_Grid::render_vfunc(
71         const Glib::RefPtr<Gdk::Drawable>& drawable,
72         const Gdk::Rectangle& expose_area
73 )
74 {
75         assert(get_work_area());
76         if(!get_work_area())
77                 return;
78
79 //      const synfig::RendDesc &rend_desc(get_work_area()->get_canvas()->rend_desc());
80
81         const synfig::Vector focus_point(get_work_area()->get_focus_point());
82
83 //      std::vector< std::pair<Glib::RefPtr<Gdk::Pixbuf>,int> >& tile_book(get_tile_book());
84
85         int drawable_w,drawable_h;
86         drawable->get_size(drawable_w,drawable_h);
87
88         // Calculate the window coordinates of the top-left
89         // corner of the canvas.
90 //      const synfig::Vector::value_type
91 //              x(focus_point[0]/get_pw()+drawable_w/2-get_w()/2),
92 //              y(focus_point[1]/get_ph()+drawable_h/2-get_h()/2);
93
94         /*const synfig::Vector::value_type window_startx(window_tl[0]);
95         const synfig::Vector::value_type window_endx(window_br[0]);
96         const synfig::Vector::value_type window_starty(window_tl[1]);
97         const synfig::Vector::value_type window_endy(window_br[1]);
98         */
99 //      const int
100 //              tile_w(get_work_area()->get_tile_w()),
101 //              tile_h(get_work_area()->get_tile_h());
102
103 //      const int
104 //              w(get_w()),
105 //              h(get_h());
106
107         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(drawable));
108
109         const synfig::Vector grid_size(get_grid_size());
110
111         const synfig::Vector::value_type window_startx(get_work_area()->get_window_tl()[0]);
112         const synfig::Vector::value_type window_endx(get_work_area()->get_window_br()[0]);
113         const synfig::Vector::value_type window_starty(get_work_area()->get_window_tl()[1]);
114         const synfig::Vector::value_type window_endy(get_work_area()->get_window_br()[1]);
115         const float pw(get_pw()),ph(get_ph());
116
117         // Draw out the grid
118         if(grid_size[0]>pw*3.5 && grid_size[1]>ph*3.5)
119         {
120                 synfig::Vector::value_type x,y;
121
122                 x=floor(window_startx/grid_size[0])*grid_size[0];
123                 y=floor(window_starty/grid_size[1])*grid_size[1];
124
125                 gc->set_function(Gdk::COPY);
126                 gc->set_rgb_fg_color(Gdk::Color("#9f9f9f"));
127                 gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
128
129                 if(x<window_endx)
130                         for(;x<window_endx;x+=grid_size[0])
131                         {
132                                 drawable->draw_line(gc,
133                                         round_to_int((x-window_startx)/pw),
134                                         0,
135                                         round_to_int((x-window_startx)/pw),
136                                         drawable_h
137                                 );
138                         }
139                 else
140                         for(;x>window_endx;x-=grid_size[0])
141                         {
142                                 drawable->draw_line(gc,
143                                         round_to_int((x-window_startx)/pw),
144                                         0,
145                                         round_to_int((x-window_startx)/pw),
146                                         drawable_h
147                                 );
148                         }
149
150                 if(y<window_endy)
151                         for(;y<window_endy;y+=grid_size[1])
152                         {
153                                 drawable->draw_line(gc,
154                                         0,
155                                         round_to_int((y-window_starty)/ph),
156                                         drawable_w,
157                                         round_to_int((y-window_starty)/ph)
158                                 );
159                         }
160                 else
161                         for(;y>window_endy;y-=grid_size[1])
162                         {
163                                 drawable->draw_line(gc,
164                                         0,
165                                         round_to_int((y-window_starty)/ph),
166                                         drawable_w,
167                                         round_to_int((y-window_starty)/ph)
168                                 );
169                         }
170         }
171 }