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