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