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