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