Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
9.9 KiB

  1. #ifndef __standrd_h__
  2. #define __standrd_h__
  3. // Useful macros
  4. #ifndef TRACENOTIMPL
  5. #define TRACENOTIMPL(funcname) TRACE_OUT((_T("%s not implemented.\n"), funcname)); hr = E_NOTIMPL
  6. #endif
  7. #define CONSTANT( x ) enum{ x }
  8. #define CASERET(x) case x: return _T(#x)
  9. #define ClearStruct(lpv) ZeroMemory((LPVOID) (lpv), sizeof(*(lpv)))
  10. #ifndef STRING_RESOURCE_MODULE
  11. # define STRING_RESOURCE_MODULE _Module.GetModuleInstance()
  12. #endif
  13. typedef struct _tagCol {
  14. UINT dwWidth;
  15. LPTSTR lpsz;
  16. } COL;
  17. typedef COL * LPCOL;
  18. inline LPTSTR CopyLPCTSTR( LPCTSTR sz )
  19. {
  20. LPTSTR newString = new TCHAR[ lstrlen( sz ) + 1 ];
  21. if( !lstrcpy( newString, sz ) )
  22. {
  23. // Somethnig fialide
  24. delete [] newString;
  25. newString = NULL;
  26. }
  27. return newString;
  28. }
  29. #ifndef CchMax
  30. #define CchMax(pcsz) (sizeof(pcsz) / sizeof((pcsz)[0]))
  31. #endif // CchMax
  32. #define MAX_RESOURCE_STRING_LEN 256
  33. /* C O N V E R T S Z C H */
  34. /*-------------------------------------------------------------------------
  35. %%Function: ConvertSzCh
  36. Replace every instance of chSrc to chDest in the string
  37. -------------------------------------------------------------------------*/
  38. inline VOID ConvertSzCh(LPTSTR psz, TCHAR chSrc, TCHAR chDest)
  39. {
  40. while (_T('\0') != *psz)
  41. {
  42. if (chSrc == *psz)
  43. {
  44. *psz = chDest;
  45. psz++;
  46. }
  47. else
  48. {
  49. psz = CharNext(psz);
  50. }
  51. }
  52. }
  53. inline VOID ConvertSzCh(LPTSTR psz, TCHAR chSrc = _T('|'), TCHAR chDest = _T('\0'));
  54. inline HRESULT NmCtlLoadString(UINT id, LPTSTR lpsz, UINT cch)
  55. {
  56. HRESULT hr = S_OK;
  57. if( NULL != STRING_RESOURCE_MODULE )
  58. {
  59. if( NULL != lpsz )
  60. {
  61. if (0 == ::LoadString(STRING_RESOURCE_MODULE, id, lpsz, cch))
  62. {
  63. ERROR_OUT(("*** Resource %d does not exist", id));
  64. *lpsz = _T('\0');
  65. hr = HRESULT_FROM_WIN32( ::GetLastError() );
  66. }
  67. }
  68. else
  69. {
  70. ERROR_OUT(("LoadString passed an empty buffer"));
  71. hr = E_INVALIDARG;
  72. }
  73. }
  74. else
  75. {
  76. ERROR_OUT(("LoadString could not find the module"));
  77. hr = E_UNEXPECTED;
  78. }
  79. return hr;
  80. }
  81. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. // this works if you have a member variable CComBSTR m_bstrProp ( substitute Prop for the Prop param )
  83. // Example:
  84. //
  85. // CComBSTR m_bstrName;
  86. //
  87. //
  88. // DECLARE_CCOMBSTR_PROPPUT( Name, DISPID_PROP_NAME );
  89. // DECLARE_CCOMBSTR_PROPGET( Name );
  90. //
  91. #define DECLARE_CCOMBSTR_PROPPUT( Prop, PROP_DISPID ) \
  92. STDMETHOD(put_##Prop)( BSTR newVal ) \
  93. {\
  94. HRESULT hr = S_OK;\
  95. if( S_OK == __ATL_PROP_NOTIFY_EVENT_CLASS::FireOnRequestEdit(GetUnknown(), PROP_DISPID) )\
  96. {\
  97. m_bstr##Prop = newVal;\
  98. hr = __ATL_PROP_NOTIFY_EVENT_CLASS::FireOnChanged(GetUnknown(), PROP_DISPID);\
  99. }\
  100. return hr; \
  101. }
  102. #define DECLARE_CCOMBSTR_PROPGET( Prop ) \
  103. STDMETHOD(get_##Prop)( BSTR *pVal ) \
  104. {\
  105. if( pVal )\
  106. {\
  107. *pVal = m_bstr##Prop.Copy();\
  108. }\
  109. else\
  110. {\
  111. return E_POINTER;\
  112. }\
  113. return S_OK;\
  114. }
  115. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  116. // this works if you have a member variable <class with iterator> m_aryProp ( substitute Prop for the Prop param )
  117. // Example:
  118. //
  119. // lst<IUnknown*> m_AryName;
  120. //
  121. //
  122. // DECLARE_SAFEARRAY_UNK_PROPPUT( Name, DISPID_PROP_NAME );
  123. // DECLARE_SAFEARRAY_UNK_PROPGET( Name );
  124. //
  125. #define DECLARE_SAFEARRAY_UNK_PROPPUT( Prop, PROP_DISPID ) \
  126. STDMETHOD(put_##Prop)( SAFEARRAY newVal ) \
  127. {\
  128. HRESULT hr = S_OK;\
  129. if( S_OK == __ATL_PROP_NOTIFY_EVENT_CLASS::FireOnRequestEdit(GetUnknown(), PROP_DISPID) )\
  130. {\
  131. lst<IUnknown*>::iterator I = m_ary##Prop.begin();\
  132. while( I != m_ary##Prop.end() )\
  133. {\
  134. (*I)->Release();\
  135. ++I;\
  136. }\
  137. m_ary##Prop.erase(m_ary##Prop.begin(), m_ary##Prop.end());\
  138. IUnknown** ppUnkArray;\
  139. SafeArrayAccessData( &newVal, reinterpret_cast<void**>(&ppUnkArray) );\
  140. for (UINT x = 0; x < newVal.rgsabound->cElements; x++)\
  141. {\
  142. IUnknown* pUnk = ppUnkArray[x];\
  143. pUnk->AddRef();\
  144. m_ary##Prop.push_back( pUnk );\
  145. }\
  146. SafeArrayUnaccessData(&newVal);\
  147. hr = __ATL_PROP_NOTIFY_EVENT_CLASS::FireOnChanged(GetUnknown(), PROP_DISPID);\
  148. }\
  149. return hr; \
  150. }
  151. #define DECLARE_SAFEARRAY_UNK_PROPGET( Prop ) \
  152. STDMETHOD(get_##Prop)( SAFEARRAY *pVal ) \
  153. {\
  154. if( pVal )\
  155. {\
  156. int nItems = m_ary##Prop.size();\
  157. SAFEARRAYBOUND bounds = { nItems, 0 };\
  158. pVal = SafeArrayCreate( VT_UNKNOWN, 1, &bounds );\
  159. IUnknown** ppUnkArray;\
  160. SafeArrayAccessData( pVal, reinterpret_cast<void**>(&ppUnkArray) );\
  161. lst<IUnknown*>::iterator I = m_ary##Prop.begin();\
  162. for( int i = 0; I != m_ary##Prop.end(); ++I, ++i )\
  163. {\
  164. ppUnkArray[i] = (*I);\
  165. }\
  166. SafeArrayUnaccessData(pVal);\
  167. }\
  168. else\
  169. {\
  170. return E_POINTER;\
  171. }\
  172. return S_OK;\
  173. }
  174. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  175. // The following DECLARE_PROPXXX macros define functions if you have a member variable that supports
  176. // an issignment operator ( cleaning up memory if need be....
  177. #define DECLARE_PROPPUT( Type, lVal, Prop, PROP_DISPID ) \
  178. STDMETHOD(put_##Prop)( Type newVal ) \
  179. {\
  180. HRESULT hr = S_OK;\
  181. if( S_OK == __ATL_PROP_NOTIFY_EVENT_CLASS::FireOnRequestEdit(GetUnknown(), PROP_DISPID) )\
  182. {\
  183. lVal = newVal;\
  184. hr = __ATL_PROP_NOTIFY_EVENT_CLASS::FireOnChanged(GetUnknown(), PROP_DISPID);\
  185. }\
  186. return hr; \
  187. }
  188. #define DECLARE_PROPGET( Type, rVal, Prop ) \
  189. STDMETHOD(get_##Prop)( Type* pVal )\
  190. {\
  191. if( pVal )\
  192. {\
  193. *pVal = rVal;\
  194. }\
  195. else\
  196. {\
  197. return E_POINTER;\
  198. }\
  199. return S_OK;\
  200. }
  201. inline HRESULT GetTextBoxHeight( HWND hwnd, int* pcy )
  202. {
  203. HRESULT hr = S_OK;
  204. HDC hdc = NULL;
  205. HFONT hSysFont = NULL;
  206. HFONT hOldFont = NULL;
  207. TEXTMETRIC tm;
  208. int HeightOfCurrentFont = 0;
  209. int HeightOfSystemFont = 0;
  210. if( pcy )
  211. {
  212. if( IsWindow( hwnd ) )
  213. {
  214. //get the DC for the control
  215. hdc = GetDC(hwnd);
  216. //get the metrics for the system font
  217. hSysFont = reinterpret_cast<HFONT>(GetStockObject(SYSTEM_FONT));
  218. hOldFont = reinterpret_cast<HFONT>(SelectObject(hdc, hSysFont));
  219. GetTextMetrics(hdc, &tm);
  220. HeightOfSystemFont = tm.tmHeight;
  221. //select the original font back into the DC and release the DC
  222. SelectObject(hdc, hOldFont);
  223. DeleteObject(hSysFont);
  224. GetTextMetrics(hdc, &tm);
  225. ReleaseDC(hwnd, hdc);
  226. HeightOfCurrentFont = tm.tmHeight;
  227. *pcy = HeightOfCurrentFont +
  228. ( min( HeightOfSystemFont, HeightOfCurrentFont ) >> 1 ) +
  229. (GetSystemMetrics(SM_CYEDGE) * 2);
  230. }
  231. else
  232. {
  233. hr = E_INVALIDARG;
  234. }
  235. }
  236. else
  237. {
  238. hr = E_POINTER;
  239. }
  240. return hr;
  241. }
  242. #define RES_CH_MAX 256
  243. inline TCHAR* Res2THelper( UINT uID, TCHAR* psz, int cch )
  244. {
  245. if( LoadString( STRING_RESOURCE_MODULE, uID, psz, cch ) )
  246. {
  247. return psz;
  248. }
  249. return _T("");
  250. }
  251. #ifndef RES2T
  252. #define RES2T(uID) ( Res2THelper( uID, static_cast<TCHAR*>(_alloca( RES_CH_MAX )), RES_CH_MAX ) )
  253. #endif
  254. inline LPCTSTR CommDlgLastErrorToa( DWORD dwErr )
  255. {
  256. switch( dwErr )
  257. {
  258. CASERET ( CDERR_DIALOGFAILURE );
  259. CASERET ( CDERR_GENERALCODES );
  260. CASERET ( CDERR_STRUCTSIZE );
  261. CASERET ( CDERR_INITIALIZATION );
  262. CASERET ( CDERR_NOTEMPLATE );
  263. CASERET ( CDERR_NOHINSTANCE );
  264. CASERET ( CDERR_LOADSTRFAILURE );
  265. CASERET ( CDERR_FINDRESFAILURE );
  266. CASERET ( CDERR_LOADRESFAILURE );
  267. CASERET ( CDERR_LOCKRESFAILURE );
  268. CASERET ( CDERR_MEMALLOCFAILURE );
  269. CASERET ( CDERR_MEMLOCKFAILURE );
  270. CASERET ( CDERR_NOHOOK );
  271. CASERET ( CDERR_REGISTERMSGFAIL );
  272. CASERET ( PDERR_PRINTERCODES );
  273. CASERET ( PDERR_SETUPFAILURE );
  274. CASERET ( PDERR_PARSEFAILURE );
  275. CASERET ( PDERR_RETDEFFAILURE );
  276. CASERET ( PDERR_LOADDRVFAILURE );
  277. CASERET ( PDERR_GETDEVMODEFAIL );
  278. CASERET ( PDERR_INITFAILURE );
  279. CASERET ( PDERR_NODEVICES );
  280. CASERET ( PDERR_NODEFAULTPRN );
  281. CASERET ( PDERR_DNDMMISMATCH );
  282. CASERET ( PDERR_CREATEICFAILURE );
  283. CASERET ( PDERR_PRINTERNOTFOUND );
  284. CASERET ( PDERR_DEFAULTDIFFERENT );
  285. CASERET ( CFERR_CHOOSEFONTCODES );
  286. CASERET ( CFERR_NOFONTS );
  287. CASERET ( CFERR_MAXLESSTHANMIN );
  288. CASERET ( FNERR_FILENAMECODES );
  289. CASERET ( FNERR_SUBCLASSFAILURE );
  290. CASERET ( FNERR_INVALIDFILENAME );
  291. CASERET ( FNERR_BUFFERTOOSMALL );
  292. CASERET ( FRERR_FINDREPLACECODES );
  293. CASERET ( FRERR_BUFFERLENGTHZERO );
  294. CASERET ( CCERR_CHOOSECOLORCODES );
  295. }
  296. return _T("NOERROR");
  297. }
  298. inline void DumpCommDlgLastError()
  299. {
  300. ATLTRACE(_T("CommDlgExtendedError == %s"), CommDlgLastErrorToa( CommDlgExtendedError() ));
  301. }
  302. inline int _Points_From_LogFontHeight( int height, HWND hwnd )
  303. {
  304. HDC hdc = ::GetDC( hwnd );
  305. if( NULL != hdc )
  306. {
  307. int iRet = MulDiv( -height, 72, GetDeviceCaps( hdc, LOGPIXELSY ) );
  308. ::ReleaseDC( hwnd, hdc );
  309. return iRet;
  310. }
  311. return 0;
  312. }
  313. #endif // __standrd_h__