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