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