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

对话框与窗口

开发平台:

Visual C++

  1. // XTPControlPopupColor.cpp : implementation of the CXTPControlPopupColor class.
  2. //
  3. // This file is a part of the XTREME COMMANDBARS 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 "Resource.h"
  22. #include "Common/Resource.h"
  23. #include "Common/XTPResourceManager.h"
  24. #include "Common/XTPDrawHelpers.h"
  25. #include "Common/XTPColorManager.h"
  26. #include "Common/XTPImageManager.h"
  27. #include "Common/XTPPropExchange.h"
  28. #include "Common/XTPToolTipContext.h"
  29. #include "XTPControlPopup.h"
  30. #include "XTPControlButton.h"
  31. #include "XTPControlPopupColor.h"
  32. #include "XTPCommandBar.h"
  33. #include "XTPPaintManager.h"
  34. #ifdef _DEBUG
  35. #define new DEBUG_NEW
  36. #undef THIS_FILE
  37. static char THIS_FILE[] = __FILE__;
  38. #endif
  39. #define CLR_TO_RGBQUAD(clr) (RGB(GetBValue(clr), GetGValue(clr), GetRValue(clr)))
  40. #define XTP_SELECTOR_SIZE 18
  41. #define XTP_SELECTOR_MARGIN 2
  42. #define XTP_SELECTOR_WIDTH (XTP_SELECTOR_SIZE * 8 + XTP_SELECTOR_MARGIN * 2)
  43. #define XTP_SELECTOR_HEIGHT (XTP_SELECTOR_SIZE * 5 + XTP_SELECTOR_MARGIN * 2)
  44. IMPLEMENT_XTP_CONTROL(CXTPControlPopupColor, CXTPControlPopup)
  45. CXTPControlPopupColor::CXTPControlPopupColor()
  46. {
  47. m_controlType = xtpControlSplitButtonPopup;
  48. m_clr = (COLORREF)-1;
  49. EnableAutomation();
  50. }
  51. CXTPControlPopupColor::~CXTPControlPopupColor()
  52. {
  53. }
  54. void CXTPControlPopupColor::Copy(CXTPControl* pControl, BOOL bRecursive)
  55. {
  56. CXTPControlPopup::Copy(pControl, bRecursive);
  57. m_clr = ((CXTPControlPopupColor*)pControl)->m_clr;
  58. }
  59. void CXTPControlPopupColor::RedrawIcon(CXTPImageManagerIcon* pImage, CXTPImageManagerIconHandle& hHandle)
  60. {
  61. CXTPImageManagerIconHandle& hIcon = pImage->GetIcon();
  62. if (hIcon.IsEmpty())
  63. return;
  64. hHandle.CopyHandle(hIcon);
  65. if (!hHandle.IsAlpha())
  66. {
  67. ICONINFO info;
  68. if (GetIconInfo(hHandle.GetIcon(), &info))
  69. {
  70. {
  71. CXTPCompatibleDC dc(NULL, CBitmap::FromHandle(info.hbmColor));
  72. CXTPCompatibleDC dcMask(NULL, CBitmap::FromHandle(info.hbmMask));
  73. BITMAP bmp;
  74. ZeroMemory(&bmp, sizeof(BITMAP));
  75. ::GetObject(info.hbmColor, sizeof(BITMAP), &bmp);
  76. int nHeight = int((double)bmp.bmHeight / 5);
  77. CRect rc(0, bmp.bmHeight - nHeight, bmp.bmWidth, bmp.bmHeight);
  78. dc.FillSolidRect(rc, m_clr);
  79. dcMask.FillSolidRect(rc, 1);
  80. }
  81. hHandle = CreateIconIndirect(&info);
  82. ::DeleteObject(info.hbmMask);
  83. ::DeleteObject(info.hbmColor);
  84. }
  85. }
  86. else if (hHandle.PreMultiply())
  87. {
  88. LPDWORD pBits = (LPDWORD)hHandle.PreMultiply();
  89. CSize szIcon = hHandle.GetExtent();
  90. DWORD dwColor = 0xFF000000 | CLR_TO_RGBQUAD(m_clr);
  91. int nCount = (szIcon.cy / 5) * szIcon.cx;
  92. for (int i = 0; i < nCount; i++)
  93. {
  94. *pBits++ = dwColor;
  95. }
  96. }
  97. }
  98. void CXTPControlPopupColor::SetColor(COLORREF clr)
  99. {
  100. if (clr != m_clr)
  101. {
  102. m_clr = clr;
  103. CXTPImageManagerIconSet* pIconSet = GetImageManager()->GetIconSet(GetIconId());
  104. if (pIconSet)
  105. {
  106. pIconSet->RefreshAll();
  107. CXTPImageManagerIconSet::CIconSetMap* pMap = pIconSet->GetIcons();
  108. POSITION pos = pMap->GetStartPosition();
  109. UINT nWidth;
  110. CXTPImageManagerIcon* pImage;
  111. while (pos != NULL)
  112. {
  113. pMap->GetNextAssoc(pos, nWidth, pImage);
  114. CXTPImageManagerIconHandle hIcon;
  115. RedrawIcon(pImage, hIcon);
  116. if (!hIcon.IsEmpty())
  117. {
  118. pImage->SetIcon(hIcon);
  119. }
  120. }
  121. RedrawParent(FALSE);
  122. }
  123. }
  124. }
  125. COLORREF CXTPControlPopupColor::GetColor() const
  126. {
  127. return m_clr;
  128. }
  129. //////////////////////////////////////////////////////////////////////////
  130. IMPLEMENT_XTP_CONTROL(CXTPControlButtonColor, CXTPControlButton)
  131. CXTPControlButtonColor::CXTPControlButtonColor()
  132. {
  133. SetStyle(xtpButtonCaption);
  134. m_clr = COLORREF_NULL;
  135. }
  136. void CXTPControlButtonColor::Copy(CXTPControl* pControl, BOOL bRecursive)
  137. {
  138. CXTPControlButton::Copy(pControl, bRecursive);
  139. m_clr = ((CXTPControlButtonColor*)pControl)->m_clr;
  140. }
  141. void CXTPControlButtonColor::DoPropExchange(CXTPPropExchange* pPX)
  142. {
  143. CXTPControlButton::DoPropExchange(pPX);
  144. if (pPX->GetSchema() > _XTP_SCHEMA_1041)
  145. {
  146. PX_DWord(pPX, _T("Color"), m_clr, COLORREF_NULL);
  147. }
  148. }
  149. CSize CXTPControlButtonColor::GetSize(CDC* /*pDC*/)
  150. {
  151. return CSize(XTP_SELECTOR_WIDTH, max(23, GetPaintManager()->GetControlHeight()));
  152. }
  153. void CXTPControlButtonColor::Draw(CDC* pDC)
  154. {
  155. CXTPPaintManager* pPaintManager = GetPaintManager();
  156. CXTPFontDC font (pDC, pPaintManager->GetRegularFont());
  157. pPaintManager->DrawControlEntry(pDC, this);
  158. pDC->SetTextColor(pPaintManager->GetControlTextColor(this));
  159. CRect rcText = GetRect();
  160. pPaintManager->DrawControlText(pDC, this, &rcText, TRUE, FALSE, TRUE, FALSE);
  161. if (m_clr != COLORREF_NULL)
  162. {
  163. CRect rcButton(CPoint(m_rcControl.left + 2, m_rcControl.CenterPoint().y - XTP_SELECTOR_SIZE / 2), CSize(XTP_SELECTOR_SIZE, XTP_SELECTOR_SIZE));
  164. rcButton.DeflateRect(3, 3);
  165. if (GetEnabled())
  166. pDC->FillSolidRect(rcButton, m_clr);
  167. pDC->Draw3dRect(rcButton, GetXtremeColor(COLOR_3DSHADOW), GetXtremeColor(COLOR_3DSHADOW));
  168. }
  169. }
  170. void CXTPControlButtonColor::SetColor(COLORREF clr)
  171. {
  172. if (clr != m_clr)
  173. {
  174. m_clr = clr;
  175. RedrawParent(FALSE);
  176. }
  177. }
  178. COLORREF CXTPControlButtonColor::GetColor() const
  179. {
  180. return m_clr;
  181. }
  182. struct CXTPControlColorSelector::CColorInfo
  183. {
  184. CColorInfo(COLORREF _clrButton, UINT _toolTipID)
  185. {
  186. clrButton = _clrButton;
  187. toolTipID = _toolTipID;
  188. }
  189. COLORREF clrButton;
  190. UINT  toolTipID;
  191. CString strTip;
  192. };
  193. static CXTPControlColorSelector::CColorInfo extendedColors[] =
  194. {
  195. CXTPControlColorSelector::CColorInfo(RGB(0x00, 0x00, 0x00), XTP_IDS_CLR_BLACK),
  196. CXTPControlColorSelector::CColorInfo(RGB(0x99, 0x33, 0x00), XTP_IDS_CLR_BROWN),
  197. CXTPControlColorSelector::CColorInfo(RGB(0x33, 0x33, 0x00), XTP_IDS_CLR_OLIVE_GREEN),
  198. CXTPControlColorSelector::CColorInfo(RGB(0x00, 0x33, 0x00), XTP_IDS_CLR_DARK_GREEN),
  199. CXTPControlColorSelector::CColorInfo(RGB(0x00, 0x33, 0x66), XTP_IDS_CLR_DARK_TEAL),
  200. CXTPControlColorSelector::CColorInfo(RGB(0x00, 0x00, 0x80), XTP_IDS_CLR_DARK_BLUE),
  201. CXTPControlColorSelector::CColorInfo(RGB(0x33, 0x33, 0x99), XTP_IDS_CLR_INDIGO),
  202. CXTPControlColorSelector::CColorInfo(RGB(0x33, 0x33, 0x33), XTP_IDS_CLR_GRAY80),
  203. CXTPControlColorSelector::CColorInfo(RGB(0x80, 0x00, 0x00), XTP_IDS_CLR_DARK_RED),
  204. CXTPControlColorSelector::CColorInfo(RGB(0xFF, 0x66, 0x00), XTP_IDS_CLR_ORANGE),
  205. CXTPControlColorSelector::CColorInfo(RGB(0x80, 0x80, 0x00), XTP_IDS_CLR_DARK_YELLOW),
  206. CXTPControlColorSelector::CColorInfo(RGB(0x00, 0x80, 0x00), XTP_IDS_CLR_GREEN),
  207. CXTPControlColorSelector::CColorInfo(RGB(0x00, 0x80, 0x80), XTP_IDS_CLR_TEAL),
  208. CXTPControlColorSelector::CColorInfo(RGB(0x00, 0x00, 0xFF), XTP_IDS_CLR_BLUE),
  209. CXTPControlColorSelector::CColorInfo(RGB(0x66, 0x66, 0x99), XTP_IDS_CLR_BLUEGRAY),
  210. CXTPControlColorSelector::CColorInfo(RGB(0x80, 0x80, 0x80), XTP_IDS_CLR_GRAY50),
  211. CXTPControlColorSelector::CColorInfo(RGB(0xFF, 0x00, 0x00), XTP_IDS_CLR_RED),
  212. CXTPControlColorSelector::CColorInfo(RGB(0xFF, 0x99, 0x00), XTP_IDS_CLR_LIGHTORANGE),
  213. CXTPControlColorSelector::CColorInfo(RGB(0x99, 0xCC, 0x00), XTP_IDS_CLR_LIME),
  214. CXTPControlColorSelector::CColorInfo(RGB(0x33, 0x99, 0x66), XTP_IDS_CLR_SEA_GREEN),
  215. CXTPControlColorSelector::CColorInfo(RGB(0x33, 0xCC, 0xCC), XTP_IDS_CLR_AQUA),
  216. CXTPControlColorSelector::CColorInfo(RGB(0x33, 0x66, 0xFF), XTP_IDS_CLR_LIGHT_BLUE),
  217. CXTPControlColorSelector::CColorInfo(RGB(0x80, 0x00, 0x80), XTP_IDS_CLR_VIOLET),
  218. CXTPControlColorSelector::CColorInfo(RGB(0x99, 0x99, 0x99), XTP_IDS_CLR_GRAY40),
  219. CXTPControlColorSelector::CColorInfo(RGB(0xFF, 0x00, 0xFF), XTP_IDS_CLR_PINK),
  220. CXTPControlColorSelector::CColorInfo(RGB(0xFF, 0xCC, 0x00), XTP_IDS_CLR_GOLD),
  221. CXTPControlColorSelector::CColorInfo(RGB(0xFF, 0xFF, 0x00), XTP_IDS_CLR_YELLOW),
  222. CXTPControlColorSelector::CColorInfo(RGB(0x00, 0xFF, 0x00), XTP_IDS_CLR_BRIGHT_GREEN),
  223. CXTPControlColorSelector::CColorInfo(RGB(0x00, 0xFF, 0xFF), XTP_IDS_CLR_TURQUOISE),
  224. CXTPControlColorSelector::CColorInfo(RGB(0x00, 0xCC, 0xFF), XTP_IDS_CLR_SKY_BLUE),
  225. CXTPControlColorSelector::CColorInfo(RGB(0x99, 0x33, 0x66), XTP_IDS_CLR_PLUM),
  226. CXTPControlColorSelector::CColorInfo(RGB(0xC0, 0xC0, 0xC0), XTP_IDS_CLR_GRAY25),
  227. CXTPControlColorSelector::CColorInfo(RGB(0xFF, 0x99, 0xCC), XTP_IDS_CLR_ROSE),
  228. CXTPControlColorSelector::CColorInfo(RGB(0xFF, 0xCC, 0x99), XTP_IDS_CLR_TAN),
  229. CXTPControlColorSelector::CColorInfo(RGB(0xFF, 0xFF, 0x99), XTP_IDS_CLR_LIGHT_YELLOW),
  230. CXTPControlColorSelector::CColorInfo(RGB(0xCC, 0xFF, 0xCC), XTP_IDS_CLR_LIGHT_GREEN),
  231. CXTPControlColorSelector::CColorInfo(RGB(0xCC, 0xFF, 0xFF), XTP_IDS_CLR_LIGHT_TURQUOISE),
  232. CXTPControlColorSelector::CColorInfo(RGB(0x99, 0xCC, 0xFF), XTP_IDS_CLR_PALE_BLUE),
  233. CXTPControlColorSelector::CColorInfo(RGB(0xCC, 0x99, 0xFF), XTP_IDS_CLR_LAVENDER),
  234. CXTPControlColorSelector::CColorInfo(RGB(0xFF, 0xFF, 0xFF), XTP_IDS_CLR_WHITE),
  235. };
  236. IMPLEMENT_XTP_CONTROL(CXTPControlColorSelector, CXTPControl)
  237. CXTPControlColorSelector::CXTPControlColorSelector()
  238. {
  239. EnableAutomation();
  240. m_nSelected = -1;
  241. m_clr = (COLORREF)-1;
  242. SetFlags(xtpFlagNoMovable);
  243. int nCount = GetItemCount();
  244. for (int i = 0; i < nCount; i++)
  245. {
  246. XTPResourceManager()->LoadString(&extendedColors[i].strTip, extendedColors[i].toolTipID);
  247. }
  248. m_nPressed = -1;
  249. }
  250. CSize CXTPControlColorSelector::GetSize(CDC* /*pDC*/)
  251. {
  252. return CSize (XTP_SELECTOR_WIDTH, XTP_SELECTOR_HEIGHT);
  253. }
  254. INT_PTR CXTPControlColorSelector::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
  255. {
  256. int nIndex = HitTest(point);
  257. if (nIndex == -1)
  258. return 0;
  259. INT_PTR nHit = extendedColors[nIndex].toolTipID;
  260. CXTPToolTipContext::FillInToolInfo(pTI, GetParent()->GetSafeHwnd(), GetRect(nIndex), nHit, extendedColors[nIndex].strTip);
  261. return nHit;
  262. }
  263. CRect CXTPControlColorSelector::GetRect(int nIndex) const
  264. {
  265. int x = nIndex % 8, y = nIndex / 8;
  266. return CRect(
  267. CPoint(
  268. XTP_SELECTOR_MARGIN + m_rcControl.left + x * XTP_SELECTOR_SIZE,
  269. XTP_SELECTOR_MARGIN + m_rcControl.top + y * XTP_SELECTOR_SIZE),
  270. CSize(XTP_SELECTOR_SIZE, XTP_SELECTOR_SIZE));
  271. }
  272. int CXTPControlColorSelector::HitTest(CPoint point) const
  273. {
  274. int nCount = GetItemCount();
  275. if (!m_rcControl.PtInRect(point))
  276. return -1;
  277. for (int i = 0; i < nCount; i++)
  278. {
  279. if (GetRect(i).PtInRect(point))
  280. return i;
  281. }
  282. return -1;
  283. }
  284. void CXTPControlColorSelector::Draw(CDC* pDC)
  285. {
  286. int nCount = GetItemCount();
  287. for (int i = 0; i < nCount; i++)
  288. {
  289. CRect rcButton = GetRect(i);
  290. if (i == m_nSelected || extendedColors[i].clrButton == m_clr)
  291. {
  292. BOOL bPressed = GetPressed() && i == m_nSelected;
  293. GetPaintManager()->DrawRectangle(pDC, rcButton, i == m_nSelected, bPressed, GetEnabled(), extendedColors[i].clrButton == m_clr, FALSE, xtpBarTypeNormal, xtpBarPopup);
  294. }
  295. rcButton.DeflateRect(3, 3);
  296. if (GetEnabled())
  297. pDC->FillSolidRect(rcButton, extendedColors[i].clrButton);
  298. pDC->Draw3dRect(rcButton, GetXtremeColor(COLOR_3DSHADOW), GetXtremeColor(COLOR_3DSHADOW));
  299. }
  300. }
  301. void CXTPControlColorSelector::SetSelectedItem(int nItem)
  302. {
  303. if (m_nSelected != nItem)
  304. {
  305. m_nSelected = nItem;
  306. RedrawParent();
  307. }
  308. }
  309. void CXTPControlColorSelector::OnMouseMove(CPoint point)
  310. {
  311. int nHit = HitTest(point);
  312. if (nHit != -1)
  313. {
  314. SetSelectedItem(nHit);
  315. }
  316. }
  317. BOOL CXTPControlColorSelector::OnSetSelected(int bSelected)
  318. {
  319. if (!bSelected) m_nSelected = -1;
  320. if (IsKeyboardSelected(bSelected) && !m_bSelected)
  321. {
  322. SetSelectedItem(bSelected == TRUE_KEYBOARD_PREV ? GetItemCount() - 1 : 0);
  323. }
  324. return CXTPControl::OnSetSelected(bSelected);
  325. }
  326. BOOL CXTPControlColorSelector::IsFocused() const
  327. {
  328. return GetSelected();
  329. }
  330. int CXTPControlColorSelector::GetItemCount() const
  331. {
  332. return sizeof(extendedColors) / sizeof(extendedColors[0]);
  333. }
  334. BOOL CXTPControlColorSelector::OnHookKeyDown(UINT nChar, LPARAM /*lParam*/)
  335. {
  336. ASSERT(IsFocused());
  337. if (!IsFocused())
  338. return FALSE;
  339. CXTPDrawHelpers::KeyToLayout(GetParent(), nChar);
  340. switch (nChar)
  341. {
  342. case VK_HOME:
  343. SetSelectedItem(0);
  344. return TRUE;
  345. case VK_END:
  346. SetSelectedItem(GetItemCount() - 1);
  347. return TRUE;
  348. case VK_LEFT:
  349. if (m_nSelected <= 0)
  350. return FALSE;
  351. SetSelectedItem(m_nSelected - 1);
  352. return TRUE;
  353. case VK_RIGHT:
  354. if (m_nSelected >= GetItemCount() - 1)
  355. return FALSE;
  356. SetSelectedItem(m_nSelected + 1);
  357. return TRUE;
  358. case VK_UP:
  359. if (m_nSelected < 8)
  360. return FALSE;
  361. SetSelectedItem(m_nSelected - 8);
  362. return TRUE;
  363. case VK_DOWN:
  364. if (m_nSelected >= GetItemCount() - 8)
  365. return FALSE;
  366. SetSelectedItem(m_nSelected + 8);
  367. return TRUE;
  368. }
  369. return FALSE;
  370. }
  371. void CXTPControlColorSelector::OnExecute()
  372. {
  373. if (m_nPressed != -1)
  374. {
  375. m_clr = extendedColors[m_nPressed].clrButton;
  376. }
  377. CXTPControl::OnExecute();
  378. }
  379. COLORREF CXTPControlColorSelector::GetColor() const
  380. {
  381. return m_clr;
  382. }
  383. void CXTPControlColorSelector::SetColor(COLORREF clr)
  384. {
  385. if (clr != m_clr)
  386. {
  387. m_clr = clr;
  388. RedrawParent();
  389. }
  390. }
  391. void CXTPControlColorSelector::OnClick(BOOL bKeyboard, CPoint pt)
  392. {
  393. if (!GetEnabled()) return;
  394. int nHit = bKeyboard ? m_nSelected : HitTest(pt);
  395. if (nHit == -1) return;
  396. m_nPressed = nHit;
  397. if (!bKeyboard)
  398. {
  399. ClickToolBarButton();
  400. }
  401. else
  402. {
  403. OnExecute();
  404. }
  405. m_nPressed = -1;
  406. }
  407. void CXTPControlColorSelector::OnLButtonUp(CPoint point)
  408. {
  409. if (GetEnabled() && m_pParent->GetPosition() == xtpBarPopup)
  410. {
  411. OnClick(TRUE, point);
  412. }
  413. }