wxWindowDC compiles (wxGTK doesn't link yet because of dcpsg.cpp problems...)
[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, Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "dc.h"
13 #endif
14
15 #include "wx/dc.h"
16
17 #include "gdk/gdk.h"
18 #include "gtk/gtk.h"
19
20 //-----------------------------------------------------------------------------
21 // constants
22 //-----------------------------------------------------------------------------
23
24 #define mm2inches 0.0393700787402
25 #define inches2mm 25.4
26 #define mm2twips 56.6929133859
27 #define twips2mm 0.0176388888889
28 #define mm2pt 2.83464566929
29 #define pt2mm 0.352777777778
30
31 //-----------------------------------------------------------------------------
32 // wxDC
33 //-----------------------------------------------------------------------------
34
35 IMPLEMENT_ABSTRACT_CLASS(wxDC, wxDCBase)
36
37 wxDC::wxDC()
38 {
39 m_ok = FALSE;
40
41 m_mm_to_pix_x = 1.0;
42 m_mm_to_pix_y = 1.0;
43
44 m_needComputeScaleX = FALSE; /* not used yet */
45 m_needComputeScaleY = FALSE; /* not used yet */
46
47 m_logicalFunction = wxCOPY;
48
49 m_pen = *wxBLACK_PEN;
50 m_font = *wxNORMAL_FONT;
51 m_brush = *wxTRANSPARENT_BRUSH;
52 }
53
54 void wxDC::DoSetClippingRegion( long x, long y, long width, long height )
55 {
56 m_clipping = TRUE;
57 m_clipX1 = x;
58 m_clipY1 = y;
59 m_clipX2 = x + width;
60 m_clipY2 = y + height;
61 }
62
63 void wxDC::DestroyClippingRegion()
64 {
65 m_clipping = FALSE;
66 }
67
68 // ---------------------------------------------------------------------------
69 // get DC capabilities
70 // ---------------------------------------------------------------------------
71
72 void wxDC::DoGetSize( int* width, int* height ) const
73 {
74 if (width) *width = m_maxX-m_minX;
75 if (height) *height = m_maxY-m_minY;
76 }
77
78 void wxDC::DoGetSizeMM( int* width, int* height ) const
79 {
80 int w = 0;
81 int h = 0;
82 GetSize( &w, &h );
83 if (width) *width = int( double(w) / (m_scaleX*m_mm_to_pix_x) );
84 if (height) *height = int( double(h) / (m_scaleY*m_mm_to_pix_y) );
85 }
86
87 // Resolution in pixels per logical inch
88 wxSize wxDC::GetPPI() const
89 {
90 // TODO (should probably be pure virtual)
91 return wxSize(0, 0);
92 }
93
94 // ---------------------------------------------------------------------------
95 // set various DC parameters
96 // ---------------------------------------------------------------------------
97
98 void wxDC::ComputeScaleAndOrigin()
99 {
100 // CMB: copy scale to see if it changes
101 double origScaleX = m_scaleX;
102 double origScaleY = m_scaleY;
103
104 m_scaleX = m_logicalScaleX * m_userScaleX;
105 m_scaleY = m_logicalScaleY * m_userScaleY;
106
107 // CMB: if scale has changed call SetPen to recalulate the line width
108 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
109 {
110 // this is a bit artificial, but we need to force wxDC to think
111 // the pen has changed
112 // It gives an Assert, Robert Roebling
113 /*
114 wxPen pen = m_pen;
115 m_pen = wxNullPen;
116 SetPen( pen );
117 */
118 }
119 }
120
121 void wxDC::SetMapMode( int mode )
122 {
123 switch (mode)
124 {
125 case wxMM_TWIPS:
126 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
127 break;
128 case wxMM_POINTS:
129 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
130 break;
131 case wxMM_METRIC:
132 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
133 break;
134 case wxMM_LOMETRIC:
135 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
136 break;
137 default:
138 case wxMM_TEXT:
139 SetLogicalScale( 1.0, 1.0 );
140 break;
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( long x, long 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( long x, long 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 }
189
190 // ---------------------------------------------------------------------------
191 // coordinates transformations
192 // ---------------------------------------------------------------------------
193
194 long wxDCBase::DeviceToLogicalX(long x) const
195 {
196 return ((wxDC *)this)->XDEV2LOG(x);
197 }
198
199 long wxDCBase::DeviceToLogicalY(long y) const
200 {
201 return ((wxDC *)this)->YDEV2LOG(y);
202 }
203
204 long wxDCBase::DeviceToLogicalXRel(long x) const
205 {
206 return ((wxDC *)this)->XDEV2LOGREL(x);
207 }
208
209 long wxDCBase::DeviceToLogicalYRel(long y) const
210 {
211 return ((wxDC *)this)->YDEV2LOGREL(y);
212 }
213
214 long wxDCBase::LogicalToDeviceX(long x) const
215 {
216 return ((wxDC *)this)->XLOG2DEV(x);
217 }
218
219 long wxDCBase::LogicalToDeviceY(long y) const
220 {
221 return ((wxDC *)this)->YLOG2DEV(y);
222 }
223
224 long wxDCBase::LogicalToDeviceXRel(long x) const
225 {
226 return ((wxDC *)this)->XLOG2DEVREL(x);
227 }
228
229 long wxDCBase::LogicalToDeviceYRel(long y) const
230 {
231 return ((wxDC *)this)->YLOG2DEVREL(y);
232 }
233