initial version
[synfig.git] / synfig-studio / trunk / src / gtkmm / renderer_grid.cpp
1 /* === S I N F G =========================================================== */
2 /*!     \file template.cpp
3 **      \brief Template File
4 **
5 **      $Id: renderer_grid.cpp,v 1.1.1.1 2005/01/07 03:34:36 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 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "renderer_grid.h"
32 #include "workarea.h"
33 #include <ETL/misc>
34
35 #endif
36
37 /* === U S I N G =========================================================== */
38
39 using namespace std;
40 using namespace etl;
41 using namespace sinfg;
42 using namespace studio;
43
44 /* === M A C R O S ========================================================= */
45
46 /* === G L O B A L S ======================================================= */
47
48 /* === P R O C E D U R E S ================================================= */
49
50 /* === M E T H O D S ======================================================= */
51
52 Renderer_Grid::~Renderer_Grid()
53 {
54 }
55
56 bool
57 Renderer_Grid::get_enabled_vfunc()const
58 {
59         return get_work_area()->grid_status();
60 }
61
62 sinfg::Vector
63 Renderer_Grid::get_grid_size()const
64 {
65         return get_work_area()->get_grid_size();
66 }
67
68 void
69 Renderer_Grid::render_vfunc(
70         const Glib::RefPtr<Gdk::Drawable>& drawable,
71         const Gdk::Rectangle& expose_area
72 )
73 {
74         assert(get_work_area());
75         if(!get_work_area())
76                 return;
77         
78 //      const sinfg::RendDesc &rend_desc(get_work_area()->get_canvas()->rend_desc());
79         
80         const sinfg::Vector focus_point(get_work_area()->get_focus_point());
81
82 //      std::vector< std::pair<Glib::RefPtr<Gdk::Pixbuf>,int> >& tile_book(get_tile_book());
83         
84         int drawable_w,drawable_h;
85         drawable->get_size(drawable_w,drawable_h);
86         
87         // Calculate the window coordinates of the top-left
88         // corner of the canvas.
89 //      const sinfg::Vector::value_type
90 //              x(focus_point[0]/get_pw()+drawable_w/2-get_w()/2),
91 //              y(focus_point[1]/get_ph()+drawable_h/2-get_h()/2);
92
93         /*const sinfg::Vector::value_type window_startx(window_tl[0]);
94         const sinfg::Vector::value_type window_endx(window_br[0]);
95         const sinfg::Vector::value_type window_starty(window_tl[1]);
96         const sinfg::Vector::value_type window_endy(window_br[1]);
97         */
98 //      const int
99 //              tile_w(get_work_area()->get_tile_w()),
100 //              tile_h(get_work_area()->get_tile_h());
101
102 //      const int
103 //              w(get_w()),
104 //              h(get_h());
105         
106         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(drawable));
107         
108         const sinfg::Vector grid_size(get_grid_size());
109
110         const sinfg::Vector::value_type window_startx(get_work_area()->get_window_tl()[0]);
111         const sinfg::Vector::value_type window_endx(get_work_area()->get_window_br()[0]);
112         const sinfg::Vector::value_type window_starty(get_work_area()->get_window_tl()[1]);
113         const sinfg::Vector::value_type window_endy(get_work_area()->get_window_br()[1]);
114         const float pw(get_pw()),ph(get_ph());
115         
116         // Draw out the grid
117         if(grid_size[0]>pw*3.5 && grid_size[1]>ph*3.5)
118         {
119                 sinfg::Vector::value_type x,y;
120
121                 x=floor(window_startx/grid_size[0])*grid_size[0];
122                 y=floor(window_starty/grid_size[1])*grid_size[1];
123                 
124                 gc->set_function(Gdk::COPY);
125                 gc->set_rgb_fg_color(Gdk::Color("#9f9f9f"));
126                 gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
127
128                 if(x<window_endx)
129                         for(;x<window_endx;x+=grid_size[0])
130                         {
131                                 drawable->draw_line(gc,
132                                         round_to_int((x-window_startx)/pw),
133                                         0,
134                                         round_to_int((x-window_startx)/pw),
135                                         drawable_h
136                                 );
137                         }
138                 else
139                         for(;x>window_endx;x-=grid_size[0])
140                         {
141                                 drawable->draw_line(gc,
142                                         round_to_int((x-window_startx)/pw),
143                                         0,
144                                         round_to_int((x-window_startx)/pw),
145                                         drawable_h
146                                 );
147                         }
148
149                 if(y<window_endy)
150                         for(;y<window_endy;y+=grid_size[1])
151                         {
152                                 drawable->draw_line(gc,
153                                         0,
154                                         round_to_int((y-window_starty)/ph),
155                                         drawable_w,
156                                         round_to_int((y-window_starty)/ph)
157                                 );
158                         }
159                 else
160                         for(;y>window_endy;y-=grid_size[1])
161                         {
162                                 drawable->draw_line(gc,
163                                         0,
164                                         round_to_int((y-window_starty)/ph),
165                                         drawable_w,
166                                         round_to_int((y-window_starty)/ph)
167                                 );
168                         }
169         }
170 }