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