]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: dc.cpp | |
3 | // Purpose: wxDC class | |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "dc.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/dc.h" | |
17 | ||
18 | #if !USE_SHARED_LIBRARY | |
19 | IMPLEMENT_ABSTRACT_CLASS(wxDC, wxObject) | |
20 | #endif | |
21 | ||
22 | //----------------------------------------------------------------------------- | |
23 | // constants | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | #define mm2inches 0.0393700787402 | |
27 | #define inches2mm 25.4 | |
28 | #define mm2twips 56.6929133859 | |
29 | #define twips2mm 0.0176388888889 | |
30 | #define mm2pt 2.83464566929 | |
31 | #define pt2mm 0.352777777778 | |
32 | ||
33 | //----------------------------------------------------------------------------- | |
34 | // wxDC | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
37 | IMPLEMENT_ABSTRACT_CLASS(wxDC,wxObject) | |
38 | ||
39 | wxDC::wxDC(void) | |
40 | { | |
41 | m_ok = FALSE; | |
42 | m_optimize = FALSE; | |
43 | m_autoSetting = FALSE; | |
44 | m_colour = TRUE; | |
45 | m_clipping = FALSE; | |
46 | ||
47 | m_mm_to_pix_x = 1.0; | |
48 | m_mm_to_pix_y = 1.0; | |
49 | ||
50 | m_logicalOriginX = 0; | |
51 | m_logicalOriginY = 0; | |
52 | m_deviceOriginX = 0; | |
53 | m_deviceOriginY = 0; | |
54 | m_internalDeviceOriginX = 0; | |
55 | m_internalDeviceOriginY = 0; | |
56 | m_externalDeviceOriginX = 0; | |
57 | m_externalDeviceOriginY = 0; | |
58 | ||
59 | m_logicalScaleX = 1.0; | |
60 | m_logicalScaleY = 1.0; | |
61 | m_userScaleX = 1.0; | |
62 | m_userScaleY = 1.0; | |
63 | m_scaleX = 1.0; | |
64 | m_scaleY = 1.0; | |
65 | ||
66 | m_mappingMode = wxMM_TEXT; | |
67 | m_needComputeScaleX = FALSE; | |
68 | m_needComputeScaleY = FALSE; | |
69 | ||
70 | m_signX = 1; // default x-axis left to right | |
71 | m_signY = 1; // default y-axis top down | |
72 | ||
73 | m_maxX = m_maxY = -100000; | |
74 | m_minY = m_minY = 100000; | |
75 | ||
76 | m_logicalFunction = wxCOPY; | |
77 | // m_textAlignment = wxALIGN_TOP_LEFT; | |
78 | m_backgroundMode = wxTRANSPARENT; | |
79 | ||
80 | m_textForegroundColour = *wxBLACK; | |
81 | m_textBackgroundColour = *wxWHITE; | |
82 | m_pen = *wxBLACK_PEN; | |
83 | m_font = *wxNORMAL_FONT; | |
84 | m_brush = *wxTRANSPARENT_BRUSH; | |
85 | m_backgroundBrush = *wxWHITE_BRUSH; | |
86 | ||
87 | // m_palette = wxAPP_COLOURMAP; | |
88 | }; | |
89 | ||
90 | wxDC::~wxDC(void) | |
91 | { | |
92 | }; | |
93 | ||
94 | void wxDC::DrawIcon( const wxIcon &WXUNUSED(icon), long WXUNUSED(x), long WXUNUSED(y), bool WXUNUSED(useMask) ) | |
95 | { | |
96 | }; | |
97 | ||
98 | void wxDC::DrawPoint( wxPoint& point ) | |
99 | { | |
100 | DrawPoint( point.x, point.y ); | |
101 | }; | |
102 | ||
103 | void wxDC::DrawPolygon( wxList *list, long xoffset, long yoffset, int fillStyle ) | |
104 | { | |
105 | int n = list->Number(); | |
106 | wxPoint *points = new wxPoint[n]; | |
107 | ||
108 | int i = 0; | |
109 | for( wxNode *node = list->First(); node; node = node->Next() ) | |
110 | { | |
111 | wxPoint *point = (wxPoint *)node->Data(); | |
112 | points[i].x = point->x; | |
113 | points[i++].y = point->y; | |
114 | }; | |
115 | DrawPolygon( n, points, xoffset, yoffset, fillStyle ); | |
116 | delete[] points; | |
117 | }; | |
118 | ||
119 | void wxDC::DrawLines( wxList *list, long xoffset, long yoffset ) | |
120 | { | |
121 | int n = list->Number(); | |
122 | wxPoint *points = new wxPoint[n]; | |
123 | ||
124 | int i = 0; | |
125 | for( wxNode *node = list->First(); node; node = node->Next() ) | |
126 | { | |
127 | wxPoint *point = (wxPoint *)node->Data(); | |
128 | points[i].x = point->x; | |
129 | points[i++].y = point->y; | |
130 | }; | |
131 | DrawLines( n, points, xoffset, yoffset ); | |
132 | delete []points; | |
133 | }; | |
134 | ||
135 | void wxDC::DrawSpline( long x1, long y1, long x2, long y2, long x3, long y3 ) | |
136 | { | |
137 | wxList list; | |
138 | list.Append( (wxObject*)new wxPoint(x1, y1) ); | |
139 | list.Append( (wxObject*)new wxPoint(x2, y2) ); | |
140 | list.Append( (wxObject*)new wxPoint(x3, y3) ); | |
141 | DrawSpline(&list); | |
142 | wxNode *node = list.First(); | |
143 | while (node) | |
144 | { | |
145 | wxPoint *p = (wxPoint*)node->Data(); | |
146 | delete p; | |
147 | node = node->Next(); | |
148 | }; | |
149 | }; | |
150 | ||
151 | void wxDC::DrawSpline( wxList *points ) | |
152 | { | |
153 | DrawOpenSpline( points ); | |
154 | }; | |
155 | ||
156 | void wxDC::DrawSpline( int n, wxPoint points[] ) | |
157 | { | |
158 | wxList list; | |
159 | for (int i = 0; i < n; i++) list.Append( (wxObject*)&points[i] ); | |
160 | DrawSpline( &list ); | |
161 | }; | |
162 | ||
163 | void wxDC::SetClippingRegion( long x, long y, long width, long height ) | |
164 | { | |
165 | m_clipping = TRUE; | |
166 | m_clipX1 = x; | |
167 | m_clipY1 = y; | |
168 | m_clipX2 = x + width; | |
169 | m_clipY2 = y + height; | |
170 | }; | |
171 | ||
172 | void wxDC::DestroyClippingRegion(void) | |
173 | { | |
174 | m_clipping = FALSE; | |
175 | }; | |
176 | ||
177 | void wxDC::GetClippingBox( long *x, long *y, long *width, long *height ) const | |
178 | { | |
179 | if (m_clipping) | |
180 | { | |
181 | if (x) *x = m_clipX1; | |
182 | if (y) *y = m_clipY1; | |
183 | if (width) *width = (m_clipX2 - m_clipX1); | |
184 | if (height) *height = (m_clipY2 - m_clipY1); | |
185 | } | |
186 | else | |
187 | *x = *y = *width = *height = 0; | |
188 | }; | |
189 | ||
190 | void wxDC::GetSize( int* width, int* height ) const | |
191 | { | |
192 | *width = m_maxX-m_minX; | |
193 | *height = m_maxY-m_minY; | |
194 | }; | |
195 | ||
196 | void wxDC::GetSizeMM( long* width, long* height ) const | |
197 | { | |
198 | int w = 0; | |
199 | int h = 0; | |
200 | GetSize( &w, &h ); | |
201 | *width = long( double(w) / (m_scaleX*m_mm_to_pix_x) ); | |
202 | *height = long( double(h) / (m_scaleY*m_mm_to_pix_y) ); | |
203 | }; | |
204 | ||
205 | void wxDC::SetTextForeground( const wxColour &col ) | |
206 | { | |
207 | if (!Ok()) return; | |
208 | m_textForegroundColour = col; | |
209 | }; | |
210 | ||
211 | void wxDC::SetTextBackground( const wxColour &col ) | |
212 | { | |
213 | if (!Ok()) return; | |
214 | m_textBackgroundColour = col; | |
215 | }; | |
216 | ||
217 | void wxDC::SetMapMode( int mode ) | |
218 | { | |
219 | switch (mode) | |
220 | { | |
221 | case wxMM_TWIPS: | |
222 | SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y ); | |
223 | break; | |
224 | case wxMM_POINTS: | |
225 | SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y ); | |
226 | break; | |
227 | case wxMM_METRIC: | |
228 | SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y ); | |
229 | break; | |
230 | case wxMM_LOMETRIC: | |
231 | SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 ); | |
232 | break; | |
233 | default: | |
234 | case wxMM_TEXT: | |
235 | SetLogicalScale( 1.0, 1.0 ); | |
236 | break; | |
237 | }; | |
238 | if (mode != wxMM_TEXT) | |
239 | { | |
240 | m_needComputeScaleX = TRUE; | |
241 | m_needComputeScaleY = TRUE; | |
242 | }; | |
243 | }; | |
244 | ||
245 | void wxDC::SetUserScale( double x, double y ) | |
246 | { | |
247 | // allow negative ? -> no | |
248 | m_userScaleX = x; | |
249 | m_userScaleY = y; | |
250 | ComputeScaleAndOrigin(); | |
251 | }; | |
252 | ||
253 | void wxDC::GetUserScale( double *x, double *y ) | |
254 | { | |
255 | if (x) *x = m_userScaleX; | |
256 | if (y) *y = m_userScaleY; | |
257 | }; | |
258 | ||
259 | void wxDC::SetLogicalScale( double x, double y ) | |
260 | { | |
261 | // allow negative ? | |
262 | m_logicalScaleX = x; | |
263 | m_logicalScaleY = y; | |
264 | ComputeScaleAndOrigin(); | |
265 | }; | |
266 | ||
267 | void wxDC::GetLogicalScale( double *x, double *y ) | |
268 | { | |
269 | if (x) *x = m_logicalScaleX; | |
270 | if (y) *y = m_logicalScaleY; | |
271 | }; | |
272 | ||
273 | void wxDC::SetLogicalOrigin( long x, long y ) | |
274 | { | |
275 | m_logicalOriginX = x * m_signX; // is this still correct ? | |
276 | m_logicalOriginY = y * m_signY; | |
277 | ComputeScaleAndOrigin(); | |
278 | }; | |
279 | ||
280 | void wxDC::GetLogicalOrigin( long *x, long *y ) | |
281 | { | |
282 | if (x) *x = m_logicalOriginX; | |
283 | if (y) *y = m_logicalOriginY; | |
284 | }; | |
285 | ||
286 | void wxDC::SetDeviceOrigin( long x, long y ) | |
287 | { | |
288 | m_externalDeviceOriginX = x; | |
289 | m_externalDeviceOriginY = y; | |
290 | ComputeScaleAndOrigin(); | |
291 | }; | |
292 | ||
293 | void wxDC::GetDeviceOrigin( long *x, long *y ) | |
294 | { | |
295 | // if (x) *x = m_externalDeviceOriginX; | |
296 | // if (y) *y = m_externalDeviceOriginY; | |
297 | if (x) *x = m_deviceOriginX; | |
298 | if (y) *y = m_deviceOriginY; | |
299 | }; | |
300 | ||
301 | void wxDC::SetInternalDeviceOrigin( long x, long y ) | |
302 | { | |
303 | m_internalDeviceOriginX = x; | |
304 | m_internalDeviceOriginY = y; | |
305 | ComputeScaleAndOrigin(); | |
306 | }; | |
307 | ||
308 | void wxDC::GetInternalDeviceOrigin( long *x, long *y ) | |
309 | { | |
310 | if (x) *x = m_internalDeviceOriginX; | |
311 | if (y) *y = m_internalDeviceOriginY; | |
312 | }; | |
313 | ||
314 | void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp ) | |
315 | { | |
316 | m_signX = (xLeftRight ? 1 : -1); | |
317 | m_signY = (yBottomUp ? -1 : 1); | |
318 | ComputeScaleAndOrigin(); | |
319 | }; | |
320 | ||
321 | long wxDC::DeviceToLogicalX(long x) const | |
322 | { | |
323 | return XDEV2LOG(x); | |
324 | }; | |
325 | ||
326 | long wxDC::DeviceToLogicalY(long y) const | |
327 | { | |
328 | return YDEV2LOG(y); | |
329 | }; | |
330 | ||
331 | long wxDC::DeviceToLogicalXRel(long x) const | |
332 | { | |
333 | return XDEV2LOGREL(x); | |
334 | }; | |
335 | ||
336 | long wxDC::DeviceToLogicalYRel(long y) const | |
337 | { | |
338 | return YDEV2LOGREL(y); | |
339 | }; | |
340 | ||
341 | long wxDC::LogicalToDeviceX(long x) const | |
342 | { | |
343 | return XLOG2DEV(x); | |
344 | }; | |
345 | ||
346 | long wxDC::LogicalToDeviceY(long y) const | |
347 | { | |
348 | return YLOG2DEV(y); | |
349 | }; | |
350 | ||
351 | long wxDC::LogicalToDeviceXRel(long x) const | |
352 | { | |
353 | return XLOG2DEVREL(x); | |
354 | }; | |
355 | ||
356 | long wxDC::LogicalToDeviceYRel(long y) const | |
357 | { | |
358 | return YLOG2DEVREL(y); | |
359 | }; | |
360 | ||
361 | void wxDC::CalcBoundingBox( long x, long y ) | |
362 | { | |
363 | if (x < m_minX) m_minX = x; | |
364 | if (y < m_minY) m_minY = y; | |
365 | if (x > m_maxX) m_maxX = x; | |
366 | if (y > m_maxY) m_maxY = y; | |
367 | }; | |
368 | ||
369 | void wxDC::ComputeScaleAndOrigin(void) | |
370 | { | |
371 | // CMB: copy scale to see if it changes | |
372 | double origScaleX = m_scaleX; | |
373 | double origScaleY = m_scaleY; | |
374 | ||
375 | m_scaleX = m_logicalScaleX * m_userScaleX; | |
376 | m_scaleY = m_logicalScaleY * m_userScaleY; | |
377 | ||
378 | m_deviceOriginX = m_internalDeviceOriginX + m_externalDeviceOriginX; | |
379 | m_deviceOriginY = m_internalDeviceOriginY + m_externalDeviceOriginY; | |
380 | ||
381 | // CMB: if scale has changed call SetPen to recalulate the line width | |
382 | if (m_scaleX != origScaleX || m_scaleY != origScaleY) | |
383 | { | |
384 | // this is a bit artificial, but we need to force wxDC to think | |
385 | // the pen has changed | |
386 | wxPen* pen = GetPen(); | |
387 | wxPen tempPen; | |
388 | m_pen = tempPen; | |
389 | SetPen(pen); | |
390 | } | |
391 | }; | |
392 |