EGPropertyGridItemColor.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 8k
Category:

Graph program

Development Platform:

Visual C++

  1. // PropTreeItemColor.cpp : implementation file
  2. //
  3. //  Copyright (C) 1998-2001 Scott Ramsay
  4. // sramsay@gonavi.com
  5. // http://www.gonavi.com
  6. //
  7. //  This material is provided "as is", with absolutely no warranty expressed
  8. //  or implied. Any use is at your own risk.
  9. // 
  10. //  Permission to use or copy this software for any purpose is hereby granted 
  11. //  without fee, provided the above notices are retained on all copies.
  12. //  Permission to modify the code and to distribute modified code is granted,
  13. //  provided the above notices are retained, and a notice that the code was
  14. //  modified is included with the above copyright notice.
  15. // 
  16. // If you use this code, drop me an email.  I'd like to know if you find the code
  17. // useful.
  18. #include "stdafx.h"
  19. #include "EGPropertyGrid.h"
  20. //#include "Resource.h"
  21. #include "EGPropertyGridItemColor.h"
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. extern HINSTANCE ghInst;
  28. typedef struct _ColorTableEntry
  29. {
  30. COLORREF color;
  31. RECT rcSpot;
  32. } ColorTableEntry;
  33. static ColorTableEntry _crColors[] = 
  34. {
  35.     {RGB(0x00, 0x00, 0x00)},
  36.     {RGB(0xA5, 0x2A, 0x00)},
  37.     {RGB(0x00, 0x40, 0x40)},
  38.     {RGB(0x00, 0x55, 0x00)},
  39.     {RGB(0x00, 0x00, 0x5E)},
  40.     {RGB(0x00, 0x00, 0x8B)},
  41.     {RGB(0x4B, 0x00, 0x82)},
  42.     {RGB(0x28, 0x28, 0x28)},
  43.     {RGB(0x8B, 0x00, 0x00)},
  44.     {RGB(0xFF, 0x68, 0x20)},
  45.     {RGB(0x8B, 0x8B, 0x00)},
  46.     {RGB(0x00, 0x93, 0x00)},
  47.     {RGB(0x38, 0x8E, 0x8E)},
  48.     {RGB(0x00, 0x00, 0xFF)},
  49.     {RGB(0x7B, 0x7B, 0xC0)},
  50.     {RGB(0x66, 0x66, 0x66)},
  51.     {RGB(0xFF, 0x00, 0x00)},
  52.     {RGB(0xFF, 0xAD, 0x5B)},
  53.     {RGB(0x32, 0xCD, 0x32)}, 
  54.     {RGB(0x3C, 0xB3, 0x71)},
  55.     {RGB(0x7F, 0xFF, 0xD4)},
  56.     {RGB(0x7D, 0x9E, 0xC0)},
  57.     {RGB(0x80, 0x00, 0x80)},
  58.     {RGB(0x7F, 0x7F, 0x7F)},
  59.     {RGB(0xFF, 0xC0, 0xCB)},
  60.     {RGB(0xFF, 0xD7, 0x00)},
  61.     {RGB(0xFF, 0xFF, 0x00)},    
  62.     {RGB(0x00, 0xFF, 0x00)},
  63.     {RGB(0x40, 0xE0, 0xD0)},
  64.     {RGB(0xC0, 0xFF, 0xFF)},
  65.     {RGB(0x48, 0x00, 0x48)},
  66.     {RGB(0xC0, 0xC0, 0xC0)},
  67.     {RGB(0xFF, 0xE4, 0xE1)},
  68.     {RGB(0xD2, 0xB4, 0x8C)},
  69.     {RGB(0xFF, 0xFF, 0xE0)},
  70.     {RGB(0x98, 0xFB, 0x98)},
  71.     {RGB(0xAF, 0xEE, 0xEE)},
  72.     {RGB(0x68, 0x83, 0x8B)},
  73.     {RGB(0xE6, 0xE6, 0xFA)},
  74.     {RGB(0xFF, 0xFF, 0xFF)}
  75. };
  76. static void ColorBox(CDC* pDC, CPoint pt, COLORREF clr, BOOL bHover)
  77. {
  78. CBrush br(clr);
  79. CBrush* obr = pDC->SelectObject(&br);
  80. pDC->PatBlt(pt.x, pt.y, 13, 13, PATCOPY);
  81. pDC->SelectObject(obr);
  82. CRect rc;
  83. rc.SetRect(pt.x - 2, pt.y - 2, pt.x + 15, pt.y + 15);
  84. pDC->DrawEdge(&rc, (bHover) ? BDR_SUNKENOUTER : BDR_RAISEDINNER, BF_RECT);
  85. }
  86. static LONG FindSpot(CPoint point)
  87. {
  88. for (LONG i=0; i<40; i++)
  89. {
  90. if (PtInRect(&_crColors[i].rcSpot, point))
  91. return i;
  92. }
  93. return -1;
  94. }
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CEGPropertyGridItemColor
  97. COLORREF* CEGPropertyGridItemColor::s_pColors = NULL;
  98. CEGPropertyGridItemColor::CEGPropertyGridItemColor() :
  99. m_cColor(0),
  100. m_cPrevColor(0),
  101. m_nSpot(-1),
  102. m_bButton(FALSE),
  103. m_bInDialog(FALSE)
  104. {
  105. }
  106. CEGPropertyGridItemColor::~CEGPropertyGridItemColor()
  107. {
  108. }
  109. BEGIN_MESSAGE_MAP(CEGPropertyGridItemColor, CWnd)
  110. //{{AFX_MSG_MAP(CEGPropertyGridItemColor)
  111. ON_WM_KILLFOCUS()
  112. ON_WM_PAINT()
  113. ON_WM_CLOSE()
  114. ON_WM_MOUSEMOVE()
  115. ON_WM_SETCURSOR()
  116. ON_WM_LBUTTONDOWN()
  117. //}}AFX_MSG_MAP
  118. END_MESSAGE_MAP()
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CEGPropertyGridItemColor message handlers
  121. void CEGPropertyGridItemColor::SetDefaultColorsList(COLORREF* pColors)
  122. {
  123. s_pColors = pColors;
  124. }
  125. void CEGPropertyGridItemColor::DrawAttribute(CDC* pDC, const RECT& rc)
  126. {
  127. ASSERT(m_pProp!=NULL);
  128. CRect r(rc);
  129. pDC->SelectObject(IsReadOnly() ? m_pProp->GetNormalFont() : m_pProp->GetBoldFont());
  130. if (!m_pProp->IsWindowEnabled())
  131. pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
  132. else
  133. pDC->SetTextColor(RGB(0,0,0));
  134. r.top += 1;
  135. r.right = r.left + r.Height() - 1;
  136. CBrush br(m_cColor);
  137. CBrush* pold = pDC->SelectObject(&br);
  138. pDC->PatBlt(r.left, r.top, r.Width(), r.Height(), PATCOPY);
  139. pDC->SelectObject(pold);
  140. pDC->DrawEdge(&r, EDGE_SUNKEN, BF_RECT);
  141. CString s;
  142. r = rc;
  143. r.left += r.Height();
  144. s.Format(_T("R = %d, G = %d, B = %d"), GetRValue(m_cColor),GetGValue(m_cColor), GetBValue(m_cColor));
  145. pDC->DrawText(s, r, DT_SINGLELINE|DT_VCENTER);
  146. }
  147. LPARAM CEGPropertyGridItemColor::GetItemValue()
  148. {
  149. return m_cColor;
  150. }
  151. void CEGPropertyGridItemColor::SetItemValue(LPARAM lParam)
  152. {
  153. m_cColor = (COLORREF)lParam;
  154. }
  155. void CEGPropertyGridItemColor::OnMove()
  156. {
  157. }
  158. void CEGPropertyGridItemColor::OnRefresh()
  159. {
  160. }
  161. void CEGPropertyGridItemColor::OnCommit()
  162. {
  163. ShowWindow(SW_HIDE);
  164. }
  165. void CEGPropertyGridItemColor::OnActivate()
  166. {
  167. CRect r;
  168. m_cPrevColor = m_cColor;
  169. r = m_rc;
  170. r.right = r.left + 150;
  171. r.bottom = r.top + 120;
  172. ASSERT(m_pProp!=NULL);
  173. m_pProp->GetCtrlParent()->ClientToScreen(r);
  174. if (!IsWindow(m_hWnd))
  175. {
  176. LPCTSTR pszClassName;
  177. pszClassName = AfxRegisterWndClass(CS_VREDRAW|CS_HREDRAW, LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_BTNFACE + 1));
  178. DWORD dwStyle = WS_POPUP|WS_DLGFRAME;
  179. CreateEx(0, pszClassName, _T(""), dwStyle, r, m_pProp->GetCtrlParent(), 0);
  180. m_rcButton.SetRect(40, 94, 110, 114);
  181. }
  182. SetWindowPos(NULL, r.left, r.top, r.Width() + 1, r.Height(), SWP_NOZORDER|SWP_SHOWWINDOW);
  183. SetFocus();
  184. }
  185. void CEGPropertyGridItemColor::OnKillFocus(CWnd* pNewWnd) 
  186. {
  187. CWnd::OnKillFocus(pNewWnd);
  188. if (!m_bInDialog)
  189. CommitChanges();
  190. }
  191. void CEGPropertyGridItemColor::OnPaint() 
  192. {
  193. CPaintDC dc(this);
  194. CPoint pt;
  195. for (LONG i=0; i<40; i++)
  196. {
  197. pt.x = (i & 7) * 18 + 3;
  198. pt.y = (i >> 3) * 18 + 3;
  199. ColorBox(&dc, pt, _crColors[i].color, m_nSpot==i);
  200. SetRect(&_crColors[i].rcSpot, pt.x, pt.y, pt.x + 13, pt.y + 13);
  201. }
  202. ASSERT(m_pProp!=NULL);
  203. dc.SelectObject(m_pProp->GetNormalFont());
  204. CString s(_T("More Colors"));
  205. dc.SetBkMode(TRANSPARENT);
  206. dc.SetTextColor(GetSysColor(COLOR_BTNTEXT));
  207. dc.DrawText(s, &m_rcButton, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
  208. dc.DrawEdge(&m_rcButton, m_bButton ? BDR_SUNKENOUTER : BDR_RAISEDINNER, BF_RECT);
  209. }
  210. void CEGPropertyGridItemColor::OnClose() 
  211. {
  212. CommitChanges();
  213. }
  214. void CEGPropertyGridItemColor::OnMouseMove(UINT, CPoint point) 
  215. {
  216. BOOL bButton;
  217. LONG nSpot;
  218. nSpot = FindSpot(point);
  219. if (nSpot!=m_nSpot)
  220. {
  221. Invalidate(FALSE);
  222. m_nSpot = nSpot;
  223. }
  224. bButton = m_rcButton.PtInRect(point);
  225. if (bButton!=m_bButton)
  226. {
  227. m_bButton = bButton;
  228. Invalidate(FALSE);
  229. }
  230. }
  231. BOOL CEGPropertyGridItemColor::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
  232. {
  233. if (nHitTest==HTCLIENT)
  234. {
  235. CPoint point;
  236. GetCursorPos(&point);
  237. ScreenToClient(&point);
  238. if (FindSpot(point)!=-1 || m_rcButton.PtInRect(point))
  239. {
  240. SetCursor( ::LoadCursor( NULL, MAKEINTRESOURCE(IDC_HAND) ) );
  241. return TRUE;
  242. }
  243. }
  244. return CWnd::OnSetCursor(pWnd, nHitTest, message);
  245. }
  246. void CEGPropertyGridItemColor::OnLButtonDown(UINT, CPoint point) 
  247. {
  248. if (m_nSpot!=-1)
  249. {
  250. m_cColor = _crColors[m_nSpot].color;
  251. CommitChanges();
  252. }
  253. else
  254. if (m_rcButton.PtInRect(point))
  255. {
  256. CHOOSECOLOR cc;
  257. COLORREF clr[16];
  258. ZeroMemory(&cc, sizeof(CHOOSECOLOR));
  259. cc.Flags = CC_FULLOPEN|CC_ANYCOLOR|CC_RGBINIT;
  260. cc.lStructSize = sizeof(CHOOSECOLOR);
  261. cc.hwndOwner = m_hWnd;
  262. cc.rgbResult = m_cColor;
  263. cc.lpCustColors = s_pColors ? s_pColors : clr;
  264. memset(clr, 0xff, sizeof(COLORREF) * 16);
  265. clr[0] = m_cColor;
  266. m_bInDialog = TRUE;
  267. ASSERT(m_pProp!=NULL);
  268. m_pProp->DisableInput();
  269. ShowWindow(SW_HIDE);
  270. if (ChooseColor(&cc))
  271. m_cColor = cc.rgbResult;
  272. m_pProp->DisableInput(FALSE);
  273. CommitChanges();
  274. }
  275. }