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

对话框与窗口

开发平台:

Visual C++

  1. // XTThemeManager.cpp: implementation of the CXTThemeManager class.
  2. //
  3. // This file is a part of the XTREME CONTROLS 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 "XTThemeManager.h"
  22. #include "Common/XTPDrawHelpers.h"
  23. #ifdef _DEBUG
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #define new DEBUG_NEW
  27. #endif
  28. CProcessLocal<CXTThemeManager> CXTThemeManager::m_sThemeManager;
  29. //////////////////////////////////////////////////////////////////////////
  30. // CXTThemeManagerStyle
  31. CXTThemeManagerStyle::CXTThemeManagerStyle()
  32. : m_enumTheme(xtThemeDefault)
  33. , m_pFactory(NULL)
  34. {
  35. }
  36. CXTThemeManagerStyle::~CXTThemeManagerStyle()
  37. {
  38. }
  39. void CXTThemeManagerStyle::RefreshMetrics()
  40. {
  41. }
  42. BOOL CXTThemeManagerStyle::IsOfficeTheme()
  43. {
  44. return m_enumTheme != xtThemeDefault;
  45. }
  46. BOOL CXTThemeManagerStyle::TakeSnapShot(CWnd* pWndOwner)
  47. {
  48. CWnd *pWndParent = pWndOwner->GetParent();
  49. if (::IsWindow(pWndParent->GetSafeHwnd()))
  50. {
  51. if (m_bmpSnapShot.GetSafeHandle() != NULL)
  52. m_bmpSnapShot.DeleteObject();
  53. //convert our coordinates to our parent coordinates.
  54. CXTPWindowRect rc(pWndOwner);
  55. pWndParent->ScreenToClient(&rc);
  56. //copy what's on the parents background at this point
  57. CDC *pDC = pWndParent->GetDC();
  58. CDC memDC;
  59. memDC.CreateCompatibleDC(pDC);
  60. m_bmpSnapShot.CreateCompatibleBitmap(pDC, rc.Width(), rc.Height());
  61. CXTPBitmapDC bitmapDC(&memDC, &m_bmpSnapShot);
  62. memDC.BitBlt(0, 0, rc.Width(), rc.Height(), pDC, rc.left, rc.top, SRCCOPY);
  63. pWndParent->ReleaseDC(pDC);
  64. return TRUE;
  65. }
  66. return FALSE;
  67. }
  68. BOOL CXTThemeManagerStyle::DrawTransparentBack(CDC* pDC, CWnd* pWndOwner)
  69. {
  70. if (::GetWindowLong(pWndOwner->GetSafeHwnd(), GWL_EXSTYLE) & WS_EX_TRANSPARENT)
  71. {
  72. // Get background.
  73. if (!TakeSnapShot(pWndOwner))
  74. return FALSE;
  75. CXTPClientRect rc(pWndOwner);
  76. CDC memDC;
  77. memDC.CreateCompatibleDC(pDC);
  78. CXTPBitmapDC bitmapDC(&memDC, &m_bmpSnapShot);
  79. pDC->BitBlt(0, 0, rc.Width(), rc.Height(), &memDC, 0, 0, SRCCOPY);
  80. return TRUE;
  81. }
  82. return FALSE;
  83. }
  84. //////////////////////////////////////////////////////////////////////////
  85. // CXTThemeManagerStyleFactory
  86. IMPLEMENT_DYNCREATE(CXTThemeManagerStyleFactory, CObject)
  87. CXTThemeManagerStyleFactory::CXTThemeManagerStyleFactory()
  88. : m_pTheme(NULL)
  89. , m_pNextFactory(NULL)
  90. {
  91. XTThemeManager()->m_factoryList.AddHead(this);
  92. m_hostList.Construct(offsetof(CXTThemeManagerStyleHost, m_pNextHost));
  93. }
  94. CXTThemeManagerStyleFactory::~CXTThemeManagerStyleFactory()
  95. {
  96. SAFE_DELETE(m_pTheme);
  97. XTThemeManager()->m_factoryList.Remove(this);
  98. m_hostList.RemoveAll();
  99. }
  100. CXTThemeManagerStyle* CXTThemeManagerStyleFactory::CreateTheme(XTThemeStyle /*theme*/)
  101. {
  102. ASSERT(FALSE);
  103. return NULL;
  104. }
  105. void CXTThemeManagerStyleFactory::SetTheme(CXTThemeManagerStyle* pTheme)
  106. {
  107. ASSERT(pTheme);
  108. if (!pTheme)
  109. return;
  110. SAFE_DELETE(m_pTheme);
  111. m_pTheme = pTheme;
  112. m_pTheme->m_pFactory = this;
  113. m_pTheme->RefreshMetrics();
  114. for (CXTThemeManagerStyleHost* pHost = m_hostList.GetHead();
  115. pHost != NULL; pHost = pHost->m_pNextHost)
  116. {
  117. pHost->OnThemeChanged();
  118. }
  119. }
  120. void CXTThemeManagerStyleFactory::SetTheme(XTThemeStyle theme)
  121. {
  122. SetTheme(CreateTheme(theme));
  123. }
  124. CXTThemeManagerStyle* CXTThemeManagerStyleFactory::GetSafeTheme()
  125. {
  126. if (m_pTheme == 0)
  127. {
  128. m_pTheme = CreateTheme(XTThemeManager()->m_enumTheme);
  129. m_pTheme->m_pFactory = this;
  130. m_pTheme->RefreshMetrics();
  131. }
  132. return m_pTheme;
  133. }
  134. void CXTThemeManagerStyleFactory::RefreshMetrics()
  135. {
  136. if (m_pTheme)
  137. {
  138. m_pTheme->RefreshMetrics();
  139. }
  140. }
  141. //////////////////////////////////////////////////////////////////////////
  142. // CXTThemeManagerStyleHost
  143. CXTThemeManagerStyleHost::CXTThemeManagerStyleHost(CRuntimeClass* pThemeFactoryClass)
  144. {
  145. m_bAutoDeleteCustomFactory = FALSE;
  146. m_pDefaultFactory = NULL;
  147. m_pCustomFactory = NULL;
  148. m_pNextHost = NULL;
  149. m_pThemeFactoryClass = NULL;
  150. if (pThemeFactoryClass)
  151. {
  152. InitStyleHost(pThemeFactoryClass);
  153. }
  154. }
  155. void CXTThemeManagerStyleHost::InitStyleHost(CRuntimeClass* pThemeFactoryClass)
  156. {
  157. ASSERT(pThemeFactoryClass);
  158. if (!pThemeFactoryClass)
  159. return;
  160. ASSERT(pThemeFactoryClass->IsDerivedFrom(RUNTIME_CLASS(CXTThemeManagerStyleFactory)));
  161. if (m_pDefaultFactory)
  162. {
  163. m_pDefaultFactory->m_hostList.Remove(this);
  164. }
  165. m_pThemeFactoryClass = pThemeFactoryClass;
  166. m_pDefaultFactory = XTThemeManager()->GetDefaultThemeFactory(pThemeFactoryClass);
  167. m_pDefaultFactory->m_hostList.AddHead(this);
  168. }
  169. CXTThemeManagerStyleHost::~CXTThemeManagerStyleHost()
  170. {
  171. GetSafeThemeFactory()->m_hostList.Remove(this);
  172. if (m_bAutoDeleteCustomFactory)
  173. {
  174. SAFE_DELETE(m_pCustomFactory);
  175. }
  176. }
  177. CXTThemeManagerStyleFactory* CXTThemeManagerStyleHost::CreateThemeFactory()
  178. {
  179. return (CXTThemeManagerStyleFactory*)m_pThemeFactoryClass->CreateObject();
  180. }
  181. CXTThemeManagerStyleFactory* CXTThemeManagerStyleHost::GetSafeThemeFactory() const
  182. {
  183. if (m_pCustomFactory != NULL)
  184. return m_pCustomFactory;
  185. return m_pDefaultFactory;
  186. }
  187. CXTThemeManagerStyle* CXTThemeManagerStyleHost::GetSafeTheme() const
  188. {
  189. CXTThemeManagerStyleFactory* pFactory = GetSafeThemeFactory();
  190. ASSERT(pFactory);
  191. if (!pFactory)
  192. return NULL;
  193. return pFactory->GetSafeTheme();
  194. }
  195. void CXTThemeManagerStyleHost::SetTheme (XTThemeStyle theme)
  196. {
  197. SetTheme(m_pDefaultFactory->CreateTheme(theme));
  198. }
  199. void CXTThemeManagerStyleHost::SetTheme (CXTThemeManagerStyle* pTheme)
  200. {
  201. if (m_pCustomFactory == NULL && pTheme == NULL)
  202. return;
  203. GetSafeThemeFactory()->m_hostList.Remove(this);
  204. if (m_bAutoDeleteCustomFactory)
  205. {
  206. SAFE_DELETE(m_pCustomFactory);
  207. }
  208. if (pTheme != NULL)
  209. {
  210. m_pCustomFactory = CreateThemeFactory();
  211. m_pCustomFactory->SetTheme(pTheme);
  212. m_bAutoDeleteCustomFactory = TRUE;
  213. }
  214. else
  215. {
  216. m_pCustomFactory = NULL;
  217. m_bAutoDeleteCustomFactory = FALSE;
  218. }
  219. OnThemeChanged();
  220. GetSafeThemeFactory()->m_hostList.AddHead(this);
  221. }
  222. void CXTThemeManagerStyleHost::AttachThemeFactory(CXTThemeManagerStyleFactory* pFactory)
  223. {
  224. GetSafeThemeFactory()->m_hostList.Remove(this);
  225. if (m_bAutoDeleteCustomFactory)
  226. {
  227. SAFE_DELETE(m_pCustomFactory);
  228. }
  229. m_pCustomFactory = pFactory;
  230. m_bAutoDeleteCustomFactory = FALSE;
  231. OnThemeChanged();
  232. GetSafeThemeFactory()->m_hostList.AddHead(this);
  233. }
  234. void CXTThemeManagerStyleHost::OnThemeChanged()
  235. {
  236. // Can be overridden by subclass
  237. }
  238. //////////////////////////////////////////////////////////////////////
  239. // Construction/Destruction
  240. //////////////////////////////////////////////////////////////////////
  241. CXTThemeManager::CXTThemeManager()
  242. {
  243. m_factoryList.Construct(offsetof(CXTThemeManagerStyleFactory, m_pNextFactory));
  244. m_enumTheme = xtThemeDefault;
  245. }
  246. CXTThemeManager::~CXTThemeManager()
  247. {
  248. POSITION pos = m_mapDefaultFactories.GetStartPosition();
  249. void* pThemeFactoryClass;
  250. CXTThemeManagerStyleFactory* pFactory = 0;
  251. while (pos)
  252. {
  253. m_mapDefaultFactories.GetNextAssoc(pos, pThemeFactoryClass, (void *&)pFactory);
  254. SAFE_DELETE(pFactory);
  255. }
  256. ASSERT(m_factoryList.m_pHead == 0);
  257. m_factoryList.RemoveAll();
  258. }
  259. CXTThemeManagerStyleFactory* CXTThemeManager::GetDefaultThemeFactory(CRuntimeClass* pThemeFactoryClass)
  260. {
  261. ASSERT(pThemeFactoryClass);
  262. if (!pThemeFactoryClass)
  263. return 0;
  264. ASSERT(pThemeFactoryClass->IsDerivedFrom(RUNTIME_CLASS(CXTThemeManagerStyleFactory)));
  265. CXTThemeManagerStyleFactory* pFactory = 0;
  266. if (!m_mapDefaultFactories.Lookup(pThemeFactoryClass, (void *&)pFactory))
  267. {
  268. pFactory = (CXTThemeManagerStyleFactory*)pThemeFactoryClass->CreateObject();
  269. m_mapDefaultFactories.SetAt(pThemeFactoryClass, pFactory);
  270. }
  271. return pFactory;
  272. }
  273. void CXTThemeManager::SetTheme(XTThemeStyle eTheme)
  274. {
  275. if (m_enumTheme == eTheme)
  276. return;
  277. m_enumTheme = eTheme;
  278. RefreshXtremeColors();
  279. POSITION pos = m_mapDefaultFactories.GetStartPosition();
  280. void* pThemeFactoryClass;
  281. CXTThemeManagerStyleFactory* pFactory = 0;
  282. while (pos)
  283. {
  284. m_mapDefaultFactories.GetNextAssoc(pos, pThemeFactoryClass, (void *&)pFactory);
  285. pFactory->SetTheme(eTheme);
  286. }
  287. }
  288. void CXTThemeManager::RefreshMetrics()
  289. {
  290. RefreshXtremeColors();
  291. for (CXTThemeManagerStyleFactory* pFactory = m_factoryList.GetHead();
  292. pFactory != NULL; pFactory = pFactory->m_pNextFactory)
  293. {
  294. pFactory->RefreshMetrics();
  295. }
  296. }
  297. _XTP_EXT_CLASS CXTThemeManager* AFX_CDECL XTThemeManager()
  298. {
  299. return CXTThemeManager::m_sThemeManager.GetData();
  300. }