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.

470 lines
14 KiB

  1. //---------------------------------------------------------------------------
  2. // Info.cpp - implements the information services of the CRenderObj object
  3. //---------------------------------------------------------------------------
  4. #include "stdafx.h"
  5. #include "Render.h"
  6. #include "Utils.h"
  7. #include "Loader.h"
  8. #include "sethook.h"
  9. #include "info.h"
  10. #include "RenderList.h"
  11. #include "Services.h"
  12. #include "appinfo.h"
  13. #include "tmutils.h"
  14. #include "borderfill.h"
  15. #include "imagefile.h"
  16. #include "textdraw.h"
  17. //---------------------------------------------------------------------------
  18. HRESULT MatchThemeClass(LPCTSTR pszAppName, LPCTSTR pszClassId,
  19. CUxThemeFile *pThemeFile, int *piOffset, int *piClassNameOffset)
  20. {
  21. THEMEHDR *pHdr = (THEMEHDR *)pThemeFile->_pbThemeData;
  22. MIXEDPTRS u;
  23. u.pb = pThemeFile->_pbThemeData + pHdr->iSectionIndexOffset;
  24. DWORD dwCount = pHdr->iSectionIndexLength/sizeof(APPCLASSLIVE);
  25. APPCLASSLIVE *acl = (APPCLASSLIVE *)u.pb;
  26. for (DWORD i=0; i < dwCount; i++, acl++)
  27. {
  28. if (acl->dwAppNameIndex)
  29. {
  30. if ((! pszAppName) || (! *pszAppName))
  31. continue; // not a match
  32. LPCWSTR pszApp = ThemeString(pThemeFile, acl->dwAppNameIndex);
  33. if (AsciiStrCmpI(pszAppName, pszApp) != 0)
  34. continue; // not a match
  35. }
  36. if (acl->dwClassNameIndex)
  37. {
  38. LPCWSTR pszClass = ThemeString(pThemeFile, acl->dwClassNameIndex);
  39. if (AsciiStrCmpI(pszClassId, pszClass)==0) // matches
  40. {
  41. *piOffset = acl->iIndex;
  42. *piClassNameOffset = acl->dwClassNameIndex;
  43. return S_OK;
  44. }
  45. }
  46. }
  47. return MakeError32(ERROR_NOT_FOUND); // not found
  48. }
  49. //---------------------------------------------------------------------------
  50. HRESULT MatchThemeClassList(HWND hwnd, LPCTSTR pszClassIdList,
  51. CUxThemeFile *pThemeFile, int *piOffset, int *piClassNameOffset)
  52. {
  53. LPCTSTR pszAppName = NULL;
  54. WCHAR *pszIdListBuff = NULL;
  55. WCHAR szAppSubName[MAX_PATH];
  56. WCHAR szIdSubName[MAX_PATH];
  57. int len;
  58. Log(LOG_TM, L"MatchThemeClassList(): classlist=%s", pszClassIdList);
  59. HRESULT hr = S_OK;
  60. if (! pszClassIdList)
  61. return MakeError32(E_INVALIDARG);
  62. //---- first check Hwnd IdList substitutions ----
  63. if (hwnd)
  64. {
  65. ATOM atomIdSub = (ATOM)GetProp(hwnd, MAKEINTATOM(GetThemeAtom(THEMEATOM_SUBIDLIST)));
  66. if (atomIdSub)
  67. {
  68. if (GetAtomName(atomIdSub, szIdSubName, ARRAYSIZE(szIdSubName)))
  69. {
  70. pszClassIdList = szIdSubName;
  71. Log(LOG_TM, L"MatchThemeClassList: hwnd prop IdList OVERRIDE: %s", pszClassIdList);
  72. }
  73. }
  74. }
  75. //---- now check Hwnd AppName substitutions ----
  76. if (hwnd)
  77. {
  78. ATOM atomAppSub = (ATOM)GetProp(hwnd, MAKEINTATOM(GetThemeAtom(THEMEATOM_SUBAPPNAME)));
  79. if (atomAppSub)
  80. {
  81. if (GetAtomName(atomAppSub, szAppSubName, ARRAYSIZE(szAppSubName)))
  82. {
  83. pszAppName = szAppSubName;
  84. Log(LOG_TM, L"MatchThemeClassList: hwnd prop AppName OVERRIDE: %s", pszAppName);
  85. }
  86. }
  87. }
  88. //---- make a copy of pszClassIdList ----
  89. len = lstrlen(pszClassIdList);
  90. pszIdListBuff = new WCHAR[len+1];
  91. if (! pszIdListBuff)
  92. {
  93. hr = MakeError32(E_OUTOFMEMORY);
  94. goto exit;
  95. }
  96. StringCchCopyW(pszIdListBuff, len + 1, pszClassIdList);
  97. LPTSTR classId;
  98. BOOL fContinue;
  99. classId = pszIdListBuff;
  100. fContinue = TRUE;
  101. //---- check each ClassId in the list ----
  102. while (fContinue)
  103. {
  104. fContinue = lstrtoken(classId, _TEXT(';'));
  105. hr = MatchThemeClass(pszAppName, classId, pThemeFile, piOffset, piClassNameOffset);
  106. if (SUCCEEDED(hr))
  107. break;
  108. classId += lstrlen(classId)+1;
  109. }
  110. exit:
  111. if (pszIdListBuff)
  112. delete [] pszIdListBuff;
  113. return hr;
  114. }
  115. //---------------------------------------------------------------------------
  116. HTHEME _OpenThemeDataFromFile(HTHEMEFILE hLoadedThemeFile, HWND hwnd,
  117. LPCWSTR pszClassIdList, DWORD dwFlags)
  118. {
  119. HRESULT hr = S_OK;
  120. RESOURCE CUxThemeFile *pThemeFile = (CUxThemeFile *)hLoadedThemeFile;
  121. int iOffset;
  122. int iClassNameOffset;
  123. HTHEME hTheme = NULL;
  124. //---- match classid list to theme and get the offset ----
  125. hr = MatchThemeClassList(hwnd, pszClassIdList, pThemeFile, &iOffset,
  126. &iClassNameOffset);
  127. if (FAILED(hr))
  128. {
  129. Log(LOG_TMOPEN, L"hLoadedThemeFile: No match for class=%s", pszClassIdList);
  130. goto exit;
  131. }
  132. hr = g_pRenderList->OpenRenderObject(pThemeFile, iOffset, iClassNameOffset, NULL,
  133. NULL, hwnd, dwFlags, &hTheme);
  134. if (FAILED(hr))
  135. goto exit;
  136. //---- store hTheme with window ----
  137. if (! (dwFlags & OTD_NONCLIENT))
  138. {
  139. //---- store the hTheme so we know its themed ----
  140. if (hwnd)
  141. SetProp(hwnd, MAKEINTATOM(GetThemeAtom(THEMEATOM_HTHEME)), (void *)hTheme);
  142. }
  143. Log(LOG_TMOPEN, L"hLoadedThemeFile: returning hTheme=0x%x", hTheme);
  144. exit:
  145. SET_LAST_ERROR(hr);
  146. return hTheme;
  147. }
  148. //---------------------------------------------------------------------------
  149. HTHEME _OpenThemeData(HWND hwnd, LPCWSTR pszClassIdList, DWORD dwFlags)
  150. {
  151. HRESULT hr = S_OK;
  152. RESOURCE CUxThemeFile *pThemeFile = NULL;
  153. HTHEME hTheme = NULL;
  154. BOOL fOk;
  155. DWORD dwAppFlags;
  156. SET_LAST_ERROR(hr);
  157. if (! g_fUxthemeInitialized)
  158. goto exit;
  159. Log(LOG_TMOPEN, L"_OpenThemeData: hwnd=0x%x, ClassIdList=%s", hwnd, pszClassIdList);
  160. //---- remove previous HTHEME property ----
  161. if (hwnd)
  162. RemoveProp(hwnd, MAKEINTATOM(GetThemeAtom(THEMEATOM_HTHEME)));
  163. if (! g_pAppInfo->AppIsThemed()) // this process has been excluded from theming
  164. {
  165. Log(LOG_TMOPEN, L"App not themed");
  166. hr = MakeError32(ERROR_NOT_FOUND);
  167. SET_LAST_ERROR(hr);
  168. goto exit;
  169. }
  170. //---- ensure app allows this type of themeing ----
  171. dwAppFlags = g_pAppInfo->GetAppFlags();
  172. if (dwFlags & OTD_NONCLIENT)
  173. {
  174. fOk = ((dwAppFlags & STAP_ALLOW_NONCLIENT) != 0);
  175. }
  176. else
  177. {
  178. fOk = ((dwAppFlags & STAP_ALLOW_CONTROLS) != 0);
  179. }
  180. if (! fOk)
  181. {
  182. Log(LOG_TMOPEN, L"AppFlags don't allow theming client/nonclient windows");
  183. hr = MakeError32(ERROR_NOT_FOUND);
  184. SET_LAST_ERROR(hr);
  185. goto exit;
  186. }
  187. //---- find Theme File for this HWND and REFCOUNT it for _OpenThemeDataFromFile call ----
  188. hr = GetHwndThemeFile(hwnd, pszClassIdList, &pThemeFile);
  189. if (FAILED(hr))
  190. {
  191. Log(LOG_TMOPEN, L"no theme entry for this classidlist: %s", pszClassIdList);
  192. SET_LAST_ERROR(hr);
  193. goto exit;
  194. }
  195. hTheme = _OpenThemeDataFromFile(pThemeFile, hwnd, pszClassIdList, dwFlags);
  196. exit:
  197. //---- always close the pThemeFile here and decrement its refcnt ----
  198. //---- case 1: if we failed to get an HTHEME, we don't want a refcnt on it ----
  199. //---- case 2: if we do get an HTHEME, it get's its own refcnt on it ----
  200. if (pThemeFile)
  201. g_pAppInfo->CloseThemeFile(pThemeFile);
  202. return hTheme;
  203. }
  204. //---------------------------------------------------------------------------
  205. HRESULT GetHwndThemeFile(HWND hwnd, LPCWSTR pszClassIdList, CUxThemeFile **ppThemeFile)
  206. {
  207. HRESULT hr = S_OK;
  208. //----- check input params ----
  209. if ((! pszClassIdList) || (! *pszClassIdList))
  210. {
  211. hr = MakeError32(E_INVALIDARG);
  212. goto exit;
  213. }
  214. //---- get a shared CUxThemeFile object for the hwnd ----
  215. hr = g_pAppInfo->OpenWindowThemeFile(hwnd, ppThemeFile);
  216. if (FAILED(hr))
  217. goto exit;
  218. exit:
  219. return hr;
  220. }
  221. //---------------------------------------------------------------------------
  222. HRESULT _OpenThemeFileFromData(CRenderObj *pRender, HTHEMEFILE *phThemeFile)
  223. {
  224. LogEntry(L"OpenThemeFileFromData");
  225. HRESULT hr = S_OK;
  226. *phThemeFile = pRender->_pThemeFile;
  227. LogExit(L"OpenThemeFileFromData");
  228. return hr;
  229. }
  230. //---------------------------------------------------------------------------
  231. void ClearExStyleBits(HWND hwnd)
  232. {
  233. Log(LOG_COMPOSITE, L"ClearExStyleBits called for hwnd=0x%x", hwnd);
  234. //---- see if window needs its exstyle cleared ----
  235. DWORD dwFlags = PtrToInt(GetProp(hwnd, MAKEINTATOM(GetThemeAtom(THEMEATOM_PROPFLAGS))));
  236. if (dwFlags & (PROPFLAGS_RESET_TRANSPARENT | PROPFLAGS_RESET_COMPOSITED))
  237. {
  238. DWORD dwExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
  239. if (dwFlags & PROPFLAGS_RESET_TRANSPARENT)
  240. {
  241. Log(LOG_COMPOSITE, L"Clearning WS_EX_TRANSPARENT for hwnd=0x%x", hwnd);
  242. dwExStyle &= ~(WS_EX_TRANSPARENT);
  243. }
  244. if (dwFlags & PROPFLAGS_RESET_COMPOSITED)
  245. {
  246. Log(LOG_COMPOSITE, L"Clearning WS_EX_COMPOSITED for hwnd=0x%x", hwnd);
  247. dwExStyle &= ~(WS_EX_COMPOSITED);
  248. }
  249. //---- reset the correct ExStyle bits ----
  250. SetWindowLong(hwnd, GWL_EXSTYLE, dwExStyle);
  251. //---- reset the property flags ----
  252. dwFlags &= ~(PROPFLAGS_RESET_TRANSPARENT | PROPFLAGS_RESET_COMPOSITED);
  253. SetProp(hwnd, MAKEINTATOM(GetThemeAtom(THEMEATOM_PROPFLAGS)), IntToPtr(dwFlags));
  254. }
  255. }
  256. //---------------------------------------------------------------------------
  257. //---------------------------------------------------------------------------
  258. struct EPW
  259. {
  260. WNDENUMPROC lpCallBackCaller;
  261. LPARAM lParamCaller;
  262. HWND *pHwnds; // OPTIONAL list of hwnds to remove as they are enum-ed
  263. int iCountHwnds; // count of remaining HWND's in pHwnds
  264. };
  265. //---------------------------------------------------------------------------
  266. BOOL CALLBACK ChildWinCallBack(HWND hwnd, LPARAM lParam)
  267. {
  268. BOOL fResult = TRUE;
  269. if (IsWindowProcess(hwnd, g_dwProcessId))
  270. {
  271. EPW *pEpw = (EPW *)lParam;
  272. fResult = pEpw->lpCallBackCaller(hwnd, pEpw->lParamCaller);
  273. //---- remove from list ----
  274. if (pEpw->pHwnds)
  275. {
  276. for (int i=0; i < pEpw->iCountHwnds; i++)
  277. {
  278. if (pEpw->pHwnds[i] == hwnd) // found it
  279. {
  280. pEpw->iCountHwnds--;
  281. if (i != pEpw->iCountHwnds) // switch last with current
  282. pEpw->pHwnds[i] = pEpw->pHwnds[pEpw->iCountHwnds];
  283. break;
  284. }
  285. }
  286. }
  287. }
  288. return fResult;
  289. }
  290. //---------------------------------------------------------------------------
  291. BOOL CALLBACK TopWinCallBack(HWND hwnd, LPARAM lParam)
  292. {
  293. BOOL fResult = ChildWinCallBack(hwnd, lParam);
  294. if (fResult)
  295. {
  296. //---- we need to check for hwnd having at least one child ----
  297. //---- since EnumChildWindows() of a hwnd without children ----
  298. //---- returns an error ----
  299. if (GetWindow(hwnd, GW_CHILD)) // if hwnd has at least one child
  300. {
  301. fResult = EnumChildWindows(hwnd, ChildWinCallBack, lParam);
  302. }
  303. }
  304. return fResult;
  305. }
  306. //---------------------------------------------------------------------------
  307. BOOL CALLBACK DesktopWinCallBack(LPTSTR lpszDesktop, LPARAM lParam)
  308. {
  309. //---- open the desktop ----
  310. HDESK hDesk = OpenDesktop(lpszDesktop, DF_ALLOWOTHERACCOUNTHOOK, FALSE,
  311. DESKTOP_READOBJECTS | DESKTOP_ENUMERATE);
  312. if (hDesk)
  313. {
  314. //---- enum windows on desktop ----
  315. EnumDesktopWindows(hDesk, TopWinCallBack, lParam);
  316. CloseDesktop(hDesk);
  317. }
  318. return TRUE; // return values from EnumDesktopWindows() not reliable
  319. }
  320. //---------------------------------------------------------------------------
  321. BOOL EnumProcessWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
  322. {
  323. HWND *pHwnds = NULL;
  324. int iCount = 0;
  325. EPW epw = {lpEnumFunc, lParam};
  326. //---- get list of themed windows on "foreign" desktops for this process ----
  327. BOOL fGotForeignList = g_pAppInfo->GetForeignWindows(&pHwnds, &iCount);
  328. if (fGotForeignList)
  329. {
  330. epw.pHwnds = pHwnds;
  331. epw.iCountHwnds = iCount;
  332. }
  333. //---- this will enum all windows for this process (all desktops, all child levels) ----
  334. BOOL fOk = EnumDesktops(GetProcessWindowStation(), DesktopWinCallBack, (LPARAM)&epw);
  335. if ((fOk) && (fGotForeignList) && (epw.iCountHwnds))
  336. {
  337. //---- get updated count ----
  338. iCount = epw.iCountHwnds;
  339. //---- turn off list maintainance ----
  340. epw.pHwnds = NULL;
  341. epw.iCountHwnds = 0;
  342. Log(LOG_TMHANDLE, L"---- Enuming %d Foreign Windows ----", iCount);
  343. //---- enumerate remaining hwnd's in list ----
  344. for (int i=0; i < iCount; i++)
  345. {
  346. fOk = ChildWinCallBack(pHwnds[i], (LPARAM)&epw);
  347. if (! fOk)
  348. break;
  349. }
  350. }
  351. if (pHwnds)
  352. delete [] pHwnds;
  353. return fOk;
  354. }
  355. //---------------------------------------------------------------------------
  356. //---------------------------------------------------------------------------
  357. //---------------------------------------------------------------------------
  358. BOOL CALLBACK DumpCallback(HWND hwnd, LPARAM lParam)
  359. {
  360. WCHAR szName[MAX_PATH];
  361. WCHAR szDeskName[MAX_PATH] = {0};
  362. BOOL fIsForeign = TRUE;
  363. //---- get classname of window ----
  364. GetClassName(hwnd, szName, MAX_PATH);
  365. //---- get desktop name for window ----
  366. if (GetWindowDesktopName(hwnd, szDeskName, ARRAYSIZE(szDeskName)))
  367. {
  368. if (AsciiStrCmpI(szDeskName, L"default")==0)
  369. {
  370. fIsForeign = FALSE;
  371. }
  372. }
  373. if (fIsForeign)
  374. {
  375. Log(LOG_WINDUMP, L" hwnd=0x%x, class=%s, DESK=%s", hwnd, szName, szDeskName);
  376. }
  377. else
  378. {
  379. Log(LOG_WINDUMP, L" hwnd=0x%x, class=%s", hwnd, szName);
  380. }
  381. return TRUE;
  382. }
  383. //---------------------------------------------------------------------------
  384. void WindowDump(LPCWSTR pszWhere)
  385. {
  386. if (LogOptionOn(LO_WINDUMP))
  387. {
  388. Log(LOG_WINDUMP, L"---- Window Dump for Process [%s] ----", pszWhere);
  389. EnumProcessWindows(DumpCallback, NULL);
  390. }
  391. else
  392. {
  393. Log(LOG_TMHANDLE, L"---- %s ----", pszWhere);
  394. }
  395. }
  396. //---------------------------------------------------------------------------