Made device to logical and vv conversion methods
[wxWidgets.git] / include / wx / msw / dc.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/dc.h
3 // Purpose: wxDC class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_MSW_DC_H_
13 #define _WX_MSW_DC_H_
14
15 #include "wx/defs.h"
16
17 // ---------------------------------------------------------------------------
18 // macros
19 // ---------------------------------------------------------------------------
20
21 #if wxUSE_DC_CACHEING
22 /*
23 * Cached blitting, maintaining a cache
24 * of bitmaps required for transparent blitting
25 * instead of constant creation/deletion
26 */
27
28 class wxDCCacheEntry: public wxObject
29 {
30 public:
31 wxDCCacheEntry(WXHBITMAP hBitmap, int w, int h, int depth);
32 wxDCCacheEntry(WXHDC hDC, int depth);
33 virtual ~wxDCCacheEntry();
34
35 WXHBITMAP m_bitmap;
36 WXHDC m_dc;
37 int m_width;
38 int m_height;
39 int m_depth;
40 };
41 #endif
42
43 // this is an ABC: use one of the derived classes to create a DC associated
44 // with a window, screen, printer and so on
45 class WXDLLEXPORT wxDC : public wxDCBase
46 {
47 public:
48 wxDC(WXHDC hDC) { Init(); m_hDC = hDC; }
49 virtual ~wxDC();
50
51 // implement base class pure virtuals
52 // ----------------------------------
53
54 virtual void Clear();
55
56 virtual bool StartDoc(const wxString& message);
57 virtual void EndDoc();
58
59 virtual void StartPage();
60 virtual void EndPage();
61
62 virtual void SetFont(const wxFont& font);
63 virtual void SetPen(const wxPen& pen);
64 virtual void SetBrush(const wxBrush& brush);
65 virtual void SetBackground(const wxBrush& brush);
66 virtual void SetBackgroundMode(int mode);
67 #if wxUSE_PALETTE
68 virtual void SetPalette(const wxPalette& palette);
69 #endif // wxUSE_PALETTE
70
71 virtual void DestroyClippingRegion();
72
73 virtual wxCoord GetCharHeight() const;
74 virtual wxCoord GetCharWidth() const;
75
76 virtual bool CanDrawBitmap() const;
77 virtual bool CanGetTextExtent() const;
78 virtual int GetDepth() const;
79 virtual wxSize GetPPI() const;
80
81 virtual wxCoord DeviceToLogicalX(wxCoord x) const;
82 virtual wxCoord DeviceToLogicalY(wxCoord y) const;
83 virtual wxCoord DeviceToLogicalXRel(wxCoord x) const;
84 virtual wxCoord DeviceToLogicalYRel(wxCoord y) const;
85 virtual wxCoord LogicalToDeviceX(wxCoord x) const;
86 virtual wxCoord LogicalToDeviceY(wxCoord y) const;
87 virtual wxCoord LogicalToDeviceXRel(wxCoord x) const;
88 virtual wxCoord LogicalToDeviceYRel(wxCoord y) const ;
89
90 virtual void SetMapMode(int mode);
91 virtual void SetUserScale(double x, double y);
92 virtual void SetSystemScale(double x, double y);
93 virtual void SetLogicalScale(double x, double y);
94 virtual void SetLogicalOrigin(wxCoord x, wxCoord y);
95 virtual void SetDeviceOrigin(wxCoord x, wxCoord y);
96 virtual void SetAxisOrientation(bool xLeftRight, bool yBottomUp);
97 virtual void SetLogicalFunction(int function);
98
99 // implementation from now on
100 // --------------------------
101
102 virtual void SetRop(WXHDC cdc);
103 virtual void SelectOldObjects(WXHDC dc);
104
105 wxWindow *GetWindow() const { return m_canvas; }
106 void SetWindow(wxWindow *win)
107 {
108 m_canvas = win;
109
110 #if wxUSE_PALETTE
111 // if we have palettes use the correct one for this window
112 InitializePalette();
113 #endif // wxUSE_PALETTE
114 }
115
116 WXHDC GetHDC() const { return m_hDC; }
117 void SetHDC(WXHDC dc, bool bOwnsDC = false)
118 {
119 m_hDC = dc;
120 m_bOwnsDC = bOwnsDC;
121
122 // we might have a pre existing clipping region, make sure that we
123 // return it if asked -- but avoid calling ::GetClipBox() right now as
124 // it could be unnecessary wasteful
125 m_clipping = true;
126 m_clipX1 =
127 m_clipX2 = 0;
128 }
129
130 const wxBitmap& GetSelectedBitmap() const { return m_selectedBitmap; }
131 wxBitmap& GetSelectedBitmap() { return m_selectedBitmap; }
132
133 // update the internal clip box variables
134 void UpdateClipBox();
135
136 #if wxUSE_DC_CACHEING
137 static wxDCCacheEntry* FindBitmapInCache(WXHDC hDC, int w, int h);
138 static wxDCCacheEntry* FindDCInCache(wxDCCacheEntry* notThis, WXHDC hDC);
139
140 static void AddToBitmapCache(wxDCCacheEntry* entry);
141 static void AddToDCCache(wxDCCacheEntry* entry);
142 static void ClearCache();
143 #endif
144
145 // RTL related functions
146 // ---------------------
147
148 // get or change the layout direction (LTR or RTL) for this dc,
149 // wxLayout_Default is returned if layout direction is not supported
150 virtual wxLayoutDirection GetLayoutDirection() const;
151 virtual void SetLayoutDirection(wxLayoutDirection dir);
152
153 protected:
154 void Init()
155 {
156 m_canvas = NULL;
157 m_bOwnsDC = false;
158 m_hDC = NULL;
159
160 m_oldBitmap = NULL;
161 m_oldPen = NULL;
162 m_oldBrush = NULL;
163 m_oldFont = NULL;
164
165 #if wxUSE_PALETTE
166 m_oldPalette = NULL;
167 #endif // wxUSE_PALETTE
168 }
169
170 // create an uninitialized DC: this should be only used by the derived
171 // classes
172 wxDC() { Init(); }
173
174 virtual void DoGetTextExtent(const wxString& string,
175 wxCoord *x, wxCoord *y,
176 wxCoord *descent = NULL,
177 wxCoord *externalLeading = NULL,
178 const wxFont *theFont = NULL) const;
179 virtual bool DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const;
180
181 virtual bool DoFloodFill(wxCoord x, wxCoord y, const wxColour& col,
182 int style = wxFLOOD_SURFACE);
183
184 virtual void DoGradientFillLinear(const wxRect& rect,
185 const wxColour& initialColour,
186 const wxColour& destColour,
187 wxDirection nDirection = wxEAST);
188
189 virtual bool DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const;
190
191 virtual void DoDrawPoint(wxCoord x, wxCoord y);
192 virtual void DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2);
193
194 virtual void DoDrawArc(wxCoord x1, wxCoord y1,
195 wxCoord x2, wxCoord y2,
196 wxCoord xc, wxCoord yc);
197 virtual void DoDrawCheckMark(wxCoord x, wxCoord y,
198 wxCoord width, wxCoord height);
199 virtual void DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord h,
200 double sa, double ea);
201
202 virtual void DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height);
203 virtual void DoDrawRoundedRectangle(wxCoord x, wxCoord y,
204 wxCoord width, wxCoord height,
205 double radius);
206 virtual void DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height);
207
208 #if wxUSE_SPLINES
209 virtual void DoDrawSpline(wxList *points);
210 #endif
211
212 virtual void DoCrossHair(wxCoord x, wxCoord y);
213
214 virtual void DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y);
215 virtual void DoDrawBitmap(const wxBitmap &bmp, wxCoord x, wxCoord y,
216 bool useMask = false);
217
218 virtual void DoDrawText(const wxString& text, wxCoord x, wxCoord y);
219 virtual void DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y,
220 double angle);
221
222 virtual bool DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height,
223 wxDC *source, wxCoord xsrc, wxCoord ysrc,
224 int rop = wxCOPY, bool useMask = false, wxCoord xsrcMask = wxDefaultCoord, wxCoord ysrcMask = wxDefaultCoord);
225
226 virtual bool DoStretchBlit(wxCoord xdest, wxCoord ydest,
227 wxCoord dstWidth, wxCoord dstHeight,
228 wxDC *source,
229 wxCoord xsrc, wxCoord ysrc,
230 wxCoord srcWidth, wxCoord srcHeight,
231 int rop = wxCOPY, bool useMask = false,
232 wxCoord xsrcMask = wxDefaultCoord, wxCoord ysrcMask = wxDefaultCoord);
233
234 // this is gnarly - we can't even call this function DoSetClippingRegion()
235 // because of virtual function hiding
236 virtual void DoSetClippingRegionAsRegion(const wxRegion& region);
237 virtual void DoSetClippingRegion(wxCoord x, wxCoord y,
238 wxCoord width, wxCoord height);
239 virtual void DoGetClippingBox(wxCoord *x, wxCoord *y,
240 wxCoord *w, wxCoord *h) const;
241
242 virtual void DoGetSizeMM(int* width, int* height) const;
243
244 virtual void DoDrawLines(int n, wxPoint points[],
245 wxCoord xoffset, wxCoord yoffset);
246 virtual void DoDrawPolygon(int n, wxPoint points[],
247 wxCoord xoffset, wxCoord yoffset,
248 int fillStyle = wxODDEVEN_RULE);
249 virtual void DoDrawPolyPolygon(int n, int count[], wxPoint points[],
250 wxCoord xoffset, wxCoord yoffset,
251 int fillStyle = wxODDEVEN_RULE);
252 virtual wxBitmap DoGetAsBitmap(const wxRect *subrect) const
253 { return subrect == NULL ? GetSelectedBitmap() : GetSelectedBitmap().GetSubBitmap(*subrect); }
254
255
256 #if wxUSE_PALETTE
257 // MSW specific, select a logical palette into the HDC
258 // (tell windows to translate pixel from other palettes to our custom one
259 // and vice versa)
260 // Realize tells it to also reset the system palette to this one.
261 void DoSelectPalette(bool realize = false);
262
263 // Find out what palette our parent window has, then select it into the dc
264 void InitializePalette();
265 #endif // wxUSE_PALETTE
266
267 // common part of DoDrawText() and DoDrawRotatedText()
268 void DrawAnyText(const wxString& text, wxCoord x, wxCoord y);
269
270 // common part of DoSetClippingRegion() and DoSetClippingRegionAsRegion()
271 void SetClippingHrgn(WXHRGN hrgn);
272
273 // implementation of DoGetSize() for wxScreen/PrinterDC: this simply
274 // returns the size of the entire device this DC is associated with
275 //
276 // notice that we intentionally put it in a separate function instead of
277 // DoGetSize() itself because we want it to remain pure virtual both
278 // because each derived class should take care to define it as needed (this
279 // implementation is not at all always appropriate) and because we want
280 // wxDC to be an ABC to prevent it from being created directly
281 void GetDeviceSize(int *width, int *height) const;
282
283
284 // MSW-specific member variables
285 // -----------------------------
286
287 // the window associated with this DC (may be NULL)
288 wxWindow *m_canvas;
289
290 wxBitmap m_selectedBitmap;
291
292 // TRUE => DeleteDC() in dtor, FALSE => only ReleaseDC() it
293 bool m_bOwnsDC:1;
294
295 // our HDC
296 WXHDC m_hDC;
297
298 // Store all old GDI objects when do a SelectObject, so we can select them
299 // back in (this unselecting user's objects) so we can safely delete the
300 // DC.
301 WXHBITMAP m_oldBitmap;
302 WXHPEN m_oldPen;
303 WXHBRUSH m_oldBrush;
304 WXHFONT m_oldFont;
305
306 #if wxUSE_PALETTE
307 WXHPALETTE m_oldPalette;
308 #endif // wxUSE_PALETTE
309
310 #if wxUSE_DC_CACHEING
311 static wxList sm_bitmapCache;
312 static wxList sm_dcCache;
313 #endif
314
315 DECLARE_DYNAMIC_CLASS(wxDC)
316 DECLARE_NO_COPY_CLASS(wxDC)
317 };
318
319 // ----------------------------------------------------------------------------
320 // wxDCTemp: a wxDC which doesn't free the given HDC (used by wxWidgets
321 // only/mainly)
322 // ----------------------------------------------------------------------------
323
324 class WXDLLEXPORT wxDCTemp : public wxDC
325 {
326 public:
327 // construct a temporary DC with the specified HDC and size (it should be
328 // specified whenever we know it for this HDC)
329 wxDCTemp(WXHDC hdc, const wxSize& size = wxDefaultSize)
330 : wxDC(hdc),
331 m_size(size)
332 {
333 }
334
335 virtual ~wxDCTemp()
336 {
337 // prevent base class dtor from freeing it
338 SetHDC((WXHDC)NULL);
339 }
340
341 protected:
342 virtual void DoGetSize(int *w, int *h) const
343 {
344 wxASSERT_MSG( m_size.IsFullySpecified(),
345 _T("size of this DC hadn't been set and is unknown") );
346
347 if ( w )
348 *w = m_size.x;
349 if ( h )
350 *h = m_size.y;
351 }
352
353 private:
354 // size of this DC must be explicitly set by SetSize() as we have no way to
355 // find it ourselves
356 const wxSize m_size;
357
358 DECLARE_NO_COPY_CLASS(wxDCTemp)
359 };
360
361 #endif // _WX_MSW_DC_H_
362