private.h 30 KB

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