Leaked source code of windows server 2003
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.

333 lines
13 KiB

  1. //---------------------------------------------------------------------------
  2. // utils.h - theme code utilities
  3. //---------------------------------------------------------------------------
  4. #pragma once
  5. //---------------------------------------------------------------------------
  6. #include <uxthemep.h>
  7. #include <commctrl.h>
  8. #include <math.h>
  9. //---------------------------------------------------------------------------
  10. #define THEMEDLL_EXT L".msstyles"
  11. #define DEFAULT_THEME L".\\luna\\luna.msstyles"
  12. #define CONTAINER_NAME L"themes.ini"
  13. #define CONTAINER_RESNAME L"themes_ini"
  14. #define USUAL_CLASSDATA_NAME L"default.ini"
  15. //---------------------------------------------------------------------------
  16. #define RESOURCE // marks vars as being needed to be freed at block exit
  17. //---------------------------------------------------------------------------
  18. #ifdef DEBUG
  19. #define _DEBUG
  20. inline int _DebugExceptionFilter( LONG ecode, EXCEPTION_POINTERS* pep, LPSTR pszMsg, LONG lExceptionRet )
  21. {
  22. CHAR szBuf[512];
  23. #ifdef _STRSAFE_H_INCLUDED_
  24. StringCchPrintfA(szBuf, sizeof(szBuf)/sizeof(*szBuf),
  25. #else _STRSAFE_H_INCLUDED_
  26. wsprintfA(szBuf,
  27. #endif _STRSAFE_H_INCLUDED_
  28. "\n%s\nEXCEPTION INFO: code: %08lx, record (.exr): %08lx, context (.cxr): %08lx\n\n",
  29. pszMsg, ecode, pep->ExceptionRecord, pep->ContextRecord);
  30. OutputDebugStringA(szBuf);
  31. return lExceptionRet;
  32. }
  33. #define DEBUG_TRY() __try {
  34. #define DEBUG_EXCEPT(pszAssertMsg) } __except(_DebugExceptionFilter(_exception_code(), (EXCEPTION_POINTERS*)_exception_info(), \
  35. pszAssertMsg, EXCEPTION_EXECUTE_HANDLER)) {\
  36. DebugBreak();}
  37. #else // DEBUG
  38. #define DEBUG_TRY()
  39. #define DEBUG_EXCEPT(pszAssertMsg)
  40. #endif // DEBUG
  41. //---------------------------------------------------------------------------
  42. #define COMBOENTRY(combo, color, size) \
  43. (combo->sFileNums[size*combo->cColorSchemes + color])
  44. //---------------------------------------------------------------------------
  45. #define SAFE_ATOM_DELETE(x) if (1) {GlobalDeleteAtom(x); x = 0;} else
  46. #define SAFE_DELETE_GDIOBJ(hgdiobj) if((hgdiobj)) {DeleteObject(hgdiobj); (hgdiobj)=NULL;}
  47. //---------------------------------------------------------------------------
  48. #ifndef ARRAYSIZE
  49. #define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
  50. #endif ARRAYSIZE
  51. #define WIDTH(r) ((r).right - (r).left)
  52. #define HEIGHT(r) ((r).bottom - (r).top)
  53. #ifndef RECTWIDTH
  54. #define RECTWIDTH(prc) ((prc)->right - (prc)->left)
  55. #endif RECTWIDTH
  56. #ifndef RECTHEIGHT
  57. #define RECTHEIGHT(prc) ((prc)->bottom - (prc)->top)
  58. #endif RECTHEIGHT
  59. #ifndef BOOLIFY
  60. #define BOOLIFY(val) ((val) ? TRUE : FALSE)
  61. #endif BOOLIFY
  62. #ifndef TESTFLAG
  63. #define TESTFLAG(field,bits) (((field)&(bits)) ? TRUE : FALSE)
  64. #endif TESTFLAG
  65. //---------------------------------------------------------------------------
  66. #ifdef __cplusplus
  67. #define SAFE_DELETE(p) { delete (p); (p)=NULL; }
  68. #define SAFE_DELETE_ARRAY(prg) { delete [] (prg); (prg)=NULL; }
  69. #endif //__cplusplus
  70. //---------------------------------------------------------------------------
  71. #define VALID_CRITICALSECTION(pcs) ((pcs)!=NULL && (pcs)->DebugInfo != NULL)
  72. #define SAFE_ENTERCRITICALSECTION(pcs) {if(VALID_CRITICALSECTION(pcs)) {EnterCriticalSection(pcs);}}
  73. #define SAFE_LEAVECRITICALSECTION(pcs) {if(VALID_CRITICALSECTION(pcs)) {LeaveCriticalSection(pcs);}}
  74. #define SAFE_DELETECRITICALSECTION(pcs) {if(VALID_CRITICALSECTION(pcs)) {\
  75. DeleteCriticalSection(pcs); ZeroMemory(pcs, sizeof(*(pcs)));}}
  76. BOOL TokenHasPrivilege( IN OPTIONAL HANDLE hToken, DWORD dwPrivilege );
  77. //---------------------------------------------------------------------------
  78. #define RED(c) GetRValue(c)
  79. #define GREEN(c) GetGValue(c)
  80. #define BLUE(c) GetBValue(c)
  81. #define ALPHACHANNEL(c) BYTE((c) >> 24)
  82. //---------------------------------------------------------------------------
  83. #define REVERSE3(c) ((RED(c) << 16) | (GREEN(c) << 8) | BLUE(c))
  84. //---------------------------------------------------------------------------
  85. #define LAST_SYSCOLOR (COLOR_MENUBAR) // last color defined in winuser.h
  86. #define TM_COLORCOUNT (LAST_SYSCOLOR+1) // # of colors we care about
  87. //---------------------------------------------------------------------------
  88. #define ULONGAT(p) (*((LONG *)(p)))
  89. //---------------------------------------------------------------------------
  90. #define THEME_DPI 96
  91. #define DPISCALE(val, DcDpi) MulDiv(val, DcDpi, THEME_DPI)
  92. #define ROUND(flNum) (int(floor((flNum)+.5)))
  93. //---------------------------------------------------------------------------
  94. inline BOOL DpiDiff(HDC hdc, OUT int *piDcDpiH, OUT int *piDcDpiV = NULL)
  95. {
  96. BOOL fDiff = FALSE;
  97. BOOL fGotDc = (hdc != NULL);
  98. if (! fGotDc)
  99. hdc = GetWindowDC(NULL);
  100. if (hdc)
  101. {
  102. *piDcDpiH = GetDeviceCaps(hdc, LOGPIXELSX);
  103. if (piDcDpiV)
  104. {
  105. *piDcDpiV = GetDeviceCaps(hdc, LOGPIXELSY);
  106. }
  107. if (! fGotDc)
  108. ReleaseDC(NULL, hdc);
  109. fDiff = (*piDcDpiH != THEME_DPI);
  110. }
  111. return fDiff;
  112. }
  113. //---------------------------------------------------------------------------
  114. typedef BOOL (WINAPI *IMAGE_DRAWPROC) (IMAGELISTDRAWPARAMS* pimldp);
  115. typedef int (WINAPI *PFNDRAWSHADOWTEXT)(HDC hdc, LPCTSTR pszText, UINT cch, RECT* prc,
  116. DWORD dwFlags, COLORREF crText, COLORREF crShadow, int ixOffset, int iyOffset);
  117. //---------------------------------------------------------------------------
  118. #if 1 // testing DrawThemeIcon()
  119. typedef HIMAGELIST (WINAPI *IMAGE_LOADPROC) (HINSTANCE hi, LPCTSTR lpbmp,
  120. int cx, int cGrow, COLORREF crMask, UINT uType, UINT uFlags);
  121. typedef BOOL (WINAPI *IMAGE_DESTROYPROC) (HIMAGELIST himl);
  122. #endif
  123. //---------------------------------------------------------------------------
  124. struct COLORSIZECOMBOS // binary resource in package
  125. {
  126. WORD cColorSchemes; // number of color schemes defined
  127. WORD cSizes; // number of sizes defined
  128. SHORT sFileNums[1]; // 2 dim array (colors x sizes)
  129. };
  130. //---------------------------------------------------------------------------
  131. union MIXEDPTRS
  132. {
  133. BYTE *pb;
  134. char *pc;
  135. WORD *pw;
  136. SHORT *ps;
  137. WCHAR *px;
  138. int *pi;
  139. DWORD *pdw;
  140. POINT *ppt;
  141. SIZE *psz;
  142. RECT *prc;
  143. };
  144. //---------------------------------------------------------------------------
  145. enum GRIDNUM
  146. {
  147. GN_LEFTTOP = 0,
  148. GN_MIDDLETOP = 1,
  149. GN_RIGHTTOP = 2,
  150. GN_LEFTMIDDLE = 3,
  151. GN_MIDDLEMIDDLE = 4,
  152. GN_RIGHTMIDDLE = 5,
  153. GN_LEFTBOTTOM = 6,
  154. GN_MIDDLEBOTTOM = 7,
  155. GN_RIGHTBOTTOM = 8
  156. };
  157. //---------------------------------------------------------------------------
  158. extern IMAGE_DRAWPROC ImageList_DrawProc;
  159. extern HINSTANCE g_hInst;
  160. extern PFNDRAWSHADOWTEXT CCDrawShadowText;
  161. extern int g_iScreenDpi;
  162. //---------------------------------------------------------------------------
  163. inline bool IsSpace(WCHAR wch)
  164. {
  165. WORD w = 0;
  166. GetStringTypeW(CT_CTYPE1, &wch, 1, &w);
  167. return (w & C1_SPACE) ? true : false;
  168. }
  169. //---------------------------------------------------------------------------
  170. inline bool IsHexDigit(WCHAR wch)
  171. {
  172. WORD w = 0;
  173. GetStringTypeW(CT_CTYPE1, &wch, 1, &w);
  174. return (w & C1_XDIGIT) ? true : false;
  175. }
  176. //---------------------------------------------------------------------------
  177. inline bool IsDigit(WCHAR wch)
  178. {
  179. WORD w = 0;
  180. GetStringTypeW(CT_CTYPE1, &wch, 1, &w);
  181. return (w & C1_DIGIT) ? true : false;
  182. }
  183. // A string compare that explicitely only works on english characters
  184. int AsciiStrCmpI(const WCHAR *dst, const WCHAR *src);
  185. //---------------------------------------------------------------------------
  186. HRESULT SafeStringCchCopyW( LPWSTR pszDest, ULONG cchDest, LPCWSTR pszSrc );
  187. HRESULT SafeStringCmpIW( LPCWSTR psz1, LPCWSTR psz2, UINT cchMax, OUT int* piCompare );
  188. void ApplyStringProp(HWND hwnd, LPCWSTR psz, ATOM atom);
  189. //---------------------------------------------------------------------------
  190. BOOL UtilsStartUp();
  191. BOOL UtilsShutDown();
  192. BOOL IsUnicode(LPCSTR pszBuff, int *piUnicodeStartOffset);
  193. BOOL FileExists(LPCTSTR pszFullNameAndPath);
  194. BOOL lstrtoken(LPWSTR psz, WCHAR wch);
  195. //---------------------------------------------------------------------------
  196. HANDLE CmdLineRun(LPCTSTR pszExeName, LPCTSTR pszCmdLine=NULL, BOOL fHide=TRUE);
  197. //---------------------------------------------------------------------------
  198. HRESULT SyncCmdLineRun(LPCTSTR pszExeName, LPCTSTR pszParams=NULL);
  199. HRESULT TextToFile(LPCWSTR szFileName, LPCWSTR szText);
  200. HRESULT GetPtrToResource(HINSTANCE hInst, LPCWSTR pszResType, LPCWSTR pszResName,
  201. OUT void **ppBytes, OPTIONAL OUT DWORD *pdwBytes=NULL);
  202. HRESULT GetResString(HINSTANCE hInst, LPCWSTR pszResType, int id, LPWSTR pszBuff,
  203. DWORD dwMaxBuffChars);
  204. HRESULT AllocateTextResource(HINSTANCE hInst, LPCWSTR pszResName, LPWSTR *ppszText);
  205. //---- uses LocalAlloc() to put text file into a string; use LocalFree() to release ----
  206. HRESULT AllocateTextFile(LPCWSTR szFileName, OUT LPWSTR *ppszFileText,
  207. OUT OPTIONAL BOOL *pfWasAnsi);
  208. HRESULT AddPathIfNeeded(LPCWSTR pszFileName, LPCWSTR pszPath, OUT LPWSTR pszFullName, ULONG cchFullName);
  209. HRESULT AnsiToUnicode(LPSTR pszSource, LPWSTR pszDest, DWORD dwMaxDestChars);
  210. BOOL UnExpandEnvironmentString(LPCWSTR pszPath, LPCWSTR pszEnvVar, LPWSTR pszResult, UINT cbResult);
  211. HRESULT EnsureUxCtrlLoaded();
  212. HRESULT RegistryIntWrite(HKEY hKey, LPCWSTR pszValueName, int iValue);
  213. HRESULT RegistryStrWrite(HKEY hKey, LPCWSTR pszValueName, LPCWSTR pszValue);
  214. HRESULT RegistryStrWriteExpand(HKEY hKey, LPCWSTR pszValueName, LPCWSTR pszValue);
  215. HRESULT RegistryIntRead(HKEY hKey, LPCWSTR pszValueName, int *piValue);
  216. HRESULT RegistryStrRead(HKEY hKey, LPCWSTR pszValueName, LPWSTR pszBuff, DWORD dwMaxChars);
  217. BOOL PreMultiplyAlpha(DWORD *pPixelBuff, UINT iWidth, UINT iHeight);
  218. BOOL IsBiDiLocalizedSystem(void);
  219. DWORD MinimumDisplayColorDepth (void);
  220. bool CheckMinColorDepth(HINSTANCE hInst, DWORD dwCurMinDepth, int iIndex = -1);
  221. void SafeSendMessage(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  222. //---------------------------------------------------------------------------
  223. BOOL AsciiScanStringList( LPCWSTR pwszString, LPCWSTR* rgpwszList, int cStrings,
  224. BOOL fIgnoreCase );
  225. HICON _GetWindowIcon(HWND hwnd, BOOL fPerferLargeIcon);
  226. BOOL GetWindowDesktopName(HWND hwnd, LPWSTR pszName, DWORD dwMaxChars);
  227. int FontPointSize(int iFontHeight);
  228. //---------------------------------------------------------------------------
  229. inline BOOL IsWindowProcess( HWND hwnd, DWORD dwProcessId )
  230. {
  231. DWORD dwPid = 0;
  232. GetWindowThreadProcessId(hwnd, &dwPid);
  233. return dwProcessId == dwPid;
  234. }
  235. //---------------------------------------------------------------------------
  236. void RGBtoHLS(COLORREF crColor, WORD *pfHue, WORD *pfSat, WORD *pfLum);
  237. COLORREF HLStoRGB(WORD bHue, WORD bLum, WORD bSat);
  238. int string2number(LPCWSTR psz);
  239. //---------------------------------------------------------------------------
  240. inline void MIRROR_POINT( IN const RECT& rcWindow, IN OUT POINT& pt )
  241. {
  242. pt.x = rcWindow.right + rcWindow.left - pt.x;
  243. }
  244. //-------------------------------------------------------------------------//
  245. inline void MIRROR_RECT( IN const RECT& rcWindow, IN OUT RECT& rc )
  246. {
  247. DWORD dwWidth = RECTWIDTH(&rc);
  248. rc.right = rcWindow.right + rcWindow.left - rc.left;
  249. rc.left = rc.right - dwWidth;
  250. }
  251. //-------------------------------------------------------------------------//
  252. inline BOOL IsMirrored(HDC hdc)
  253. {
  254. BOOL fMirrored = FALSE;
  255. DWORD dwVal = GetLayout(hdc);
  256. if ((dwVal != GDI_ERROR) && (dwVal & LAYOUT_RTL))
  257. fMirrored = TRUE;
  258. return fMirrored;
  259. }
  260. //-------------------------------------------------------------------------//
  261. inline BOOL IsFlippingBitmaps(HDC hdc)
  262. {
  263. BOOL fFlipping = FALSE;
  264. DWORD dwVal = GetLayout(hdc);
  265. if ((dwVal != GDI_ERROR) && (dwVal & LAYOUT_RTL))
  266. {
  267. if (! (dwVal & LAYOUT_BITMAPORIENTATIONPRESERVED))
  268. fFlipping = TRUE;
  269. }
  270. return fFlipping;
  271. }
  272. //---------------------------------------------------------------------------
  273. inline void ScaleFontForScreenDpi(LOGFONT *plf)
  274. {
  275. //---- scale from 96 dpi to current logical screen dpi ----
  276. if (plf->lfHeight < 0) // specified in points
  277. {
  278. plf->lfHeight = MulDiv(plf->lfHeight, g_iScreenDpi, THEME_DPI);
  279. }
  280. }
  281. //---------------------------------------------------------------------------
  282. inline int ScaleSizeForScreenDpi(int iValue)
  283. {
  284. //---- scale from 96 dpi to current logical screen dpi ----
  285. return MulDiv(iValue, g_iScreenDpi, THEME_DPI);
  286. }
  287. //---------------------------------------------------------------------------
  288. void ScaleFontForHdcDpi(HDC hdc, LOGFONT *plf);
  289. int ScaleSizeForHdcDpi(HDC hdc, int iValue);
  290. //---------------------------------------------------------------------------