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