]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dc.cpp
wxGTK's built-in dockable menu now give their
[wxWidgets.git] / src / gtk / dc.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: dc.cpp
3// Purpose:
4// Author: Robert Roebling
6f65e337 5// RCS-ID: $Id$
dbf858b5 6// Copyright: (c) 1998 Robert Roebling, Markus Holzem
c801d85f
KB
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "dc.h"
13#endif
14
15#include "wx/dc.h"
16
83624f79
RR
17#include "gdk/gdk.h"
18#include "gtk/gtk.h"
19
c801d85f
KB
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
35IMPLEMENT_ABSTRACT_CLASS(wxDC,wxObject)
36
4bc67cc5 37wxDC::wxDC()
c801d85f 38{
4bc67cc5
RR
39 m_ok = FALSE;
40 m_optimize = FALSE;
41 m_autoSetting = FALSE;
42 m_colour = TRUE;
43 m_clipping = FALSE;
c801d85f 44
4bc67cc5
RR
45 m_mm_to_pix_x = 1.0;
46 m_mm_to_pix_y = 1.0;
c801d85f 47
4bc67cc5
RR
48 m_logicalOriginX = 0;
49 m_logicalOriginY = 0;
50 m_deviceOriginX = 0;
51 m_deviceOriginY = 0;
c801d85f 52
4bc67cc5
RR
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;
c801d85f 59
e3065973 60 m_mappingMode = wxMM_TEXT;
ea6f44b5
RR
61 m_needComputeScaleX = FALSE; /* not used yet */
62 m_needComputeScaleY = FALSE; /* not used yet */
c801d85f 63
ea6f44b5
RR
64 m_signX = 1; /* default x-axis left to right */
65 m_signY = 1; /* default y-axis top down. -1 in postscript. */
c801d85f 66
ea6f44b5
RR
67 m_maxX = 0;
68 m_maxY = 0;
69 m_minX = 0;
70 m_minY = 0;
c801d85f 71
4bc67cc5 72 m_logicalFunction = wxCOPY;
c801d85f 73// m_textAlignment = wxALIGN_TOP_LEFT;
4bc67cc5 74 m_backgroundMode = wxTRANSPARENT;
c801d85f 75
4bc67cc5
RR
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;
c801d85f 82
ea6f44b5 83// m_palette = wxAPP_COLOURMAP; /* I'll learn to handle palettes later in my life */
ff7b1510 84}
c801d85f 85
4bc67cc5 86wxDC::~wxDC()
c801d85f 87{
ff7b1510 88}
c801d85f 89
4bc67cc5 90bool wxDC::Ok() const
cf7a7e13 91{
4bc67cc5 92 return m_ok;
cf7a7e13
RR
93}
94
c801d85f
KB
95void wxDC::DrawArc( long WXUNUSED(x1), long WXUNUSED(y1), long WXUNUSED(x2), long WXUNUSED(y2),
96 double WXUNUSED(xc), double WXUNUSED(yc) )
97{
ff7b1510 98}
c801d85f 99
c801d85f
KB
100void wxDC::DrawPoint( wxPoint& point )
101{
4bc67cc5 102 DrawPoint( point.x, point.y );
ff7b1510 103}
c801d85f
KB
104
105void wxDC::DrawPolygon( wxList *list, long xoffset, long yoffset, int fillStyle )
106{
4bc67cc5
RR
107 int n = list->Number();
108 wxPoint *points = new wxPoint[n];
c801d85f 109
4bc67cc5
RR
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;
ff7b1510 120}
c801d85f
KB
121
122void wxDC::DrawLines( wxList *list, long xoffset, long yoffset )
123{
4bc67cc5
RR
124 int n = list->Number();
125 wxPoint *points = new wxPoint[n];
c801d85f 126
4bc67cc5
RR
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;
ff7b1510 137}
c801d85f
KB
138
139void wxDC::DrawSpline( long x1, long y1, long x2, long y2, long x3, long y3 )
140{
4bc67cc5
RR
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 }
ff7b1510 153}
c801d85f 154
c801d85f
KB
155void wxDC::DrawSpline( int n, wxPoint points[] )
156{
4bc67cc5
RR
157 wxList list;
158 for (int i = 0; i < n; i++) list.Append( (wxObject*)&points[i] );
159 DrawSpline( &list );
ff7b1510 160}
c801d85f
KB
161
162void wxDC::SetClippingRegion( long x, long y, long width, long height )
163{
4bc67cc5
RR
164 m_clipping = TRUE;
165 m_clipX1 = x;
166 m_clipY1 = y;
167 m_clipX2 = x + width;
168 m_clipY2 = y + height;
ff7b1510 169}
c801d85f 170
4bc67cc5 171void wxDC::DestroyClippingRegion()
c801d85f 172{
4bc67cc5 173 m_clipping = FALSE;
ff7b1510 174}
c801d85f
KB
175
176void wxDC::GetClippingBox( long *x, long *y, long *width, long *height ) const
177{
4bc67cc5
RR
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 }
ff7b1510 189}
c801d85f
KB
190
191void wxDC::GetSize( int* width, int* height ) const
192{
4bc67cc5
RR
193 if (width) *width = m_maxX-m_minX;
194 if (height) *height = m_maxY-m_minY;
ff7b1510 195}
c801d85f 196
7bcb11d3 197void wxDC::GetSizeMM( int* width, int* height ) const
c801d85f 198{
4bc67cc5
RR
199 int w = 0;
200 int h = 0;
201 GetSize( &w, &h );
7bcb11d3
JS
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
207wxSize wxDC::GetPPI(void) const
208{
209 // TODO (should probably be pure virtual)
210 return wxSize(0, 0);
ff7b1510 211}
c801d85f
KB
212
213void wxDC::SetTextForeground( const wxColour &col )
214{
4bc67cc5 215 m_textForegroundColour = col;
ff7b1510 216}
c801d85f
KB
217
218void wxDC::SetTextBackground( const wxColour &col )
219{
4bc67cc5 220 m_textBackgroundColour = col;
ff7b1510 221}
c801d85f
KB
222
223void wxDC::SetMapMode( int mode )
224{
4bc67cc5
RR
225 switch (mode)
226 {
e3065973 227 case wxMM_TWIPS:
4bc67cc5
RR
228 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
229 break;
e3065973 230 case wxMM_POINTS:
4bc67cc5
RR
231 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
232 break;
e3065973 233 case wxMM_METRIC:
4bc67cc5
RR
234 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
235 break;
e3065973 236 case wxMM_LOMETRIC:
4bc67cc5
RR
237 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
238 break;
239 default:
e3065973 240 case wxMM_TEXT:
4bc67cc5
RR
241 SetLogicalScale( 1.0, 1.0 );
242 break;
243 }
244/* we don't do this mega optimisation
e3065973 245 if (mode != wxMM_TEXT)
4bc67cc5
RR
246 {
247 m_needComputeScaleX = TRUE;
248 m_needComputeScaleY = TRUE;
249 }
250*/
ff7b1510 251}
c801d85f
KB
252
253void wxDC::SetUserScale( double x, double y )
254{
4bc67cc5
RR
255 // allow negative ? -> no
256 m_userScaleX = x;
257 m_userScaleY = y;
258 ComputeScaleAndOrigin();
ff7b1510 259}
c801d85f
KB
260
261void wxDC::GetUserScale( double *x, double *y )
262{
4bc67cc5
RR
263 if (x) *x = m_userScaleX;
264 if (y) *y = m_userScaleY;
ff7b1510 265}
c801d85f
KB
266
267void wxDC::SetLogicalScale( double x, double y )
268{
4bc67cc5
RR
269 // allow negative ?
270 m_logicalScaleX = x;
271 m_logicalScaleY = y;
272 ComputeScaleAndOrigin();
ff7b1510 273}
c801d85f
KB
274
275void wxDC::GetLogicalScale( double *x, double *y )
276{
4bc67cc5
RR
277 if (x) *x = m_logicalScaleX;
278 if (y) *y = m_logicalScaleY;
ff7b1510 279}
c801d85f
KB
280
281void wxDC::SetLogicalOrigin( long x, long y )
282{
4bc67cc5
RR
283 m_logicalOriginX = x * m_signX; // is this still correct ?
284 m_logicalOriginY = y * m_signY;
285 ComputeScaleAndOrigin();
ff7b1510 286}
c801d85f
KB
287
288void wxDC::GetLogicalOrigin( long *x, long *y )
289{
4bc67cc5
RR
290 if (x) *x = m_logicalOriginX;
291 if (y) *y = m_logicalOriginY;
ff7b1510 292}
c801d85f
KB
293
294void wxDC::SetDeviceOrigin( long x, long y )
295{
4bc67cc5
RR
296 // only wxPostScripDC has m_signX = -1, we override SetDeviceOrigin there
297 m_deviceOriginX = x;
298 m_deviceOriginY = y;
299 ComputeScaleAndOrigin();
ff7b1510 300}
c801d85f
KB
301
302void wxDC::GetDeviceOrigin( long *x, long *y )
303{
4bc67cc5
RR
304 if (x) *x = m_deviceOriginX;
305 if (y) *y = m_deviceOriginY;
ff7b1510 306}
c801d85f 307
c801d85f
KB
308void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
309{
4bc67cc5
RR
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();
ff7b1510 314}
c801d85f
KB
315
316long wxDC::DeviceToLogicalX(long x) const
317{
4bc67cc5 318 return XDEV2LOG(x);
ff7b1510 319}
c801d85f
KB
320
321long wxDC::DeviceToLogicalY(long y) const
322{
4bc67cc5 323 return YDEV2LOG(y);
ff7b1510 324}
c801d85f
KB
325
326long wxDC::DeviceToLogicalXRel(long x) const
327{
4bc67cc5 328 return XDEV2LOGREL(x);
ff7b1510 329}
c801d85f
KB
330
331long wxDC::DeviceToLogicalYRel(long y) const
332{
4bc67cc5 333 return YDEV2LOGREL(y);
ff7b1510 334}
c801d85f
KB
335
336long wxDC::LogicalToDeviceX(long x) const
337{
4bc67cc5 338 return XLOG2DEV(x);
ff7b1510 339}
c801d85f
KB
340
341long wxDC::LogicalToDeviceY(long y) const
342{
4bc67cc5 343 return YLOG2DEV(y);
ff7b1510 344}
c801d85f
KB
345
346long wxDC::LogicalToDeviceXRel(long x) const
347{
4bc67cc5 348 return XLOG2DEVREL(x);
ff7b1510 349}
c801d85f
KB
350
351long wxDC::LogicalToDeviceYRel(long y) const
352{
4bc67cc5 353 return YLOG2DEVREL(y);
ff7b1510 354}
c801d85f
KB
355
356void wxDC::CalcBoundingBox( long x, long y )
357{
4bc67cc5
RR
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;
ff7b1510 362}
c801d85f 363
4bc67cc5 364void wxDC::ComputeScaleAndOrigin()
c801d85f 365{
4bc67cc5
RR
366 // CMB: copy scale to see if it changes
367 double origScaleX = m_scaleX;
368 double origScaleY = m_scaleY;
6f65e337 369
4bc67cc5
RR
370 m_scaleX = m_logicalScaleX * m_userScaleX;
371 m_scaleY = m_logicalScaleY * m_userScaleY;
c801d85f 372
4bc67cc5
RR
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
5b6ec980 379/*
4bc67cc5
RR
380 wxPen pen = m_pen;
381 m_pen = wxNullPen;
382 SetPen( pen );
5b6ec980 383*/
6f65e337 384 }
ff7b1510 385}
c801d85f 386