Some more Motif work; included utils.h in fileconf.cpp (for wxGetHomeDir or something)
[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_isInteractive = FALSE;
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
150 void wxDC::DrawSpline( wxList *points )
151 {
152 DrawOpenSpline( points );
153 };
154
155 void wxDC::DrawSpline( int n, wxPoint points[] )
156 {
157 wxList list;
158 for (int i = 0; i < n; i++) list.Append( (wxObject*)&points[i] );
159 DrawSpline( &list );
160 };
161
162 void wxDC::SetClippingRegion( long x, long y, long width, long height )
163 {
164 m_clipping = TRUE;
165 m_clipX1 = x;
166 m_clipY1 = y;
167 m_clipX2 = x + width;
168 m_clipY2 = y + height;
169 };
170
171 void wxDC::DestroyClippingRegion(void)
172 {
173 m_clipping = FALSE;
174 };
175
176 void wxDC::GetClippingBox( long *x, long *y, long *width, long *height ) const
177 {
178 if (m_clipping)
179 {
180 if (x) *x = m_clipX1;
181 if (y) *y = m_clipY1;
182 if (width) *width = (m_clipX2 - m_clipX1);
183 if (height) *height = (m_clipY2 - m_clipY1);
184 }
185 else
186 *x = *y = *width = *height = 0;
187 };
188
189 void wxDC::GetSize( int* width, int* height ) const
190 {
191 *width = m_maxX-m_minX;
192 *height = m_maxY-m_minY;
193 };
194
195 void wxDC::GetSizeMM( long* width, long* height ) const
196 {
197 int w = 0;
198 int h = 0;
199 GetSize( &w, &h );
200 *width = long( double(w) / (m_scaleX*m_mm_to_pix_x) );
201 *height = long( double(h) / (m_scaleY*m_mm_to_pix_y) );
202 };
203
204 void wxDC::SetTextForeground( const wxColour &col )
205 {
206 if (!Ok()) return;
207 m_textForegroundColour = col;
208 };
209
210 void wxDC::SetTextBackground( const wxColour &col )
211 {
212 if (!Ok()) return;
213 m_textBackgroundColour = col;
214 };
215
216 void wxDC::SetMapMode( int mode )
217 {
218 switch (mode)
219 {
220 case MM_TWIPS:
221 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
222 break;
223 case MM_POINTS:
224 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
225 break;
226 case MM_METRIC:
227 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
228 break;
229 case MM_LOMETRIC:
230 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
231 break;
232 default:
233 case MM_TEXT:
234 SetLogicalScale( 1.0, 1.0 );
235 break;
236 };
237 if (mode != MM_TEXT)
238 {
239 m_needComputeScaleX = TRUE;
240 m_needComputeScaleY = TRUE;
241 };
242 };
243
244 void wxDC::SetUserScale( double x, double y )
245 {
246 // allow negative ? -> no
247 m_userScaleX = x;
248 m_userScaleY = y;
249 ComputeScaleAndOrigin();
250 };
251
252 void wxDC::GetUserScale( double *x, double *y )
253 {
254 if (x) *x = m_userScaleX;
255 if (y) *y = m_userScaleY;
256 };
257
258 void wxDC::SetLogicalScale( double x, double y )
259 {
260 // allow negative ?
261 m_logicalScaleX = x;
262 m_logicalScaleY = y;
263 ComputeScaleAndOrigin();
264 };
265
266 void wxDC::GetLogicalScale( double *x, double *y )
267 {
268 if (x) *x = m_logicalScaleX;
269 if (y) *y = m_logicalScaleY;
270 };
271
272 void wxDC::SetLogicalOrigin( long x, long y )
273 {
274 m_logicalOriginX = x * m_signX; // is this still correct ?
275 m_logicalOriginY = y * m_signY;
276 ComputeScaleAndOrigin();
277 };
278
279 void wxDC::GetLogicalOrigin( long *x, long *y )
280 {
281 if (x) *x = m_logicalOriginX;
282 if (y) *y = m_logicalOriginY;
283 };
284
285 void wxDC::SetDeviceOrigin( long x, long y )
286 {
287 m_externalDeviceOriginX = x;
288 m_externalDeviceOriginY = y;
289 ComputeScaleAndOrigin();
290 };
291
292 void wxDC::GetDeviceOrigin( long *x, long *y )
293 {
294 // if (x) *x = m_externalDeviceOriginX;
295 // if (y) *y = m_externalDeviceOriginY;
296 if (x) *x = m_deviceOriginX;
297 if (y) *y = m_deviceOriginY;
298 };
299
300 void wxDC::SetInternalDeviceOrigin( long x, long y )
301 {
302 m_internalDeviceOriginX = x;
303 m_internalDeviceOriginY = y;
304 ComputeScaleAndOrigin();
305 };
306
307 void wxDC::GetInternalDeviceOrigin( long *x, long *y )
308 {
309 if (x) *x = m_internalDeviceOriginX;
310 if (y) *y = m_internalDeviceOriginY;
311 };
312
313 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
314 {
315 m_signX = (xLeftRight ? 1 : -1);
316 m_signY = (yBottomUp ? -1 : 1);
317 ComputeScaleAndOrigin();
318 };
319
320 long wxDC::DeviceToLogicalX(long x) const
321 {
322 return XDEV2LOG(x);
323 };
324
325 long wxDC::DeviceToLogicalY(long y) const
326 {
327 return YDEV2LOG(y);
328 };
329
330 long wxDC::DeviceToLogicalXRel(long x) const
331 {
332 return XDEV2LOGREL(x);
333 };
334
335 long wxDC::DeviceToLogicalYRel(long y) const
336 {
337 return YDEV2LOGREL(y);
338 };
339
340 long wxDC::LogicalToDeviceX(long x) const
341 {
342 return XLOG2DEV(x);
343 };
344
345 long wxDC::LogicalToDeviceY(long y) const
346 {
347 return YLOG2DEV(y);
348 };
349
350 long wxDC::LogicalToDeviceXRel(long x) const
351 {
352 return XLOG2DEVREL(x);
353 };
354
355 long wxDC::LogicalToDeviceYRel(long y) const
356 {
357 return YLOG2DEVREL(y);
358 };
359
360 void wxDC::CalcBoundingBox( long x, long y )
361 {
362 if (x < m_minX) m_minX = x;
363 if (y < m_minY) m_minY = y;
364 if (x > m_maxX) m_maxX = x;
365 if (y > m_maxY) m_maxY = y;
366 };
367
368 void wxDC::ComputeScaleAndOrigin(void)
369 {
370 // CMB: copy scale to see if it changes
371 double origScaleX = m_scaleX;
372 double origScaleY = m_scaleY;
373
374 m_scaleX = m_logicalScaleX * m_userScaleX;
375 m_scaleY = m_logicalScaleY * m_userScaleY;
376
377 m_deviceOriginX = m_internalDeviceOriginX + m_externalDeviceOriginX;
378 m_deviceOriginY = m_internalDeviceOriginY + m_externalDeviceOriginY;
379
380 // CMB: if scale has changed call SetPen to recalulate the line width
381 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
382 {
383 // this is a bit artificial, but we need to force wxDC to think
384 // the pen has changed
385 wxPen* pen = GetPen();
386 wxPen tempPen;
387 m_pen = tempPen;
388 SetPen(pen);
389 }
390 };
391