Made device to logical and vv conversion methods
[wxWidgets.git] / src / gtk / dc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dc.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/dc.h"
14
15 //-----------------------------------------------------------------------------
16 // wxDC
17 //-----------------------------------------------------------------------------
18
19 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxDCBase)
20
21 wxDC::wxDC()
22 {
23 m_ok = FALSE;
24
25 m_mm_to_pix_x = (double)wxGetDisplaySize().GetWidth() /
26 (double)wxGetDisplaySizeMM().GetWidth();
27 m_mm_to_pix_y = (double)wxGetDisplaySize().GetHeight() /
28 (double)wxGetDisplaySizeMM().GetHeight();
29
30 m_needComputeScaleX = FALSE; /* not used yet */
31 m_needComputeScaleY = FALSE; /* not used yet */
32
33 m_logicalFunction = wxCOPY;
34
35 m_pen = *wxBLACK_PEN;
36 m_font = *wxNORMAL_FONT;
37 m_brush = *wxWHITE_BRUSH;
38 }
39
40 void wxDC::DoSetClippingRegion( wxCoord x, wxCoord y, wxCoord width, wxCoord height )
41 {
42 m_clipping = TRUE;
43 m_clipX1 = x;
44 m_clipY1 = y;
45 m_clipX2 = x + width;
46 m_clipY2 = y + height;
47 }
48
49 // ---------------------------------------------------------------------------
50 // get DC capabilities
51 // ---------------------------------------------------------------------------
52
53 void wxDC::DoGetSizeMM( int* width, int* height ) const
54 {
55 int w = 0;
56 int h = 0;
57 GetSize( &w, &h );
58 if (width) *width = int( double(w) / (m_userScaleX*m_mm_to_pix_x) );
59 if (height) *height = int( double(h) / (m_userScaleY*m_mm_to_pix_y) );
60 }
61
62 // Resolution in pixels per logical inch
63 wxSize wxDC::GetPPI() const
64 {
65 // TODO (should probably be pure virtual)
66 return wxSize(0, 0);
67 }
68
69 // ---------------------------------------------------------------------------
70 // set various DC parameters
71 // ---------------------------------------------------------------------------
72
73 wxCoord wxDC::DeviceToLogicalX(wxCoord x) const
74 {
75 return wxRound((x - m_deviceOriginX) / m_scaleX) * m_signX + m_logicalOriginX;
76 }
77
78 wxCoord wxDC::DeviceToLogicalY(wxCoord y) const
79 {
80 return wxRound((y - m_deviceOriginY) / m_scaleY) * m_signY + m_logicalOriginY;
81 }
82
83 wxCoord wxDC::DeviceToLogicalXRel(wxCoord x) const
84 {
85 return wxRound(x / m_scaleX);
86 }
87
88 wxCoord wxDC::DeviceToLogicalYRel(wxCoord y) const
89 {
90 return wxRound(y / m_scaleY);
91 }
92
93 wxCoord wxDC::LogicalToDeviceX(wxCoord x) const
94 {
95 return wxRound((x - m_logicalOriginX) * m_scaleX) * m_signX + m_deviceOriginX;
96 }
97
98 wxCoord wxDC::LogicalToDeviceY(wxCoord y) const
99 {
100 return wxRound((y - m_logicalOriginY) * m_scaleY) * m_signY + m_deviceOriginY;
101 }
102
103 wxCoord wxDC::LogicalToDeviceXRel(wxCoord x) const
104 {
105 return wxRound(x * m_scaleX);
106 }
107
108 wxCoord wxDC::LogicalToDeviceYRel(wxCoord y) const
109 {
110 return wxRound(y * m_scaleY);
111 }
112
113 void wxDC::ComputeScaleAndOrigin()
114 {
115 m_scaleX = m_logicalScaleX * m_userScaleX;
116 m_scaleY = m_logicalScaleY * m_userScaleY;
117 }
118
119 void wxDC::SetMapMode( int mode )
120 {
121 switch (mode)
122 {
123 case wxMM_TWIPS:
124 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
125 break;
126 case wxMM_POINTS:
127 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
128 break;
129 case wxMM_METRIC:
130 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
131 break;
132 case wxMM_LOMETRIC:
133 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
134 break;
135 default:
136 case wxMM_TEXT:
137 SetLogicalScale( 1.0, 1.0 );
138 break;
139 }
140 m_mappingMode = mode;
141
142 /* we don't do this mega optimisation
143 if (mode != wxMM_TEXT)
144 {
145 m_needComputeScaleX = TRUE;
146 m_needComputeScaleY = TRUE;
147 }
148 */
149 }
150
151 void wxDC::SetUserScale( double x, double y )
152 {
153 // allow negative ? -> no
154 m_userScaleX = x;
155 m_userScaleY = y;
156 ComputeScaleAndOrigin();
157 }
158
159 void wxDC::SetLogicalScale( double x, double y )
160 {
161 // allow negative ?
162 m_logicalScaleX = x;
163 m_logicalScaleY = y;
164 ComputeScaleAndOrigin();
165 }
166
167 void wxDC::SetLogicalOrigin( wxCoord x, wxCoord y )
168 {
169 m_logicalOriginX = x * m_signX; // is this still correct ?
170 m_logicalOriginY = y * m_signY;
171 ComputeScaleAndOrigin();
172 }
173
174 void wxDC::SetDeviceOrigin( wxCoord x, wxCoord y )
175 {
176 // only wxPostScripDC has m_signX = -1, we override SetDeviceOrigin there
177 m_deviceOriginX = x;
178 m_deviceOriginY = y;
179 ComputeScaleAndOrigin();
180 }
181
182 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
183 {
184 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
185 m_signX = (xLeftRight ? 1 : -1);
186 m_signY = (yBottomUp ? -1 : 1);
187 ComputeScaleAndOrigin();
188 }