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