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