]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/private.h
Applied patch #11078 - DEVMODE creation in wxWindowsPrintNativeData::TransferFrom...
[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 HIMETRICToPixel(LONG *x, LONG *y, HDC hdcRef);
293 extern void PixelToHIMETRIC(LONG *x, LONG *y);
294 extern void PixelToHIMETRIC(LONG *x, LONG *y, HDC hdcRef);
295
296 // Windows convention of the mask is opposed to the wxWidgets one, so we need
297 // to invert the mask each time we pass one/get one to/from Windows
298 extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w = 0, int h = 0);
299
300 // Creates an icon or cursor depending from a bitmap
301 //
302 // The bitmap must be valid and it should have a mask. If it doesn't, a default
303 // mask is created using light grey as the transparent colour.
304 extern HICON wxBitmapToHICON(const wxBitmap& bmp);
305
306 // Same requirments as above apply and the bitmap must also have the correct
307 // size.
308 extern
309 HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY);
310
311
312 #if wxUSE_OWNER_DRAWN
313
314 // Draw the bitmap in specified state (this is used by owner drawn controls)
315 enum wxDSBStates
316 {
317 wxDSB_NORMAL = 0,
318 wxDSB_SELECTED,
319 wxDSB_DISABLED
320 };
321
322 extern
323 BOOL wxDrawStateBitmap(HDC hDC, HBITMAP hBitmap, int x, int y, UINT uState);
324
325 #endif // wxUSE_OWNER_DRAWN
326
327 // get (x, y) from DWORD - notice that HI/LOWORD can *not* be used because they
328 // will fail on system with multiple monitors where the coords may be negative
329 //
330 // these macros are standard now (Win98) but some older headers don't have them
331 #ifndef GET_X_LPARAM
332 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
333 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
334 #endif // GET_X_LPARAM
335
336 // get the current state of SHIFT/CTRL/ALT keys
337 inline bool wxIsModifierDown(int vk)
338 {
339 // GetKeyState() returns different negative values on WinME and WinNT,
340 // so simply test for negative value.
341 return ::GetKeyState(vk) < 0;
342 }
343
344 inline bool wxIsShiftDown()
345 {
346 return wxIsModifierDown(VK_SHIFT);
347 }
348
349 inline bool wxIsCtrlDown()
350 {
351 return wxIsModifierDown(VK_CONTROL);
352 }
353
354 inline bool wxIsAltDown()
355 {
356 return wxIsModifierDown(VK_MENU);
357 }
358
359 inline bool wxIsAnyModifierDown()
360 {
361 return wxIsShiftDown() || wxIsCtrlDown() || wxIsAltDown();
362 }
363
364 // wrapper around GetWindowRect() and GetClientRect() APIs doing error checking
365 // for Win32
366 inline RECT wxGetWindowRect(HWND hwnd)
367 {
368 RECT rect;
369
370 if ( !::GetWindowRect(hwnd, &rect) )
371 {
372 wxLogLastError(wxT("GetWindowRect"));
373 }
374
375 return rect;
376 }
377
378 inline RECT wxGetClientRect(HWND hwnd)
379 {
380 RECT rect;
381
382 if ( !::GetClientRect(hwnd, &rect) )
383 {
384 wxLogLastError(wxT("GetClientRect"));
385 }
386
387 return rect;
388 }
389
390 // ---------------------------------------------------------------------------
391 // small helper classes
392 // ---------------------------------------------------------------------------
393
394 // create an instance of this class and use it as the HDC for screen, will
395 // automatically release the DC going out of scope
396 class ScreenHDC
397 {
398 public:
399 ScreenHDC() { m_hdc = ::GetDC(NULL); }
400 ~ScreenHDC() { ::ReleaseDC(NULL, m_hdc); }
401
402 operator HDC() const { return m_hdc; }
403
404 private:
405 HDC m_hdc;
406
407 wxDECLARE_NO_COPY_CLASS(ScreenHDC);
408 };
409
410 // the same as ScreenHDC but for window DCs
411 class WindowHDC
412 {
413 public:
414 WindowHDC(HWND hwnd) { m_hdc = ::GetDC(m_hwnd = hwnd); }
415 ~WindowHDC() { ::ReleaseDC(m_hwnd, m_hdc); }
416
417 operator HDC() const { return m_hdc; }
418
419 private:
420 HWND m_hwnd;
421 HDC m_hdc;
422
423 wxDECLARE_NO_COPY_CLASS(WindowHDC);
424 };
425
426 // the same as ScreenHDC but for memory DCs: creates the HDC compatible with
427 // the given one (screen by default) in ctor and destroys it in dtor
428 class MemoryHDC
429 {
430 public:
431 MemoryHDC(HDC hdc = 0) { m_hdc = ::CreateCompatibleDC(hdc); }
432 ~MemoryHDC() { ::DeleteDC(m_hdc); }
433
434 operator HDC() const { return m_hdc; }
435
436 private:
437 HDC m_hdc;
438
439 wxDECLARE_NO_COPY_CLASS(MemoryHDC);
440 };
441
442 // a class which selects a GDI object into a DC in its ctor and deselects in
443 // dtor
444 class SelectInHDC
445 {
446 private:
447 void DoInit(HGDIOBJ hgdiobj) { m_hgdiobj = ::SelectObject(m_hdc, hgdiobj); }
448
449 public:
450 SelectInHDC() : m_hdc(NULL) { }
451 SelectInHDC(HDC hdc, HGDIOBJ hgdiobj) : m_hdc(hdc) { DoInit(hgdiobj); }
452
453 void Init(HDC hdc, HGDIOBJ hgdiobj)
454 {
455 wxASSERT_MSG( !m_hdc, wxT("initializing twice?") );
456
457 m_hdc = hdc;
458
459 DoInit(hgdiobj);
460 }
461
462 ~SelectInHDC() { if ( m_hdc ) ::SelectObject(m_hdc, m_hgdiobj); }
463
464 // return true if the object was successfully selected
465 operator bool() const { return m_hgdiobj != 0; }
466
467 private:
468 HDC m_hdc;
469 HGDIOBJ m_hgdiobj;
470
471 wxDECLARE_NO_COPY_CLASS(SelectInHDC);
472 };
473
474 // a class which cleans up any GDI object
475 class AutoGDIObject
476 {
477 protected:
478 AutoGDIObject() { m_gdiobj = NULL; }
479 AutoGDIObject(HGDIOBJ gdiobj) : m_gdiobj(gdiobj) { }
480 ~AutoGDIObject() { if ( m_gdiobj ) ::DeleteObject(m_gdiobj); }
481
482 void InitGdiobj(HGDIOBJ gdiobj)
483 {
484 wxASSERT_MSG( !m_gdiobj, wxT("initializing twice?") );
485
486 m_gdiobj = gdiobj;
487 }
488
489 HGDIOBJ GetObject() const { return m_gdiobj; }
490
491 private:
492 HGDIOBJ m_gdiobj;
493 };
494
495 // TODO: all this asks for using a AutoHandler<T, CreateFunc> template...
496
497 // a class for temporary brushes
498 class AutoHBRUSH : private AutoGDIObject
499 {
500 public:
501 AutoHBRUSH(COLORREF col)
502 : AutoGDIObject(::CreateSolidBrush(col)) { }
503
504 operator HBRUSH() const { return (HBRUSH)GetObject(); }
505 };
506
507 // a class for temporary fonts
508 class AutoHFONT : private AutoGDIObject
509 {
510 private:
511 public:
512 AutoHFONT()
513 : AutoGDIObject() { }
514
515 AutoHFONT(const LOGFONT& lf)
516 : AutoGDIObject(::CreateFontIndirect(&lf)) { }
517
518 void Init(const LOGFONT& lf) { InitGdiobj(::CreateFontIndirect(&lf)); }
519
520 operator HFONT() const { return (HFONT)GetObject(); }
521 };
522
523 // a class for temporary pens
524 class AutoHPEN : private AutoGDIObject
525 {
526 public:
527 AutoHPEN(COLORREF col)
528 : AutoGDIObject(::CreatePen(PS_SOLID, 0, col)) { }
529
530 operator HPEN() const { return (HPEN)GetObject(); }
531 };
532
533 // classes for temporary bitmaps
534 class AutoHBITMAP : private AutoGDIObject
535 {
536 public:
537 AutoHBITMAP(HBITMAP hbmp) : AutoGDIObject(hbmp) { }
538
539 operator HBITMAP() const { return (HBITMAP)GetObject(); }
540 };
541
542 class CompatibleBitmap : public AutoHBITMAP
543 {
544 public:
545 CompatibleBitmap(HDC hdc, int w, int h)
546 : AutoHBITMAP(::CreateCompatibleBitmap(hdc, w, h))
547 {
548 }
549 };
550
551 class MonoBitmap : public AutoHBITMAP
552 {
553 public:
554 MonoBitmap(int w, int h)
555 : AutoHBITMAP(::CreateBitmap(w, h, 1, 1, 0))
556 {
557 }
558 };
559
560 // class automatically destroys the region object
561 class AutoHRGN : private AutoGDIObject
562 {
563 public:
564 AutoHRGN(HRGN hrgn) : AutoGDIObject(hrgn) { }
565
566 operator HRGN() const { return (HRGN)GetObject(); }
567 };
568
569 // class sets the specified clipping region during its life time
570 class HDCClipper
571 {
572 public:
573 HDCClipper(HDC hdc, HRGN hrgn)
574 : m_hdc(hdc)
575 {
576 if ( !::SelectClipRgn(hdc, hrgn) )
577 {
578 wxLogLastError(wxT("SelectClipRgn"));
579 }
580 }
581
582 ~HDCClipper()
583 {
584 ::SelectClipRgn(m_hdc, NULL);
585 }
586
587 private:
588 HDC m_hdc;
589
590 wxDECLARE_NO_COPY_CLASS(HDCClipper);
591 };
592
593 // set the given map mode for the life time of this object
594 //
595 // NB: SetMapMode() is not supported by CE so we also define a helper macro
596 // to avoid using it there
597 #ifdef __WXWINCE__
598 #define wxCHANGE_HDC_MAP_MODE(hdc, mm)
599 #else // !__WXWINCE__
600 class HDCMapModeChanger
601 {
602 public:
603 HDCMapModeChanger(HDC hdc, int mm)
604 : m_hdc(hdc)
605 {
606 m_modeOld = ::SetMapMode(hdc, mm);
607 if ( !m_modeOld )
608 {
609 wxLogLastError(wxT("SelectClipRgn"));
610 }
611 }
612
613 ~HDCMapModeChanger()
614 {
615 if ( m_modeOld )
616 ::SetMapMode(m_hdc, m_modeOld);
617 }
618
619 private:
620 HDC m_hdc;
621 int m_modeOld;
622
623 wxDECLARE_NO_COPY_CLASS(HDCMapModeChanger);
624 };
625
626 #define wxCHANGE_HDC_MAP_MODE(hdc, mm) \
627 HDCMapModeChanger wxMAKE_UNIQUE_NAME(wxHDCMapModeChanger)(hdc, mm)
628 #endif // __WXWINCE__/!__WXWINCE__
629
630 // smart pointer using GlobalAlloc/GlobalFree()
631 class GlobalPtr
632 {
633 public:
634 // default ctor, call Init() later
635 GlobalPtr()
636 {
637 m_hGlobal = NULL;
638 }
639
640 // allocates a block of given size
641 void Init(size_t size, unsigned flags = GMEM_MOVEABLE)
642 {
643 m_hGlobal = ::GlobalAlloc(flags, size);
644 if ( !m_hGlobal )
645 {
646 wxLogLastError(wxT("GlobalAlloc"));
647 }
648 }
649
650 GlobalPtr(size_t size, unsigned flags = GMEM_MOVEABLE)
651 {
652 Init(size, flags);
653 }
654
655 ~GlobalPtr()
656 {
657 if ( m_hGlobal && ::GlobalFree(m_hGlobal) )
658 {
659 wxLogLastError(wxT("GlobalFree"));
660 }
661 }
662
663 // implicit conversion
664 operator HGLOBAL() const { return m_hGlobal; }
665
666 private:
667 HGLOBAL m_hGlobal;
668
669 wxDECLARE_NO_COPY_CLASS(GlobalPtr);
670 };
671
672 // when working with global pointers (which is unfortunately still necessary
673 // sometimes, e.g. for clipboard) it is important to unlock them exactly as
674 // many times as we lock them which just asks for using a "smart lock" class
675 class GlobalPtrLock
676 {
677 public:
678 // default ctor, use Init() later -- should only be used if the HGLOBAL can
679 // be NULL (in which case Init() shouldn't be called)
680 GlobalPtrLock()
681 {
682 m_hGlobal = NULL;
683 m_ptr = NULL;
684 }
685
686 // initialize the object, may be only called if we were created using the
687 // default ctor; HGLOBAL must not be NULL
688 void Init(HGLOBAL hGlobal)
689 {
690 m_hGlobal = hGlobal;
691
692 // NB: GlobalLock() is a macro, not a function, hence don't use the
693 // global scope operator with it (and neither with GlobalUnlock())
694 m_ptr = GlobalLock(hGlobal);
695 if ( !m_ptr )
696 {
697 wxLogLastError(wxT("GlobalLock"));
698 }
699 }
700
701 // initialize the object, HGLOBAL must not be NULL
702 GlobalPtrLock(HGLOBAL hGlobal)
703 {
704 Init(hGlobal);
705 }
706
707 ~GlobalPtrLock()
708 {
709 if ( m_hGlobal && !GlobalUnlock(m_hGlobal) )
710 {
711 // this might happen simply because the block became unlocked
712 DWORD dwLastError = ::GetLastError();
713 if ( dwLastError != NO_ERROR )
714 {
715 wxLogApiError(wxT("GlobalUnlock"), dwLastError);
716 }
717 }
718 }
719
720 void *Get() const { return m_ptr; }
721 operator void *() const { return m_ptr; }
722
723 private:
724 HGLOBAL m_hGlobal;
725 void *m_ptr;
726
727 wxDECLARE_NO_COPY_CLASS(GlobalPtrLock);
728 };
729
730 // smart pointer like class using OpenPrinter and ClosePrinter
731 class WinPrinter
732 {
733 public:
734 // default ctor
735 WinPrinter()
736 {
737 m_hPrinter = (HANDLE)NULL;
738 }
739
740 WinPrinter( const wxString& printerName )
741 {
742 Open( printerName );
743 }
744
745 ~WinPrinter()
746 {
747 Close();
748 }
749
750 BOOL Open( const wxString& printerName, LPPRINTER_DEFAULTS pDefault=(LPPRINTER_DEFAULTS)NULL )
751 {
752 Close();
753 return OpenPrinter( (LPTSTR)printerName.wx_str(), &m_hPrinter, pDefault );
754 }
755
756 BOOL Close()
757 {
758 BOOL result = TRUE;
759 if( m_hPrinter )
760 {
761 result = ClosePrinter( m_hPrinter );
762 m_hPrinter = (HANDLE)NULL;
763 }
764 return result;
765 }
766
767 operator HANDLE() { return m_hPrinter; }
768 operator bool() { return m_hPrinter != (HANDLE)NULL; }
769
770 private:
771 HANDLE m_hPrinter;
772
773 wxDECLARE_NO_COPY_CLASS(WinPrinter);
774 };
775
776 // register the class when it is first needed and unregister it in dtor
777 class ClassRegistrar
778 {
779 public:
780 // ctor doesn't register the class, call Initialize() for this
781 ClassRegistrar() { m_registered = -1; }
782
783 // return true if the class is already registered
784 bool IsInitialized() const { return m_registered != -1; }
785
786 // return true if the class had been already registered
787 bool IsRegistered() const { return m_registered == 1; }
788
789 // try to register the class if not done yet, return true on success
790 bool Register(const WNDCLASS& wc)
791 {
792 // we should only be called if we hadn't been initialized yet
793 wxASSERT_MSG( m_registered == -1,
794 wxT("calling ClassRegistrar::Register() twice?") );
795
796 m_registered = ::RegisterClass(&wc) ? 1 : 0;
797 if ( !IsRegistered() )
798 {
799 wxLogLastError(wxT("RegisterClassEx()"));
800 }
801 else
802 {
803 m_clsname = wc.lpszClassName;
804 }
805
806 return m_registered == 1;
807 }
808
809 // get the name of the registered class (returns empty string if not
810 // registered)
811 const wxString& GetName() const { return m_clsname; }
812
813 // unregister the class if it had been registered
814 ~ClassRegistrar()
815 {
816 if ( IsRegistered() )
817 {
818 if ( !::UnregisterClass(m_clsname.wx_str(), wxhInstance) )
819 {
820 wxLogLastError(wxT("UnregisterClass"));
821 }
822 }
823 }
824
825 private:
826 // initial value is -1 which means that we hadn't tried registering the
827 // class yet, it becomes true or false (1 or 0) when Initialize() is called
828 int m_registered;
829
830 // the name of the class, only non empty if it had been registered
831 wxString m_clsname;
832 };
833
834 // ---------------------------------------------------------------------------
835 // macros to make casting between WXFOO and FOO a bit easier: the GetFoo()
836 // returns Foo cast to the Windows type for oruselves, while GetFooOf() takes
837 // an argument which should be a pointer or reference to the object of the
838 // corresponding class (this depends on the macro)
839 // ---------------------------------------------------------------------------
840
841 #define GetHwnd() ((HWND)GetHWND())
842 #define GetHwndOf(win) ((HWND)((win)->GetHWND()))
843 // old name
844 #define GetWinHwnd GetHwndOf
845
846 #define GetHdc() ((HDC)GetHDC())
847 #define GetHdcOf(dc) ((HDC)(dc).GetHDC())
848
849 #define GetHbitmap() ((HBITMAP)GetHBITMAP())
850 #define GetHbitmapOf(bmp) ((HBITMAP)(bmp).GetHBITMAP())
851
852 #define GetHicon() ((HICON)GetHICON())
853 #define GetHiconOf(icon) ((HICON)(icon).GetHICON())
854
855 #define GetHaccel() ((HACCEL)GetHACCEL())
856 #define GetHaccelOf(table) ((HACCEL)((table).GetHACCEL()))
857
858 #define GetHbrush() ((HBRUSH)GetResourceHandle())
859 #define GetHbrushOf(brush) ((HBRUSH)(brush).GetResourceHandle())
860
861 #define GetHmenu() ((HMENU)GetHMenu())
862 #define GetHmenuOf(menu) ((HMENU)(menu)->GetHMenu())
863
864 #define GetHcursor() ((HCURSOR)GetHCURSOR())
865 #define GetHcursorOf(cursor) ((HCURSOR)(cursor).GetHCURSOR())
866
867 #define GetHfont() ((HFONT)GetHFONT())
868 #define GetHfontOf(font) ((HFONT)(font).GetHFONT())
869
870 #define GetHimagelist() ((HIMAGELIST)GetHIMAGELIST())
871 #define GetHimagelistOf(imgl) ((HIMAGELIST)(imgl)->GetHIMAGELIST())
872
873 #define GetHpalette() ((HPALETTE)GetHPALETTE())
874 #define GetHpaletteOf(pal) ((HPALETTE)(pal).GetHPALETTE())
875
876 #define GetHpen() ((HPEN)GetResourceHandle())
877 #define GetHpenOf(pen) ((HPEN)(pen).GetResourceHandle())
878
879 #define GetHrgn() ((HRGN)GetHRGN())
880 #define GetHrgnOf(rgn) ((HRGN)(rgn).GetHRGN())
881
882 #endif // wxUSE_GUI
883
884 // ---------------------------------------------------------------------------
885 // global functions
886 // ---------------------------------------------------------------------------
887
888 extern "C"
889 {
890 WXDLLIMPEXP_BASE HINSTANCE wxGetInstance();
891 }
892
893 WXDLLIMPEXP_BASE void wxSetInstance(HINSTANCE hInst);
894
895 // return the full path of the given module
896 inline wxString wxGetFullModuleName(HMODULE hmod)
897 {
898 wxString fullname;
899 if ( !::GetModuleFileName
900 (
901 hmod,
902 wxStringBuffer(fullname, MAX_PATH),
903 MAX_PATH
904 ) )
905 {
906 wxLogLastError(wxT("GetModuleFileName"));
907 }
908
909 return fullname;
910 }
911
912 // return the full path of the program file
913 inline wxString wxGetFullModuleName()
914 {
915 return wxGetFullModuleName((HMODULE)wxGetInstance());
916 }
917
918 // return the run-time version of the OS in a format similar to
919 // WINVER/_WIN32_WINNT compile-time macros:
920 //
921 // 0x0300 Windows NT 3.51
922 // 0x0400 Windows 95, NT4
923 // 0x0410 Windows 98
924 // 0x0500 Windows ME, 2000
925 // 0x0501 Windows XP
926 // 0x0502 Windows 2003
927 // 0x0600 Longhorn
928 //
929 // for the other Windows versions 0 is currently returned
930 enum wxWinVersion
931 {
932 wxWinVersion_Unknown = 0,
933
934 wxWinVersion_3 = 0x0300,
935 wxWinVersion_NT3 = wxWinVersion_3,
936
937 wxWinVersion_4 = 0x0400,
938 wxWinVersion_95 = wxWinVersion_4,
939 wxWinVersion_NT4 = wxWinVersion_4,
940 wxWinVersion_98 = 0x0410,
941
942 wxWinVersion_5 = 0x0500,
943 wxWinVersion_ME = wxWinVersion_5,
944 wxWinVersion_NT5 = wxWinVersion_5,
945 wxWinVersion_2000 = wxWinVersion_5,
946 wxWinVersion_XP = 0x0501,
947 wxWinVersion_2003 = 0x0502,
948
949 wxWinVersion_6 = 0x0600,
950 wxWinVersion_Vista = wxWinVersion_6,
951 wxWinVersion_NT6 = wxWinVersion_6
952 };
953
954 WXDLLIMPEXP_BASE wxWinVersion wxGetWinVersion();
955
956 #if wxUSE_GUI
957
958 // cursor stuff
959 extern HCURSOR wxGetCurrentBusyCursor(); // from msw/utils.cpp
960 extern const wxCursor *wxGetGlobalCursor(); // from msw/cursor.cpp
961
962 WXDLLIMPEXP_CORE void wxGetCharSize(WXHWND wnd, int *x, int *y, const wxFont& the_font);
963 WXDLLIMPEXP_CORE void wxFillLogFont(LOGFONT *logFont, const wxFont *font);
964 WXDLLIMPEXP_CORE wxFont wxCreateFontFromLogFont(const LOGFONT *logFont);
965 WXDLLIMPEXP_CORE wxFontEncoding wxGetFontEncFromCharSet(int charset);
966
967 WXDLLIMPEXP_CORE void wxSliderEvent(WXHWND control, WXWORD wParam, WXWORD pos);
968 WXDLLIMPEXP_CORE void wxScrollBarEvent(WXHWND hbar, WXWORD wParam, WXWORD pos);
969
970 // Find maximum size of window/rectangle
971 extern WXDLLIMPEXP_CORE void wxFindMaxSize(WXHWND hwnd, RECT *rect);
972
973 // Safely get the window text (i.e. without using fixed size buffer)
974 extern WXDLLIMPEXP_CORE wxString wxGetWindowText(WXHWND hWnd);
975
976 // get the window class name
977 extern WXDLLIMPEXP_CORE wxString wxGetWindowClass(WXHWND hWnd);
978
979 // get the window id (should be unsigned, hence this is not wxWindowID which
980 // is, for mainly historical reasons, signed)
981 extern WXDLLIMPEXP_CORE int wxGetWindowId(WXHWND hWnd);
982
983 // check if hWnd's WNDPROC is wndProc. Return true if yes, false if they are
984 // different
985 extern WXDLLIMPEXP_CORE bool wxCheckWindowWndProc(WXHWND hWnd, WXFARPROC wndProc);
986
987 // Does this window style specify any border?
988 inline bool wxStyleHasBorder(long style)
989 {
990 return (style & (wxSIMPLE_BORDER | wxRAISED_BORDER |
991 wxSUNKEN_BORDER | wxDOUBLE_BORDER)) != 0;
992 }
993
994 inline long wxGetWindowExStyle(const wxWindowMSW *win)
995 {
996 return ::GetWindowLong(GetHwndOf(win), GWL_EXSTYLE);
997 }
998
999 inline bool wxHasWindowExStyle(const wxWindowMSW *win, long style)
1000 {
1001 return (wxGetWindowExStyle(win) & style) != 0;
1002 }
1003
1004 inline long wxSetWindowExStyle(const wxWindowMSW *win, long style)
1005 {
1006 return ::SetWindowLong(GetHwndOf(win), GWL_EXSTYLE, style);
1007 }
1008
1009 // ----------------------------------------------------------------------------
1010 // functions mapping HWND to wxWindow
1011 // ----------------------------------------------------------------------------
1012
1013 // this function simply checks whether the given hwnd corresponds to a wxWindow
1014 // and returns either that window if it does or NULL otherwise
1015 extern WXDLLIMPEXP_CORE wxWindow* wxFindWinFromHandle(HWND hwnd);
1016
1017 // without STRICT WXHWND is the same as HWND anyhow
1018 inline wxWindow* wxFindWinFromHandle(WXHWND hWnd)
1019 {
1020 return wxFindWinFromHandle(static_cast<HWND>(hWnd));
1021 }
1022
1023 // find the window for HWND which is part of some wxWindow, i.e. unlike
1024 // wxFindWinFromHandle() above it will also work for "sub controls" of a
1025 // wxWindow.
1026 //
1027 // returns the wxWindow corresponding to the given HWND or NULL.
1028 extern WXDLLIMPEXP_CORE wxWindow *wxGetWindowFromHWND(WXHWND hwnd);
1029
1030 // Get the size of an icon
1031 extern WXDLLIMPEXP_CORE wxSize wxGetHiconSize(HICON hicon);
1032
1033 // Lines are drawn differently for WinCE and regular WIN32
1034 WXDLLIMPEXP_CORE void wxDrawLine(HDC hdc, int x1, int y1, int x2, int y2);
1035
1036 // fill the client rect of the given window on the provided dc using this brush
1037 inline void wxFillRect(HWND hwnd, HDC hdc, HBRUSH hbr)
1038 {
1039 RECT rc;
1040 ::GetClientRect(hwnd, &rc);
1041 ::FillRect(hdc, &rc, hbr);
1042 }
1043
1044 // ----------------------------------------------------------------------------
1045 // 32/64 bit helpers
1046 // ----------------------------------------------------------------------------
1047
1048 #ifdef __WIN64__
1049
1050 inline void *wxGetWindowProc(HWND hwnd)
1051 {
1052 return (void *)::GetWindowLongPtr(hwnd, GWLP_WNDPROC);
1053 }
1054
1055 inline void *wxGetWindowUserData(HWND hwnd)
1056 {
1057 return (void *)::GetWindowLongPtr(hwnd, GWLP_USERDATA);
1058 }
1059
1060 inline WNDPROC wxSetWindowProc(HWND hwnd, WNDPROC func)
1061 {
1062 return (WNDPROC)::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)func);
1063 }
1064
1065 inline void *wxSetWindowUserData(HWND hwnd, void *data)
1066 {
1067 return (void *)::SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)data);
1068 }
1069
1070 #else // __WIN32__
1071
1072 // note that the casts to LONG_PTR here are required even on 32-bit machines
1073 // for the 64-bit warning mode of later versions of MSVC (C4311/4312)
1074 inline WNDPROC wxGetWindowProc(HWND hwnd)
1075 {
1076 return (WNDPROC)(LONG_PTR)::GetWindowLong(hwnd, GWL_WNDPROC);
1077 }
1078
1079 inline void *wxGetWindowUserData(HWND hwnd)
1080 {
1081 return (void *)(LONG_PTR)::GetWindowLong(hwnd, GWL_USERDATA);
1082 }
1083
1084 inline WNDPROC wxSetWindowProc(HWND hwnd, WNDPROC func)
1085 {
1086 return (WNDPROC)(LONG_PTR)::SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR)func);
1087 }
1088
1089 inline void *wxSetWindowUserData(HWND hwnd, void *data)
1090 {
1091 return (void *)(LONG_PTR)::SetWindowLong(hwnd, GWL_USERDATA, (LONG_PTR)data);
1092 }
1093
1094 #endif // __WIN64__/__WIN32__
1095
1096 #endif // wxUSE_GUI
1097
1098 #endif // _WX_PRIVATE_H_