XTPSkinManager.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:23k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // XTPSkinManager.cpp: implementation of the CXTPSkinManager class.
  2. //
  3. // This file is a part of the XTREME SKINFRAMEWORK MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. #include "Common/XTPWinThemeWrapper.h"
  22. #include "Common/XTPColorManager.h"
  23. #include "Common/XTPSystemHelpers.h"
  24. #include "XTPSkinManager.h"
  25. #include "XTPSkinImage.h"
  26. #include "XTPSkinManagerResource.h"
  27. #include "XTPSkinObject.h"
  28. #include "XTPSkinObjectFrame.h"
  29. #include "XTPSkinManagerSchema.h"
  30. #include "XTPSkinManagerApiHook.h"
  31. #include "XTPSkinManagerModuleList.h"
  32. #ifdef _DEBUG
  33. #undef THIS_FILE
  34. static char THIS_FILE[]=__FILE__;
  35. #define new DEBUG_NEW
  36. #endif
  37. #define EVENT_OBJECT_REORDER 0x8004
  38. CXTPSkinManagerMetrics::CXTPSkinManagerMetrics(CXTPSkinManager* pManager)
  39. {
  40. m_pManager = pManager;
  41. m_brushDialog = NULL;
  42. m_brushTabControl = NULL;
  43. ZeroMemory(m_brTheme, sizeof(m_brTheme));
  44. memset(m_clrTheme, -1, sizeof(m_clrTheme));
  45. m_cyCaption = m_cySmallCaption = m_cyOsCaption = m_cyOsSmallCaption = 0;
  46. }
  47. CXTPSkinManagerMetrics::~CXTPSkinManagerMetrics()
  48. {
  49. if (m_brushDialog)
  50. {
  51. DeleteObject(m_brushDialog);
  52. }
  53. if (m_brushTabControl)
  54. {
  55. DeleteObject(m_brushTabControl);
  56. }
  57. for (int iColorId = 0; iColorId < XTP_SKINMETRICS_COLORTABLESIZE; iColorId++)
  58. {
  59. if (m_brTheme[iColorId])
  60. DeleteObject(m_brTheme[iColorId]);
  61. }
  62. }
  63. COLORREF CXTPSkinManagerMetrics::GetColor(int nIndex) const
  64. {
  65. return nIndex < XTP_SKINMETRICS_COLORTABLESIZE ? m_clrTheme[nIndex] : 0;
  66. }
  67. BOOL CXTPSkinManagerMetrics::IsMetricObject(HGDIOBJ hObject) const
  68. {
  69. if (hObject == 0)
  70. return FALSE;
  71. if (GetObjectType(hObject) != OBJ_BRUSH)
  72. return FALSE;
  73. for (int i = 0; i < XTP_SKINMETRICS_COLORTABLESIZE; i++)
  74. {
  75. if (hObject == m_brTheme[i])
  76. return TRUE;
  77. }
  78. return FALSE;
  79. }
  80. void CXTPSkinManagerMetrics::DestroyMetrics()
  81. {
  82. m_fntCaption.DeleteObject();
  83. m_fntSmCaption.DeleteObject();
  84. m_fntMenu.DeleteObject();
  85. for (int iColorId = 0; iColorId < XTP_SKINMETRICS_COLORTABLESIZE; iColorId++)
  86. {
  87. HBRUSH hBrush = m_brTheme[iColorId];
  88. m_brTheme[iColorId] = NULL;
  89. if (hBrush)
  90. DeleteObject(hBrush);
  91. }
  92. if (m_brushDialog)
  93. {
  94. DeleteObject(m_brushDialog);
  95. m_brushDialog = NULL;
  96. }
  97. if (m_brushTabControl)
  98. {
  99. DeleteObject(m_brushTabControl);
  100. m_brushTabControl = NULL;
  101. }
  102. }
  103. void CXTPSkinManagerMetrics::RefreshMetrics()
  104. {
  105. static const COLORREF defColors[XTP_SKINMETRICS_COLORTABLESIZE] =
  106. {
  107. RGB(192, 192, 192),
  108. RGB( 58, 110, 165),
  109. RGB(  0,   0, 128),
  110. RGB(128, 128, 128),
  111. RGB(192, 192, 192),
  112. RGB(255, 255, 255),
  113. RGB(  0,   0,   0),
  114. RGB(  0,   0,   0),
  115. RGB(  0,   0,   0),
  116. RGB(255, 255, 255),
  117. RGB(192, 192, 192),
  118. RGB(192, 192, 192),
  119. RGB(128, 128, 128),
  120. RGB(  0,   0, 128),
  121. RGB(255, 255, 255),
  122. RGB(192, 192, 192),
  123. RGB(128, 128, 128),
  124. RGB(128, 128, 128),
  125. RGB(  0,   0,   0),
  126. RGB(192, 192, 192),
  127. RGB(255, 255, 255),
  128. RGB(  0,   0,   0),
  129. RGB(223, 223, 223),
  130. RGB(  0,   0,   0),
  131. RGB(255, 255, 225),
  132. RGB(180, 180, 180),
  133. RGB(  0,   0, 255),
  134. RGB( 16, 132, 208),
  135. RGB(181, 181, 181),
  136. RGB(  0,   0, 128),
  137. RGB(192, 192, 192)
  138. };
  139. CXTPSkinManagerSchema* pSchema = m_pManager->GetSchema();
  140. if (!pSchema)
  141. {
  142. for (int iColorId = 0; iColorId < XTP_SKINMETRICS_COLORTABLESIZE; iColorId++)
  143. {
  144. m_clrTheme[iColorId] = GetSysColor(iColorId);
  145. HBRUSH hBrush = m_brTheme[iColorId];
  146. m_brTheme[iColorId] = NULL;
  147. if (hBrush)
  148. DeleteObject(hBrush);
  149. }
  150. return;
  151. }
  152. UINT nSysMetrics = pSchema->GetClassCode(_T("SYSMETRICS"));
  153. NONCLIENTMETRICS ncm;
  154. ZeroMemory(&ncm, sizeof(NONCLIENTMETRICS));
  155. ncm.cbSize = sizeof(NONCLIENTMETRICS);
  156. VERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0));
  157. m_fntCaption.DeleteObject();
  158. m_fntCaption.CreateFontIndirect(&ncm.lfCaptionFont);
  159. m_fntSmCaption.DeleteObject();
  160. m_fntSmCaption.CreateFontIndirect(&ncm.lfSmCaptionFont);
  161. m_fntMenu.DeleteObject();
  162. m_fntMenu.CreateFontIndirect(&ncm.lfMenuFont);
  163. m_nBorderSize = 1;
  164. SystemParametersInfo(SPI_GETBORDER, 0, &m_nBorderSize, FALSE);
  165. m_cxBorder = GetSystemMetrics(SM_CXBORDER);
  166. m_cyBorder = GetSystemMetrics(SM_CYBORDER);
  167. m_cxEdge = GetSystemMetrics(SM_CXEDGE);
  168. m_cyEdge = GetSystemMetrics(SM_CYEDGE);
  169. m_cxHScroll = GetSystemMetrics(SM_CXHSCROLL);
  170. m_cyHScroll = GetSystemMetrics(SM_CYHSCROLL);
  171. m_cxVScroll = GetSystemMetrics(SM_CXVSCROLL);
  172. m_cyVScroll = GetSystemMetrics(SM_CYVSCROLL);
  173. BOOL bApplyMetrics = m_pManager->GetApplyOptions() & xtpSkinApplyMetrics;
  174. m_cyOsCaption = m_cyCaption = GetSystemMetrics(SM_CYCAPTION);
  175. m_cyOsSmallCaption = m_cySmallCaption = GetSystemMetrics(SM_CYSMCAPTION);
  176. if (bApplyMetrics)
  177. {
  178. pSchema->GetIntProperty(nSysMetrics, 0, 0, TMT_CAPTIONBARHEIGHT, m_cyCaption);
  179. m_cyCaption++;
  180. pSchema->GetIntProperty(nSysMetrics, 0, 0, TMT_SMCAPTIONBARHEIGHT, m_cySmallCaption);
  181. m_cySmallCaption++;
  182. int nScrollbarWidth = 0, nScrollbarHeight = 0;
  183. if (SUCCEEDED(pSchema->GetIntProperty(nSysMetrics, 0, 0, TMT_SCROLLBARWIDTH, nScrollbarWidth)) && nScrollbarWidth > 0)
  184. {
  185. m_cxHScroll = m_cxVScroll = nScrollbarWidth;
  186. }
  187. if (SUCCEEDED(pSchema->GetIntProperty(nSysMetrics, 0, 0, TMT_SCROLLBARHEIGHT, nScrollbarHeight)) && nScrollbarHeight > 0)
  188. {
  189. m_cyHScroll = m_cyVScroll = nScrollbarHeight;
  190. }
  191. LOGFONT lfCaption, lfSmCaption;
  192. if (SUCCEEDED(pSchema->GetFontProperty(nSysMetrics, 0, 0, TMT_CAPTIONFONT, lfCaption)))
  193. {
  194. m_fntCaption.DeleteObject();
  195. m_fntCaption.CreateFontIndirect(&lfCaption);
  196. }
  197. if (SUCCEEDED(pSchema->GetFontProperty(nSysMetrics, 0, 0, TMT_SMALLCAPTIONFONT, lfSmCaption)))
  198. {
  199. m_fntSmCaption.DeleteObject();
  200. m_fntSmCaption.CreateFontIndirect(&lfSmCaption);
  201. }
  202. }
  203. for (int iColorId = 0; iColorId < XTP_SKINMETRICS_COLORTABLESIZE; iColorId++)
  204. {
  205. COLORREF clrOld = m_clrTheme[iColorId];
  206. m_clrTheme[iColorId] = (COLORREF)-1;
  207. COLORREF clrVal;
  208. if (FAILED(pSchema->GetColorProperty(nSysMetrics, 0, 0, iColorId + TMT_FIRSTCOLOR, clrVal)))
  209. {
  210. clrVal = defColors[iColorId];
  211. m_pManager->ApplyColorFilter(clrVal);
  212. }
  213. m_clrTheme[iColorId] = clrVal;
  214. if ((clrOld != clrVal) || (m_brTheme[iColorId] == NULL))
  215. {
  216. HBRUSH hBrush = m_brTheme[iColorId];
  217. m_brTheme[iColorId] = NULL;
  218. if (hBrush)
  219. DeleteObject(hBrush);
  220. m_brTheme[iColorId] = CreateSolidBrush(clrVal);
  221. }
  222. }
  223. m_clrEdgeHighLight = m_clrTheme[COLOR_BTNHIGHLIGHT];
  224. m_clrEdgeShadow = m_clrTheme[COLOR_BTNSHADOW];
  225. m_clrEdgeLight = m_clrTheme[COLOR_3DLIGHT];
  226. m_clrEdgeDkShadow = m_clrTheme[COLOR_3DDKSHADOW];
  227. if (m_brushDialog)
  228. {
  229. DeleteObject(m_brushDialog);
  230. }
  231. m_brushDialog = ::CreateSolidBrush(m_clrTheme[COLOR_3DFACE]);
  232. if (m_brushTabControl)
  233. {
  234. DeleteObject(m_brushTabControl);
  235. m_brushTabControl = NULL;
  236. }
  237. }
  238. //////////////////////////////////////////////////////////////////////
  239. // Construction/Destruction
  240. //////////////////////////////////////////////////////////////////////
  241. //////////////////////////////////////////////////////////////////////////
  242. // CXTPSkinManager
  243. class XTP_SKINAMANGER_THREAD_STATE : public CNoTrackObject
  244. {
  245. public:
  246. XTP_SKINAMANGER_THREAD_STATE();
  247. virtual ~XTP_SKINAMANGER_THREAD_STATE();
  248. public:
  249. HHOOK m_hHookOldCbtFilter;
  250. };
  251. XTP_SKINAMANGER_THREAD_STATE::XTP_SKINAMANGER_THREAD_STATE()
  252. {
  253. m_hHookOldCbtFilter = 0;
  254. }
  255. XTP_SKINAMANGER_THREAD_STATE::~XTP_SKINAMANGER_THREAD_STATE()
  256. {
  257. if (m_hHookOldCbtFilter)
  258. {
  259. UnhookWindowsHookEx(m_hHookOldCbtFilter);
  260. }
  261. }
  262. THREAD_LOCAL(XTP_SKINAMANGER_THREAD_STATE, _xtpSkinManagerThreadState)
  263. CXTPSkinManager::CXTPSkinManager()
  264. {
  265. m_pSchema = NULL;
  266. m_pMetrics = new CXTPSkinManagerMetrics(this);
  267. m_bEnabled = FALSE;
  268. m_pResourceFile = new CXTPSkinManagerResourceFile();
  269. m_pResourceFile->m_pManager = this;
  270. m_pClassMap = new CXTPSkinObjectClassMap();
  271. m_pClassMap->AddStandardClasses();
  272. m_mapObjects.InitHashTable(199, FALSE);
  273. m_bAutoApplyWindows = TRUE;
  274. m_bAutoApplyThreads = TRUE;
  275. m_dwApplyOptions = xtpSkinApplyFrame | xtpSkinApplyColors | xtpSkinApplyMetrics;
  276. m_dwComCtrl = XTPSystemVersion()->GetComCtlVersion();
  277. m_bWin9x = XTPSystemVersion()->IsWin9x();
  278. m_hWinEventHook = NULL;
  279. m_pSetWinEventHook = NULL;
  280. m_pUnhookWinEvent = NULL;
  281. CXTPSkinManagerApiHook* pApiHook = CXTPSkinManagerApiHook::GetInstance();
  282. pApiHook;
  283. HMODULE hLib = GetModuleHandle(_T("USER32"));
  284. if (hLib)
  285. {
  286. m_pSetWinEventHook = (LPFNSETWINEVENTHOOK)GetProcAddress(hLib, "SetWinEventHook");
  287. m_pUnhookWinEvent = (LPFNUNHOOKWINEVENT)GetProcAddress(hLib, "UnhookWinEvent");
  288. if (m_pSetWinEventHook && m_pUnhookWinEvent)
  289. {
  290. m_hWinEventHook = m_pSetWinEventHook(EVENT_OBJECT_REORDER, EVENT_OBJECT_REORDER, NULL,
  291. &CXTPSkinManager::WinEventProc, GetCurrentProcessId(), 0, 0);
  292. }
  293. }
  294. EnableCurrentThread();
  295. }
  296. void CXTPSkinManager::EnableCurrentThread()
  297. {
  298. DWORD dwThread = ::GetCurrentThreadId();
  299. XTP_SKINAMANGER_THREAD_STATE* pThreadState = _xtpSkinManagerThreadState.GetData();
  300. if (pThreadState)
  301. {
  302. if (pThreadState->m_hHookOldCbtFilter == 0)
  303. {
  304. pThreadState->m_hHookOldCbtFilter = ::SetWindowsHookEx(WH_CBT, CbtFilterHook, NULL, dwThread);
  305. }
  306. }
  307. }
  308. CXTPSkinManager::~CXTPSkinManager()
  309. {
  310. FreeSkinData();
  311. RemoveColorFilters();
  312. POSITION pos = m_mapClasses.GetStartPosition();
  313. CString strClassName;
  314. CXTPSkinManagerClass* pClass;
  315. while (pos != NULL)
  316. {
  317. m_mapClasses.GetNextAssoc( pos, strClassName, (void*&)pClass);
  318. delete pClass;
  319. }
  320. m_mapClasses.RemoveAll();
  321. SAFE_DELETE(m_pSchema);
  322. SAFE_DELETE(m_pMetrics);
  323. SAFE_DELETE(m_pResourceFile);
  324. SAFE_DELETE(m_pClassMap);
  325. RemoveAll();
  326. CXTPSkinManagerApiHook* pApiHook = CXTPSkinManagerApiHook::GetInstance();
  327. pApiHook->FinalizeHookManagement();
  328. HMODULE hLib = GetModuleHandle(_T("USER32"));
  329. if (m_hWinEventHook && hLib && m_pUnhookWinEvent)
  330. {
  331. m_pUnhookWinEvent(m_hWinEventHook);
  332. }
  333. }
  334. BOOL CXTPSkinManager::IsColorFilterExists() const
  335. {
  336. return m_arrFilters.GetSize() > 0;
  337. }
  338. void CXTPSkinManager::RemoveColorFilters()
  339. {
  340. for (int i = 0; i < (int)m_arrFilters.GetSize(); i++)
  341. {
  342. delete m_arrFilters[i];
  343. }
  344. m_arrFilters.RemoveAll();
  345. }
  346. void CXTPSkinManager::AddColorFilter(CXTPSkinManagerColorFilter* pFilter)
  347. {
  348. m_arrFilters.Add(pFilter);
  349. }
  350. void CXTPSkinManager::RedrawAllControls()
  351. {
  352. if (!m_bEnabled)
  353. return;
  354. POSITION pos = m_mapClasses.GetStartPosition();
  355. CString strClassName;
  356. CXTPSkinManagerClass* pClass;
  357. while (pos != NULL)
  358. {
  359. m_mapClasses.GetNextAssoc( pos, strClassName, (void*&)pClass);
  360. pClass->GetImages()->RemoveAll();
  361. }
  362. m_pMetrics->RefreshMetrics();
  363. XTPColorManager()->RefreshColors();
  364. OnSkinChanged(TRUE, TRUE);
  365. }
  366. void CXTPSkinManager::ApplyColorFilter(COLORREF& clr)
  367. {
  368. for (int i = 0; i < (int)m_arrFilters.GetSize(); i++)
  369. {
  370. m_arrFilters[i]->ApplyColorFilter(clr);
  371. }
  372. }
  373. BOOL CXTPSkinManager::LoadSkin(LPCTSTR lpszResourcePath, LPCTSTR lpszIniFileName)
  374. {
  375. SAFE_DELETE(m_pSchema);
  376. CXTPSkinManagerApiHook* pApiHook = CXTPSkinManagerApiHook::GetInstance();
  377. pApiHook->FinalizeHookManagement();
  378. BOOL bEnabled = m_bEnabled;
  379. if (XTPColorManager()->IsLowResolution() ||
  380. !CXTPSkinManagerModuleList::IsEnumeratorExists() ||
  381. !ReadSkinData(lpszResourcePath, lpszIniFileName))
  382. {
  383. m_bEnabled = FALSE;
  384. SAFE_DELETE(m_pSchema);
  385. m_pMetrics->RefreshMetrics();
  386. XTPColorManager()->RefreshColors();
  387. OnSkinChanged(bEnabled, m_bEnabled);
  388. return FALSE;
  389. }
  390. m_bEnabled = TRUE;
  391. pApiHook->InitializeHookManagement();
  392. m_pMetrics->RefreshMetrics();
  393. m_pSchema->RefreshMetrcis();
  394. XTPColorManager()->RefreshColors();
  395. OnSkinChanged(bEnabled, m_bEnabled);
  396. return TRUE;
  397. }
  398. CXTPSkinManagerClass* CXTPSkinManager::FromHandle(HTHEME hTheme)
  399. {
  400. POSITION pos = m_mapClasses.GetStartPosition();
  401. CString strClassName;
  402. CXTPSkinManagerClass* pClass;
  403. while (pos != NULL)
  404. {
  405. m_mapClasses.GetNextAssoc( pos, strClassName, (void*&)pClass);
  406. if (pClass == (CXTPSkinManagerClass*)hTheme)
  407. return pClass;
  408. }
  409. return NULL;
  410. }
  411. void CXTPSkinManager::ExcludeModule(LPCTSTR lpszModule)
  412. {
  413. CXTPSkinManagerApiHook* pApiHook = CXTPSkinManagerApiHook::GetInstance();
  414. pApiHook->ExcludeModule(lpszModule, FALSE);
  415. }
  416. void CXTPSkinManager::SetApplyOptions(DWORD dwOptions)
  417. {
  418. m_dwApplyOptions = dwOptions;
  419. if (!m_bEnabled)
  420. return;
  421. CXTPSkinManagerApiHook* pApiHook = CXTPSkinManagerApiHook::GetInstance();
  422. pApiHook->FinalizeHookManagement();
  423. pApiHook->InitializeHookManagement();
  424. m_pMetrics->RefreshMetrics();
  425. m_pSchema->RefreshMetrcis();
  426. XTPColorManager()->RefreshColors();
  427. OnSkinChanged(m_bEnabled, m_bEnabled);
  428. }
  429. void CXTPSkinManager::SetResourceFile(CXTPSkinManagerResourceFile* pResourceFile)
  430. {
  431. ASSERT(pResourceFile);
  432. if (!pResourceFile)
  433. return;
  434. SAFE_DELETE(m_pResourceFile);
  435. m_pResourceFile = pResourceFile;
  436. m_pResourceFile->m_pManager = this;
  437. }
  438. void CXTPSkinManager::FreeSkinData()
  439. {
  440. POSITION pos = m_mapClasses.GetStartPosition();
  441. CString strClassName;
  442. CXTPSkinManagerClass* pClass;
  443. while (pos != NULL)
  444. {
  445. m_mapClasses.GetNextAssoc( pos, strClassName, (void*&)pClass);
  446. delete pClass;
  447. }
  448. m_mapClasses.RemoveAll();
  449. m_pResourceFile->Close();
  450. }
  451. BOOL CXTPSkinManager::ReadSkinData(LPCTSTR strResourcePath, LPCTSTR strIniFileName)
  452. {
  453. FreeSkinData();
  454. if (!m_pResourceFile->Open(strResourcePath, strIniFileName))
  455. return FALSE;
  456. m_pSchema = m_pResourceFile->CreateSchema();
  457. if (!m_pSchema)
  458. return FALSE;
  459. if (FAILED(m_pSchema->ReadProperties(m_pResourceFile)))
  460. return FALSE;
  461. return TRUE;
  462. }
  463. CXTPSkinManager* CXTPSkinManager::s_pInstance = NULL;
  464. static class CXTPSkinManager::CDestructor
  465. {
  466. public:
  467. ~CDestructor()
  468. {
  469. if (s_pInstance != NULL)
  470. delete s_pInstance;
  471. }
  472. } __xtpSkinManagerDestructor;
  473. LRESULT CALLBACK CXTPSkinManager::CbtFilterHook(int code, WPARAM wParam, LPARAM lParam)
  474. {
  475. XTP_SKINAMANGER_THREAD_STATE* pThreadState = _xtpSkinManagerThreadState.GetData();
  476. LRESULT lResult = CallNextHookEx(pThreadState->m_hHookOldCbtFilter, code,
  477. wParam, lParam);
  478. if (code == HCBT_CREATEWND && XTPSkinManager()->m_bAutoApplyWindows)
  479. {
  480. ASSERT(lParam != NULL);
  481. LPCREATESTRUCT lpcs = ((LPCBT_CREATEWND)lParam)->lpcs;
  482. ASSERT(lpcs != NULL);
  483. ASSERT(wParam != NULL); // should be non-NULL HWND
  484. HWND hWnd = (HWND)wParam;
  485. TCHAR lpszClassName[50];
  486. GetClassName(hWnd, lpszClassName, 50);
  487. XTPSkinManager()->SetHook(hWnd, lpszClassName, lpcs, TRUE);
  488. }
  489. return lResult;
  490. }
  491. _XTP_EXT_CLASS CXTPSkinManager* AFX_CDECL XTPSkinManager()
  492. {
  493. if (CXTPSkinManager::s_pInstance == NULL)
  494. CXTPSkinManager::s_pInstance = new CXTPSkinManager();
  495. return CXTPSkinManager::s_pInstance;
  496. }
  497. void CXTPSkinManager::SetSkinManager(CXTPSkinManager* pSkinManager)
  498. {
  499. if (s_pInstance != NULL)
  500. delete s_pInstance;
  501. s_pInstance = pSkinManager;
  502. }
  503. CXTPSkinManagerClass* CXTPSkinManager::GetSkinClass(CString strClassList)
  504. {
  505. strClassList.MakeUpper();
  506. CXTPSkinManagerClass* pClass;
  507. if (m_mapClasses.Lookup(strClassList, (void *&)pClass))
  508. return pClass;
  509. int nClassCode = m_pSchema->GetClassCode(strClassList);
  510. if (nClassCode == 0)
  511. return NULL; // Not supported;
  512. pClass = new CXTPSkinManagerClass(this);
  513. pClass->m_strClass = strClassList;
  514. pClass->m_nClassCode =  nClassCode;
  515. m_mapClasses.SetAt(strClassList, pClass);
  516. return pClass;
  517. }
  518. void CXTPSkinManager::GetCurrentThemeName(CString& strThemeFileName, CString& strColorBuff)
  519. {
  520. if (m_pResourceFile)
  521. {
  522. strThemeFileName = m_pResourceFile->GetResourcePath();
  523. strColorBuff = m_pResourceFile->GetIniFileName();
  524. }
  525. }
  526. int CXTPSkinManager::GetThemeSysSize(int iSizeId)
  527. {
  528. int iVal = 0;
  529. if (!SUCCEEDED(m_pSchema->GetIntProperty(m_pSchema->GetClassCode(_T("SYSMETRICS")), 0, 0, iSizeId, iVal)))
  530. return 0;
  531. return iVal;
  532. }
  533. BOOL CXTPSkinManager::GetThemeSysBool(int iBoolId)
  534. {
  535. BOOL bVal = FALSE;
  536. if (!SUCCEEDED(m_pSchema->GetBoolProperty(m_pSchema->GetClassCode(_T("SYSMETRICS")), 0, 0, iBoolId, bVal)))
  537. return FALSE;
  538. return bVal;
  539. }
  540. COLORREF CXTPSkinManager::GetThemeSysColor(int iColorId)
  541. {
  542. return iColorId >= TMT_FIRSTCOLOR && iColorId <= TMT_LASTCOLOR ?
  543. m_pMetrics->m_clrTheme[iColorId - TMT_FIRSTCOLOR] : (COLORREF)-1;
  544. }
  545. HRESULT CXTPSkinManager::GetThemeSysFont(int iFontId, LOGFONT *plf)
  546. {
  547. HRESULT hr = m_pSchema->GetFontProperty(m_pSchema->GetClassCode(_T("SYSMETRICS")), 0, 0, iFontId, *plf);
  548. return hr;
  549. }
  550. HRESULT CXTPSkinManager::EnableThemeDialogTexture(HWND hWnd, DWORD dwFlags)
  551. {
  552. CXTPSkinObjectFrame* pObject = (CXTPSkinObjectFrame*)Lookup(hWnd);
  553. if (!pObject)
  554. return E_INVALIDARG;
  555. pObject->m_dwDialogTexture = dwFlags;
  556. return S_OK;
  557. }
  558. //////////////////////////////////////////////////////////////////////////
  559. CXTPSkinManagerClass::CXTPSkinManagerClass(CXTPSkinManager* pManager)
  560. {
  561. m_pImages = new CXTPSkinImages();
  562. m_pManager = pManager;
  563. m_mapCachedProperties.InitHashTable(199);
  564. }
  565. CXTPSkinManagerClass::~CXTPSkinManagerClass()
  566. {
  567. delete m_pImages;
  568. }
  569. BOOL CXTPSkinManagerClass::DrawThemeText(CDC* pDC, int iPartId, int iStateId, const CString& strText, DWORD dwFormat, const RECT *pRect)
  570. {
  571. pDC->SetBkMode(TRANSPARENT);
  572. CRect rcCaptionMargins = GetThemeRect(iPartId, iStateId, TMT_CAPTIONMARGINS);
  573. rcCaptionMargins.top = rcCaptionMargins.bottom = 0;
  574. CRect rc(pRect);
  575. rc.DeflateRect(rcCaptionMargins);
  576. if (GetThemeEnumValue(iPartId, iStateId, TMT_CONTENTALIGNMENT) == CA_CENTER)
  577. {
  578. dwFormat |=  DT_CENTER;
  579. }
  580. pDC->SetTextColor(GetThemeColor(iPartId, iStateId, TMT_TEXTCOLOR, pDC->GetTextColor()));
  581. if (GetThemeEnumValue(iPartId, iStateId, TMT_TEXTSHADOWTYPE) == TST_SINGLE)
  582. {
  583. CSize sz = GetThemeSize(iPartId, iStateId, TMT_TEXTSHADOWOFFSET);
  584. if (sz != CSize(0, 0))
  585. {
  586. COLORREF clrShadow = GetThemeColor(iPartId, iStateId, TMT_TEXTSHADOWCOLOR);
  587. CRect rcShadow(rc);
  588. rcShadow.OffsetRect(sz);
  589. COLORREF clr = pDC->SetTextColor(clrShadow);
  590. pDC->DrawText(strText, rcShadow, dwFormat);
  591. pDC->SetTextColor(clr);
  592. }
  593. }
  594. pDC->DrawText(strText, rc, dwFormat);
  595. return TRUE;
  596. }
  597. CXTPSkinManagerSchemaProperty* CXTPSkinManagerClass::GetProperty(XTPSkinManagerProperty propType, int iPartId, int iStateId, int iPropId)
  598. {
  599. UINT nCachedProp = iPropId + ((iPartId + (iStateId << 6 )) << 14);
  600. CXTPSkinManagerSchemaProperty* pProperty = NULL;
  601. if (m_mapCachedProperties.Lookup(nCachedProp, pProperty))
  602. return pProperty;
  603. pProperty = m_pManager->GetSchema()->GetProperty(m_nClassCode, iPartId, iStateId, iPropId);
  604. if (!pProperty)
  605. {
  606. m_mapCachedProperties.SetAt(nCachedProp, NULL);
  607. return 0;
  608. }
  609. if (pProperty->propType != propType)
  610. return 0;
  611. m_mapCachedProperties.SetAt(nCachedProp , pProperty);
  612. return pProperty;
  613. }
  614. CString CXTPSkinManagerClass::GetThemeString(int iPartId, int iStateId, int iPropId, LPCTSTR lpszDefault)
  615. {
  616. CXTPSkinManagerSchemaProperty* pProperty = GetProperty(XTP_SKINPROPERTY_STRING, iPartId, iStateId, iPropId);
  617. if (!pProperty)
  618. return lpszDefault;
  619. return pProperty->lpszVal;
  620. }
  621. CRect CXTPSkinManagerClass::GetThemeRect(int iPartId, int iStateId, int iPropId, CRect rcDefault)
  622. {
  623. CXTPSkinManagerSchemaProperty* pProperty = GetProperty(XTP_SKINPROPERTY_RECT, iPartId, iStateId, iPropId);
  624. if (!pProperty)
  625. return rcDefault;
  626. return pProperty->rcVal;
  627. }
  628. int CXTPSkinManagerClass::GetThemeInt(int iPartId, int iStateId, int iPropId, int nDefault)
  629. {
  630. CXTPSkinManagerSchemaProperty* pProperty = GetProperty(XTP_SKINPROPERTY_INT, iPartId, iStateId, iPropId);
  631. if (!pProperty)
  632. return nDefault;
  633. return pProperty->iVal;
  634. }
  635. BOOL CXTPSkinManagerClass::GetThemeBool(int iPartId, int iStateId, int iPropId, BOOL bDefault)
  636. {
  637. CXTPSkinManagerSchemaProperty* pProperty = GetProperty(XTP_SKINPROPERTY_BOOL, iPartId, iStateId, iPropId);
  638. if (!pProperty)
  639. return bDefault;
  640. return pProperty->bVal;
  641. }
  642. COLORREF CXTPSkinManagerClass::GetThemeColor(int iPartId, int iStateId, int iPropId, COLORREF clrDefault)
  643. {
  644. CXTPSkinManagerSchemaProperty* pProperty = GetProperty(XTP_SKINPROPERTY_COLOR, iPartId, iStateId, iPropId);
  645. if (!pProperty)
  646. return clrDefault;
  647. COLORREF clrVal = pProperty->clrVal;
  648. m_pManager->ApplyColorFilter(clrVal);
  649. return clrVal;
  650. }
  651. int CXTPSkinManagerClass::GetThemeEnumValue(int iPartId, int iStateId, int iPropId, int nDefault)
  652. {
  653. CXTPSkinManagerSchemaProperty* pProperty = GetProperty(XTP_SKINPROPERTY_ENUM, iPartId, iStateId, iPropId);
  654. if (!pProperty)
  655. return nDefault;
  656. return pProperty->iVal;
  657. }
  658. CSize CXTPSkinManagerClass::GetThemeSize(int iPartId, int iStateId, int iPropId, CSize szDefault)
  659. {
  660. CXTPSkinManagerSchemaProperty* pProperty = GetProperty(XTP_SKINPROPERTY_POSITION, iPartId, iStateId, iPropId);
  661. if (!pProperty)
  662. return szDefault;
  663. return pProperty->szVal;
  664. }
  665. BOOL CXTPSkinManagerClass::DrawThemeBackground(CDC* pDC, int iPartId, int iStateId, const RECT *pRect)
  666. {
  667. return m_pManager->GetSchema()->DrawThemeBackground(pDC, this, iPartId, iStateId, pRect);
  668. }
  669. BOOL CXTPSkinManagerClass::GetThemePartSize(int iPartId, int iStateId, RECT *pRect, THEMESIZE eSize, SIZE* pSize)
  670. {
  671. if (GetThemeEnumValue(iPartId, iStateId, TMT_BGTYPE) != BT_IMAGEFILE)
  672. return FALSE;
  673. int nImageFile = (GetThemeEnumValue(iPartId, iStateId, TMT_IMAGESELECTTYPE) != IST_NONE) &&
  674. (GetThemeEnumValue(iPartId, iStateId, TMT_GLYPHTYPE, GT_NONE) == GT_NONE) ?
  675. TMT_IMAGEFILE1: TMT_IMAGEFILE;
  676. CString strImageFile = GetThemeString(iPartId, iStateId, nImageFile);
  677. if (strImageFile.IsEmpty())
  678. {
  679. if (nImageFile != TMT_IMAGEFILE1)
  680. return FALSE;
  681. strImageFile = GetThemeString(iPartId, iStateId, TMT_IMAGEFILE);
  682. if (strImageFile.IsEmpty())
  683. return FALSE;
  684. }
  685. CXTPSkinImage* pImage = GetImages()->LoadFile(m_pManager->GetResourceFile(), strImageFile);
  686. if (!pImage)
  687. {
  688. return FALSE;
  689. }
  690. int nImageCount = GetThemeInt(iPartId, iStateId, TMT_IMAGECOUNT, 1);
  691. if (nImageCount < 1)
  692. nImageCount = 1;
  693. BOOL bHorizontalImageLayout = GetThemeEnumValue(iPartId, iStateId, TMT_IMAGELAYOUT, IL_HORIZONTAL) == IL_HORIZONTAL;
  694. CSize sz(pImage->GetWidth(), pImage->GetHeight());
  695. if (bHorizontalImageLayout) sz.cx /= nImageCount; else sz.cy /= nImageCount;
  696. if (eSize == TS_TRUE)
  697. {
  698. *pSize = sz;
  699. }
  700. if (eSize == TS_DRAW)
  701. {
  702. if (GetThemeEnumValue(iPartId, iStateId, TMT_SIZINGTYPE, ST_STRETCH) == ST_TRUESIZE)
  703. {
  704. *pSize = sz;
  705. return TRUE;
  706. }
  707. if (!pRect)
  708. {
  709. pSize->cy = 0;
  710. pSize->cx = 0;
  711. }
  712. else
  713. {
  714. pSize->cy = pRect->bottom - pRect->top;
  715. pSize->cx = pRect->right - pRect->left;
  716. }
  717. if (GetThemeBool(iPartId, iStateId, TMT_UNIFORMSIZING, FALSE))
  718. {
  719. pSize->cx = MulDiv(pSize->cy, sz.cx, sz.cy);
  720. }
  721. }
  722. return TRUE;
  723. }