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