1. MSW message handling simplifications
[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, wxDCBase)
36
37 wxDC::wxDC()
38 {
39 m_ok = FALSE;
40
41 m_optimize = FALSE;
42 m_autoSetting = FALSE;
43
44 m_mm_to_pix_x = 1.0;
45 m_mm_to_pix_y = 1.0;
46
47 m_needComputeScaleX = FALSE; /* not used yet */
48 m_needComputeScaleY = FALSE; /* not used yet */
49
50 m_logicalFunction = wxCOPY;
51
52 m_pen = *wxBLACK_PEN;
53 m_font = *wxNORMAL_FONT;
54 m_brush = *wxTRANSPARENT_BRUSH;
55 }
56
57 void wxDC::DoSetClippingRegion( long x, long y, long width, long height )
58 {
59 m_clipping = TRUE;
60 m_clipX1 = x;
61 m_clipY1 = y;
62 m_clipX2 = x + width;
63 m_clipY2 = y + height;
64 }
65
66 void wxDC::DestroyClippingRegion()
67 {
68 m_clipping = FALSE;
69 }
70
71 // ---------------------------------------------------------------------------
72 // get DC capabilities
73 // ---------------------------------------------------------------------------
74
75 void wxDC::DoGetSize( int* width, int* height ) const
76 {
77 if (width) *width = m_maxX-m_minX;
78 if (height) *height = m_maxY-m_minY;
79 }
80
81 void wxDC::DoGetSizeMM( int* width, int* height ) const
82 {
83 int w = 0;
84 int h = 0;
85 GetSize( &w, &h );
86 if (width) *width = int( double(w) / (m_scaleX*m_mm_to_pix_x) );
87 if (height) *height = int( double(h) / (m_scaleY*m_mm_to_pix_y) );
88 }
89
90 // Resolution in pixels per logical inch
91 wxSize wxDC::GetPPI() const
92 {
93 // TODO (should probably be pure virtual)
94 return wxSize(0, 0);
95 }
96
97 // ---------------------------------------------------------------------------
98 // set various DC parameters
99 // ---------------------------------------------------------------------------
100
101 void wxDC::ComputeScaleAndOrigin()
102 {
103 // CMB: copy scale to see if it changes
104 double origScaleX = m_scaleX;
105 double origScaleY = m_scaleY;
106
107 m_scaleX = m_logicalScaleX * m_userScaleX;
108 m_scaleY = m_logicalScaleY * m_userScaleY;
109
110 // CMB: if scale has changed call SetPen to recalulate the line width
111 if (m_scaleX != origScaleX || m_scaleY != origScaleY)
112 {
113 // this is a bit artificial, but we need to force wxDC to think
114 // the pen has changed
115 // It gives an Assert, Robert Roebling
116 /*
117 wxPen pen = m_pen;
118 m_pen = wxNullPen;
119 SetPen( pen );
120 */
121 }
122 }
123
124 void wxDC::SetMapMode( int mode )
125 {
126 switch (mode)
127 {
128 case wxMM_TWIPS:
129 SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y );
130 break;
131 case wxMM_POINTS:
132 SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y );
133 break;
134 case wxMM_METRIC:
135 SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y );
136 break;
137 case wxMM_LOMETRIC:
138 SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 );
139 break;
140 default:
141 case wxMM_TEXT:
142 SetLogicalScale( 1.0, 1.0 );
143 break;
144 }
145 /* we don't do this mega optimisation
146 if (mode != wxMM_TEXT)
147 {
148 m_needComputeScaleX = TRUE;
149 m_needComputeScaleY = TRUE;
150 }
151 */
152 }
153
154 void wxDC::SetUserScale( double x, double y )
155 {
156 // allow negative ? -> no
157 m_userScaleX = x;
158 m_userScaleY = y;
159 ComputeScaleAndOrigin();
160 }
161
162 void wxDC::SetLogicalScale( double x, double y )
163 {
164 // allow negative ?
165 m_logicalScaleX = x;
166 m_logicalScaleY = y;
167 ComputeScaleAndOrigin();
168 }
169
170 void wxDC::SetLogicalOrigin( long x, long y )
171 {
172 m_logicalOriginX = x * m_signX; // is this still correct ?
173 m_logicalOriginY = y * m_signY;
174 ComputeScaleAndOrigin();
175 }
176
177 void wxDC::SetDeviceOrigin( long x, long y )
178 {
179 // only wxPostScripDC has m_signX = -1, we override SetDeviceOrigin there
180 m_deviceOriginX = x;
181 m_deviceOriginY = y;
182 ComputeScaleAndOrigin();
183 }
184
185 void wxDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
186 {
187 // only wxPostScripDC has m_signX = -1, we override SetAxisOrientation there
188 m_signX = (xLeftRight ? 1 : -1);
189 m_signY = (yBottomUp ? -1 : 1);
190 ComputeScaleAndOrigin();
191 }
192
193 // ---------------------------------------------------------------------------
194 // coordinates transformations
195 // ---------------------------------------------------------------------------
196
197 long wxDCBase::DeviceToLogicalX(long x) const
198 {
199 return XDEV2LOG(x);
200 }
201
202 long wxDCBase::DeviceToLogicalY(long y) const
203 {
204 return YDEV2LOG(y);
205 }
206
207 long wxDCBase::DeviceToLogicalXRel(long x) const
208 {
209 return XDEV2LOGREL(x);
210 }
211
212 long wxDCBase::DeviceToLogicalYRel(long y) const
213 {
214 return YDEV2LOGREL(y);
215 }
216
217 long wxDCBase::LogicalToDeviceX(long x) const
218 {
219 return XLOG2DEV(x);
220 }
221
222 long wxDCBase::LogicalToDeviceY(long y) const
223 {
224 return YLOG2DEV(y);
225 }
226
227 long wxDCBase::LogicalToDeviceXRel(long x) const
228 {
229 return XLOG2DEVREL(x);
230 }
231
232 long wxDCBase::LogicalToDeviceYRel(long y) const
233 {
234 return YLOG2DEVREL(y);
235 }
236