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