]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dc.cpp
Don't eliminate text completely in Ellipsize().
[wxWidgets.git] / src / msw / dc.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
ad0ac642
WS
2// Name: src/msw/dc.cpp
3// Purpose: wxDC class for MSW port
2bda0e17
KB
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
a23fd0e1
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
a23fd0e1 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
27#ifndef WX_PRECOMP
57bd4c60 28 #include "wx/msw/wrapcdlg.h"
fb6724f1 29 #include "wx/image.h"
4286a5b5 30 #include "wx/window.h"
a23fd0e1
VZ
31 #include "wx/utils.h"
32 #include "wx/dialog.h"
33 #include "wx/app.h"
34 #include "wx/bitmap.h"
35 #include "wx/dcmemory.h"
0c589ad0 36 #include "wx/log.h"
4286a5b5 37 #include "wx/icon.h"
6d50343d 38 #include "wx/dcprint.h"
02761f6c 39 #include "wx/module.h"
2bda0e17
KB
40#endif
41
025f7d77 42#include "wx/msw/dc.h"
0cbff120 43#include "wx/sysopt.h"
19fc41a3 44#include "wx/dynlib.h"
1e3c12d7 45
3fb1e059 46#ifdef wxHAS_RAW_BITMAP
d8c89c48 47 #include "wx/rawbmp.h"
1e3c12d7 48#endif
2bda0e17
KB
49
50#include <string.h>
2bda0e17 51
d8c89c48
VZ
52#include "wx/msw/private/dc.h"
53
54using namespace wxMSWImpl;
2bda0e17 55
878711c0 56#ifndef AC_SRC_ALPHA
6ae7410f
VZ
57 #define AC_SRC_ALPHA 1
58#endif
59
60#ifndef LAYOUT_RTL
61 #define LAYOUT_RTL 1
878711c0
VZ
62#endif
63
3bce6687
JS
64/* Quaternary raster codes */
65#ifndef MAKEROP4
66#define MAKEROP4(fore,back) (DWORD)((((back) << 8) & 0xFF000000) | (fore))
67#endif
68
82a306b7
VZ
69// apparently with MicroWindows it is possible that HDC is 0 so we have to
70// check for this ourselves
71#ifdef __WXMICROWIN__
72 #define WXMICROWIN_CHECK_HDC if ( !GetHDC() ) return;
73 #define WXMICROWIN_CHECK_HDC_RET(x) if ( !GetHDC() ) return x;
74#else
75 #define WXMICROWIN_CHECK_HDC
76 #define WXMICROWIN_CHECK_HDC_RET(x)
77#endif
78
888dde65 79IMPLEMENT_ABSTRACT_CLASS(wxMSWDCImpl, wxDCImpl)
2bda0e17 80
a23fd0e1
VZ
81// ---------------------------------------------------------------------------
82// constants
83// ---------------------------------------------------------------------------
84
42e69d6b
VZ
85static const int VIEWPORT_EXTENT = 1000;
86
4aff28fc
VZ
87// ROPs which don't have standard names (see "Ternary Raster Operations" in the
88// MSDN docs for how this and other numbers in wxDC::Blit() are obtained)
89#define DSTCOPY 0x00AA0029 // a.k.a. NOP operation
90
1e2081a1
VZ
91// ----------------------------------------------------------------------------
92// macros for logical <-> device coords conversion
93// ----------------------------------------------------------------------------
94
95/*
96 We currently let Windows do all the translations itself so these macros are
97 not really needed (any more) but keep them to enhance readability of the
98 code by allowing to see where are the logical and where are the device
99 coordinates used.
100 */
101
1a4b50d2 102#ifdef __WXWINCE__
1d63de4a
JS
103 #define XLOG2DEV(x) ((x-m_logicalOriginX)*m_signX)
104 #define YLOG2DEV(y) ((y-m_logicalOriginY)*m_signY)
105 #define XDEV2LOG(x) ((x)*m_signX+m_logicalOriginX)
106 #define YDEV2LOG(y) ((y)*m_signY+m_logicalOriginY)
1a4b50d2
RR
107#else
108 #define XLOG2DEV(x) (x)
109 #define YLOG2DEV(y) (y)
110 #define XDEV2LOG(x) (x)
111 #define YDEV2LOG(y) (y)
112#endif
1e2081a1 113
4314ec48
VZ
114// ---------------------------------------------------------------------------
115// private functions
116// ---------------------------------------------------------------------------
117
118// convert degrees to radians
119static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
120
275a63e3
VZ
121// call AlphaBlend() to blit contents of hdcSrc to hdcDst using alpha
122//
123// NB: bmpSrc is the bitmap selected in hdcSrc, it is not really needed
124// to pass it to this function but as we already have it at the point
125// of call anyhow we do
126//
127// return true if we could draw the bitmap in one way or the other, false
128// otherwise
129static bool AlphaBlt(HDC hdcDst,
e3b81044 130 int x, int y, int dstWidth, int dstHeight,
1ee280b7 131 int srcX, int srcY,
e3b81044
VZ
132 int srcWidth, int srcHeight,
133 HDC hdcSrc,
134 const wxBitmap& bmp);
1e3c12d7 135
3fb1e059 136#ifdef wxHAS_RAW_BITMAP
761598d4
VZ
137
138// our (limited) AlphaBlend() replacement for Windows versions not providing it
878711c0 139static void
e3b81044
VZ
140wxAlphaBlend(HDC hdcDst, int xDst, int yDst,
141 int dstWidth, int dstHeight,
1ee280b7 142 int srcX, int srcY,
e3b81044
VZ
143 int srcWidth, int srcHeight,
144 const wxBitmap& bmpSrc);
761598d4 145
3fb1e059 146#endif // wxHAS_RAW_BITMAP
878711c0 147
f6bcfd97
BP
148// ----------------------------------------------------------------------------
149// private classes
150// ----------------------------------------------------------------------------
151
152// instead of duplicating the same code which sets and then restores text
153// colours in each wxDC method working with wxSTIPPLE_MASK_OPAQUE brushes,
154// encapsulate this in a small helper class
155
76f91e77 156// wxBrushAttrsSetter: changes the text colours in the ctor if required and
f6bcfd97 157// restores them in the dtor
76f91e77
VZ
158class wxBrushAttrsSetter : private wxBkModeChanger,
159 private wxTextColoursChanger
f6bcfd97
BP
160{
161public:
76f91e77 162 wxBrushAttrsSetter(wxMSWDCImpl& dc);
f6bcfd97
BP
163
164private:
c0c133e1 165 wxDECLARE_NO_COPY_CLASS(wxBrushAttrsSetter);
f6bcfd97
BP
166};
167
4f64fde7
VZ
168#ifdef __WXWINCE__
169
170#define SET_STRETCH_BLT_MODE(hdc)
171
172#else // !__WXWINCE__
173
174// this class sets the stretch blit mode to COLORONCOLOR during its lifetime
175//
176// don't use it directly, use SET_STRETCH_BLT_MODE() macro instead as it
177// expands to nothing under WinCE which doesn't have SetStretchBltMode()
f178ab7e
VZ
178class StretchBltModeChanger
179{
180public:
4f64fde7 181 StretchBltModeChanger(HDC hdc)
f178ab7e
VZ
182 : m_hdc(hdc)
183 {
4f64fde7 184 m_modeOld = ::SetStretchBltMode(m_hdc, COLORONCOLOR);
f178ab7e 185 if ( !m_modeOld )
43b2d5e7 186 {
9a83f860 187 wxLogLastError(wxT("SetStretchBltMode"));
43b2d5e7 188 }
f178ab7e
VZ
189 }
190
191 ~StretchBltModeChanger()
192 {
193 if ( !::SetStretchBltMode(m_hdc, m_modeOld) )
43b2d5e7 194 {
9a83f860 195 wxLogLastError(wxT("SetStretchBltMode"));
43b2d5e7 196 }
f178ab7e
VZ
197 }
198
199private:
200 const HDC m_hdc;
201
202 int m_modeOld;
2eb10e2a 203
c0c133e1 204 wxDECLARE_NO_COPY_CLASS(StretchBltModeChanger);
f178ab7e
VZ
205};
206
4f64fde7
VZ
207#define SET_STRETCH_BLT_MODE(hdc) \
208 StretchBltModeChanger wxMAKE_UNIQUE_NAME(stretchModeChanger)(hdc)
209
210#endif // __WXWINCE__/!__WXWINCE__
211
a8ff046b
VZ
212#if wxUSE_DYNLIB_CLASS
213
6ae7410f
VZ
214// helper class to cache dynamically loaded libraries and not attempt reloading
215// them if it fails
216class wxOnceOnlyDLLLoader
213ad8e7
VZ
217{
218public:
6ae7410f
VZ
219 // ctor argument must be a literal string as we don't make a copy of it!
220 wxOnceOnlyDLLLoader(const wxChar *dllName)
221 : m_dllName(dllName)
213ad8e7 222 {
47b378bd 223 }
6ae7410f
VZ
224
225
226 // return the symbol with the given name or NULL if the DLL not loaded
227 // or symbol not present
228 void *GetSymbol(const wxChar *name)
229 {
230 // we're prepared to handle errors here
213ad8e7
VZ
231 wxLogNull noLog;
232
6ae7410f 233 if ( m_dllName )
213ad8e7 234 {
6ae7410f
VZ
235 m_dll.Load(m_dllName);
236
237 // reset the name whether we succeeded or failed so that we don't
238 // try again the next time
239 m_dllName = NULL;
213ad8e7
VZ
240 }
241
6ae7410f 242 return m_dll.IsLoaded() ? m_dll.GetSymbol(name) : NULL;
213ad8e7
VZ
243 }
244
60b0c3b4
VZ
245 void Unload()
246 {
247 if ( m_dll.IsLoaded() )
248 {
249 m_dll.Unload();
250 }
251 }
252
213ad8e7 253private:
6ae7410f
VZ
254 wxDynamicLibrary m_dll;
255 const wxChar *m_dllName;
213ad8e7
VZ
256};
257
9a83f860 258static wxOnceOnlyDLLLoader wxMSIMG32DLL(wxT("msimg32"));
213ad8e7 259
60b0c3b4
VZ
260// we must ensure that DLLs are unloaded before the static objects cleanup time
261// because we may hit the notorious DllMain() dead lock in this case if wx is
262// used as a DLL (attempting to unload another DLL from inside DllMain() hangs
263// under Windows because it tries to reacquire the same lock)
264class wxGDIDLLsCleanupModule : public wxModule
265{
266public:
267 virtual bool OnInit() { return true; }
268 virtual void OnExit() { wxMSIMG32DLL.Unload(); }
269
270private:
271 DECLARE_DYNAMIC_CLASS(wxGDIDLLsCleanupModule)
272};
273
274IMPLEMENT_DYNAMIC_CLASS(wxGDIDLLsCleanupModule, wxModule)
275
a8ff046b
VZ
276#endif // wxUSE_DYNLIB_CLASS
277
a23fd0e1
VZ
278// ===========================================================================
279// implementation
280// ===========================================================================
281
f6bcfd97 282// ----------------------------------------------------------------------------
76f91e77 283// wxBrushAttrsSetter
f6bcfd97
BP
284// ----------------------------------------------------------------------------
285
76f91e77
VZ
286wxBrushAttrsSetter::wxBrushAttrsSetter(wxMSWDCImpl& dc)
287 : wxBkModeChanger(GetHdcOf(dc)),
288 wxTextColoursChanger(GetHdcOf(dc))
f6bcfd97 289{
cafbad8f 290 const wxBrush& brush = dc.GetBrush();
cb129171 291 if ( brush.IsOk() && brush.GetStyle() == wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE )
f6bcfd97 292 {
77ffb593 293 // note that Windows convention is opposite to wxWidgets one, this is
f6bcfd97 294 // why text colour becomes the background one and vice versa
76f91e77
VZ
295 wxTextColoursChanger::Change(dc.GetTextBackground(),
296 dc.GetTextForeground());
f6bcfd97 297
76f91e77 298 wxBkModeChanger::Change(dc.GetBackgroundMode());
f6bcfd97
BP
299 }
300}
301
f5725872
VZ
302// ----------------------------------------------------------------------------
303// wxDC MSW-specific methods
304// ----------------------------------------------------------------------------
305
306WXHDC wxDC::GetHDC() const
307{
308 wxMSWDCImpl * const impl = wxDynamicCast(GetImpl(), wxMSWDCImpl);
309 return impl ? impl->GetHDC() : 0;
310}
311
a23fd0e1 312// ---------------------------------------------------------------------------
888dde65 313// wxMSWDCImpl
a23fd0e1 314// ---------------------------------------------------------------------------
2bda0e17 315
1ee280b7 316wxMSWDCImpl::wxMSWDCImpl( wxDC *owner, WXHDC hDC ) :
888dde65 317 wxDCImpl( owner )
1ee280b7
VZ
318{
319 Init();
320 m_hDC = hDC;
888dde65
RR
321}
322
323wxMSWDCImpl::~wxMSWDCImpl()
2bda0e17 324{
7ba4fbeb
VZ
325 if ( m_hDC != 0 )
326 {
7bcb11d3 327 SelectOldObjects(m_hDC);
7ba4fbeb
VZ
328
329 // if we own the HDC, we delete it, otherwise we just release it
330
331 if ( m_bOwnsDC )
332 {
333 ::DeleteDC(GetHdc());
7bcb11d3 334 }
7ba4fbeb
VZ
335 else // we don't own our HDC
336 {
888dde65 337 if (m_window)
db400410 338 {
888dde65 339 ::ReleaseDC(GetHwndOf(m_window), GetHdc());
db400410
JS
340 }
341 else
342 {
343 // Must have been a wxScreenDC
344 ::ReleaseDC((HWND) NULL, GetHdc());
345 }
7ba4fbeb
VZ
346 }
347 }
2bda0e17
KB
348}
349
350// This will select current objects out of the DC,
351// which is what you have to do before deleting the
352// DC.
888dde65 353void wxMSWDCImpl::SelectOldObjects(WXHDC dc)
2bda0e17 354{
7bcb11d3 355 if (dc)
2bda0e17 356 {
7bcb11d3
JS
357 if (m_oldBitmap)
358 {
359 ::SelectObject((HDC) dc, (HBITMAP) m_oldBitmap);
87d3576f 360 if (m_selectedBitmap.IsOk())
7bcb11d3
JS
361 {
362 m_selectedBitmap.SetSelectedInto(NULL);
363 }
364 }
a23fd0e1 365 m_oldBitmap = 0;
7bcb11d3
JS
366 if (m_oldPen)
367 {
368 ::SelectObject((HDC) dc, (HPEN) m_oldPen);
369 }
a23fd0e1 370 m_oldPen = 0;
7bcb11d3
JS
371 if (m_oldBrush)
372 {
373 ::SelectObject((HDC) dc, (HBRUSH) m_oldBrush);
374 }
a23fd0e1 375 m_oldBrush = 0;
7bcb11d3
JS
376 if (m_oldFont)
377 {
378 ::SelectObject((HDC) dc, (HFONT) m_oldFont);
379 }
a23fd0e1 380 m_oldFont = 0;
d275c7eb
VZ
381
382#if wxUSE_PALETTE
7bcb11d3
JS
383 if (m_oldPalette)
384 {
19193a2c 385 ::SelectPalette((HDC) dc, (HPALETTE) m_oldPalette, FALSE);
7bcb11d3 386 }
a23fd0e1 387 m_oldPalette = 0;
d275c7eb 388#endif // wxUSE_PALETTE
2bda0e17 389 }
a23fd0e1
VZ
390
391 m_brush = wxNullBrush;
7bcb11d3 392 m_pen = wxNullPen;
d275c7eb 393#if wxUSE_PALETTE
7bcb11d3 394 m_palette = wxNullPalette;
d275c7eb 395#endif // wxUSE_PALETTE
7bcb11d3
JS
396 m_font = wxNullFont;
397 m_backgroundBrush = wxNullBrush;
398 m_selectedBitmap = wxNullBitmap;
2bda0e17
KB
399}
400
a23fd0e1
VZ
401// ---------------------------------------------------------------------------
402// clipping
403// ---------------------------------------------------------------------------
404
888dde65 405void wxMSWDCImpl::UpdateClipBox()
1e6feb95 406{
82a306b7 407 WXMICROWIN_CHECK_HDC
d275c7eb 408
1e6feb95 409 RECT rect;
5230934a 410 ::GetClipBox(GetHdc(), &rect);
1e6feb95
VZ
411
412 m_clipX1 = (wxCoord) XDEV2LOG(rect.left);
413 m_clipY1 = (wxCoord) YDEV2LOG(rect.top);
414 m_clipX2 = (wxCoord) XDEV2LOG(rect.right);
415 m_clipY2 = (wxCoord) YDEV2LOG(rect.bottom);
f547e9bb
RL
416}
417
2e76da54 418void
888dde65 419wxMSWDCImpl::DoGetClippingBox(wxCoord *x, wxCoord *y, wxCoord *w, wxCoord *h) const
2e76da54
VZ
420{
421 // check if we should try to retrieve the clipping region possibly not set
422 // by our SetClippingRegion() but preset by Windows:this can only happen
423 // when we're associated with an existing HDC usign SetHDC(), see there
424 if ( m_clipping && !m_clipX1 && !m_clipX2 )
425 {
888dde65 426 wxMSWDCImpl *self = wxConstCast(this, wxMSWDCImpl);
395b914e 427 self->UpdateClipBox();
2e76da54
VZ
428
429 if ( !m_clipX1 && !m_clipX2 )
395b914e 430 self->m_clipping = false;
2e76da54
VZ
431 }
432
888dde65 433 wxDCImpl::DoGetClippingBox(x, y, w, h);
2e76da54
VZ
434}
435
fdaad94e 436// common part of DoSetClippingRegion() and DoSetDeviceClippingRegion()
888dde65 437void wxMSWDCImpl::SetClippingHrgn(WXHRGN hrgn)
2bda0e17 438{
5230934a
VZ
439 wxCHECK_RET( hrgn, wxT("invalid clipping region") );
440
82a306b7 441 WXMICROWIN_CHECK_HDC
5230934a
VZ
442
443 // note that we combine the new clipping region with the existing one: this
444 // is compatible with what the other ports do and is the documented
445 // behaviour now (starting with 2.3.3)
3a5bcc4d 446#if defined(__WXWINCE__)
5230934a
VZ
447 RECT rectClip;
448 if ( !::GetClipBox(GetHdc(), &rectClip) )
449 return;
450
1d63de4a
JS
451 // GetClipBox returns logical coordinates, so transform to device
452 rectClip.left = LogicalToDeviceX(rectClip.left);
453 rectClip.top = LogicalToDeviceY(rectClip.top);
454 rectClip.right = LogicalToDeviceX(rectClip.right);
455 rectClip.bottom = LogicalToDeviceY(rectClip.bottom);
456
5230934a
VZ
457 HRGN hrgnDest = ::CreateRectRgn(0, 0, 0, 0);
458 HRGN hrgnClipOld = ::CreateRectRgn(rectClip.left, rectClip.top,
459 rectClip.right, rectClip.bottom);
460
461 if ( ::CombineRgn(hrgnDest, hrgnClipOld, (HRGN)hrgn, RGN_AND) != ERROR )
462 {
463 ::SelectClipRgn(GetHdc(), hrgnDest);
464 }
465
466 ::DeleteObject(hrgnClipOld);
467 ::DeleteObject(hrgnDest);
3a5bcc4d 468#else // !WinCE
5230934a
VZ
469 if ( ::ExtSelectClipRgn(GetHdc(), (HRGN)hrgn, RGN_AND) == ERROR )
470 {
9a83f860 471 wxLogLastError(wxT("ExtSelectClipRgn"));
5230934a
VZ
472
473 return;
474 }
3a5bcc4d 475#endif // WinCE/!WinCE
d275c7eb 476
beb966c5 477 m_clipping = true;
40a89076 478
5230934a
VZ
479 UpdateClipBox();
480}
481
888dde65 482void wxMSWDCImpl::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
5230934a 483{
c4218a74
VZ
484 // the region coords are always the device ones, so do the translation
485 // manually
1e6feb95
VZ
486 //
487 // FIXME: possible +/-1 error here, to check!
c4218a74
VZ
488 HRGN hrgn = ::CreateRectRgn(LogicalToDeviceX(x),
489 LogicalToDeviceY(y),
490 LogicalToDeviceX(x + w),
491 LogicalToDeviceY(y + h));
40a89076
VZ
492 if ( !hrgn )
493 {
9a83f860 494 wxLogLastError(wxT("CreateRectRgn"));
40a89076
VZ
495 }
496 else
497 {
5230934a 498 SetClippingHrgn((WXHRGN)hrgn);
40a89076 499
5230934a 500 ::DeleteObject(hrgn);
40a89076 501 }
2bda0e17
KB
502}
503
fdaad94e 504void wxMSWDCImpl::DoSetDeviceClippingRegion(const wxRegion& region)
a724d789 505{
5230934a 506 SetClippingHrgn(region.GetHRGN());
2bda0e17
KB
507}
508
888dde65 509void wxMSWDCImpl::DestroyClippingRegion()
2bda0e17 510{
82a306b7 511 WXMICROWIN_CHECK_HDC
d275c7eb 512
7bcb11d3
JS
513 if (m_clipping && m_hDC)
514 {
07225d48
JS
515#if 1
516 // On a PocketPC device (not necessarily emulator), resetting
517 // the clip region as per the old method causes bad display
518 // problems. In fact setting a null region is probably OK
519 // on desktop WIN32 also, since the WIN32 docs imply that the user
520 // clipping region is independent from the paint clipping region.
521 ::SelectClipRgn(GetHdc(), 0);
3cdcf4d4 522#else
7bcb11d3 523 // TODO: this should restore the previous clipping region,
5230934a
VZ
524 // so that OnPaint processing works correctly, and the update
525 // clipping region doesn't get destroyed after the first
526 // DestroyClippingRegion.
7bcb11d3 527 HRGN rgn = CreateRectRgn(0, 0, 32000, 32000);
5230934a
VZ
528 ::SelectClipRgn(GetHdc(), rgn);
529 ::DeleteObject(rgn);
3cdcf4d4 530#endif
7bcb11d3 531 }
1e6feb95 532
888dde65 533 wxDCImpl::DestroyClippingRegion();
2bda0e17
KB
534}
535
a23fd0e1
VZ
536// ---------------------------------------------------------------------------
537// query capabilities
538// ---------------------------------------------------------------------------
539
888dde65 540bool wxMSWDCImpl::CanDrawBitmap() const
2bda0e17 541{
beb966c5 542 return true;
2bda0e17
KB
543}
544
888dde65 545bool wxMSWDCImpl::CanGetTextExtent() const
2bda0e17 546{
04ef50df
JS
547#ifdef __WXMICROWIN__
548 // TODO Extend MicroWindows' GetDeviceCaps function
beb966c5 549 return true;
04ef50df 550#else
7bcb11d3 551 // What sort of display is it?
a23fd0e1
VZ
552 int technology = ::GetDeviceCaps(GetHdc(), TECHNOLOGY);
553
554 return (technology == DT_RASDISPLAY) || (technology == DT_RASPRINTER);
04ef50df 555#endif
2bda0e17
KB
556}
557
888dde65 558int wxMSWDCImpl::GetDepth() const
2bda0e17 559{
82a306b7 560 WXMICROWIN_CHECK_HDC_RET(16)
d275c7eb 561
a23fd0e1 562 return (int)::GetDeviceCaps(GetHdc(), BITSPIXEL);
2bda0e17
KB
563}
564
a23fd0e1
VZ
565// ---------------------------------------------------------------------------
566// drawing
567// ---------------------------------------------------------------------------
568
888dde65 569void wxMSWDCImpl::Clear()
2bda0e17 570{
82a306b7 571 WXMICROWIN_CHECK_HDC
d275c7eb 572
7bcb11d3 573 RECT rect;
888dde65 574 if (m_window)
2506aab6 575 {
888dde65 576 GetClientRect((HWND) m_window->GetHWND(), &rect);
2506aab6
VZ
577 }
578 else
7bcb11d3 579 {
eaeb6a3c
JS
580 // No, I think we should simply ignore this if printing on e.g.
581 // a printer DC.
87d3576f
RR
582 // wxCHECK_RET( m_selectedBitmap.IsOk(), wxT("this DC can't be cleared") );
583 if (!m_selectedBitmap.IsOk())
eaeb6a3c 584 return;
2506aab6 585
dbe6f5f0
RD
586 rect.left = -m_deviceOriginX; rect.top = -m_deviceOriginY;
587 rect.right = m_selectedBitmap.GetWidth()-m_deviceOriginX;
588 rect.bottom = m_selectedBitmap.GetHeight()-m_deviceOriginY;
7bcb11d3 589 }
2506aab6 590
4676948b 591#ifndef __WXWINCE__
a23fd0e1 592 (void) ::SetMapMode(GetHdc(), MM_TEXT);
4676948b 593#endif
a23fd0e1 594
1e2081a1
VZ
595 DWORD colour = ::GetBkColor(GetHdc());
596 HBRUSH brush = ::CreateSolidBrush(colour);
597 ::FillRect(GetHdc(), &rect, brush);
598 ::DeleteObject(brush);
599
04ab8b6d 600 RealizeScaleAndOrigin();
a23fd0e1
VZ
601}
602
888dde65 603bool wxMSWDCImpl::DoFloodFill(wxCoord WXUNUSED_IN_WINCE(x),
0c0d1521
WS
604 wxCoord WXUNUSED_IN_WINCE(y),
605 const wxColour& WXUNUSED_IN_WINCE(col),
89efaf2b 606 wxFloodFillStyle WXUNUSED_IN_WINCE(style))
a23fd0e1 607{
4676948b 608#ifdef __WXWINCE__
beb966c5 609 return false;
4676948b 610#else
beb966c5 611 WXMICROWIN_CHECK_HDC_RET(false)
d275c7eb 612
387ebd3e 613 bool success = (0 != ::ExtFloodFill(GetHdc(), XLOG2DEV(x), YLOG2DEV(y),
0bafad0c
VZ
614 col.GetPixel(),
615 style == wxFLOOD_SURFACE ? FLOODFILLSURFACE
387ebd3e
JS
616 : FLOODFILLBORDER) ) ;
617 if (!success)
0bafad0c
VZ
618 {
619 // quoting from the MSDN docs:
620 //
621 // Following are some of the reasons this function might fail:
622 //
623 // * The filling could not be completed.
624 // * The specified point has the boundary color specified by the
625 // crColor parameter (if FLOODFILLBORDER was requested).
626 // * The specified point does not have the color specified by
627 // crColor (if FLOODFILLSURFACE was requested)
628 // * The point is outside the clipping region that is, it is not
629 // visible on the device.
630 //
f6bcfd97 631 wxLogLastError(wxT("ExtFloodFill"));
0bafad0c 632 }
a23fd0e1 633
7bcb11d3 634 CalcBoundingBox(x, y);
419430a0 635
387ebd3e 636 return success;
4676948b 637#endif
2bda0e17
KB
638}
639
888dde65 640bool wxMSWDCImpl::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const
2bda0e17 641{
beb966c5 642 WXMICROWIN_CHECK_HDC_RET(false)
d275c7eb 643
9a83f860 644 wxCHECK_MSG( col, false, wxT("NULL colour parameter in wxMSWDCImpl::GetPixel") );
f6bcfd97 645
7bcb11d3 646 // get the color of the pixel
a23fd0e1 647 COLORREF pixelcolor = ::GetPixel(GetHdc(), XLOG2DEV(x), YLOG2DEV(y));
0bafad0c 648
f6bcfd97 649 wxRGBToColour(*col, pixelcolor);
dc1efb1d 650
beb966c5 651 return true;
2bda0e17
KB
652}
653
888dde65 654void wxMSWDCImpl::DoCrossHair(wxCoord x, wxCoord y)
a23fd0e1 655{
82a306b7 656 WXMICROWIN_CHECK_HDC
d275c7eb 657
72cdf4c9
VZ
658 wxCoord x1 = x-VIEWPORT_EXTENT;
659 wxCoord y1 = y-VIEWPORT_EXTENT;
660 wxCoord x2 = x+VIEWPORT_EXTENT;
661 wxCoord y2 = y+VIEWPORT_EXTENT;
a23fd0e1 662
4676948b
JS
663 wxDrawLine(GetHdc(), XLOG2DEV(x1), YLOG2DEV(y), XLOG2DEV(x2), YLOG2DEV(y));
664 wxDrawLine(GetHdc(), XLOG2DEV(x), YLOG2DEV(y1), XLOG2DEV(x), YLOG2DEV(y2));
a23fd0e1 665
7bcb11d3
JS
666 CalcBoundingBox(x1, y1);
667 CalcBoundingBox(x2, y2);
2bda0e17
KB
668}
669
888dde65 670void wxMSWDCImpl::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2)
2bda0e17 671{
82a306b7 672 WXMICROWIN_CHECK_HDC
d275c7eb 673
4676948b 674 wxDrawLine(GetHdc(), XLOG2DEV(x1), YLOG2DEV(y1), XLOG2DEV(x2), YLOG2DEV(y2));
a23fd0e1 675
7bcb11d3
JS
676 CalcBoundingBox(x1, y1);
677 CalcBoundingBox(x2, y2);
2bda0e17
KB
678}
679
f6bcfd97
BP
680// Draws an arc of a circle, centred on (xc, yc), with starting point (x1, y1)
681// and ending at (x2, y2)
888dde65 682void wxMSWDCImpl::DoDrawArc(wxCoord x1, wxCoord y1,
f6bcfd97
BP
683 wxCoord x2, wxCoord y2,
684 wxCoord xc, wxCoord yc)
2bda0e17 685{
068f78bc
VZ
686 double dx = xc - x1;
687 double dy = yc - y1;
688 wxCoord r = (wxCoord)sqrt(dx*dx + dy*dy);
689
690
4676948b 691#ifdef __WXWINCE__
4f10962b 692 // Slower emulation since WinCE doesn't support Pie and Arc
4f10962b 693 double sa = acos((x1-xc)/r)/M_PI*180; // between 0 and 180
068f78bc
VZ
694 if( y1>yc )
695 sa = -sa; // below center
4f10962b
JS
696 double ea = atan2(yc-y2, x2-xc)/M_PI*180;
697 DoDrawEllipticArcRot( xc-r, yc-r, 2*r, 2*r, sa, ea );
4676948b
JS
698#else
699
82a306b7 700 WXMICROWIN_CHECK_HDC
d275c7eb 701
76f91e77 702 wxBrushAttrsSetter cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
2d8a5cb1 703
f6bcfd97
BP
704 // treat the special case of full circle separately
705 if ( x1 == x2 && y1 == y2 )
7bcb11d3 706 {
888dde65 707 GetOwner()->DrawEllipse(xc - r, yc - r, 2*r, 2*r);
a23fd0e1 708 return;
7bcb11d3 709 }
a23fd0e1 710
72cdf4c9
VZ
711 wxCoord xx1 = XLOG2DEV(x1);
712 wxCoord yy1 = YLOG2DEV(y1);
713 wxCoord xx2 = XLOG2DEV(x2);
714 wxCoord yy2 = YLOG2DEV(y2);
715 wxCoord xxc = XLOG2DEV(xc);
716 wxCoord yyc = YLOG2DEV(yc);
068f78bc
VZ
717 dx = xxc - xx1;
718 dy = yyc - yy1;
719 wxCoord ray = (wxCoord)sqrt(dx*dx + dy*dy);
a23fd0e1 720
72cdf4c9
VZ
721 wxCoord xxx1 = (wxCoord) (xxc-ray);
722 wxCoord yyy1 = (wxCoord) (yyc-ray);
723 wxCoord xxx2 = (wxCoord) (xxc+ray);
724 wxCoord yyy2 = (wxCoord) (yyc+ray);
f6bcfd97 725
e6777e65 726 if ( m_brush.IsNonTransparent() )
7bcb11d3
JS
727 {
728 // Have to add 1 to bottom-right corner of rectangle
729 // to make semi-circles look right (crooked line otherwise).
730 // Unfortunately this is not a reliable method, depends
731 // on the size of shape.
732 // TODO: figure out why this happens!
f6bcfd97 733 Pie(GetHdc(),xxx1,yyy1,xxx2+1,yyy2+1, xx1,yy1,xx2,yy2);
7bcb11d3
JS
734 }
735 else
2d8a5cb1 736 {
f6bcfd97 737 Arc(GetHdc(),xxx1,yyy1,xxx2,yyy2, xx1,yy1,xx2,yy2);
2d8a5cb1 738 }
f6bcfd97
BP
739
740 CalcBoundingBox(xc - r, yc - r);
741 CalcBoundingBox(xc + r, yc + r);
4676948b 742#endif
2bda0e17
KB
743}
744
888dde65 745void wxMSWDCImpl::DoDrawCheckMark(wxCoord x1, wxCoord y1,
cd9da200
VZ
746 wxCoord width, wxCoord height)
747{
b76d9e76
VZ
748 // cases when we don't have DrawFrameControl()
749#if defined(__SYMANTEC__) || defined(__WXMICROWIN__)
750 return wxDCBase::DoDrawCheckMark(x1, y1, width, height);
751#else // normal case
cd9da200
VZ
752 wxCoord x2 = x1 + width,
753 y2 = y1 + height;
754
cd9da200
VZ
755 RECT rect;
756 rect.left = x1;
757 rect.top = y1;
758 rect.right = x2;
759 rect.bottom = y2;
760
4676948b 761#ifdef __WXWINCE__
c73e37e2 762 DrawFrameControl(GetHdc(), &rect, DFC_BUTTON, DFCS_BUTTONCHECK | DFCS_CHECKED);
4676948b 763#else
cd9da200 764 DrawFrameControl(GetHdc(), &rect, DFC_MENU, DFCS_MENUCHECK);
4676948b 765#endif
cd9da200
VZ
766
767 CalcBoundingBox(x1, y1);
768 CalcBoundingBox(x2, y2);
b76d9e76 769#endif // Microwin/Normal
cd9da200
VZ
770}
771
888dde65 772void wxMSWDCImpl::DoDrawPoint(wxCoord x, wxCoord y)
2bda0e17 773{
82a306b7 774 WXMICROWIN_CHECK_HDC
d275c7eb 775
7bcb11d3 776 COLORREF color = 0x00ffffff;
87d3576f 777 if (m_pen.IsOk())
7bcb11d3 778 {
a23fd0e1 779 color = m_pen.GetColour().GetPixel();
7bcb11d3 780 }
a23fd0e1
VZ
781
782 SetPixel(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), color);
783
7bcb11d3 784 CalcBoundingBox(x, y);
2bda0e17
KB
785}
786
888dde65 787void wxMSWDCImpl::DoDrawPolygon(int n,
0c0d1521
WS
788 wxPoint points[],
789 wxCoord xoffset,
790 wxCoord yoffset,
89efaf2b 791 wxPolygonFillMode WXUNUSED_IN_WINCE(fillStyle))
2bda0e17 792{
82a306b7 793 WXMICROWIN_CHECK_HDC
d275c7eb 794
76f91e77 795 wxBrushAttrsSetter cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
de2d2cdc 796
7bcb11d3
JS
797 // Do things less efficiently if we have offsets
798 if (xoffset != 0 || yoffset != 0)
6a6c0a8b 799 {
7bcb11d3
JS
800 POINT *cpoints = new POINT[n];
801 int i;
802 for (i = 0; i < n; i++)
803 {
804 cpoints[i].x = (int)(points[i].x + xoffset);
805 cpoints[i].y = (int)(points[i].y + yoffset);
a23fd0e1 806
7bcb11d3
JS
807 CalcBoundingBox(cpoints[i].x, cpoints[i].y);
808 }
4676948b 809#ifndef __WXWINCE__
a23fd0e1 810 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
4676948b 811#endif
a23fd0e1 812 (void)Polygon(GetHdc(), cpoints, n);
4676948b 813#ifndef __WXWINCE__
a23fd0e1 814 SetPolyFillMode(GetHdc(),prev);
4676948b 815#endif
7bcb11d3
JS
816 delete[] cpoints;
817 }
818 else
819 {
820 int i;
821 for (i = 0; i < n; i++)
822 CalcBoundingBox(points[i].x, points[i].y);
a23fd0e1 823
4676948b 824#ifndef __WXWINCE__
a23fd0e1 825 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
4676948b 826#endif
a23fd0e1 827 (void)Polygon(GetHdc(), (POINT*) points, n);
4676948b 828#ifndef __WXWINCE__
a23fd0e1 829 SetPolyFillMode(GetHdc(),prev);
4676948b 830#endif
6a6c0a8b 831 }
2bda0e17
KB
832}
833
6e76b35d 834void
888dde65 835wxMSWDCImpl::DoDrawPolyPolygon(int n,
793db755 836 int count[],
6e76b35d
VZ
837 wxPoint points[],
838 wxCoord xoffset,
839 wxCoord yoffset,
89efaf2b 840 wxPolygonFillMode fillStyle)
6e76b35d 841{
d61c1a6f 842#ifdef __WXWINCE__
2261baf7 843 wxDCImpl::DoDrawPolyPolygon(n, count, points, xoffset, yoffset, fillStyle);
35bbb0c6 844#else
82a306b7 845 WXMICROWIN_CHECK_HDC
6e76b35d 846
76f91e77 847 wxBrushAttrsSetter cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
6e76b35d
VZ
848 int i, cnt;
849 for (i = cnt = 0; i < n; i++)
793db755 850 cnt += count[i];
6e76b35d
VZ
851
852 // Do things less efficiently if we have offsets
853 if (xoffset != 0 || yoffset != 0)
854 {
855 POINT *cpoints = new POINT[cnt];
856 for (i = 0; i < cnt; i++)
857 {
858 cpoints[i].x = (int)(points[i].x + xoffset);
859 cpoints[i].y = (int)(points[i].y + yoffset);
860
861 CalcBoundingBox(cpoints[i].x, cpoints[i].y);
862 }
d61c1a6f 863#ifndef __WXWINCE__
6e76b35d 864 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
35bbb0c6 865#endif
793db755 866 (void)PolyPolygon(GetHdc(), cpoints, count, n);
d61c1a6f 867#ifndef __WXWINCE__
6e76b35d 868 SetPolyFillMode(GetHdc(),prev);
35bbb0c6 869#endif
6e76b35d
VZ
870 delete[] cpoints;
871 }
872 else
873 {
874 for (i = 0; i < cnt; i++)
875 CalcBoundingBox(points[i].x, points[i].y);
876
d61c1a6f 877#ifndef __WXWINCE__
6e76b35d 878 int prev = SetPolyFillMode(GetHdc(),fillStyle==wxODDEVEN_RULE?ALTERNATE:WINDING);
35bbb0c6 879#endif
793db755 880 (void)PolyPolygon(GetHdc(), (POINT*) points, count, n);
d61c1a6f 881#ifndef __WXWINCE__
6e76b35d 882 SetPolyFillMode(GetHdc(),prev);
35bbb0c6 883#endif
6e76b35d 884 }
d61c1a6f
JS
885#endif
886 // __WXWINCE__
6e76b35d
VZ
887}
888
888dde65 889void wxMSWDCImpl::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset)
2bda0e17 890{
82a306b7 891 WXMICROWIN_CHECK_HDC
d275c7eb 892
7bcb11d3
JS
893 // Do things less efficiently if we have offsets
894 if (xoffset != 0 || yoffset != 0)
6a6c0a8b 895 {
7bcb11d3
JS
896 POINT *cpoints = new POINT[n];
897 int i;
898 for (i = 0; i < n; i++)
899 {
900 cpoints[i].x = (int)(points[i].x + xoffset);
901 cpoints[i].y = (int)(points[i].y + yoffset);
a23fd0e1 902
7bcb11d3
JS
903 CalcBoundingBox(cpoints[i].x, cpoints[i].y);
904 }
a23fd0e1 905 (void)Polyline(GetHdc(), cpoints, n);
7bcb11d3
JS
906 delete[] cpoints;
907 }
908 else
909 {
910 int i;
911 for (i = 0; i < n; i++)
912 CalcBoundingBox(points[i].x, points[i].y);
a23fd0e1
VZ
913
914 (void)Polyline(GetHdc(), (POINT*) points, n);
6a6c0a8b 915 }
2bda0e17
KB
916}
917
888dde65 918void wxMSWDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
2bda0e17 919{
82a306b7 920 WXMICROWIN_CHECK_HDC
d275c7eb 921
76f91e77 922 wxBrushAttrsSetter cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
de2d2cdc 923
72cdf4c9
VZ
924 wxCoord x2 = x + width;
925 wxCoord y2 = y + height;
a23fd0e1 926
1ee280b7
VZ
927 wxCoord x2dev = XLOG2DEV(x2),
928 y2dev = YLOG2DEV(y2);
5456b916 929
1ee280b7
VZ
930 // Windows (but not Windows CE) draws the filled rectangles without outline
931 // (i.e. drawn with a transparent pen) one pixel smaller in both directions
932 // and we want them to have the same size regardless of which pen is used
750d64e6 933#ifndef __WXWINCE__
e6777e65 934 if ( m_pen.IsTransparent() )
1ee280b7
VZ
935 {
936 x2dev++;
937 y2dev++;
7bcb11d3 938 }
1ee280b7 939#endif // !__WXWINCE__
a23fd0e1 940
1ee280b7 941 (void)Rectangle(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), x2dev, y2dev);
a23fd0e1 942
7bcb11d3
JS
943 CalcBoundingBox(x, y);
944 CalcBoundingBox(x2, y2);
2bda0e17
KB
945}
946
888dde65 947void wxMSWDCImpl::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius)
2bda0e17 948{
82a306b7 949 WXMICROWIN_CHECK_HDC
d275c7eb 950
76f91e77 951 wxBrushAttrsSetter cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
2d8a5cb1 952
7bcb11d3
JS
953 // Now, a negative radius value is interpreted to mean
954 // 'the proportion of the smallest X or Y dimension'
a23fd0e1 955
7bcb11d3
JS
956 if (radius < 0.0)
957 {
999836aa 958 double smallest = (width < height) ? width : height;
7bcb11d3
JS
959 radius = (- radius * smallest);
960 }
a23fd0e1 961
72cdf4c9
VZ
962 wxCoord x2 = (x+width);
963 wxCoord y2 = (y+height);
a23fd0e1 964
dfde8cd3
GRG
965 // Windows draws the filled rectangles without outline (i.e. drawn with a
966 // transparent pen) one pixel smaller in both directions and we want them
967 // to have the same size regardless of which pen is used - adjust
e6777e65 968 if ( m_pen.IsTransparent() )
dfde8cd3
GRG
969 {
970 x2++;
971 y2++;
972 }
973
a23fd0e1 974 (void)RoundRect(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2),
25889d3c 975 YLOG2DEV(y2), (int) (2*XLOG2DEV(radius)), (int)( 2*YLOG2DEV(radius)));
a23fd0e1 976
7bcb11d3
JS
977 CalcBoundingBox(x, y);
978 CalcBoundingBox(x2, y2);
2bda0e17
KB
979}
980
888dde65 981void wxMSWDCImpl::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height)
2bda0e17 982{
82a306b7 983 WXMICROWIN_CHECK_HDC
d275c7eb 984
76f91e77 985 wxBrushAttrsSetter cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
2d8a5cb1 986
45c6df33
VZ
987 // +1 below makes the ellipse more similar to other platforms.
988 // In particular, DoDrawEllipse(x,y,1,1) should draw one point.
989 wxCoord x2 = x + width + 1;
990 wxCoord y2 = y + height + 1;
991
992 // Problem: Windows GDI Ellipse() with x2-x == y2-y == 3 and transparent
993 // pen doesn't draw anything. Should we provide a workaround?
a23fd0e1 994
45c6df33 995 ::Ellipse(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2), YLOG2DEV(y2));
a23fd0e1 996
7bcb11d3
JS
997 CalcBoundingBox(x, y);
998 CalcBoundingBox(x2, y2);
2bda0e17
KB
999}
1000
3e9db38e 1001#if wxUSE_SPLINES && !defined(__WXWINCE__)
888dde65 1002void wxMSWDCImpl::DoDrawSpline(const wxPointList *points)
ad0ac642 1003{
ad0ac642
WS
1004 // quadratic b-spline to cubic bezier spline conversion
1005 //
1006 // quadratic spline with control points P0,P1,P2
1007 // P(s) = P0*(1-s)^2 + P1*2*(1-s)*s + P2*s^2
1008 //
1009 // bezier spline with control points B0,B1,B2,B3
1010 // B(s) = B0*(1-s)^3 + B1*3*(1-s)^2*s + B2*3*(1-s)*s^2 + B3*s^3
1011 //
1012 // control points of bezier spline calculated from b-spline
1013 // B0 = P0
1014 // B1 = (2*P1 + P0)/3
1015 // B2 = (2*P1 + P2)/3
1016 // B3 = P2
1017
1018 WXMICROWIN_CHECK_HDC
1019
1020 wxASSERT_MSG( points, wxT("NULL pointer to spline points?") );
1021
1022 const size_t n_points = points->GetCount();
60e19371 1023 wxASSERT_MSG( n_points > 2 , wxT("incomplete list of spline points?") );
ad0ac642
WS
1024
1025 const size_t n_bezier_points = n_points * 3 + 1;
1026 POINT *lppt = (POINT *)malloc(n_bezier_points*sizeof(POINT));
1027 size_t bezier_pos = 0;
1028 wxCoord x1, y1, x2, y2, cx1, cy1, cx4, cy4;
1029
b0d7707b
RR
1030 wxPointList::compatibility_iterator node = points->GetFirst();
1031 wxPoint *p = node->GetData();
ad0ac642
WS
1032 lppt[ bezier_pos ].x = x1 = p->x;
1033 lppt[ bezier_pos ].y = y1 = p->y;
1034 bezier_pos++;
1035 lppt[ bezier_pos ] = lppt[ bezier_pos-1 ];
1036 bezier_pos++;
1037
1038 node = node->GetNext();
b0d7707b 1039 p = node->GetData();
ad0ac642
WS
1040
1041 x2 = p->x;
1042 y2 = p->y;
1043 cx1 = ( x1 + x2 ) / 2;
1044 cy1 = ( y1 + y2 ) / 2;
1045 lppt[ bezier_pos ].x = XLOG2DEV(cx1);
1046 lppt[ bezier_pos ].y = YLOG2DEV(cy1);
1047 bezier_pos++;
1048 lppt[ bezier_pos ] = lppt[ bezier_pos-1 ];
1049 bezier_pos++;
1050
1051#if !wxUSE_STL
1052 while ((node = node->GetNext()) != NULL)
1053#else
1054 while ((node = node->GetNext()))
1055#endif // !wxUSE_STL
1056 {
1057 p = (wxPoint *)node->GetData();
1058 x1 = x2;
1059 y1 = y2;
1060 x2 = p->x;
1061 y2 = p->y;
1062 cx4 = (x1 + x2) / 2;
1063 cy4 = (y1 + y2) / 2;
1064 // B0 is B3 of previous segment
1065 // B1:
1066 lppt[ bezier_pos ].x = XLOG2DEV((x1*2+cx1)/3);
1067 lppt[ bezier_pos ].y = YLOG2DEV((y1*2+cy1)/3);
1068 bezier_pos++;
1069 // B2:
1070 lppt[ bezier_pos ].x = XLOG2DEV((x1*2+cx4)/3);
1071 lppt[ bezier_pos ].y = YLOG2DEV((y1*2+cy4)/3);
1072 bezier_pos++;
1073 // B3:
1074 lppt[ bezier_pos ].x = XLOG2DEV(cx4);
1075 lppt[ bezier_pos ].y = YLOG2DEV(cy4);
1076 bezier_pos++;
1077 cx1 = cx4;
1078 cy1 = cy4;
1079 }
1080
1081 lppt[ bezier_pos ] = lppt[ bezier_pos-1 ];
1082 bezier_pos++;
1083 lppt[ bezier_pos ].x = XLOG2DEV(x2);
1084 lppt[ bezier_pos ].y = YLOG2DEV(y2);
1085 bezier_pos++;
1086 lppt[ bezier_pos ] = lppt[ bezier_pos-1 ];
1087 bezier_pos++;
1088
1089 ::PolyBezier( GetHdc(), lppt, bezier_pos );
1090
1091 free(lppt);
ad0ac642 1092}
3e9db38e 1093#endif // wxUSE_SPLINES
ad0ac642 1094
6f65e337 1095// Chris Breeze 20/5/98: first implementation of DrawEllipticArc on Windows
888dde65 1096void wxMSWDCImpl::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea)
6f65e337 1097{
4676948b 1098#ifdef __WXWINCE__
4f10962b 1099 DoDrawEllipticArcRot( x, y, w, h, sa, ea );
4676948b
JS
1100#else
1101
82a306b7 1102 WXMICROWIN_CHECK_HDC
d275c7eb 1103
76f91e77 1104 wxBrushAttrsSetter cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling
2d8a5cb1 1105
f6bcfd97
BP
1106 wxCoord x2 = x + w;
1107 wxCoord y2 = y + h;
a23fd0e1 1108
7bcb11d3
JS
1109 int rx1 = XLOG2DEV(x+w/2);
1110 int ry1 = YLOG2DEV(y+h/2);
1111 int rx2 = rx1;
1112 int ry2 = ry1;
4314ec48
VZ
1113
1114 sa = DegToRad(sa);
1115 ea = DegToRad(ea);
1116
1117 rx1 += (int)(100.0 * abs(w) * cos(sa));
1118 ry1 -= (int)(100.0 * abs(h) * m_signY * sin(sa));
1119 rx2 += (int)(100.0 * abs(w) * cos(ea));
1120 ry2 -= (int)(100.0 * abs(h) * m_signY * sin(ea));
a23fd0e1 1121
e6d18909
RD
1122 // Swap start and end positions if the end angle is less than the start angle.
1123 if (ea < sa) {
ef94049f
PC
1124 int temp;
1125 temp = rx2;
1126 rx2 = rx1;
1127 rx1 = temp;
1128 temp = ry2;
1129 ry2 = ry1;
1130 ry1 = temp;
e6d18909
RD
1131 }
1132
7bcb11d3
JS
1133 // draw pie with NULL_PEN first and then outline otherwise a line is
1134 // drawn from the start and end points to the centre
f6bcfd97 1135 HPEN hpenOld = (HPEN) ::SelectObject(GetHdc(), (HPEN) ::GetStockObject(NULL_PEN));
7bcb11d3
JS
1136 if (m_signY > 0)
1137 {
a23fd0e1 1138 (void)Pie(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2)+1, YLOG2DEV(y2)+1,
f6bcfd97 1139 rx1, ry1, rx2, ry2);
7bcb11d3
JS
1140 }
1141 else
1142 {
a23fd0e1 1143 (void)Pie(GetHdc(), XLOG2DEV(x), YLOG2DEV(y)-1, XLOG2DEV(x2)+1, YLOG2DEV(y2),
f6bcfd97 1144 rx1, ry1-1, rx2, ry2-1);
7bcb11d3 1145 }
f6bcfd97
BP
1146
1147 ::SelectObject(GetHdc(), hpenOld);
1148
a23fd0e1 1149 (void)Arc(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), XLOG2DEV(x2), YLOG2DEV(y2),
f6bcfd97 1150 rx1, ry1, rx2, ry2);
a23fd0e1 1151
7bcb11d3
JS
1152 CalcBoundingBox(x, y);
1153 CalcBoundingBox(x2, y2);
4676948b 1154#endif
6f65e337
JS
1155}
1156
888dde65 1157void wxMSWDCImpl::DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y)
2bda0e17 1158{
82a306b7 1159 WXMICROWIN_CHECK_HDC
d275c7eb 1160
87d3576f 1161 wxCHECK_RET( icon.IsOk(), wxT("invalid icon in DrawIcon") );
4b7f2165 1162
f6bcfd97
BP
1163#ifdef __WIN32__
1164 ::DrawIconEx(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), GetHiconOf(icon), icon.GetWidth(), icon.GetHeight(), 0, NULL, DI_NORMAL);
1165#else
4b7f2165 1166 ::DrawIcon(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), GetHiconOf(icon));
f6bcfd97 1167#endif
a23fd0e1 1168
7bcb11d3 1169 CalcBoundingBox(x, y);
4b7f2165 1170 CalcBoundingBox(x + icon.GetWidth(), y + icon.GetHeight());
2bda0e17
KB
1171}
1172
888dde65 1173void wxMSWDCImpl::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask )
f5419957 1174{
82a306b7 1175 WXMICROWIN_CHECK_HDC
d275c7eb 1176
9a83f860 1177 wxCHECK_RET( bmp.IsOk(), wxT("invalid bitmap in wxMSWDCImpl::DrawBitmap") );
4b7f2165
VZ
1178
1179 int width = bmp.GetWidth(),
1180 height = bmp.GetHeight();
1181
91b4c08d 1182 HBITMAP hbmpMask = 0;
e22c13fe
VZ
1183
1184#if wxUSE_PALETTE
19193a2c 1185 HPALETTE oldPal = 0;
e22c13fe 1186#endif // wxUSE_PALETTE
91b4c08d 1187
acf8e3d2
VZ
1188 if ( bmp.HasAlpha() )
1189 {
275a63e3
VZ
1190 MemoryHDC hdcMem;
1191 SelectInHDC select(hdcMem, GetHbitmapOf(bmp));
878711c0 1192
e3b81044 1193 if ( AlphaBlt(GetHdc(), x, y, width, height, 0, 0, width, height, hdcMem, bmp) )
275a63e3 1194 return;
acf8e3d2 1195 }
acf8e3d2 1196
4f64fde7 1197 SET_STRETCH_BLT_MODE(GetHdc());
eb72e9aa 1198
91b4c08d
VZ
1199 if ( useMask )
1200 {
1201 wxMask *mask = bmp.GetMask();
1202 if ( mask )
1203 hbmpMask = (HBITMAP)mask->GetMaskBitmap();
1204
1205 if ( !hbmpMask )
1206 {
1207 // don't give assert here because this would break existing
1208 // programs - just silently ignore useMask parameter
beb966c5 1209 useMask = false;
91b4c08d
VZ
1210 }
1211 }
91b4c08d
VZ
1212 if ( useMask )
1213 {
1214#ifdef __WIN32__
4aff28fc
VZ
1215 // use MaskBlt() with ROP which doesn't do anything to dst in the mask
1216 // points
7b8d24ad
VZ
1217 bool ok = false;
1218
1219#if wxUSE_SYSTEM_OPTIONS
d3211838 1220 // On some systems, MaskBlt succeeds yet is much much slower
77ffb593 1221 // than the wxWidgets fall-back implementation. So we need
d3211838 1222 // to be able to switch this on and off at runtime.
7b8d24ad
VZ
1223 //
1224 // NB: don't query the value of the option every time but do it only
1225 // once as otherwise it can have real (and bad) performance
1226 // implications (see #11172)
1227 static bool
1228 s_maskBltAllowed = wxSystemOptions::GetOptionInt("no-maskblt") == 0;
1229 if ( s_maskBltAllowed )
1230#endif // wxUSE_SYSTEM_OPTIONS
d3211838 1231 {
19193a2c 1232 HDC cdc = GetHdc();
d3211838 1233 HDC hdcMem = ::CreateCompatibleDC(GetHdc());
a230101e 1234 HGDIOBJ hOldBitmap = ::SelectObject(hdcMem, GetHbitmapOf(bmp));
e22c13fe
VZ
1235#if wxUSE_PALETTE
1236 wxPalette *pal = bmp.GetPalette();
1237 if ( pal && ::GetDeviceCaps(cdc,BITSPIXEL) <= 8 )
1238 {
b95edd47 1239 oldPal = ::SelectPalette(hdcMem, GetHpaletteOf(*pal), FALSE);
19193a2c 1240 ::RealizePalette(hdcMem);
e22c13fe
VZ
1241 }
1242#endif // wxUSE_PALETTE
1243
19193a2c 1244 ok = ::MaskBlt(cdc, x, y, width, height,
91b4c08d
VZ
1245 hdcMem, 0, 0,
1246 hbmpMask, 0, 0,
4aff28fc 1247 MAKEROP4(SRCCOPY, DSTCOPY)) != 0;
e22c13fe
VZ
1248
1249#if wxUSE_PALETTE
19193a2c
KB
1250 if (oldPal)
1251 ::SelectPalette(hdcMem, oldPal, FALSE);
e22c13fe
VZ
1252#endif // wxUSE_PALETTE
1253
a230101e 1254 ::SelectObject(hdcMem, hOldBitmap);
d3211838
JS
1255 ::DeleteDC(hdcMem);
1256 }
91b4c08d
VZ
1257
1258 if ( !ok )
1259#endif // Win32
1260 {
888dde65 1261 // Rather than reproduce wxMSWDCImpl::Blit, let's do it at the wxWin API
4aff28fc 1262 // level
91b4c08d 1263 wxMemoryDC memDC;
fea35690
VZ
1264
1265 memDC.SelectObjectAsSource(bmp);
91b4c08d 1266
888dde65 1267 GetOwner()->Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask);
91b4c08d
VZ
1268
1269 memDC.SelectObject(wxNullBitmap);
1270 }
1271 }
1272 else // no mask, just use BitBlt()
f5419957 1273 {
4b7f2165
VZ
1274 HDC cdc = GetHdc();
1275 HDC memdc = ::CreateCompatibleDC( cdc );
1276 HBITMAP hbitmap = (HBITMAP) bmp.GetHBITMAP( );
1277
1278 wxASSERT_MSG( hbitmap, wxT("bitmap is ok but HBITMAP is NULL?") );
1279
76f91e77 1280 wxTextColoursChanger textCol(GetHdc(), *this);
392f4b1b 1281
e22c13fe 1282#if wxUSE_PALETTE
62e1ba75
JS
1283 wxPalette *pal = bmp.GetPalette();
1284 if ( pal && ::GetDeviceCaps(cdc,BITSPIXEL) <= 8 )
1285 {
b95edd47 1286 oldPal = ::SelectPalette(memdc, GetHpaletteOf(*pal), FALSE);
62e1ba75
JS
1287 ::RealizePalette(memdc);
1288 }
e22c13fe
VZ
1289#endif // wxUSE_PALETTE
1290
a230101e 1291 HGDIOBJ hOldBitmap = ::SelectObject( memdc, hbitmap );
4b7f2165 1292 ::BitBlt( cdc, x, y, width, height, memdc, 0, 0, SRCCOPY);
e22c13fe
VZ
1293
1294#if wxUSE_PALETTE
19193a2c
KB
1295 if (oldPal)
1296 ::SelectPalette(memdc, oldPal, FALSE);
e22c13fe
VZ
1297#endif // wxUSE_PALETTE
1298
62e1ba75 1299 ::SelectObject( memdc, hOldBitmap );
4b7f2165 1300 ::DeleteDC( memdc );
f5419957 1301 }
f5419957
JS
1302}
1303
888dde65 1304void wxMSWDCImpl::DoDrawText(const wxString& text, wxCoord x, wxCoord y)
a23fd0e1 1305{
a5bb4514
VZ
1306 // For compatibility with other ports (notably wxGTK) and because it's
1307 // genuinely useful, we allow passing multiline strings to DrawText().
1308 // However there is no native MSW function to draw them directly so we
1309 // instead reuse the generic DrawLabel() method to render them. Of course,
1310 // DrawLabel() itself will call back to us but with single line strings
1311 // only so there won't be any infinite recursion here.
1312 if ( text.find('\n') != wxString::npos )
1313 {
1314 GetOwner()->DrawLabel(text, wxRect(x, y, 0, 0));
1315 return;
1316 }
1317
82a306b7 1318 WXMICROWIN_CHECK_HDC
d275c7eb 1319
4314ec48
VZ
1320 DrawAnyText(text, x, y);
1321
1322 // update the bounding box
1323 CalcBoundingBox(x, y);
1324
1325 wxCoord w, h;
888dde65 1326 GetOwner()->GetTextExtent(text, &w, &h);
4314ec48
VZ
1327 CalcBoundingBox(x + w, y + h);
1328}
1329
888dde65 1330void wxMSWDCImpl::DrawAnyText(const wxString& text, wxCoord x, wxCoord y)
4314ec48 1331{
82a306b7 1332 WXMICROWIN_CHECK_HDC
d275c7eb 1333
4314ec48 1334 // prepare for drawing the text
76f91e77 1335 wxTextColoursChanger textCol(GetHdc(), *this);
a23fd0e1 1336
76f91e77 1337 wxBkModeChanger bkMode(GetHdc(), m_backgroundMode);
a23fd0e1 1338
4676948b
JS
1339 if ( ::ExtTextOut(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), 0, NULL,
1340 text.c_str(), text.length(), NULL) == 0 )
1341 {
1342 wxLogLastError(wxT("TextOut"));
1343 }
a23fd0e1
VZ
1344}
1345
888dde65 1346void wxMSWDCImpl::DoDrawRotatedText(const wxString& text,
95724b1a
VZ
1347 wxCoord x, wxCoord y,
1348 double angle)
1349{
82a306b7 1350 WXMICROWIN_CHECK_HDC
d275c7eb 1351
696e1ea0
VZ
1352 // we test that we have some font because otherwise we should still use the
1353 // "else" part below to avoid that DrawRotatedText(angle = 180) and
1354 // DrawRotatedText(angle = 0) use different fonts (we can't use the default
1355 // font for drawing rotated fonts unfortunately)
87d3576f 1356 if ( (angle == 0.0) && m_font.IsOk() )
4314ec48
VZ
1357 {
1358 DoDrawText(text, x, y);
1359 }
04ef50df 1360#ifndef __WXMICROWIN__
4314ec48
VZ
1361 else
1362 {
4770df95
VZ
1363 // NB: don't take DEFAULT_GUI_FONT (a.k.a. wxSYS_DEFAULT_GUI_FONT)
1364 // because it's not TrueType and so can't have non zero
1365 // orientation/escapement under Win9x
87d3576f 1366 wxFont font = m_font.IsOk() ? m_font : *wxSWISS_FONT;
696e1ea0 1367 HFONT hfont = (HFONT)font.GetResourceHandle();
4314ec48 1368 LOGFONT lf;
696e1ea0
VZ
1369 if ( ::GetObject(hfont, sizeof(lf), &lf) == 0 )
1370 {
f6bcfd97 1371 wxLogLastError(wxT("GetObject(hfont)"));
696e1ea0 1372 }
4314ec48
VZ
1373
1374 // GDI wants the angle in tenth of degree
1375 long angle10 = (long)(angle * 10);
1376 lf.lfEscapement = angle10;
1377 lf. lfOrientation = angle10;
1378
696e1ea0 1379 hfont = ::CreateFontIndirect(&lf);
4314ec48
VZ
1380 if ( !hfont )
1381 {
f6bcfd97 1382 wxLogLastError(wxT("CreateFont"));
4314ec48
VZ
1383 }
1384 else
1385 {
696e1ea0 1386 HFONT hfontOld = (HFONT)::SelectObject(GetHdc(), hfont);
4314ec48
VZ
1387
1388 DrawAnyText(text, x, y);
1389
1390 (void)::SelectObject(GetHdc(), hfontOld);
a3a1ceae 1391 (void)::DeleteObject(hfont);
4314ec48
VZ
1392 }
1393
1394 // call the bounding box by adding all four vertices of the rectangle
1395 // containing the text to it (simpler and probably not slower than
1396 // determining which of them is really topmost/leftmost/...)
1397 wxCoord w, h;
888dde65 1398 GetOwner()->GetTextExtent(text, &w, &h);
4314ec48
VZ
1399
1400 double rad = DegToRad(angle);
1401
1402 // "upper left" and "upper right"
1403 CalcBoundingBox(x, y);
a98ca1ae 1404 CalcBoundingBox(x + wxCoord(w*cos(rad)), y - wxCoord(w*sin(rad)));
4314ec48
VZ
1405
1406 // "bottom left" and "bottom right"
1407 x += (wxCoord)(h*sin(rad));
1408 y += (wxCoord)(h*cos(rad));
1409 CalcBoundingBox(x, y);
a98ca1ae 1410 CalcBoundingBox(x + wxCoord(w*cos(rad)), y - wxCoord(w*sin(rad)));
4314ec48 1411 }
04ef50df 1412#endif
95724b1a
VZ
1413}
1414
a23fd0e1
VZ
1415// ---------------------------------------------------------------------------
1416// set GDI objects
1417// ---------------------------------------------------------------------------
1418
d275c7eb
VZ
1419#if wxUSE_PALETTE
1420
888dde65 1421void wxMSWDCImpl::DoSelectPalette(bool realize)
a23fd0e1 1422{
82a306b7 1423 WXMICROWIN_CHECK_HDC
d275c7eb 1424
a23fd0e1
VZ
1425 // Set the old object temporarily, in case the assignment deletes an object
1426 // that's not yet selected out.
1427 if (m_oldPalette)
1428 {
19193a2c 1429 ::SelectPalette(GetHdc(), (HPALETTE) m_oldPalette, FALSE);
a23fd0e1
VZ
1430 m_oldPalette = 0;
1431 }
1432
87d3576f 1433 if ( m_palette.IsOk() )
a23fd0e1 1434 {
b95edd47
VZ
1435 HPALETTE oldPal = ::SelectPalette(GetHdc(),
1436 GetHpaletteOf(m_palette),
beb966c5 1437 false);
a23fd0e1
VZ
1438 if (!m_oldPalette)
1439 m_oldPalette = (WXHPALETTE) oldPal;
1440
574c939e
KB
1441 if (realize)
1442 ::RealizePalette(GetHdc());
a23fd0e1 1443 }
574c939e
KB
1444}
1445
888dde65 1446void wxMSWDCImpl::SetPalette(const wxPalette& palette)
574c939e 1447{
87d3576f 1448 if ( palette.IsOk() )
b95edd47 1449 {
574c939e 1450 m_palette = palette;
beb966c5 1451 DoSelectPalette(true);
b95edd47 1452 }
a23fd0e1
VZ
1453}
1454
888dde65 1455void wxMSWDCImpl::InitializePalette()
574c939e 1456{
b95edd47
VZ
1457 if ( wxDisplayDepth() <= 8 )
1458 {
574c939e
KB
1459 // look for any window or parent that has a custom palette. If any has
1460 // one then we need to use it in drawing operations
888dde65 1461 wxWindow *win = m_window->GetAncestorWithCustomPalette();
b95edd47
VZ
1462
1463 m_hasCustomPalette = win && win->HasCustomPalette();
1464 if ( m_hasCustomPalette )
1465 {
574c939e 1466 m_palette = win->GetPalette();
b95edd47 1467
574c939e
KB
1468 // turn on MSW translation for this palette
1469 DoSelectPalette();
574c939e 1470 }
b95edd47 1471 }
574c939e 1472}
b95edd47 1473
d275c7eb
VZ
1474#endif // wxUSE_PALETTE
1475
9f7948af
VZ
1476// SetFont/Pen/Brush() really ask to be implemented as a single template
1477// function... but doing it is not worth breaking OpenWatcom build <sigh>
1478
888dde65 1479void wxMSWDCImpl::SetFont(const wxFont& font)
2bda0e17 1480{
82a306b7 1481 WXMICROWIN_CHECK_HDC
d275c7eb 1482
9f7948af
VZ
1483 if ( font == m_font )
1484 return;
a23fd0e1 1485
87d3576f 1486 if ( font.IsOk() )
7bcb11d3 1487 {
9f7948af
VZ
1488 HGDIOBJ hfont = ::SelectObject(GetHdc(), GetHfontOf(font));
1489 if ( hfont == HGDI_ERROR )
1490 {
9a83f860 1491 wxLogLastError(wxT("SelectObject(font)"));
9f7948af
VZ
1492 }
1493 else // selected ok
1494 {
1495 if ( !m_oldFont )
658ff7f1 1496 m_oldFont = (WXHFONT)hfont;
a23fd0e1 1497
9f7948af
VZ
1498 m_font = font;
1499 }
1500 }
1501 else // invalid font, reset the current font
7bcb11d3 1502 {
9f7948af 1503 if ( m_oldFont )
7bcb11d3 1504 {
9f7948af
VZ
1505 if ( ::SelectObject(GetHdc(), (HPEN) m_oldFont) == HGDI_ERROR )
1506 {
9a83f860 1507 wxLogLastError(wxT("SelectObject(old font)"));
9f7948af
VZ
1508 }
1509
beb966c5 1510 m_oldFont = 0;
7bcb11d3 1511 }
9f7948af
VZ
1512
1513 m_font = wxNullFont;
34da0970 1514 }
2bda0e17
KB
1515}
1516
888dde65 1517void wxMSWDCImpl::SetPen(const wxPen& pen)
2bda0e17 1518{
82a306b7 1519 WXMICROWIN_CHECK_HDC
d275c7eb 1520
b5371ab8
VZ
1521 if ( pen == m_pen )
1522 return;
a23fd0e1 1523
87d3576f 1524 if ( pen.IsOk() )
7bcb11d3 1525 {
b5371ab8
VZ
1526 HGDIOBJ hpen = ::SelectObject(GetHdc(), GetHpenOf(pen));
1527 if ( hpen == HGDI_ERROR )
1528 {
9a83f860 1529 wxLogLastError(wxT("SelectObject(pen)"));
b5371ab8
VZ
1530 }
1531 else // selected ok
1532 {
1533 if ( !m_oldPen )
1534 m_oldPen = (WXHPEN)hpen;
a23fd0e1 1535
b5371ab8
VZ
1536 m_pen = pen;
1537 }
1538 }
1539 else // invalid pen, reset the current pen
7bcb11d3 1540 {
b5371ab8 1541 if ( m_oldPen )
7bcb11d3 1542 {
b5371ab8
VZ
1543 if ( ::SelectObject(GetHdc(), (HPEN) m_oldPen) == HGDI_ERROR )
1544 {
9a83f860 1545 wxLogLastError(wxT("SelectObject(old pen)"));
b5371ab8
VZ
1546 }
1547
beb966c5 1548 m_oldPen = 0;
7bcb11d3 1549 }
b5371ab8
VZ
1550
1551 m_pen = wxNullPen;
2bda0e17 1552 }
2bda0e17
KB
1553}
1554
888dde65 1555void wxMSWDCImpl::SetBrush(const wxBrush& brush)
2bda0e17 1556{
82a306b7 1557 WXMICROWIN_CHECK_HDC
d275c7eb 1558
9f7948af
VZ
1559 if ( brush == m_brush )
1560 return;
a23fd0e1 1561
87d3576f 1562 if ( brush.IsOk() )
7bcb11d3 1563 {
9f7948af 1564 // we must make sure the brush is aligned with the logical coordinates
1dc39a1f
VZ
1565 // before selecting it or using the same brush for the background of
1566 // different windows would result in discontinuities
1567 wxSize sizeBrushBitmap = wxDefaultSize;
9f7948af 1568 wxBitmap *stipple = brush.GetStipple();
87d3576f 1569 if ( stipple && stipple->IsOk() )
1dc39a1f
VZ
1570 sizeBrushBitmap = stipple->GetSize();
1571 else if ( brush.IsHatch() )
1572 sizeBrushBitmap = wxSize(8, 8);
1573
1574 if ( sizeBrushBitmap.IsFullySpecified() )
2d8a5cb1 1575 {
9f7948af
VZ
1576 if ( !::SetBrushOrgEx
1577 (
1578 GetHdc(),
1dc39a1f
VZ
1579 m_deviceOriginX % sizeBrushBitmap.x,
1580 m_deviceOriginY % sizeBrushBitmap.y,
9f7948af
VZ
1581 NULL // [out] previous brush origin
1582 ) )
1583 {
9a83f860 1584 wxLogLastError(wxT("SetBrushOrgEx()"));
9f7948af 1585 }
2d8a5cb1
VZ
1586 }
1587
9f7948af
VZ
1588 HGDIOBJ hbrush = ::SelectObject(GetHdc(), GetHbrushOf(brush));
1589 if ( hbrush == HGDI_ERROR )
7bcb11d3 1590 {
9a83f860 1591 wxLogLastError(wxT("SelectObject(brush)"));
7bcb11d3 1592 }
9f7948af
VZ
1593 else // selected ok
1594 {
1595 if ( !m_oldBrush )
658ff7f1 1596 m_oldBrush = (WXHBRUSH)hbrush;
9f7948af
VZ
1597
1598 m_brush = brush;
1599 }
1600 }
1601 else // invalid brush, reset the current brush
1602 {
1603 if ( m_oldBrush )
1604 {
1605 if ( ::SelectObject(GetHdc(), (HPEN) m_oldBrush) == HGDI_ERROR )
1606 {
9a83f860 1607 wxLogLastError(wxT("SelectObject(old brush)"));
9f7948af
VZ
1608 }
1609
beb966c5 1610 m_oldBrush = 0;
9f7948af
VZ
1611 }
1612
1613 m_brush = wxNullBrush;
2bda0e17 1614 }
2bda0e17
KB
1615}
1616
888dde65 1617void wxMSWDCImpl::SetBackground(const wxBrush& brush)
2bda0e17 1618{
82a306b7 1619 WXMICROWIN_CHECK_HDC
d275c7eb 1620
7bcb11d3 1621 m_backgroundBrush = brush;
a23fd0e1 1622
87d3576f 1623 if ( m_backgroundBrush.IsOk() )
7bcb11d3 1624 {
44383ef7 1625 (void)SetBkColor(GetHdc(), m_backgroundBrush.GetColour().GetPixel());
2bda0e17 1626 }
2bda0e17
KB
1627}
1628
888dde65 1629void wxMSWDCImpl::SetBackgroundMode(int mode)
2bda0e17 1630{
82a306b7 1631 WXMICROWIN_CHECK_HDC
d275c7eb 1632
7bcb11d3 1633 m_backgroundMode = mode;
a23fd0e1 1634
c45a644e
RR
1635 // SetBackgroundColour now only refers to text background
1636 // and m_backgroundMode is used there
2bda0e17
KB
1637}
1638
89efaf2b 1639void wxMSWDCImpl::SetLogicalFunction(wxRasterOperationMode function)
2bda0e17 1640{
82a306b7 1641 WXMICROWIN_CHECK_HDC
d275c7eb 1642
7bcb11d3 1643 m_logicalFunction = function;
a23fd0e1 1644
ed791986 1645 SetRop(m_hDC);
2bda0e17
KB
1646}
1647
888dde65 1648void wxMSWDCImpl::SetRop(WXHDC dc)
2bda0e17 1649{
ed791986 1650 if ( !dc || m_logicalFunction < 0 )
7bcb11d3 1651 return;
a23fd0e1 1652
36e5a9a7 1653 int rop;
ed791986 1654
7bcb11d3
JS
1655 switch (m_logicalFunction)
1656 {
4aff28fc 1657 case wxCLEAR: rop = R2_BLACK; break;
ed791986
VZ
1658 case wxXOR: rop = R2_XORPEN; break;
1659 case wxINVERT: rop = R2_NOT; break;
1660 case wxOR_REVERSE: rop = R2_MERGEPENNOT; break;
1661 case wxAND_REVERSE: rop = R2_MASKPENNOT; break;
4aff28fc 1662 case wxCOPY: rop = R2_COPYPEN; break;
ed791986 1663 case wxAND: rop = R2_MASKPEN; break;
ed791986 1664 case wxAND_INVERT: rop = R2_MASKNOTPEN; break;
ed791986 1665 case wxNO_OP: rop = R2_NOP; break;
ed791986 1666 case wxNOR: rop = R2_NOTMERGEPEN; break;
4aff28fc
VZ
1667 case wxEQUIV: rop = R2_NOTXORPEN; break;
1668 case wxSRC_INVERT: rop = R2_NOTCOPYPEN; break;
1669 case wxOR_INVERT: rop = R2_MERGENOTPEN; break;
1670 case wxNAND: rop = R2_NOTMASKPEN; break;
1671 case wxOR: rop = R2_MERGEPEN; break;
1672 case wxSET: rop = R2_WHITE; break;
36e5a9a7
VZ
1673 default:
1674 wxFAIL_MSG( wxS("unknown logical function") );
1675 return;
7bcb11d3 1676 }
ed791986
VZ
1677
1678 SetROP2(GetHdc(), rop);
2bda0e17
KB
1679}
1680
888dde65 1681bool wxMSWDCImpl::StartDoc(const wxString& WXUNUSED(message))
2bda0e17 1682{
beb966c5
DS
1683 // We might be previewing, so return true to let it continue.
1684 return true;
2bda0e17
KB
1685}
1686
888dde65 1687void wxMSWDCImpl::EndDoc()
2bda0e17 1688{
2bda0e17
KB
1689}
1690
888dde65 1691void wxMSWDCImpl::StartPage()
2bda0e17 1692{
2bda0e17
KB
1693}
1694
888dde65 1695void wxMSWDCImpl::EndPage()
2bda0e17 1696{
2bda0e17
KB
1697}
1698
a23fd0e1
VZ
1699// ---------------------------------------------------------------------------
1700// text metrics
1701// ---------------------------------------------------------------------------
1702
888dde65 1703wxCoord wxMSWDCImpl::GetCharHeight() const
2bda0e17 1704{
82a306b7 1705 WXMICROWIN_CHECK_HDC_RET(0)
d275c7eb 1706
7bcb11d3 1707 TEXTMETRIC lpTextMetric;
a23fd0e1
VZ
1708
1709 GetTextMetrics(GetHdc(), &lpTextMetric);
1710
1e2081a1 1711 return lpTextMetric.tmHeight;
2bda0e17
KB
1712}
1713
888dde65 1714wxCoord wxMSWDCImpl::GetCharWidth() const
2bda0e17 1715{
82a306b7 1716 WXMICROWIN_CHECK_HDC_RET(0)
d275c7eb 1717
7bcb11d3 1718 TEXTMETRIC lpTextMetric;
a23fd0e1
VZ
1719
1720 GetTextMetrics(GetHdc(), &lpTextMetric);
1721
1e2081a1 1722 return lpTextMetric.tmAveCharWidth;
2bda0e17
KB
1723}
1724
888dde65 1725void wxMSWDCImpl::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
72cdf4c9 1726 wxCoord *descent, wxCoord *externalLeading,
c94f845b 1727 const wxFont *font) const
2bda0e17 1728{
5adad466
JS
1729#ifdef __WXMICROWIN__
1730 if (!GetHDC())
1731 {
a230101e
VZ
1732 if (x) *x = 0;
1733 if (y) *y = 0;
1734 if (descent) *descent = 0;
1735 if (externalLeading) *externalLeading = 0;
1736 return;
5adad466 1737 }
a230101e 1738#endif // __WXMICROWIN__
d275c7eb 1739
8bf30fe9
VZ
1740 HFONT hfontOld;
1741 if ( font )
1742 {
9a83f860 1743 wxASSERT_MSG( font->IsOk(), wxT("invalid font in wxMSWDCImpl::GetTextExtent") );
8bf30fe9
VZ
1744
1745 hfontOld = (HFONT)::SelectObject(GetHdc(), GetHfontOf(*font));
1746 }
1747 else // don't change the font
1748 {
1749 hfontOld = 0;
1750 }
a23fd0e1 1751
7bcb11d3 1752 SIZE sizeRect;
481203cb 1753 const size_t len = string.length();
e0a050e3 1754 if ( !::GetTextExtentPoint32(GetHdc(), string.wx_str(), len, &sizeRect) )
481203cb 1755 {
9a83f860 1756 wxLogLastError(wxT("GetTextExtentPoint32()"));
481203cb 1757 }
a23fd0e1 1758
3cdcf4d4 1759#if !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
481203cb
VZ
1760 // the result computed by GetTextExtentPoint32() may be too small as it
1761 // accounts for under/overhang of the first/last character while we want
1762 // just the bounding rect for this string so adjust the width as needed
3cdcf4d4 1763 // (using API not available in 2002 SDKs of WinCE)
481203cb
VZ
1764 if ( len > 0 )
1765 {
1766 ABC width;
1767 const wxChar chFirst = *string.begin();
1768 if ( ::GetCharABCWidths(GetHdc(), chFirst, chFirst, &width) )
1769 {
1770 if ( width.abcA < 0 )
1771 sizeRect.cx -= width.abcA;
1772
1773 if ( len > 1 )
1774 {
1775 const wxChar chLast = *string.rbegin();
1776 ::GetCharABCWidths(GetHdc(), chLast, chLast, &width);
1777 }
1778 //else: we already have the width of the last character
1779
1780 if ( width.abcC < 0 )
1781 sizeRect.cx -= width.abcC;
1782 }
1783 //else: GetCharABCWidths() failed, not a TrueType font?
1784 }
3cdcf4d4 1785#endif // !defined(_WIN32_WCE) || (_WIN32_WCE >= 400)
481203cb 1786
1e2081a1
VZ
1787 if (x)
1788 *x = sizeRect.cx;
1789 if (y)
1790 *y = sizeRect.cy;
247afab5
VZ
1791
1792 if ( descent || externalLeading )
1793 {
1794 TEXTMETRIC tm;
1795 ::GetTextMetrics(GetHdc(), &tm);
1796
1797 if (descent)
1798 *descent = tm.tmDescent;
1799 if (externalLeading)
1800 *externalLeading = tm.tmExternalLeading;
1801 }
8bf30fe9
VZ
1802
1803 if ( hfontOld )
1804 {
1805 ::SelectObject(GetHdc(), hfontOld);
1806 }
2bda0e17
KB
1807}
1808
553aa032
RD
1809
1810// Each element of the array will be the width of the string up to and
5c2ddebe 1811// including the coresoponding character in text.
553aa032 1812
888dde65 1813bool wxMSWDCImpl::DoGetPartialTextExtents(const wxString& text, wxArrayInt& widths) const
553aa032
RD
1814{
1815 static int maxLenText = -1;
1816 static int maxWidth = -1;
1817 int fit = 0;
1818 SIZE sz = {0,0};
41e155b4 1819 int stlen = text.length();
553aa032
RD
1820
1821 if (maxLenText == -1)
1822 {
1823 // Win9x and WinNT+ have different limits
1824 int version = wxGetOsVersion();
406d283a
PC
1825 maxLenText = version == wxOS_WINDOWS_NT ? 65535 : 8192;
1826 maxWidth = version == wxOS_WINDOWS_NT ? INT_MAX : 32767;
553aa032 1827 }
5c2ddebe 1828
553aa032
RD
1829 widths.Empty();
1830 widths.Add(0, stlen); // fill the array with zeros
f9daf953
JS
1831 if (stlen == 0)
1832 return true;
5c2ddebe 1833
553aa032
RD
1834 if (!::GetTextExtentExPoint(GetHdc(),
1835 text.c_str(), // string to check
1836 wxMin(stlen, maxLenText),
5c2ddebe
VZ
1837 maxWidth,
1838 &fit, // [out] count of chars
553aa032 1839 // that will fit
5c2ddebe
VZ
1840 &widths[0], // array to fill
1841 &sz))
1842 {
553aa032
RD
1843 // API failed
1844 wxLogLastError(wxT("GetTextExtentExPoint"));
5c2ddebe
VZ
1845 return false;
1846 }
1847
553aa032
RD
1848 return true;
1849}
1850
888dde65 1851void wxMSWDCImpl::RealizeScaleAndOrigin()
04ab8b6d 1852{
9effb77d
VZ
1853 // although it may seem wasteful to always use MM_ANISOTROPIC here instead
1854 // of using MM_TEXT if there is no scaling, benchmarking doesn't detect any
1855 // noticeable difference between these mapping modes
04ab8b6d
RR
1856#ifndef __WXWINCE__
1857 ::SetMapMode(GetHdc(), MM_ANISOTROPIC);
553aa032 1858
04ab8b6d
RR
1859 int width = DeviceToLogicalXRel(VIEWPORT_EXTENT)*m_signX,
1860 height = DeviceToLogicalYRel(VIEWPORT_EXTENT)*m_signY;
553aa032 1861
04ab8b6d
RR
1862 ::SetViewportExtEx(GetHdc(), VIEWPORT_EXTENT, VIEWPORT_EXTENT, NULL);
1863 ::SetWindowExtEx(GetHdc(), width, height, NULL);
1864
1865 ::SetViewportOrgEx(GetHdc(), m_deviceOriginX, m_deviceOriginY, NULL);
1866 ::SetWindowOrgEx(GetHdc(), m_logicalOriginX, m_logicalOriginY, NULL);
1867#endif
04ab8b6d 1868}
553aa032 1869
89efaf2b 1870void wxMSWDCImpl::SetMapMode(wxMappingMode mode)
2bda0e17 1871{
82a306b7 1872 WXMICROWIN_CHECK_HDC
d275c7eb 1873
7bcb11d3 1874 m_mappingMode = mode;
a23fd0e1 1875
1e2081a1 1876 if ( mode == wxMM_TEXT )
2bda0e17 1877 {
1e2081a1
VZ
1878 m_logicalScaleX =
1879 m_logicalScaleY = 1.0;
2bda0e17 1880 }
1e2081a1 1881 else // need to do some calculations
2bda0e17 1882 {
1e2081a1
VZ
1883 int pixel_width = ::GetDeviceCaps(GetHdc(), HORZRES),
1884 pixel_height = ::GetDeviceCaps(GetHdc(), VERTRES),
1885 mm_width = ::GetDeviceCaps(GetHdc(), HORZSIZE),
1886 mm_height = ::GetDeviceCaps(GetHdc(), VERTSIZE);
1887
1888 if ( (mm_width == 0) || (mm_height == 0) )
7bcb11d3 1889 {
1e2081a1
VZ
1890 // we can't calculate mm2pixels[XY] then!
1891 return;
7bcb11d3 1892 }
1e2081a1 1893
82922a02
VZ
1894 double mm2pixelsX = (double)pixel_width / mm_width,
1895 mm2pixelsY = (double)pixel_height / mm_height;
1e2081a1
VZ
1896
1897 switch (mode)
7bcb11d3 1898 {
1e2081a1
VZ
1899 case wxMM_TWIPS:
1900 m_logicalScaleX = twips2mm * mm2pixelsX;
1901 m_logicalScaleY = twips2mm * mm2pixelsY;
1902 break;
1903
1904 case wxMM_POINTS:
1905 m_logicalScaleX = pt2mm * mm2pixelsX;
1906 m_logicalScaleY = pt2mm * mm2pixelsY;
1907 break;
1908
1909 case wxMM_METRIC:
1910 m_logicalScaleX = mm2pixelsX;
1911 m_logicalScaleY = mm2pixelsY;
1912 break;
1913
1914 case wxMM_LOMETRIC:
1915 m_logicalScaleX = mm2pixelsX / 10.0;
1916 m_logicalScaleY = mm2pixelsY / 10.0;
1917 break;
1918
1919 default:
9a83f860 1920 wxFAIL_MSG( wxT("unknown mapping mode in SetMapMode") );
7bcb11d3 1921 }
2bda0e17 1922 }
1ee280b7 1923
04ab8b6d 1924 ComputeScaleAndOrigin();
1ee280b7 1925
04ab8b6d 1926 RealizeScaleAndOrigin();
2bda0e17
KB
1927}
1928
888dde65 1929void wxMSWDCImpl::SetUserScale(double x, double y)
2bda0e17 1930{
82a306b7 1931 WXMICROWIN_CHECK_HDC
d275c7eb 1932
1e2081a1
VZ
1933 if ( x == m_userScaleX && y == m_userScaleY )
1934 return;
1935
888dde65 1936 wxDCImpl::SetUserScale(x,y);
1ee280b7 1937
04ab8b6d 1938 RealizeScaleAndOrigin();
2bda0e17
KB
1939}
1940
888dde65 1941void wxMSWDCImpl::SetAxisOrientation(bool xLeftRight,
04ab8b6d 1942 bool yBottomUp)
6f65e337 1943{
82a306b7 1944 WXMICROWIN_CHECK_HDC
d275c7eb 1945
1e2081a1
VZ
1946 int signX = xLeftRight ? 1 : -1,
1947 signY = yBottomUp ? -1 : 1;
1ee280b7 1948
04ab8b6d 1949 if (signX == m_signX && signY == m_signY)
1e2081a1 1950 return;
1ee280b7 1951
888dde65 1952 wxDCImpl::SetAxisOrientation( xLeftRight, yBottomUp );
1e2081a1 1953
04ab8b6d 1954 RealizeScaleAndOrigin();
2bda0e17
KB
1955}
1956
888dde65 1957void wxMSWDCImpl::SetLogicalOrigin(wxCoord x, wxCoord y)
2bda0e17 1958{
82a306b7 1959 WXMICROWIN_CHECK_HDC
d275c7eb 1960
1e2081a1
VZ
1961 if ( x == m_logicalOriginX && y == m_logicalOriginY )
1962 return;
1963
888dde65 1964 wxDCImpl::SetLogicalOrigin( x, y );
a23fd0e1 1965
d225267e
RR
1966 RealizeScaleAndOrigin();
1967}
1968
1969// For use by wxWidgets only, unless custom units are required.
1970void wxMSWDCImpl::SetLogicalScale(double x, double y)
1971{
1972 WXMICROWIN_CHECK_HDC
1973
1974 wxDCImpl::SetLogicalScale(x,y);
2bda0e17
KB
1975}
1976
888dde65 1977void wxMSWDCImpl::SetDeviceOrigin(wxCoord x, wxCoord y)
2bda0e17 1978{
82a306b7 1979 WXMICROWIN_CHECK_HDC
d275c7eb 1980
1e2081a1
VZ
1981 if ( x == m_deviceOriginX && y == m_deviceOriginY )
1982 return;
1ee280b7 1983
888dde65 1984 wxDCImpl::SetDeviceOrigin( x, y );
2bda0e17 1985
a23fd0e1 1986 ::SetViewportOrgEx(GetHdc(), (int)m_deviceOriginX, (int)m_deviceOriginY, NULL);
2bda0e17
KB
1987}
1988
a23fd0e1
VZ
1989// ---------------------------------------------------------------------------
1990// bit blit
1991// ---------------------------------------------------------------------------
04ab8b6d 1992
888dde65 1993bool wxMSWDCImpl::DoBlit(wxCoord dstX, wxCoord dstY,
e3b81044
VZ
1994 wxCoord dstWidth, wxCoord dstHeight,
1995 wxDC *source,
1996 wxCoord srcX, wxCoord srcY,
89efaf2b 1997 wxRasterOperationMode rop, bool useMask,
e3b81044
VZ
1998 wxCoord srcMaskX, wxCoord srcMaskY)
1999{
2000 return DoStretchBlit(dstX, dstY, dstWidth, dstHeight, source, srcX, srcY, dstWidth, dstHeight, rop, useMask, srcMaskX, srcMaskY);
2001}
2002
888dde65 2003bool wxMSWDCImpl::DoStretchBlit(wxCoord xdest, wxCoord ydest,
e3b81044
VZ
2004 wxCoord dstWidth, wxCoord dstHeight,
2005 wxDC *source,
2006 wxCoord xsrc, wxCoord ysrc,
2007 wxCoord srcWidth, wxCoord srcHeight,
89efaf2b 2008 wxRasterOperationMode rop, bool useMask,
e3b81044 2009 wxCoord xsrcMask, wxCoord ysrcMask)
2bda0e17 2010{
9a83f860 2011 wxCHECK_MSG( source, false, wxT("wxMSWDCImpl::Blit(): NULL wxDC pointer") );
275a63e3 2012
beb966c5 2013 WXMICROWIN_CHECK_HDC_RET(false)
d275c7eb 2014
a619d8c9
VZ
2015 wxMSWDCImpl *implSrc = wxDynamicCast( source->GetImpl(), wxMSWDCImpl );
2016 if ( !implSrc )
888dde65 2017 {
a619d8c9 2018 // TODO: Do we want to be able to blit from other DCs too?
888dde65
RR
2019 return false;
2020 }
2021
a619d8c9
VZ
2022 const HDC hdcSrc = GetHdcOf(*implSrc);
2023
57c26f82
VZ
2024 // if either the source or destination has alpha channel, we must use
2025 // AlphaBlt() as other function don't handle it correctly
a619d8c9 2026 const wxBitmap& bmpSrc = implSrc->GetSelectedBitmap();
87d3576f
RR
2027 if ( bmpSrc.IsOk() && (bmpSrc.HasAlpha() ||
2028 (m_selectedBitmap.IsOk() && m_selectedBitmap.HasAlpha())) )
275a63e3 2029 {
e3b81044 2030 if ( AlphaBlt(GetHdc(), xdest, ydest, dstWidth, dstHeight,
a619d8c9 2031 xsrc, ysrc, srcWidth, srcHeight, hdcSrc, bmpSrc) )
beb966c5 2032 return true;
275a63e3 2033 }
d80f416f 2034
4b7f2165
VZ
2035 wxMask *mask = NULL;
2036 if ( useMask )
2037 {
d80f416f 2038 mask = bmpSrc.GetMask();
4b7f2165 2039
87d3576f 2040 if ( !(bmpSrc.IsOk() && mask && mask->GetMaskBitmap()) )
d5536ade
VZ
2041 {
2042 // don't give assert here because this would break existing
2043 // programs - just silently ignore useMask parameter
beb966c5 2044 useMask = false;
d5536ade 2045 }
4b7f2165 2046 }
a23fd0e1 2047
0cbff120
JS
2048 if (xsrcMask == -1 && ysrcMask == -1)
2049 {
2050 xsrcMask = xsrc; ysrcMask = ysrc;
2051 }
2052
76f91e77 2053 wxTextColoursChanger textCol(GetHdc(), *this);
a23fd0e1 2054
999836aa 2055 DWORD dwRop;
71fe5c01
RR
2056 switch (rop)
2057 {
ed791986
VZ
2058 case wxXOR: dwRop = SRCINVERT; break;
2059 case wxINVERT: dwRop = DSTINVERT; break;
2060 case wxOR_REVERSE: dwRop = 0x00DD0228; break;
2061 case wxAND_REVERSE: dwRop = SRCERASE; break;
2062 case wxCLEAR: dwRop = BLACKNESS; break;
2063 case wxSET: dwRop = WHITENESS; break;
2064 case wxOR_INVERT: dwRop = MERGEPAINT; break;
2065 case wxAND: dwRop = SRCAND; break;
2066 case wxOR: dwRop = SRCPAINT; break;
2067 case wxEQUIV: dwRop = 0x00990066; break;
2068 case wxNAND: dwRop = 0x007700E6; break;
2069 case wxAND_INVERT: dwRop = 0x00220326; break;
2070 case wxCOPY: dwRop = SRCCOPY; break;
4aff28fc 2071 case wxNO_OP: dwRop = DSTCOPY; break;
ed791986 2072 case wxSRC_INVERT: dwRop = NOTSRCCOPY; break;
71fe5c01
RR
2073 case wxNOR: dwRop = NOTSRCCOPY; break;
2074 default:
71fe5c01 2075 wxFAIL_MSG( wxT("unsupported logical function") );
beb966c5 2076 return false;
71fe5c01
RR
2077 }
2078
beb966c5 2079 bool success = false;
730bc726
JS
2080
2081 if (useMask)
7bcb11d3 2082 {
4b7f2165 2083#ifdef __WIN32__
a58a12e9 2084 // we want the part of the image corresponding to the mask to be
4aff28fc
VZ
2085 // transparent, so use "DSTCOPY" ROP for the mask points (the usual
2086 // meaning of fg and bg is inverted which corresponds to wxWin notion
2087 // of the mask which is also contrary to the Windows one)
d3211838
JS
2088
2089 // On some systems, MaskBlt succeeds yet is much much slower
77ffb593 2090 // than the wxWidgets fall-back implementation. So we need
d3211838 2091 // to be able to switch this on and off at runtime.
0cbff120
JS
2092#if wxUSE_SYSTEM_OPTIONS
2093 if (wxSystemOptions::GetOptionInt(wxT("no-maskblt")) == 0)
2094#endif
d3211838 2095 {
e3b81044
VZ
2096 if ( dstWidth == srcWidth && dstHeight == srcHeight )
2097 {
2098 success = ::MaskBlt
2099 (
f178ab7e 2100 GetHdc(),
e3b81044 2101 xdest, ydest, dstWidth, dstHeight,
a619d8c9 2102 hdcSrc,
f178ab7e
VZ
2103 xsrc, ysrc,
2104 (HBITMAP)mask->GetMaskBitmap(),
2105 xsrcMask, ysrcMask,
2106 MAKEROP4(dwRop, DSTCOPY)
e3b81044
VZ
2107 ) != 0;
2108 }
d3211838 2109 }
a58a12e9
VZ
2110
2111 if ( !success )
4b7f2165 2112#endif // Win32
7bcb11d3 2113 {
7bcb11d3 2114 // Blit bitmap with mask
0cbff120
JS
2115 HDC dc_mask ;
2116 HDC dc_buffer ;
2117 HBITMAP buffer_bmap ;
a23fd0e1 2118
0cbff120 2119#if wxUSE_DC_CACHEING
2b96d0fb 2120 // create a temp buffer bitmap and DCs to access it and the mask
a619d8c9 2121 wxDCCacheEntry* dcCacheEntry1 = FindDCInCache(NULL, hdcSrc);
2b96d0fb
VZ
2122 dc_mask = (HDC) dcCacheEntry1->m_dc;
2123
2124 wxDCCacheEntry* dcCacheEntry2 = FindDCInCache(dcCacheEntry1, GetHDC());
2125 dc_buffer = (HDC) dcCacheEntry2->m_dc;
2126
2127 wxDCCacheEntry* bitmapCacheEntry = FindBitmapInCache(GetHDC(),
e3b81044 2128 dstWidth, dstHeight);
2b96d0fb
VZ
2129
2130 buffer_bmap = (HBITMAP) bitmapCacheEntry->m_bitmap;
2131#else // !wxUSE_DC_CACHEING
2132 // create a temp buffer bitmap and DCs to access it and the mask
a619d8c9 2133 dc_mask = ::CreateCompatibleDC(hdcSrc);
2b96d0fb 2134 dc_buffer = ::CreateCompatibleDC(GetHdc());
e3b81044 2135 buffer_bmap = ::CreateCompatibleBitmap(GetHdc(), dstWidth, dstHeight);
619e52bf 2136#endif // wxUSE_DC_CACHEING/!wxUSE_DC_CACHEING
a230101e
VZ
2137 HGDIOBJ hOldMaskBitmap = ::SelectObject(dc_mask, (HBITMAP) mask->GetMaskBitmap());
2138 HGDIOBJ hOldBufferBitmap = ::SelectObject(dc_buffer, buffer_bmap);
4b7f2165
VZ
2139
2140 // copy dest to buffer
76f91e77 2141 if ( !::BitBlt(dc_buffer, 0, 0, dstWidth, dstHeight,
4b7f2165 2142 GetHdc(), xdest, ydest, SRCCOPY) )
7bcb11d3 2143 {
f6bcfd97 2144 wxLogLastError(wxT("BitBlt"));
7bcb11d3 2145 }
4b7f2165 2146
4f64fde7 2147 SET_STRETCH_BLT_MODE(GetHdc());
e3b81044 2148
4b7f2165 2149 // copy src to buffer using selected raster op
76f91e77 2150 if ( !::StretchBlt(dc_buffer, 0, 0, dstWidth, dstHeight,
a619d8c9 2151 hdcSrc, xsrc, ysrc, srcWidth, srcHeight, dwRop) )
7bcb11d3 2152 {
e3b81044 2153 wxLogLastError(wxT("StretchBlt"));
7bcb11d3 2154 }
4b7f2165 2155
76f91e77 2156 // set masked area in buffer to BLACK
4b7f2165 2157 {
76f91e77
VZ
2158 wxTextColoursChanger textCol2(GetHdc(), *wxBLACK, *wxWHITE);
2159 if ( !::StretchBlt(dc_buffer, 0, 0, dstWidth, dstHeight,
2160 dc_mask, xsrcMask, ysrcMask,
2161 srcWidth, srcHeight, SRCAND) )
2162 {
2163 wxLogLastError(wxT("StretchBlt"));
2164 }
4b7f2165 2165
76f91e77
VZ
2166 // set unmasked area in dest to BLACK
2167 ::SetBkColor(GetHdc(), RGB(0, 0, 0));
2168 ::SetTextColor(GetHdc(), RGB(255, 255, 255));
2169 if ( !::StretchBlt(GetHdc(), xdest, ydest, dstWidth, dstHeight,
2170 dc_mask, xsrcMask, ysrcMask,
2171 srcWidth, srcHeight, SRCAND) )
2172 {
2173 wxLogLastError(wxT("StretchBlt"));
2174 }
2175 } // restore the original text and background colours
4b7f2165
VZ
2176
2177 // OR buffer to dest
76f91e77 2178 success = ::BitBlt(GetHdc(), xdest, ydest, dstWidth, dstHeight,
4b7f2165
VZ
2179 dc_buffer, 0, 0, SRCPAINT) != 0;
2180 if ( !success )
2181 {
f6bcfd97 2182 wxLogLastError(wxT("BitBlt"));
4b7f2165
VZ
2183 }
2184
2185 // tidy up temporary DCs and bitmap
62e1ba75
JS
2186 ::SelectObject(dc_mask, hOldMaskBitmap);
2187 ::SelectObject(dc_buffer, hOldBufferBitmap);
0cbff120 2188
27748047 2189#if !wxUSE_DC_CACHEING
0cbff120
JS
2190 {
2191 ::DeleteDC(dc_mask);
2192 ::DeleteDC(dc_buffer);
2193 ::DeleteObject(buffer_bmap);
2194 }
27748047 2195#endif
7bcb11d3
JS
2196 }
2197 }
4b7f2165 2198 else // no mask, just BitBlt() it
730bc726 2199 {
d80f416f
VZ
2200 // if we already have a DIB, draw it using StretchDIBits(), otherwise
2201 // use StretchBlt() if available and finally fall back to BitBlt()
4676948b
JS
2202
2203 // FIXME: use appropriate WinCE functions
2204#ifndef __WXWINCE__
d80f416f 2205 const int caps = ::GetDeviceCaps(GetHdc(), RASTERCAPS);
87d3576f 2206 if ( bmpSrc.IsOk() && (caps & RC_STRETCHDIB) )
f178ab7e 2207 {
d80f416f
VZ
2208 DIBSECTION ds;
2209 wxZeroMemory(ds);
f178ab7e 2210
d80f416f
VZ
2211 if ( ::GetObject(GetHbitmapOf(bmpSrc),
2212 sizeof(ds),
2213 &ds) == sizeof(ds) )
2214 {
4f64fde7 2215 SET_STRETCH_BLT_MODE(GetHdc());
d80f416f 2216
b9b1f368
VZ
2217 // Figure out what co-ordinate system we're supposed to specify
2218 // ysrc in.
2219 const LONG hDIB = ds.dsBmih.biHeight;
2220 if ( hDIB > 0 )
2221 {
2222 // reflect ysrc
4b871421 2223 ysrc = hDIB - (ysrc + srcHeight);
b9b1f368
VZ
2224 }
2225
d80f416f
VZ
2226 if ( ::StretchDIBits(GetHdc(),
2227 xdest, ydest,
e3b81044 2228 dstWidth, dstHeight,
b9b1f368 2229 xsrc, ysrc,
e3b81044 2230 srcWidth, srcHeight,
d80f416f
VZ
2231 ds.dsBm.bmBits,
2232 (LPBITMAPINFO)&ds.dsBmih,
2233 DIB_RGB_COLORS,
b7a0abc7 2234 dwRop
d80f416f
VZ
2235 ) == (int)GDI_ERROR )
2236 {
85e7fb12
RD
2237 // On Win9x this API fails most (all?) of the time, so
2238 // logging it becomes quite distracting. Since it falls
2239 // back to the code below this is not really serious, so
35bbb0c6 2240 // don't log it.
85e7fb12 2241 //wxLogLastError(wxT("StretchDIBits"));
d80f416f
VZ
2242 }
2243 else
2244 {
beb966c5 2245 success = true;
d80f416f
VZ
2246 }
2247 }
f178ab7e 2248 }
d80f416f
VZ
2249
2250 if ( !success && (caps & RC_STRETCHBLT) )
419430a0
JS
2251#endif
2252 // __WXWINCE__
f178ab7e 2253 {
4f64fde7 2254 SET_STRETCH_BLT_MODE(GetHdc());
d80f416f
VZ
2255
2256 if ( !::StretchBlt
2257 (
2258 GetHdc(),
e3b81044 2259 xdest, ydest, dstWidth, dstHeight,
a619d8c9 2260 hdcSrc,
e3b81044 2261 xsrc, ysrc, srcWidth, srcHeight,
d80f416f
VZ
2262 dwRop
2263 ) )
2264 {
9a83f860 2265 wxLogLastError(wxT("StretchBlt"));
d80f416f
VZ
2266 }
2267 else
2268 {
beb966c5 2269 success = true;
d80f416f 2270 }
f178ab7e
VZ
2271 }
2272
4b7f2165 2273 if ( !success )
730bc726 2274 {
76f91e77
VZ
2275 if ( !::BitBlt(GetHdc(), xdest, ydest, dstWidth, dstHeight,
2276 hdcSrc, xsrc, ysrc, dwRop) )
d80f416f 2277 {
9a83f860 2278 wxLogLastError(wxT("BitBlt"));
d80f416f
VZ
2279 }
2280 else
2281 {
beb966c5 2282 success = true;
d80f416f 2283 }
730bc726 2284 }
730bc726 2285 }
f178ab7e 2286
a23fd0e1 2287 return success;
2bda0e17
KB
2288}
2289
888dde65 2290void wxMSWDCImpl::GetDeviceSize(int *width, int *height) const
2bda0e17 2291{
82a306b7 2292 WXMICROWIN_CHECK_HDC
d275c7eb 2293
7d09b97f
VZ
2294 if ( width )
2295 *width = ::GetDeviceCaps(GetHdc(), HORZRES);
2296 if ( height )
2297 *height = ::GetDeviceCaps(GetHdc(), VERTRES);
2bda0e17
KB
2298}
2299
888dde65 2300void wxMSWDCImpl::DoGetSizeMM(int *w, int *h) const
2bda0e17 2301{
82a306b7 2302 WXMICROWIN_CHECK_HDC
d275c7eb 2303
994a3786
VZ
2304 // if we implement it in terms of DoGetSize() instead of directly using the
2305 // results returned by GetDeviceCaps(HORZ/VERTSIZE) as was done before, it
2306 // will also work for wxWindowDC and wxClientDC even though their size is
2307 // not the same as the total size of the screen
2308 int wPixels, hPixels;
2309 DoGetSize(&wPixels, &hPixels);
2310
2311 if ( w )
2312 {
2313 int wTotal = ::GetDeviceCaps(GetHdc(), HORZRES);
2314
9a83f860 2315 wxCHECK_RET( wTotal, wxT("0 width device?") );
994a3786
VZ
2316
2317 *w = (wPixels * ::GetDeviceCaps(GetHdc(), HORZSIZE)) / wTotal;
2318 }
2319
2320 if ( h )
2321 {
2322 int hTotal = ::GetDeviceCaps(GetHdc(), VERTRES);
2323
9a83f860 2324 wxCHECK_RET( hTotal, wxT("0 height device?") );
994a3786
VZ
2325
2326 *h = (hPixels * ::GetDeviceCaps(GetHdc(), VERTSIZE)) / hTotal;
2327 }
7bcb11d3 2328}
2bda0e17 2329
888dde65 2330wxSize wxMSWDCImpl::GetPPI() const
7bcb11d3 2331{
c47addef 2332 WXMICROWIN_CHECK_HDC_RET(wxSize(0,0))
d275c7eb 2333
a23fd0e1
VZ
2334 int x = ::GetDeviceCaps(GetHdc(), LOGPIXELSX);
2335 int y = ::GetDeviceCaps(GetHdc(), LOGPIXELSY);
2bda0e17 2336
a23fd0e1 2337 return wxSize(x, y);
2bda0e17
KB
2338}
2339
878711c0
VZ
2340// ----------------------------------------------------------------------------
2341// DC caching
2342// ----------------------------------------------------------------------------
2343
0cbff120
JS
2344#if wxUSE_DC_CACHEING
2345
2346/*
2347 * This implementation is a bit ugly and uses the old-fashioned wxList class, so I will
2348 * improve it in due course, either using arrays, or simply storing pointers to one
2349 * entry for the bitmap, and two for the DCs. -- JACS
2350 */
2351
888dde65
RR
2352wxObjectList wxMSWDCImpl::sm_bitmapCache;
2353wxObjectList wxMSWDCImpl::sm_dcCache;
0cbff120
JS
2354
2355wxDCCacheEntry::wxDCCacheEntry(WXHBITMAP hBitmap, int w, int h, int depth)
2356{
2357 m_bitmap = hBitmap;
2358 m_dc = 0;
2359 m_width = w;
2360 m_height = h;
2361 m_depth = depth;
2362}
2363
2364wxDCCacheEntry::wxDCCacheEntry(WXHDC hDC, int depth)
2365{
2366 m_bitmap = 0;
2367 m_dc = hDC;
2368 m_width = 0;
2369 m_height = 0;
2370 m_depth = depth;
2371}
2372
2373wxDCCacheEntry::~wxDCCacheEntry()
2374{
2375 if (m_bitmap)
2376 ::DeleteObject((HBITMAP) m_bitmap);
2377 if (m_dc)
2378 ::DeleteDC((HDC) m_dc);
2379}
2380
888dde65 2381wxDCCacheEntry* wxMSWDCImpl::FindBitmapInCache(WXHDC dc, int w, int h)
0cbff120
JS
2382{
2383 int depth = ::GetDeviceCaps((HDC) dc, PLANES) * ::GetDeviceCaps((HDC) dc, BITSPIXEL);
fc10daf3 2384 wxList::compatibility_iterator node = sm_bitmapCache.GetFirst();
0cbff120
JS
2385 while (node)
2386 {
4a9dba0e 2387 wxDCCacheEntry* entry = (wxDCCacheEntry*) node->GetData();
0cbff120
JS
2388
2389 if (entry->m_depth == depth)
2390 {
2391 if (entry->m_width < w || entry->m_height < h)
2392 {
2393 ::DeleteObject((HBITMAP) entry->m_bitmap);
2394 entry->m_bitmap = (WXHBITMAP) ::CreateCompatibleBitmap((HDC) dc, w, h);
2395 if ( !entry->m_bitmap)
2396 {
2397 wxLogLastError(wxT("CreateCompatibleBitmap"));
2398 }
2399 entry->m_width = w; entry->m_height = h;
2400 return entry;
2401 }
2402 return entry;
2403 }
2404
4a9dba0e 2405 node = node->GetNext();
0cbff120
JS
2406 }
2407 WXHBITMAP hBitmap = (WXHBITMAP) ::CreateCompatibleBitmap((HDC) dc, w, h);
2408 if ( !hBitmap)
2409 {
2410 wxLogLastError(wxT("CreateCompatibleBitmap"));
2411 }
2412 wxDCCacheEntry* entry = new wxDCCacheEntry(hBitmap, w, h, depth);
2413 AddToBitmapCache(entry);
2414 return entry;
2415}
2416
888dde65 2417wxDCCacheEntry* wxMSWDCImpl::FindDCInCache(wxDCCacheEntry* notThis, WXHDC dc)
0cbff120
JS
2418{
2419 int depth = ::GetDeviceCaps((HDC) dc, PLANES) * ::GetDeviceCaps((HDC) dc, BITSPIXEL);
fc10daf3 2420 wxList::compatibility_iterator node = sm_dcCache.GetFirst();
0cbff120
JS
2421 while (node)
2422 {
4a9dba0e 2423 wxDCCacheEntry* entry = (wxDCCacheEntry*) node->GetData();
0cbff120
JS
2424
2425 // Don't return the same one as we already have
2426 if (!notThis || (notThis != entry))
2427 {
2428 if (entry->m_depth == depth)
2429 {
2430 return entry;
2431 }
2432 }
2433
4a9dba0e 2434 node = node->GetNext();
0cbff120
JS
2435 }
2436 WXHDC hDC = (WXHDC) ::CreateCompatibleDC((HDC) dc);
2437 if ( !hDC)
2438 {
2439 wxLogLastError(wxT("CreateCompatibleDC"));
2440 }
2441 wxDCCacheEntry* entry = new wxDCCacheEntry(hDC, depth);
2442 AddToDCCache(entry);
2443 return entry;
2444}
2445
888dde65 2446void wxMSWDCImpl::AddToBitmapCache(wxDCCacheEntry* entry)
0cbff120
JS
2447{
2448 sm_bitmapCache.Append(entry);
2449}
2450
888dde65 2451void wxMSWDCImpl::AddToDCCache(wxDCCacheEntry* entry)
0cbff120
JS
2452{
2453 sm_dcCache.Append(entry);
2454}
2455
888dde65 2456void wxMSWDCImpl::ClearCache()
0cbff120 2457{
fc10daf3
MB
2458 WX_CLEAR_LIST(wxList, sm_dcCache);
2459 WX_CLEAR_LIST(wxList, sm_bitmapCache);
0cbff120
JS
2460}
2461
aef94d68
JS
2462// Clean up cache at app exit
2463class wxDCModule : public wxModule
2464{
2465public:
beb966c5 2466 virtual bool OnInit() { return true; }
888dde65 2467 virtual void OnExit() { wxMSWDCImpl::ClearCache(); }
aef94d68
JS
2468
2469private:
2470 DECLARE_DYNAMIC_CLASS(wxDCModule)
2471};
2472
2473IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule)
2474
878711c0
VZ
2475#endif // wxUSE_DC_CACHEING
2476
2477// ----------------------------------------------------------------------------
275a63e3 2478// alpha channel support
878711c0
VZ
2479// ----------------------------------------------------------------------------
2480
275a63e3 2481static bool AlphaBlt(HDC hdcDst,
e3b81044 2482 int x, int y, int dstWidth, int dstHeight,
1ee280b7 2483 int srcX, int srcY,
e3b81044
VZ
2484 int srcWidth, int srcHeight,
2485 HDC hdcSrc,
275a63e3
VZ
2486 const wxBitmap& bmp)
2487{
9a83f860
VZ
2488 wxASSERT_MSG( bmp.IsOk() && bmp.HasAlpha(), wxT("AlphaBlt(): invalid bitmap") );
2489 wxASSERT_MSG( hdcDst && hdcSrc, wxT("AlphaBlt(): invalid HDC") );
275a63e3
VZ
2490
2491 // do we have AlphaBlend() and company in the headers?
3080bf59 2492#if defined(AC_SRC_OVER) && wxUSE_DYNLIB_CLASS
275a63e3
VZ
2493 // yes, now try to see if we have it during run-time
2494 typedef BOOL (WINAPI *AlphaBlend_t)(HDC,int,int,int,int,
2495 HDC,int,int,int,int,
2496 BLENDFUNCTION);
2497
6ae7410f 2498 static AlphaBlend_t
9a83f860 2499 pfnAlphaBlend = (AlphaBlend_t)wxMSIMG32DLL.GetSymbol(wxT("AlphaBlend"));
275a63e3
VZ
2500 if ( pfnAlphaBlend )
2501 {
2502 BLENDFUNCTION bf;
2503 bf.BlendOp = AC_SRC_OVER;
2504 bf.BlendFlags = 0;
2505 bf.SourceConstantAlpha = 0xff;
2506 bf.AlphaFormat = AC_SRC_ALPHA;
2507
e3b81044
VZ
2508 if ( pfnAlphaBlend(hdcDst, x, y, dstWidth, dstHeight,
2509 hdcSrc, srcX, srcY, srcWidth, srcHeight,
275a63e3
VZ
2510 bf) )
2511 {
2512 // skip wxAlphaBlend() call below
beb966c5 2513 return true;
275a63e3
VZ
2514 }
2515
9a83f860 2516 wxLogLastError(wxT("AlphaBlend"));
275a63e3 2517 }
41e155b4
WS
2518#else
2519 wxUnusedVar(hdcSrc);
275a63e3
VZ
2520#endif // defined(AC_SRC_OVER)
2521
2522 // AlphaBlend() unavailable of failed: use our own (probably much slower)
2523 // implementation
3fb1e059 2524#ifdef wxHAS_RAW_BITMAP
e3b81044 2525 wxAlphaBlend(hdcDst, x, y, dstWidth, dstHeight, srcX, srcY, srcWidth, srcHeight, bmp);
275a63e3 2526
beb966c5 2527 return true;
3fb1e059 2528#else // !wxHAS_RAW_BITMAP
275a63e3
VZ
2529 // no wxAlphaBlend() neither, fall back to using simple BitBlt() (we lose
2530 // alpha but at least something will be shown like this)
907173e5 2531 wxUnusedVar(bmp);
beb966c5 2532 return false;
3fb1e059 2533#endif // wxHAS_RAW_BITMAP/!wxHAS_RAW_BITMAP
275a63e3
VZ
2534}
2535
2536
2537// wxAlphaBlend: our fallback if ::AlphaBlend() is unavailable
3fb1e059 2538#ifdef wxHAS_RAW_BITMAP
275a63e3 2539
878711c0 2540static void
761598d4 2541wxAlphaBlend(HDC hdcDst, int xDst, int yDst,
e3b81044 2542 int dstWidth, int dstHeight,
1ee280b7 2543 int srcX, int srcY,
e3b81044
VZ
2544 int srcWidth, int srcHeight,
2545 const wxBitmap& bmpSrc)
878711c0
VZ
2546{
2547 // get the destination DC pixels
e3b81044 2548 wxBitmap bmpDst(dstWidth, dstHeight, 32 /* force creating RGBA DIB */);
878711c0
VZ
2549 MemoryHDC hdcMem;
2550 SelectInHDC select(hdcMem, GetHbitmapOf(bmpDst));
2551
e3b81044 2552 if ( !::BitBlt(hdcMem, 0, 0, dstWidth, dstHeight, hdcDst, xDst, yDst, SRCCOPY) )
878711c0 2553 {
9a83f860 2554 wxLogLastError(wxT("BitBlt"));
878711c0
VZ
2555 }
2556
2557 // combine them with the source bitmap using alpha
b9bcaf11 2558 wxAlphaPixelData dataDst(bmpDst),
a452af5e 2559 dataSrc((wxBitmap &)bmpSrc);
878711c0 2560
8ffc8f1f 2561 wxCHECK_RET( dataDst && dataSrc,
9a83f860 2562 wxT("failed to get raw data in wxAlphaBlend") );
8ffc8f1f 2563
b9bcaf11
VZ
2564 wxAlphaPixelData::Iterator pDst(dataDst),
2565 pSrc(dataSrc);
878711c0 2566
3db79902 2567
e3b81044 2568 for ( int y = 0; y < dstHeight; y++ )
878711c0 2569 {
e3b81044 2570 wxAlphaPixelData::Iterator pDstRowStart = pDst;
0c0d1521 2571
e3b81044 2572 for ( int x = 0; x < dstWidth; x++ )
878711c0 2573 {
e3b81044
VZ
2574 // source is point sampled, Alpha StretchBlit is ugly on Win95
2575 // (but does not impact performance)
2576 pSrc.MoveTo(dataSrc, srcX + (srcWidth*x/dstWidth), srcY + (srcHeight*y/dstHeight));
2577
878711c0
VZ
2578 // note that source bitmap uses premultiplied alpha (as required by
2579 // the real AlphaBlend)
2580 const unsigned beta = 255 - pSrc.Alpha();
2581
2582 pDst.Red() = pSrc.Red() + (beta * pDst.Red() + 127) / 255;
2583 pDst.Blue() = pSrc.Blue() + (beta * pDst.Blue() + 127) / 255;
2584 pDst.Green() = pSrc.Green() + (beta * pDst.Green() + 127) / 255;
2585
2586 ++pDst;
878711c0
VZ
2587 }
2588
2589 pDst = pDstRowStart;
b9bcaf11 2590 pDst.OffsetY(dataDst, 1);
878711c0
VZ
2591 }
2592
2593 // and finally blit them back to the destination DC
e3b81044 2594 if ( !::BitBlt(hdcDst, xDst, yDst, dstWidth, dstHeight, hdcMem, 0, 0, SRCCOPY) )
878711c0 2595 {
9a83f860 2596 wxLogLastError(wxT("BitBlt"));
878711c0
VZ
2597 }
2598}
7b46ecac 2599
3fb1e059 2600#endif // wxHAS_RAW_BITMAP
213ad8e7 2601
888dde65 2602void wxMSWDCImpl::DoGradientFillLinear (const wxRect& rect,
213ad8e7
VZ
2603 const wxColour& initialColour,
2604 const wxColour& destColour,
2605 wxDirection nDirection)
2606{
2607 // use native function if we have compile-time support it and can load it
2608 // during run-time (linking to it statically would make the program
2609 // unusable on earlier Windows versions)
2610#if defined(GRADIENT_FILL_RECT_H) && wxUSE_DYNLIB_CLASS
2611 typedef BOOL
2612 (WINAPI *GradientFill_t)(HDC, PTRIVERTEX, ULONG, PVOID, ULONG, ULONG);
6ae7410f 2613 static GradientFill_t pfnGradientFill =
9a83f860 2614 (GradientFill_t)wxMSIMG32DLL.GetSymbol(wxT("GradientFill"));
213ad8e7
VZ
2615
2616 if ( pfnGradientFill )
2617 {
2618 GRADIENT_RECT grect;
2619 grect.UpperLeft = 0;
2620 grect.LowerRight = 1;
2621
2622 // invert colours direction if not filling from left-to-right or
2623 // top-to-bottom
2624 int firstVertex = nDirection == wxNORTH || nDirection == wxWEST ? 1 : 0;
2625
2626 // one vertex for upper left and one for upper-right
2627 TRIVERTEX vertices[2];
2628
2629 vertices[0].x = rect.GetLeft();
2630 vertices[0].y = rect.GetTop();
e6c46ffe
BW
2631 vertices[1].x = rect.GetRight()+1;
2632 vertices[1].y = rect.GetBottom()+1;
213ad8e7 2633
2e98a222
WS
2634 vertices[firstVertex].Red = (COLOR16)(initialColour.Red() << 8);
2635 vertices[firstVertex].Green = (COLOR16)(initialColour.Green() << 8);
2636 vertices[firstVertex].Blue = (COLOR16)(initialColour.Blue() << 8);
213ad8e7 2637 vertices[firstVertex].Alpha = 0;
2e98a222
WS
2638 vertices[1 - firstVertex].Red = (COLOR16)(destColour.Red() << 8);
2639 vertices[1 - firstVertex].Green = (COLOR16)(destColour.Green() << 8);
2640 vertices[1 - firstVertex].Blue = (COLOR16)(destColour.Blue() << 8);
213ad8e7
VZ
2641 vertices[1 - firstVertex].Alpha = 0;
2642
213ad8e7
VZ
2643 if ( (*pfnGradientFill)
2644 (
2645 GetHdc(),
2646 vertices,
2647 WXSIZEOF(vertices),
2648 &grect,
2649 1,
2650 nDirection == wxWEST || nDirection == wxEAST
2651 ? GRADIENT_FILL_RECT_H
2652 : GRADIENT_FILL_RECT_V
2653 ) )
2654 {
2655 // skip call of the base class version below
2656 return;
2657 }
2658
9a83f860 2659 wxLogLastError(wxT("GradientFill"));
213ad8e7
VZ
2660 }
2661#endif // wxUSE_DYNLIB_CLASS
2662
888dde65 2663 wxDCImpl::DoGradientFillLinear(rect, initialColour, destColour, nDirection);
213ad8e7 2664}
6ae7410f 2665
a8ff046b
VZ
2666#if wxUSE_DYNLIB_CLASS
2667
6ae7410f
VZ
2668static DWORD wxGetDCLayout(HDC hdc)
2669{
2670 typedef DWORD (WINAPI *GetLayout_t)(HDC);
2671 static GetLayout_t
9a83f860 2672 wxDL_INIT_FUNC(s_pfn, GetLayout, wxDynamicLibrary(wxT("gdi32.dll")));
6ae7410f 2673
60b0c3b4 2674 return s_pfnGetLayout ? s_pfnGetLayout(hdc) : (DWORD)-1;
6ae7410f
VZ
2675}
2676
888dde65 2677wxLayoutDirection wxMSWDCImpl::GetLayoutDirection() const
6ae7410f
VZ
2678{
2679 DWORD layout = wxGetDCLayout(GetHdc());
2680
2681 if ( layout == (DWORD)-1 )
2682 return wxLayout_Default;
2683
2684 return layout & LAYOUT_RTL ? wxLayout_RightToLeft : wxLayout_LeftToRight;
2685}
2686
888dde65 2687void wxMSWDCImpl::SetLayoutDirection(wxLayoutDirection dir)
6ae7410f
VZ
2688{
2689 typedef DWORD (WINAPI *SetLayout_t)(HDC, DWORD);
2690 static SetLayout_t
9a83f860 2691 wxDL_INIT_FUNC(s_pfn, SetLayout, wxDynamicLibrary(wxT("gdi32.dll")));
60b0c3b4 2692 if ( !s_pfnSetLayout )
6ae7410f
VZ
2693 return;
2694
2695 if ( dir == wxLayout_Default )
2696 {
2697 dir = wxTheApp->GetLayoutDirection();
2698 if ( dir == wxLayout_Default )
2699 return;
2700 }
2701
2702 DWORD layout = wxGetDCLayout(GetHdc());
2703 if ( dir == wxLayout_RightToLeft )
2704 layout |= LAYOUT_RTL;
2705 else
2706 layout &= ~LAYOUT_RTL;
2707
60b0c3b4 2708 s_pfnSetLayout(GetHdc(), layout);
6ae7410f 2709}
a8ff046b
VZ
2710
2711#else // !wxUSE_DYNLIB_CLASS
2712
2713// we can't provide RTL support without dynamic loading, so stub it out
888dde65 2714wxLayoutDirection wxMSWDCImpl::GetLayoutDirection() const
a8ff046b
VZ
2715{
2716 return wxLayout_Default;
2717}
2718
888dde65 2719void wxMSWDCImpl::SetLayoutDirection(wxLayoutDirection WXUNUSED(dir))
a8ff046b
VZ
2720{
2721}
2722
2723#endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS