]> git.saurik.com Git - wxWidgets.git/blame - include/wx/msw/private.h
Make wxGCDC::GetGraphicsContext() const.
[wxWidgets.git] / include / wx / msw / private.h
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
c0089c96 2// Name: wx/msw/private.h
a23fd0e1 3// Purpose: Private declarations: as this header is only included by
77ffb593 4// wxWidgets itself, it may contain identifiers which don't start
a23fd0e1 5// with "wx".
2bda0e17
KB
6// Author: Julian Smart
7// Modified by:
8// Created: 01/02/97
9// RCS-ID: $Id$
bbcdf8bc 10// Copyright: (c) Julian Smart
65571936 11// Licence: wxWindows licence
2bda0e17
KB
12/////////////////////////////////////////////////////////////////////////////
13
bbcdf8bc
JS
14#ifndef _WX_PRIVATE_H_
15#define _WX_PRIVATE_H_
2bda0e17 16
9ed0d735 17#include "wx/msw/wrapwin.h"
2bda0e17 18
04ef50df 19#ifdef __WXMICROWIN__
b225f659
VZ
20 // Extra prototypes and symbols not defined by MicroWindows
21 #include "wx/msw/microwin.h"
04ef50df
JS
22#endif
23
3a922bb4
RL
24#include "wx/log.h"
25
d66d0500
VZ
26#if wxUSE_GUI
27 #include "wx/window.h"
28#endif // wxUSE_GUI
29
b5dbe15d
VS
30class WXDLLIMPEXP_FWD_CORE wxFont;
31class WXDLLIMPEXP_FWD_CORE wxWindow;
32class WXDLLIMPEXP_FWD_CORE wxWindowBase;
2bda0e17 33
42e69d6b
VZ
34// ---------------------------------------------------------------------------
35// private constants
36// ---------------------------------------------------------------------------
37
77d8d6cd
VZ
38// 260 was taken from windef.h
39#ifndef MAX_PATH
40 #define MAX_PATH 260
41#endif
42
a23fd0e1
VZ
43// ---------------------------------------------------------------------------
44// standard icons from the resources
45// ---------------------------------------------------------------------------
2bda0e17 46
b568d04f
VZ
47#if wxUSE_GUI
48
53a2db12
FM
49extern WXDLLIMPEXP_DATA_CORE(HICON) wxSTD_FRAME_ICON;
50extern WXDLLIMPEXP_DATA_CORE(HICON) wxSTD_MDIPARENTFRAME_ICON;
51extern WXDLLIMPEXP_DATA_CORE(HICON) wxSTD_MDICHILDFRAME_ICON;
52extern WXDLLIMPEXP_DATA_CORE(HICON) wxDEFAULT_FRAME_ICON;
53extern WXDLLIMPEXP_DATA_CORE(HICON) wxDEFAULT_MDIPARENTFRAME_ICON;
54extern WXDLLIMPEXP_DATA_CORE(HICON) wxDEFAULT_MDICHILDFRAME_ICON;
55extern WXDLLIMPEXP_DATA_CORE(HFONT) wxSTATUS_LINE_FONT;
2bda0e17 56
b568d04f
VZ
57#endif // wxUSE_GUI
58
77ec05d4
VZ
59// ---------------------------------------------------------------------------
60// global data
61// ---------------------------------------------------------------------------
62
63extern WXDLLIMPEXP_DATA_BASE(HINSTANCE) wxhInstance;
64
5431e4a6
VZ
65extern "C"
66{
67 WXDLLIMPEXP_BASE HINSTANCE wxGetInstance();
68}
69
70WXDLLIMPEXP_BASE void wxSetInstance(HINSTANCE hInst);
71
a23fd0e1 72// ---------------------------------------------------------------------------
11c7d5b6 73// define things missing from some compilers' headers
a23fd0e1 74// ---------------------------------------------------------------------------
c455ab93 75
7f0586ef 76#if defined(__WXWINCE__) || (defined(__GNUWIN32__) && !wxUSE_NORLANDER_HEADERS)
01dba85a 77#ifndef ZeroMemory
11c7d5b6 78 inline void ZeroMemory(void *buf, size_t len) { memset(buf, 0, len); }
01dba85a 79#endif
11c7d5b6
VZ
80#endif // old mingw32
81
82// this defines a CASTWNDPROC macro which casts a pointer to the type of a
83// window proc
7f0586ef 84#if defined(STRICT) || defined(__GNUC__)
ebb0781f 85 typedef WNDPROC WndProcCast;
c3b177ae 86#else
ebb0781f
VZ
87 typedef FARPROC WndProcCast;
88#endif
89
552a0ebd 90
ebb0781f 91#define CASTWNDPROC (WndProcCast)
2bda0e17 92
dd54f5d3 93
50165591 94
a23fd0e1
VZ
95// ---------------------------------------------------------------------------
96// some stuff for old Windows versions (FIXME: what does it do here??)
97// ---------------------------------------------------------------------------
98
c085e333 99#if !defined(APIENTRY) // NT defines APIENTRY, 3.x not
a23fd0e1 100 #define APIENTRY FAR PASCAL
2bda0e17 101#endif
c085e333 102
2bda0e17 103#ifdef __WIN32__
a23fd0e1 104 #define _EXPORT
2bda0e17 105#else
a23fd0e1
VZ
106 #define _EXPORT _export
107#endif
108
109#ifndef __WIN32__
110 typedef signed short int SHORT;
2bda0e17 111#endif
c085e333
VZ
112
113#if !defined(__WIN32__) // 3.x uses FARPROC for dialogs
27a9bd48 114#ifndef STRICT
a23fd0e1 115 #define DLGPROC FARPROC
2bda0e17 116#endif
27a9bd48 117#endif
2bda0e17 118
2bda0e17
KB
119/*
120 * Decide what window classes we're going to use
121 * for this combination of CTl3D/FAFA settings
122 */
c085e333 123
223d09f6 124#define STATIC_CLASS wxT("STATIC")
2bda0e17 125#define STATIC_FLAGS (SS_LEFT|WS_CHILD|WS_VISIBLE)
223d09f6 126#define CHECK_CLASS wxT("BUTTON")
2bda0e17 127#define CHECK_FLAGS (BS_AUTOCHECKBOX|WS_TABSTOP|WS_CHILD)
078cf5cb 128#define CHECK_IS_FAFA FALSE
223d09f6 129#define RADIO_CLASS wxT("BUTTON")
2bda0e17
KB
130#define RADIO_FLAGS (BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE)
131#define RADIO_SIZE 20
078cf5cb 132#define RADIO_IS_FAFA FALSE
2bda0e17 133#define PURE_WINDOWS
223d09f6 134#define GROUP_CLASS wxT("BUTTON")
2bda0e17
KB
135#define GROUP_FLAGS (BS_GROUPBOX|WS_CHILD|WS_VISIBLE)
136
137/*
138#define BITCHECK_FLAGS (FB_BITMAP|FC_BUTTONDRAW|FC_DEFAULT|WS_VISIBLE)
139#define BITRADIO_FLAGS (FC_BUTTONDRAW|FB_BITMAP|FC_RADIO|WS_CHILD|WS_VISIBLE)
140*/
141
a23fd0e1
VZ
142// ---------------------------------------------------------------------------
143// misc macros
144// ---------------------------------------------------------------------------
145
2bda0e17 146#define MEANING_CHARACTER '0'
5d368213 147#define DEFAULT_ITEM_WIDTH 100
2bda0e17 148#define DEFAULT_ITEM_HEIGHT 80
1c4a764c
VZ
149
150// Scale font to get edit control height
f6bcfd97
BP
151//#define EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) (3*(cy)/2)
152#define EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) (cy+8)
2bda0e17
KB
153
154// Generic subclass proc, for panel item moving/sizing and intercept
155// EDIT control VK_RETURN messages
156extern LONG APIENTRY _EXPORT
157 wxSubclassedGenericControlProc(WXHWND hWnd, WXUINT message, WXWPARAM wParam, WXLPARAM lParam);
158
11c7d5b6
VZ
159// ---------------------------------------------------------------------------
160// useful macros and functions
161// ---------------------------------------------------------------------------
162
163// a wrapper macro for ZeroMemory()
04ef50df 164#if defined(__WIN32__) && !defined(__WXMICROWIN__)
11c7d5b6 165#define wxZeroMemory(obj) ::ZeroMemory(&obj, sizeof(obj))
0e528b99
JS
166#else
167#define wxZeroMemory(obj) memset((void*) & obj, 0, sizeof(obj))
168#endif
11c7d5b6 169
18a1516c
MW
170// This one is a macro so that it can be tested with #ifdef, it will be
171// undefined if it cannot be implemented for a given compiler.
172// Vc++, bcc, dmc, ow, mingw, codewarrior (and rsxnt) have _get_osfhandle.
173// Cygwin has get_osfhandle. Others are currently unknown, e.g. Salford,
174// Intel, Visual Age.
175#if defined(__WXWINCE__)
176 #define wxGetOSFHandle(fd) ((HANDLE)fd)
7d45ec4a 177 #define wxOpenOSFHandle(h, flags) ((int)wxPtrToUInt(h))
18a1516c
MW
178#elif defined(__CYGWIN__)
179 #define wxGetOSFHandle(fd) ((HANDLE)get_osfhandle(fd))
180#elif defined(__VISUALC__) \
181 || defined(__BORLANDC__) \
18a1516c 182 || defined(__DMC__) \
74fe6751 183 || defined(__WATCOMC__) \
8df9a8a0 184 || defined(__MINGW32__) \
18a1516c
MW
185 || (defined(__MWERKS__) && defined(__MSL__))
186 #define wxGetOSFHandle(fd) ((HANDLE)_get_osfhandle(fd))
7d45ec4a
MW
187 #define wxOpenOSFHandle(h, flags) (_open_osfhandle(wxPtrToUInt(h), flags))
188 #define wx_fdopen _fdopen
18a1516c
MW
189#endif
190
0cdd4e19
VZ
191// close the handle in the class dtor
192class AutoHANDLE
193{
194public:
195 wxEXPLICIT AutoHANDLE(HANDLE handle) : m_handle(handle) { }
196
197 bool IsOk() const { return m_handle != INVALID_HANDLE_VALUE; }
198 operator HANDLE() const { return m_handle; }
199
200 ~AutoHANDLE() { if ( IsOk() ) ::CloseHandle(m_handle); }
201
202protected:
203 HANDLE m_handle;
204};
205
d1e4a818
VZ
206// a template to make initializing Windows styructs less painful: it zeroes all
207// the struct fields and also sets cbSize member to the correct value (and so
208// can be only used with structures which have this member...)
209template <class T>
210struct WinStruct : public T
211{
212 WinStruct()
213 {
214 ::ZeroMemory(this, sizeof(T));
215
216 // explicit qualification is required here for this to be valid C++
217 this->cbSize = sizeof(T);
218 }
219};
220
221
f6bcfd97
BP
222#if wxUSE_GUI
223
c0089c96
WS
224#include "wx/gdicmn.h"
225#include "wx/colour.h"
01dba85a 226
11c7d5b6
VZ
227// make conversion from wxColour and COLORREF a bit less painful
228inline COLORREF wxColourToRGB(const wxColour& c)
4ab861e5
VZ
229{
230 return RGB(c.Red(), c.Green(), c.Blue());
231}
232
233inline COLORREF wxColourToPalRGB(const wxColour& c)
11c7d5b6 234{
19193a2c 235 return PALETTERGB(c.Red(), c.Green(), c.Blue());
11c7d5b6
VZ
236}
237
d23c8ba2
VZ
238inline wxColour wxRGBToColour(COLORREF rgb)
239{
240 return wxColour(GetRValue(rgb), GetGValue(rgb), GetBValue(rgb));
241}
242
11c7d5b6
VZ
243inline void wxRGBToColour(wxColour& c, COLORREF rgb)
244{
245 c.Set(GetRValue(rgb), GetGValue(rgb), GetBValue(rgb));
246}
247
90c1530a
VZ
248// get the standard colour map for some standard colours - see comment in this
249// function to understand why is it needed and when should it be used
250//
251// it returns a wxCOLORMAP (can't use COLORMAP itself here as comctl32.dll
252// might be not included/available) array of size wxSTD_COLOUR_MAX
253//
254// NB: if you change these colours, update wxBITMAP_STD_COLOURS in the
255// resources as well: it must have the same number of pixels!
256enum wxSTD_COLOUR
257{
258 wxSTD_COL_BTNTEXT,
259 wxSTD_COL_BTNSHADOW,
260 wxSTD_COL_BTNFACE,
261 wxSTD_COL_BTNHIGHLIGHT,
57e075e9 262 wxSTD_COL_MAX
90c1530a
VZ
263};
264
53a2db12 265struct WXDLLIMPEXP_CORE wxCOLORMAP
90c1530a
VZ
266{
267 COLORREF from, to;
268};
269
270// this function is implemented in src/msw/window.cpp
271extern wxCOLORMAP *wxGetStdColourMap();
272
9688700c 273// create a wxRect from Windows RECT
8704b366 274inline wxRect wxRectFromRECT(const RECT& rc)
9688700c 275{
8704b366 276 return wxRect(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
9688700c
VZ
277}
278
ed791986 279// copy Windows RECT to our wxRect
8704b366 280inline void wxCopyRECTToRect(const RECT& rc, wxRect& rect)
ed791986 281{
8704b366
VZ
282 rect = wxRectFromRECT(rc);
283}
284
285// and vice versa
286inline void wxCopyRectToRECT(const wxRect& rect, RECT& rc)
287{
288 // note that we don't use wxRect::GetRight() as it is one of compared to
289 // wxRectFromRECT() above
290 rc.top = rect.y;
291 rc.left = rect.x;
292 rc.right = rect.x + rect.width;
293 rc.bottom = rect.y + rect.height;
ed791986
VZ
294}
295
d9317fd4
VZ
296// translations between HIMETRIC units (which OLE likes) and pixels (which are
297// liked by all the others) - implemented in msw/utilsexc.cpp
298extern void HIMETRICToPixel(LONG *x, LONG *y);
cc344571 299extern void HIMETRICToPixel(LONG *x, LONG *y, HDC hdcRef);
d9317fd4 300extern void PixelToHIMETRIC(LONG *x, LONG *y);
cc344571 301extern void PixelToHIMETRIC(LONG *x, LONG *y, HDC hdcRef);
d9317fd4 302
77ffb593 303// Windows convention of the mask is opposed to the wxWidgets one, so we need
4b7f2165
VZ
304// to invert the mask each time we pass one/get one to/from Windows
305extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w = 0, int h = 0);
306
211b54b1
VZ
307// Creates an icon or cursor depending from a bitmap
308//
309// The bitmap must be valid and it should have a mask. If it doesn't, a default
310// mask is created using light grey as the transparent colour.
311extern HICON wxBitmapToHICON(const wxBitmap& bmp);
312
313// Same requirments as above apply and the bitmap must also have the correct
314// size.
315extern
316HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY);
317
0a1f7784
VZ
318
319#if wxUSE_OWNER_DRAWN
320
321// Draw the bitmap in specified state (this is used by owner drawn controls)
322enum wxDSBStates
323{
324 wxDSB_NORMAL = 0,
325 wxDSB_SELECTED,
326 wxDSB_DISABLED
327};
328
329extern
330BOOL wxDrawStateBitmap(HDC hDC, HBITMAP hBitmap, int x, int y, UINT uState);
331
332#endif // wxUSE_OWNER_DRAWN
333
8614c467
VZ
334// get (x, y) from DWORD - notice that HI/LOWORD can *not* be used because they
335// will fail on system with multiple monitors where the coords may be negative
336//
337// these macros are standard now (Win98) but some older headers don't have them
338#ifndef GET_X_LPARAM
339 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
340 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
341#endif // GET_X_LPARAM
342
6719c06a
VZ
343// get the current state of SHIFT/CTRL/ALT keys
344inline bool wxIsModifierDown(int vk)
a95e38c0 345{
6719c06a 346 // GetKeyState() returns different negative values on WinME and WinNT,
1fdf858b 347 // so simply test for negative value.
6719c06a
VZ
348 return ::GetKeyState(vk) < 0;
349}
350
351inline bool wxIsShiftDown()
352{
353 return wxIsModifierDown(VK_SHIFT);
a95e38c0
VZ
354}
355
1f80a703 356inline bool wxIsCtrlDown()
a95e38c0 357{
6719c06a
VZ
358 return wxIsModifierDown(VK_CONTROL);
359}
360
361inline bool wxIsAltDown()
362{
363 return wxIsModifierDown(VK_MENU);
364}
365
366inline bool wxIsAnyModifierDown()
367{
368 return wxIsShiftDown() || wxIsCtrlDown() || wxIsAltDown();
a95e38c0
VZ
369}
370
82c9f85c
VZ
371// wrapper around GetWindowRect() and GetClientRect() APIs doing error checking
372// for Win32
373inline RECT wxGetWindowRect(HWND hwnd)
374{
375 RECT rect;
3a5bcc4d 376
82c9f85c 377 if ( !::GetWindowRect(hwnd, &rect) )
43b2d5e7 378 {
9a83f860 379 wxLogLastError(wxT("GetWindowRect"));
43b2d5e7 380 }
82c9f85c
VZ
381
382 return rect;
383}
384
385inline RECT wxGetClientRect(HWND hwnd)
386{
387 RECT rect;
3a5bcc4d 388
82c9f85c 389 if ( !::GetClientRect(hwnd, &rect) )
43b2d5e7 390 {
9a83f860 391 wxLogLastError(wxT("GetClientRect"));
43b2d5e7 392 }
82c9f85c
VZ
393
394 return rect;
395}
396
6d167489
VZ
397// ---------------------------------------------------------------------------
398// small helper classes
399// ---------------------------------------------------------------------------
400
401// create an instance of this class and use it as the HDC for screen, will
402// automatically release the DC going out of scope
403class ScreenHDC
404{
405public:
091225b4
VZ
406 ScreenHDC() { m_hdc = ::GetDC(NULL); }
407 ~ScreenHDC() { ::ReleaseDC(NULL, m_hdc); }
408
409 operator HDC() const { return m_hdc; }
410
411private:
412 HDC m_hdc;
22f3361e 413
c0c133e1 414 wxDECLARE_NO_COPY_CLASS(ScreenHDC);
091225b4
VZ
415};
416
2ff56383
VZ
417// the same as ScreenHDC but for window DCs
418class WindowHDC
419{
420public:
421 WindowHDC(HWND hwnd) { m_hdc = ::GetDC(m_hwnd = hwnd); }
422 ~WindowHDC() { ::ReleaseDC(m_hwnd, m_hdc); }
423
424 operator HDC() const { return m_hdc; }
425
426private:
427 HWND m_hwnd;
428 HDC m_hdc;
429
c0c133e1 430 wxDECLARE_NO_COPY_CLASS(WindowHDC);
2ff56383
VZ
431};
432
697b08a8
VZ
433// the same as ScreenHDC but for memory DCs: creates the HDC compatible with
434// the given one (screen by default) in ctor and destroys it in dtor
091225b4
VZ
435class MemoryHDC
436{
437public:
9cf743aa 438 MemoryHDC(HDC hdc = 0) { m_hdc = ::CreateCompatibleDC(hdc); }
697b08a8 439 ~MemoryHDC() { ::DeleteDC(m_hdc); }
091225b4
VZ
440
441 operator HDC() const { return m_hdc; }
6d167489
VZ
442
443private:
444 HDC m_hdc;
22f3361e 445
c0c133e1 446 wxDECLARE_NO_COPY_CLASS(MemoryHDC);
6d167489
VZ
447};
448
091225b4
VZ
449// a class which selects a GDI object into a DC in its ctor and deselects in
450// dtor
451class SelectInHDC
452{
6969c2c3
VZ
453private:
454 void DoInit(HGDIOBJ hgdiobj) { m_hgdiobj = ::SelectObject(m_hdc, hgdiobj); }
455
091225b4 456public:
b02d5028 457 SelectInHDC() : m_hdc(NULL), m_hgdiobj(NULL) { }
6969c2c3
VZ
458 SelectInHDC(HDC hdc, HGDIOBJ hgdiobj) : m_hdc(hdc) { DoInit(hgdiobj); }
459
460 void Init(HDC hdc, HGDIOBJ hgdiobj)
461 {
9a83f860 462 wxASSERT_MSG( !m_hdc, wxT("initializing twice?") );
6969c2c3
VZ
463
464 m_hdc = hdc;
091225b4 465
6969c2c3
VZ
466 DoInit(hgdiobj);
467 }
091225b4 468
6969c2c3
VZ
469 ~SelectInHDC() { if ( m_hdc ) ::SelectObject(m_hdc, m_hgdiobj); }
470
471 // return true if the object was successfully selected
472 operator bool() const { return m_hgdiobj != 0; }
091225b4
VZ
473
474private:
6969c2c3
VZ
475 HDC m_hdc;
476 HGDIOBJ m_hgdiobj;
22f3361e 477
c0c133e1 478 wxDECLARE_NO_COPY_CLASS(SelectInHDC);
51a2a728
VZ
479};
480
a27cbf44
VZ
481// a class which cleans up any GDI object
482class AutoGDIObject
483{
484protected:
6969c2c3 485 AutoGDIObject() { m_gdiobj = NULL; }
a27cbf44
VZ
486 AutoGDIObject(HGDIOBJ gdiobj) : m_gdiobj(gdiobj) { }
487 ~AutoGDIObject() { if ( m_gdiobj ) ::DeleteObject(m_gdiobj); }
488
6969c2c3
VZ
489 void InitGdiobj(HGDIOBJ gdiobj)
490 {
9a83f860 491 wxASSERT_MSG( !m_gdiobj, wxT("initializing twice?") );
6969c2c3
VZ
492
493 m_gdiobj = gdiobj;
494 }
495
a27cbf44
VZ
496 HGDIOBJ GetObject() const { return m_gdiobj; }
497
498private:
499 HGDIOBJ m_gdiobj;
500};
501
cf831d8e
VZ
502// TODO: all this asks for using a AutoHandler<T, CreateFunc> template...
503
6969c2c3 504// a class for temporary brushes
cf831d8e
VZ
505class AutoHBRUSH : private AutoGDIObject
506{
507public:
508 AutoHBRUSH(COLORREF col)
509 : AutoGDIObject(::CreateSolidBrush(col)) { }
510
511 operator HBRUSH() const { return (HBRUSH)GetObject(); }
512};
513
6969c2c3
VZ
514// a class for temporary fonts
515class AutoHFONT : private AutoGDIObject
516{
517private:
518public:
519 AutoHFONT()
520 : AutoGDIObject() { }
521
522 AutoHFONT(const LOGFONT& lf)
523 : AutoGDIObject(::CreateFontIndirect(&lf)) { }
524
525 void Init(const LOGFONT& lf) { InitGdiobj(::CreateFontIndirect(&lf)); }
526
527 operator HFONT() const { return (HFONT)GetObject(); }
528};
529
cf831d8e
VZ
530// a class for temporary pens
531class AutoHPEN : private AutoGDIObject
532{
533public:
534 AutoHPEN(COLORREF col)
535 : AutoGDIObject(::CreatePen(PS_SOLID, 0, col)) { }
536
537 operator HPEN() const { return (HPEN)GetObject(); }
538};
539
540// classes for temporary bitmaps
541class AutoHBITMAP : private AutoGDIObject
542{
543public:
a72ebcd0
VZ
544 AutoHBITMAP()
545 : AutoGDIObject() { }
546
cf831d8e
VZ
547 AutoHBITMAP(HBITMAP hbmp) : AutoGDIObject(hbmp) { }
548
a72ebcd0
VZ
549 void Init(HBITMAP hbmp) { InitGdiobj(hbmp); }
550
cf831d8e
VZ
551 operator HBITMAP() const { return (HBITMAP)GetObject(); }
552};
553
554class CompatibleBitmap : public AutoHBITMAP
978bb48f
VZ
555{
556public:
557 CompatibleBitmap(HDC hdc, int w, int h)
cf831d8e 558 : AutoHBITMAP(::CreateCompatibleBitmap(hdc, w, h))
978bb48f 559 {
978bb48f 560 }
cf831d8e 561};
978bb48f 562
cf831d8e
VZ
563class MonoBitmap : public AutoHBITMAP
564{
565public:
566 MonoBitmap(int w, int h)
567 : AutoHBITMAP(::CreateBitmap(w, h, 1, 1, 0))
568 {
569 }
a27cbf44 570};
978bb48f 571
a27cbf44
VZ
572// class automatically destroys the region object
573class AutoHRGN : private AutoGDIObject
574{
575public:
576 AutoHRGN(HRGN hrgn) : AutoGDIObject(hrgn) { }
978bb48f 577
a27cbf44 578 operator HRGN() const { return (HRGN)GetObject(); }
978bb48f
VZ
579};
580
a1372ae2
VZ
581// class sets the specified clipping region during its life time
582class HDCClipper
583{
584public:
585 HDCClipper(HDC hdc, HRGN hrgn)
586 : m_hdc(hdc)
587 {
588 if ( !::SelectClipRgn(hdc, hrgn) )
43b2d5e7 589 {
9a83f860 590 wxLogLastError(wxT("SelectClipRgn"));
43b2d5e7 591 }
a1372ae2
VZ
592 }
593
594 ~HDCClipper()
595 {
596 ::SelectClipRgn(m_hdc, NULL);
597 }
598
599private:
600 HDC m_hdc;
601
c0c133e1 602 wxDECLARE_NO_COPY_CLASS(HDCClipper);
a1372ae2
VZ
603};
604
566b7f7b
VZ
605// set the given map mode for the life time of this object
606//
607// NB: SetMapMode() is not supported by CE so we also define a helper macro
608// to avoid using it there
609#ifdef __WXWINCE__
610 #define wxCHANGE_HDC_MAP_MODE(hdc, mm)
611#else // !__WXWINCE__
612 class HDCMapModeChanger
613 {
614 public:
615 HDCMapModeChanger(HDC hdc, int mm)
616 : m_hdc(hdc)
617 {
618 m_modeOld = ::SetMapMode(hdc, mm);
619 if ( !m_modeOld )
43b2d5e7 620 {
9a83f860 621 wxLogLastError(wxT("SelectClipRgn"));
43b2d5e7 622 }
566b7f7b
VZ
623 }
624
625 ~HDCMapModeChanger()
626 {
627 if ( m_modeOld )
628 ::SetMapMode(m_hdc, m_modeOld);
629 }
630
631 private:
632 HDC m_hdc;
633 int m_modeOld;
634
c0c133e1 635 wxDECLARE_NO_COPY_CLASS(HDCMapModeChanger);
566b7f7b
VZ
636 };
637
638 #define wxCHANGE_HDC_MAP_MODE(hdc, mm) \
639 HDCMapModeChanger wxMAKE_UNIQUE_NAME(wxHDCMapModeChanger)(hdc, mm)
640#endif // __WXWINCE__/!__WXWINCE__
641
82d1a195 642// smart pointer using GlobalAlloc/GlobalFree()
2be57a51
VZ
643class GlobalPtr
644{
645public:
6fa6d659
VZ
646 // default ctor, call Init() later
647 GlobalPtr()
648 {
649 m_hGlobal = NULL;
650 }
651
2be57a51 652 // allocates a block of given size
6fa6d659 653 void Init(size_t size, unsigned flags = GMEM_MOVEABLE)
2be57a51
VZ
654 {
655 m_hGlobal = ::GlobalAlloc(flags, size);
656 if ( !m_hGlobal )
43b2d5e7 657 {
9a83f860 658 wxLogLastError(wxT("GlobalAlloc"));
43b2d5e7 659 }
2be57a51
VZ
660 }
661
6fa6d659
VZ
662 GlobalPtr(size_t size, unsigned flags = GMEM_MOVEABLE)
663 {
664 Init(size, flags);
665 }
666
2be57a51
VZ
667 ~GlobalPtr()
668 {
669 if ( m_hGlobal && ::GlobalFree(m_hGlobal) )
43b2d5e7 670 {
9a83f860 671 wxLogLastError(wxT("GlobalFree"));
43b2d5e7 672 }
2be57a51
VZ
673 }
674
675 // implicit conversion
676 operator HGLOBAL() const { return m_hGlobal; }
677
678private:
679 HGLOBAL m_hGlobal;
680
c0c133e1 681 wxDECLARE_NO_COPY_CLASS(GlobalPtr);
2be57a51
VZ
682};
683
51a2a728
VZ
684// when working with global pointers (which is unfortunately still necessary
685// sometimes, e.g. for clipboard) it is important to unlock them exactly as
686// many times as we lock them which just asks for using a "smart lock" class
0645e2b9 687class GlobalPtrLock
51a2a728
VZ
688{
689public:
2d2b68ba
VZ
690 // default ctor, use Init() later -- should only be used if the HGLOBAL can
691 // be NULL (in which case Init() shouldn't be called)
692 GlobalPtrLock()
51a2a728 693 {
2d2b68ba
VZ
694 m_hGlobal = NULL;
695 m_ptr = NULL;
696 }
697
698 // initialize the object, may be only called if we were created using the
699 // default ctor; HGLOBAL must not be NULL
700 void Init(HGLOBAL hGlobal)
701 {
702 m_hGlobal = hGlobal;
82d1a195
VZ
703
704 // NB: GlobalLock() is a macro, not a function, hence don't use the
705 // global scope operator with it (and neither with GlobalUnlock())
706 m_ptr = GlobalLock(hGlobal);
51a2a728 707 if ( !m_ptr )
43b2d5e7 708 {
9a83f860 709 wxLogLastError(wxT("GlobalLock"));
43b2d5e7 710 }
51a2a728
VZ
711 }
712
2d2b68ba
VZ
713 // initialize the object, HGLOBAL must not be NULL
714 GlobalPtrLock(HGLOBAL hGlobal)
715 {
716 Init(hGlobal);
717 }
718
0645e2b9 719 ~GlobalPtrLock()
51a2a728 720 {
82d1a195 721 if ( m_hGlobal && !GlobalUnlock(m_hGlobal) )
51a2a728 722 {
51a2a728
VZ
723 // this might happen simply because the block became unlocked
724 DWORD dwLastError = ::GetLastError();
725 if ( dwLastError != NO_ERROR )
726 {
9a83f860 727 wxLogApiError(wxT("GlobalUnlock"), dwLastError);
51a2a728 728 }
51a2a728
VZ
729 }
730 }
731
2d2b68ba 732 void *Get() const { return m_ptr; }
51a2a728
VZ
733 operator void *() const { return m_ptr; }
734
735private:
736 HGLOBAL m_hGlobal;
737 void *m_ptr;
738
c0c133e1 739 wxDECLARE_NO_COPY_CLASS(GlobalPtrLock);
091225b4
VZ
740};
741
77ec05d4
VZ
742// register the class when it is first needed and unregister it in dtor
743class ClassRegistrar
744{
745public:
746 // ctor doesn't register the class, call Initialize() for this
747 ClassRegistrar() { m_registered = -1; }
748
749 // return true if the class is already registered
750 bool IsInitialized() const { return m_registered != -1; }
751
752 // return true if the class had been already registered
753 bool IsRegistered() const { return m_registered == 1; }
754
755 // try to register the class if not done yet, return true on success
756 bool Register(const WNDCLASS& wc)
757 {
758 // we should only be called if we hadn't been initialized yet
759 wxASSERT_MSG( m_registered == -1,
9a83f860 760 wxT("calling ClassRegistrar::Register() twice?") );
77ec05d4
VZ
761
762 m_registered = ::RegisterClass(&wc) ? 1 : 0;
763 if ( !IsRegistered() )
764 {
9a83f860 765 wxLogLastError(wxT("RegisterClassEx()"));
77ec05d4
VZ
766 }
767 else
768 {
769 m_clsname = wc.lpszClassName;
770 }
771
772 return m_registered == 1;
773 }
774
775 // get the name of the registered class (returns empty string if not
776 // registered)
777 const wxString& GetName() const { return m_clsname; }
778
779 // unregister the class if it had been registered
780 ~ClassRegistrar()
781 {
782 if ( IsRegistered() )
783 {
5431e4a6 784 if ( !::UnregisterClass(m_clsname.wx_str(), wxGetInstance()) )
77ec05d4 785 {
9a83f860 786 wxLogLastError(wxT("UnregisterClass"));
77ec05d4
VZ
787 }
788 }
789 }
790
791private:
792 // initial value is -1 which means that we hadn't tried registering the
793 // class yet, it becomes true or false (1 or 0) when Initialize() is called
794 int m_registered;
795
796 // the name of the class, only non empty if it had been registered
797 wxString m_clsname;
798};
799
a23fd0e1 800// ---------------------------------------------------------------------------
42e69d6b 801// macros to make casting between WXFOO and FOO a bit easier: the GetFoo()
c50f1fb9
VZ
802// returns Foo cast to the Windows type for oruselves, while GetFooOf() takes
803// an argument which should be a pointer or reference to the object of the
804// corresponding class (this depends on the macro)
a23fd0e1
VZ
805// ---------------------------------------------------------------------------
806
807#define GetHwnd() ((HWND)GetHWND())
c50f1fb9
VZ
808#define GetHwndOf(win) ((HWND)((win)->GetHWND()))
809// old name
810#define GetWinHwnd GetHwndOf
a23fd0e1
VZ
811
812#define GetHdc() ((HDC)GetHDC())
c50f1fb9 813#define GetHdcOf(dc) ((HDC)(dc).GetHDC())
a23fd0e1 814
11c7d5b6
VZ
815#define GetHbitmap() ((HBITMAP)GetHBITMAP())
816#define GetHbitmapOf(bmp) ((HBITMAP)(bmp).GetHBITMAP())
817
42e69d6b 818#define GetHicon() ((HICON)GetHICON())
c50f1fb9 819#define GetHiconOf(icon) ((HICON)(icon).GetHICON())
42e69d6b 820
a23fd0e1 821#define GetHaccel() ((HACCEL)GetHACCEL())
c50f1fb9
VZ
822#define GetHaccelOf(table) ((HACCEL)((table).GetHACCEL()))
823
a27cbf44
VZ
824#define GetHbrush() ((HBRUSH)GetResourceHandle())
825#define GetHbrushOf(brush) ((HBRUSH)(brush).GetResourceHandle())
ee50eab8 826
c50f1fb9 827#define GetHmenu() ((HMENU)GetHMenu())
28ac82bf 828#define GetHmenuOf(menu) ((HMENU)(menu)->GetHMenu())
a23fd0e1 829
bfbd6dc1
VZ
830#define GetHcursor() ((HCURSOR)GetHCURSOR())
831#define GetHcursorOf(cursor) ((HCURSOR)(cursor).GetHCURSOR())
832
f6bcfd97
BP
833#define GetHfont() ((HFONT)GetHFONT())
834#define GetHfontOf(font) ((HFONT)(font).GetHFONT())
835
f6840be6 836#define GetHimagelist() ((HIMAGELIST)GetHIMAGELIST())
28ac82bf 837#define GetHimagelistOf(imgl) ((HIMAGELIST)(imgl)->GetHIMAGELIST())
f6840be6 838
1816a070 839#define GetHpalette() ((HPALETTE)GetHPALETTE())
b95edd47 840#define GetHpaletteOf(pal) ((HPALETTE)(pal).GetHPALETTE())
1816a070 841
75776373
VZ
842#define GetHpen() ((HPEN)GetResourceHandle())
843#define GetHpenOf(pen) ((HPEN)(pen).GetResourceHandle())
844
1e6feb95
VZ
845#define GetHrgn() ((HRGN)GetHRGN())
846#define GetHrgnOf(rgn) ((HRGN)(rgn).GetHRGN())
847
f6bcfd97
BP
848#endif // wxUSE_GUI
849
a23fd0e1
VZ
850// ---------------------------------------------------------------------------
851// global functions
852// ---------------------------------------------------------------------------
853
25b0e7c8
VZ
854// return the full path of the given module
855inline wxString wxGetFullModuleName(HMODULE hmod)
77d8d6cd
VZ
856{
857 wxString fullname;
858 if ( !::GetModuleFileName
859 (
25b0e7c8 860 hmod,
77d8d6cd
VZ
861 wxStringBuffer(fullname, MAX_PATH),
862 MAX_PATH
863 ) )
864 {
9a83f860 865 wxLogLastError(wxT("GetModuleFileName"));
77d8d6cd
VZ
866 }
867
868 return fullname;
869}
870
25b0e7c8
VZ
871// return the full path of the program file
872inline wxString wxGetFullModuleName()
873{
874 return wxGetFullModuleName((HMODULE)wxGetInstance());
875}
876
4c5da5e4
VZ
877// return the run-time version of the OS in a format similar to
878// WINVER/_WIN32_WINNT compile-time macros:
879//
880// 0x0300 Windows NT 3.51
881// 0x0400 Windows 95, NT4
882// 0x0410 Windows 98
883// 0x0500 Windows ME, 2000
6e60d56a
VZ
884// 0x0501 Windows XP, 2003
885// 0x0502 Windows XP SP2, 2003 SP1
886// 0x0600 Windows Vista, 2008
887// 0x0601 Windows 7
4c5da5e4
VZ
888//
889// for the other Windows versions 0 is currently returned
890enum wxWinVersion
891{
892 wxWinVersion_Unknown = 0,
893
894 wxWinVersion_3 = 0x0300,
895 wxWinVersion_NT3 = wxWinVersion_3,
896
897 wxWinVersion_4 = 0x0400,
898 wxWinVersion_95 = wxWinVersion_4,
899 wxWinVersion_NT4 = wxWinVersion_4,
900 wxWinVersion_98 = 0x0410,
901
902 wxWinVersion_5 = 0x0500,
903 wxWinVersion_ME = wxWinVersion_5,
904 wxWinVersion_NT5 = wxWinVersion_5,
905 wxWinVersion_2000 = wxWinVersion_5,
906 wxWinVersion_XP = 0x0501,
6e60d56a
VZ
907 wxWinVersion_2003 = 0x0501,
908 wxWinVersion_XP_SP2 = 0x0502,
909 wxWinVersion_2003_SP1 = 0x0502,
4c5da5e4
VZ
910
911 wxWinVersion_6 = 0x0600,
0df7cb7e 912 wxWinVersion_Vista = wxWinVersion_6,
6e60d56a
VZ
913 wxWinVersion_NT6 = wxWinVersion_6,
914
915 wxWinVersion_7 = 0x601
4c5da5e4
VZ
916};
917
be2572a3 918WXDLLIMPEXP_BASE wxWinVersion wxGetWinVersion();
4c5da5e4 919
b568d04f
VZ
920#if wxUSE_GUI
921
bfbd6dc1
VZ
922// cursor stuff
923extern HCURSOR wxGetCurrentBusyCursor(); // from msw/utils.cpp
924extern const wxCursor *wxGetGlobalCursor(); // from msw/cursor.cpp
925
53a2db12
FM
926WXDLLIMPEXP_CORE void wxGetCharSize(WXHWND wnd, int *x, int *y, const wxFont& the_font);
927WXDLLIMPEXP_CORE void wxFillLogFont(LOGFONT *logFont, const wxFont *font);
928WXDLLIMPEXP_CORE wxFont wxCreateFontFromLogFont(const LOGFONT *logFont);
929WXDLLIMPEXP_CORE wxFontEncoding wxGetFontEncFromCharSet(int charset);
a23fd0e1 930
53a2db12
FM
931WXDLLIMPEXP_CORE void wxSliderEvent(WXHWND control, WXWORD wParam, WXWORD pos);
932WXDLLIMPEXP_CORE void wxScrollBarEvent(WXHWND hbar, WXWORD wParam, WXWORD pos);
a23fd0e1
VZ
933
934// Find maximum size of window/rectangle
53a2db12 935extern WXDLLIMPEXP_CORE void wxFindMaxSize(WXHWND hwnd, RECT *rect);
a23fd0e1 936
1c4a764c 937// Safely get the window text (i.e. without using fixed size buffer)
53a2db12 938extern WXDLLIMPEXP_CORE wxString wxGetWindowText(WXHWND hWnd);
1c4a764c 939
cc2b7472 940// get the window class name
53a2db12 941extern WXDLLIMPEXP_CORE wxString wxGetWindowClass(WXHWND hWnd);
cc2b7472 942
42e69d6b
VZ
943// get the window id (should be unsigned, hence this is not wxWindowID which
944// is, for mainly historical reasons, signed)
53a2db12 945extern WXDLLIMPEXP_CORE int wxGetWindowId(WXHWND hWnd);
cc2b7472 946
ae304744
VZ
947// check if hWnd's WNDPROC is wndProc. Return true if yes, false if they are
948// different
53a2db12 949extern WXDLLIMPEXP_CORE bool wxCheckWindowWndProc(WXHWND hWnd, WXFARPROC wndProc);
eb5e4d9a 950
c085e333 951// Does this window style specify any border?
184b5d99 952inline bool wxStyleHasBorder(long style)
c085e333 953{
82c9f85c
VZ
954 return (style & (wxSIMPLE_BORDER | wxRAISED_BORDER |
955 wxSUNKEN_BORDER | wxDOUBLE_BORDER)) != 0;
c085e333
VZ
956}
957
d026fee4 958inline long wxGetWindowExStyle(const wxWindowMSW *win)
d66d0500
VZ
959{
960 return ::GetWindowLong(GetHwndOf(win), GWL_EXSTYLE);
961}
962
d026fee4 963inline bool wxHasWindowExStyle(const wxWindowMSW *win, long style)
d66d0500
VZ
964{
965 return (wxGetWindowExStyle(win) & style) != 0;
966}
967
d026fee4 968inline long wxSetWindowExStyle(const wxWindowMSW *win, long style)
d66d0500
VZ
969{
970 return ::SetWindowLong(GetHwndOf(win), GWL_EXSTYLE, style);
971}
972
ae304744
VZ
973// ----------------------------------------------------------------------------
974// functions mapping HWND to wxWindow
975// ----------------------------------------------------------------------------
976
dca0f651 977// this function simply checks whether the given hwnd corresponds to a wxWindow
ae304744 978// and returns either that window if it does or NULL otherwise
53a2db12 979extern WXDLLIMPEXP_CORE wxWindow* wxFindWinFromHandle(HWND hwnd);
dca0f651 980
ae304744
VZ
981// find the window for HWND which is part of some wxWindow, i.e. unlike
982// wxFindWinFromHandle() above it will also work for "sub controls" of a
983// wxWindow.
8614c467 984//
ae304744 985// returns the wxWindow corresponding to the given HWND or NULL.
53a2db12 986extern WXDLLIMPEXP_CORE wxWindow *wxGetWindowFromHWND(WXHWND hwnd);
8614c467 987
6bad4c32 988// Get the size of an icon
53a2db12 989extern WXDLLIMPEXP_CORE wxSize wxGetHiconSize(HICON hicon);
6bad4c32 990
086b3a5b 991// Lines are drawn differently for WinCE and regular WIN32
53a2db12 992WXDLLIMPEXP_CORE void wxDrawLine(HDC hdc, int x1, int y1, int x2, int y2);
086b3a5b 993
c3732409
VZ
994// fill the client rect of the given window on the provided dc using this brush
995inline void wxFillRect(HWND hwnd, HDC hdc, HBRUSH hbr)
996{
997 RECT rc;
998 ::GetClientRect(hwnd, &rc);
999 ::FillRect(hdc, &rc, hbr);
1000}
1001
5171ea0e
VZ
1002// ----------------------------------------------------------------------------
1003// 32/64 bit helpers
1004// ----------------------------------------------------------------------------
1005
1006#ifdef __WIN64__
1007
1008inline void *wxGetWindowProc(HWND hwnd)
1009{
1010 return (void *)::GetWindowLongPtr(hwnd, GWLP_WNDPROC);
1011}
1012
1013inline void *wxGetWindowUserData(HWND hwnd)
1014{
1015 return (void *)::GetWindowLongPtr(hwnd, GWLP_USERDATA);
1016}
1017
1018inline WNDPROC wxSetWindowProc(HWND hwnd, WNDPROC func)
1019{
1020 return (WNDPROC)::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)func);
1021}
1022
1023inline void *wxSetWindowUserData(HWND hwnd, void *data)
1024{
1025 return (void *)::SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)data);
1026}
1027
1028#else // __WIN32__
1029
54892273
VZ
1030// note that the casts to LONG_PTR here are required even on 32-bit machines
1031// for the 64-bit warning mode of later versions of MSVC (C4311/4312)
315a49a1 1032inline WNDPROC wxGetWindowProc(HWND hwnd)
5171ea0e 1033{
315a49a1 1034 return (WNDPROC)(LONG_PTR)::GetWindowLong(hwnd, GWL_WNDPROC);
5171ea0e
VZ
1035}
1036
1037inline void *wxGetWindowUserData(HWND hwnd)
1038{
54892273 1039 return (void *)(LONG_PTR)::GetWindowLong(hwnd, GWL_USERDATA);
5171ea0e
VZ
1040}
1041
1042inline WNDPROC wxSetWindowProc(HWND hwnd, WNDPROC func)
1043{
54892273 1044 return (WNDPROC)(LONG_PTR)::SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR)func);
5171ea0e
VZ
1045}
1046
1047inline void *wxSetWindowUserData(HWND hwnd, void *data)
1048{
54892273 1049 return (void *)(LONG_PTR)::SetWindowLong(hwnd, GWL_USERDATA, (LONG_PTR)data);
5171ea0e
VZ
1050}
1051
1052#endif // __WIN64__/__WIN32__
1053
b568d04f
VZ
1054#endif // wxUSE_GUI
1055
a27cbf44 1056#endif // _WX_PRIVATE_H_