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