Update for bitmap, image on scaling, transparancy,
[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()
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()
82 {
83 }
84
85 bool wxDC::Ok() const
86 {
87 return m_ok;
88 }
89
90 void wxDC::DrawArc( long WXUNUSED(x1), long WXUNUSED(y1), long WXUNUSED(x2), long WXUNUSED(y2),
91 double WXUNUSED(xc), double WXUNUSED(yc) )
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
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
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( int n, wxPoint points[] )
151 {
152 wxList list;
153 for (int i = 0; i < n; i++) list.Append( (wxObject*)&points[i] );
154 DrawSpline( &list );
155 }
156
157 void wxDC::SetClippingRegion( long x, long y, long width, long height )
158 {
159 m_clipping = TRUE;
160 m_clipX1 = x;
161 m_clipY1 = y;
162 m_clipX2 = x + width;
163 m_clipY2 = y + height;
164 }
165
166 void wxDC::DestroyClippingRegion()
167 {
168 m_clipping = FALSE;
169 }
170
171 void wxDC::GetClippingBox( long *x, long *y, long *width, long *height ) const
172 {
173 if (m_clipping)
174 {
175 if (x) *x = m_clipX1;
176 if (y) *y = m_clipY1;
177 if (width) *width = (m_clipX2 - m_clipX1);
178 if (height) *height = (m_clipY2 - m_clipY1);
179 }
180 else
181 {
182 *x = *y = *width = *height = 0;
183 }
184 }
185
186 void wxDC::GetSize( int* width, int* height ) const
187 {
188 if (width) *width = m_maxX-m_minX;
189 if (height) *height = m_maxY-m_minY;
190 }
191
192 void wxDC::GetSizeMM( long* width, long* height ) const
193 {
194 int w = 0;
195 int h = 0;
196 GetSize( &w, &h );
197 if (width) *width = long( double(w) / (m_scaleX*m_mm_to_pix_x) );
198 if (height) *height = long( double(h) / (m_scaleY*m_mm_to_pix_y) );
199 }
200
201 void wxDC::SetTextForeground( const wxColour &col )
202 {
203 m_textForegroundColour = col;
204 }
205
206 void wxDC::SetTextBackground( const wxColour &col )
207 {
208 m_textBackgroundColour = col;
209 }
210
211 void wxDC::SetMapMode( int mode )
212 {
213 switch (mode)
214 {
215 case MM_TWIPS:
216 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
217 break;
218 case MM_POINTS:
219 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
220 break;
221 case MM_METRIC:
222 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
223 break;
224 case MM_LOMETRIC:
225 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
226 break;
227 default:
228 case MM_TEXT:
229 SetLogicalScale( 1.0, 1.0 );
230 break;
231 }
232 /* we don't do this mega optimisation
233 if (mode != MM_TEXT)
234 {
235 m_needComputeScaleX = TRUE;
236 m_needComputeScaleY = TRUE;
237 }
238 */
239 }
240
241 void wxDC::SetUserScale( double x, double y )
242 {
243 // allow negative ? -> no
244 m_userScaleX = x;
245 m_userScaleY = y;
246 ComputeScaleAndOrigin();
247 }
248
249 void wxDC::GetUserScale( double *x, double *y )
250 {
251 if (x) *x = m_userScaleX;
252 if (y) *y = m_userScaleY;
253 }
254
255 void wxDC::SetLogicalScale( double x, double y )
256 {
257 // allow negative ?
258 m_logicalScaleX = x;
259 m_logicalScaleY = y;
260 ComputeScaleAndOrigin();
261 }
262
263 void wxDC::GetLogicalScale( double *x, double *y )
264 {
265 if (x) *x = m_logicalScaleX;
266 if (y) *y = m_logicalScaleY;
267 }
268
269 void wxDC::SetLogicalOrigin( long x, long y )
270 {
271 m_logicalOriginX = x * m_signX; // is this still correct ?
272 m_logicalOriginY = y * m_signY;
273 ComputeScaleAndOrigin();
274 }
275
276 void wxDC::GetLogicalOrigin( long *x, long *y )
277 {
278 if (x) *x = m_logicalOriginX;
279 if (y) *y = m_logicalOriginY;
280 }
281
282 void wxDC::SetDeviceOrigin( long x, long y )
283 {
284 // only wxPostScripDC has m_signX = -1, we override SetDeviceOrigin there
285 m_deviceOriginX = x;
286 m_deviceOriginY = y;
287 ComputeScaleAndOrigin();
288 }
289
290 void wxDC::GetDeviceOrigin( long *x, long *y )
291 {
292 if (x) *x = m_deviceOriginX;
293 if (y) *y = m_deviceOriginY;
294 }
295
296 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
297 {
298 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
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()
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 // It gives an Assert, Robert Roebling
367 /*
368 wxPen pen = m_pen;
369 m_pen = wxNullPen;
370 SetPen( pen );
371 */
372 }
373 }
374