]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dc.cpp
Had to change the time point, when widgets are
[wxWidgets.git] / src / gtk1 / 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 = wxMM_TEXT;
61 m_needComputeScaleX = FALSE; /* not used yet */
62 m_needComputeScaleY = FALSE; /* not used yet */
63
64 m_signX = 1; /* default x-axis left to right */
65 m_signY = 1; /* default y-axis top down. -1 in postscript. */
66
67 m_maxX = 0;
68 m_maxY = 0;
69 m_minX = 0;
70 m_minY = 0;
71
72 m_logicalFunction = wxCOPY;
73 // m_textAlignment = wxALIGN_TOP_LEFT;
74 m_backgroundMode = wxTRANSPARENT;
75
76 m_textForegroundColour = *wxBLACK;
77 m_textBackgroundColour = *wxWHITE;
78 m_pen = *wxBLACK_PEN;
79 m_font = *wxNORMAL_FONT;
80 m_brush = *wxTRANSPARENT_BRUSH;
81 m_backgroundBrush = *wxWHITE_BRUSH;
82
83 // m_palette = wxAPP_COLOURMAP; /* I'll learn to handle palettes later in my life */
84 }
85
86 wxDC::~wxDC()
87 {
88 }
89
90 bool wxDC::Ok() const
91 {
92 return m_ok;
93 }
94
95 void wxDC::DrawArc( long WXUNUSED(x1), long WXUNUSED(y1), long WXUNUSED(x2), long WXUNUSED(y2),
96 double WXUNUSED(xc), double WXUNUSED(yc) )
97 {
98 }
99
100 void wxDC::DrawPoint( wxPoint& point )
101 {
102 DrawPoint( point.x, point.y );
103 }
104
105 void wxDC::DrawPolygon( wxList *list, long xoffset, long yoffset, int fillStyle )
106 {
107 int n = list->Number();
108 wxPoint *points = new wxPoint[n];
109
110 int i = 0;
111 for( wxNode *node = list->First(); node; node = node->Next() )
112 {
113 wxPoint *point = (wxPoint *)node->Data();
114 points[i].x = point->x;
115 points[i++].y = point->y;
116 }
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
135 DrawLines( n, points, xoffset, yoffset );
136 delete []points;
137 }
138
139 void wxDC::DrawSpline( long x1, long y1, long x2, long y2, long x3, long y3 )
140 {
141 wxList list;
142 list.Append( (wxObject*)new wxPoint(x1, y1) );
143 list.Append( (wxObject*)new wxPoint(x2, y2) );
144 list.Append( (wxObject*)new wxPoint(x3, y3) );
145 DrawSpline(&list);
146 wxNode *node = list.First();
147 while (node)
148 {
149 wxPoint *p = (wxPoint*)node->Data();
150 delete p;
151 node = node->Next();
152 }
153 }
154
155 void wxDC::DrawSpline( int n, wxPoint points[] )
156 {
157 wxList list;
158 for (int i = 0; i < n; i++) list.Append( (wxObject*)&points[i] );
159 DrawSpline( &list );
160 }
161
162 void wxDC::SetClippingRegion( long x, long y, long width, long height )
163 {
164 m_clipping = TRUE;
165 m_clipX1 = x;
166 m_clipY1 = y;
167 m_clipX2 = x + width;
168 m_clipY2 = y + height;
169 }
170
171 void wxDC::DestroyClippingRegion()
172 {
173 m_clipping = FALSE;
174 }
175
176 void wxDC::GetClippingBox( long *x, long *y, long *width, long *height ) const
177 {
178 if (m_clipping)
179 {
180 if (x) *x = m_clipX1;
181 if (y) *y = m_clipY1;
182 if (width) *width = (m_clipX2 - m_clipX1);
183 if (height) *height = (m_clipY2 - m_clipY1);
184 }
185 else
186 {
187 *x = *y = *width = *height = 0;
188 }
189 }
190
191 void wxDC::GetSize( int* width, int* height ) const
192 {
193 if (width) *width = m_maxX-m_minX;
194 if (height) *height = m_maxY-m_minY;
195 }
196
197 void wxDC::GetSizeMM( int* width, int* height ) const
198 {
199 int w = 0;
200 int h = 0;
201 GetSize( &w, &h );
202 if (width) *width = int( double(w) / (m_scaleX*m_mm_to_pix_x) );
203 if (height) *height = int( double(h) / (m_scaleY*m_mm_to_pix_y) );
204 }
205
206 // Resolution in pixels per logical inch
207 wxSize wxDC::GetPPI(void) const
208 {
209 // TODO (should probably be pure virtual)
210 return wxSize(0, 0);
211 }
212
213 void wxDC::SetTextForeground( const wxColour &col )
214 {
215 m_textForegroundColour = col;
216 }
217
218 void wxDC::SetTextBackground( const wxColour &col )
219 {
220 m_textBackgroundColour = col;
221 }
222
223 void wxDC::SetMapMode( int mode )
224 {
225 switch (mode)
226 {
227 case wxMM_TWIPS:
228 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
229 break;
230 case wxMM_POINTS:
231 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
232 break;
233 case wxMM_METRIC:
234 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
235 break;
236 case wxMM_LOMETRIC:
237 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
238 break;
239 default:
240 case wxMM_TEXT:
241 SetLogicalScale( 1.0, 1.0 );
242 break;
243 }
244 /* we don't do this mega optimisation
245 if (mode != wxMM_TEXT)
246 {
247 m_needComputeScaleX = TRUE;
248 m_needComputeScaleY = TRUE;
249 }
250 */
251 }
252
253 void wxDC::SetUserScale( double x, double y )
254 {
255 // allow negative ? -> no
256 m_userScaleX = x;
257 m_userScaleY = y;
258 ComputeScaleAndOrigin();
259 }
260
261 void wxDC::GetUserScale( double *x, double *y )
262 {
263 if (x) *x = m_userScaleX;
264 if (y) *y = m_userScaleY;
265 }
266
267 void wxDC::SetLogicalScale( double x, double y )
268 {
269 // allow negative ?
270 m_logicalScaleX = x;
271 m_logicalScaleY = y;
272 ComputeScaleAndOrigin();
273 }
274
275 void wxDC::GetLogicalScale( double *x, double *y )
276 {
277 if (x) *x = m_logicalScaleX;
278 if (y) *y = m_logicalScaleY;
279 }
280
281 void wxDC::SetLogicalOrigin( long x, long y )
282 {
283 m_logicalOriginX = x * m_signX; // is this still correct ?
284 m_logicalOriginY = y * m_signY;
285 ComputeScaleAndOrigin();
286 }
287
288 void wxDC::GetLogicalOrigin( long *x, long *y )
289 {
290 if (x) *x = m_logicalOriginX;
291 if (y) *y = m_logicalOriginY;
292 }
293
294 void wxDC::SetDeviceOrigin( long x, long y )
295 {
296 // only wxPostScripDC has m_signX = -1, we override SetDeviceOrigin there
297 m_deviceOriginX = x;
298 m_deviceOriginY = y;
299 ComputeScaleAndOrigin();
300 }
301
302 void wxDC::GetDeviceOrigin( long *x, long *y )
303 {
304 if (x) *x = m_deviceOriginX;
305 if (y) *y = m_deviceOriginY;
306 }
307
308 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
309 {
310 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
311 m_signX = (xLeftRight ? 1 : -1);
312 m_signY = (yBottomUp ? -1 : 1);
313 ComputeScaleAndOrigin();
314 }
315
316 long wxDC::DeviceToLogicalX(long x) const
317 {
318 return XDEV2LOG(x);
319 }
320
321 long wxDC::DeviceToLogicalY(long y) const
322 {
323 return YDEV2LOG(y);
324 }
325
326 long wxDC::DeviceToLogicalXRel(long x) const
327 {
328 return XDEV2LOGREL(x);
329 }
330
331 long wxDC::DeviceToLogicalYRel(long y) const
332 {
333 return YDEV2LOGREL(y);
334 }
335
336 long wxDC::LogicalToDeviceX(long x) const
337 {
338 return XLOG2DEV(x);
339 }
340
341 long wxDC::LogicalToDeviceY(long y) const
342 {
343 return YLOG2DEV(y);
344 }
345
346 long wxDC::LogicalToDeviceXRel(long x) const
347 {
348 return XLOG2DEVREL(x);
349 }
350
351 long wxDC::LogicalToDeviceYRel(long y) const
352 {
353 return YLOG2DEVREL(y);
354 }
355
356 void wxDC::CalcBoundingBox( long x, long y )
357 {
358 if (x < m_minX) m_minX = x;
359 if (y < m_minY) m_minY = y;
360 if (x > m_maxX) m_maxX = x;
361 if (y > m_maxY) m_maxY = y;
362 }
363
364 void wxDC::ComputeScaleAndOrigin()
365 {
366 // CMB: copy scale to see if it changes
367 double origScaleX = m_scaleX;
368 double origScaleY = m_scaleY;
369
370 m_scaleX = m_logicalScaleX * m_userScaleX;
371 m_scaleY = m_logicalScaleY * m_userScaleY;
372
373 // CMB: if scale has changed call SetPen to recalulate the line width
374 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
375 {
376 // this is a bit artificial, but we need to force wxDC to think
377 // the pen has changed
378 // It gives an Assert, Robert Roebling
379 /*
380 wxPen pen = m_pen;
381 m_pen = wxNullPen;
382 SetPen( pen );
383 */
384 }
385 }
386