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