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