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.

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