SymbolEdit.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // AeroEdit.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "SymbolEdit.h"
  5. #include "cp_main.h"
  6. // CSymbolEdit
  7. IMPLEMENT_DYNAMIC(CSymbolEdit, CEdit)
  8. CSymbolEdit::CSymbolEdit() :
  9. m_hSymbolIcon(NULL),
  10. m_bInternalIcon(false),
  11. m_colorPromptText(RGB(127, 127, 127))
  12. {
  13. m_fontPrompt.CreateFont(
  14. 16, // nHeight
  15. 0, // nWidth
  16. 0, // nEscapement
  17. 0, // nOrientation
  18. FW_NORMAL, // nWeight
  19. TRUE, // bItalic
  20. FALSE, // bUnderline
  21. 0, // cStrikeOut
  22. DEFAULT_CHARSET, // nCharSet
  23. OUT_DEFAULT_PRECIS, // nOutPrecision
  24. CLIP_DEFAULT_PRECIS, // nClipPrecision
  25. DEFAULT_QUALITY, // nQuality
  26. DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
  27. _T("Calibri"));
  28. m_editFocusColor = RGB(255, 255, 255);
  29. m_editNonFocusColor = RGB(240, 240, 240);
  30. m_editFocusBrush.CreateSolidBrush(m_editFocusColor);
  31. m_editNonFocusBrush.CreateSolidBrush(m_editNonFocusColor);
  32. m_searchButton.LoadStdImageDPI(Search_16, Search_20, Search_24, Search_32, _T("PNG"));
  33. }
  34. CSymbolEdit::~CSymbolEdit()
  35. {
  36. DestroyIcon();
  37. }
  38. BEGIN_MESSAGE_MAP(CSymbolEdit, CEdit)
  39. ON_WM_PAINT()
  40. ON_MESSAGE(WM_SETFONT, OnSetFont)
  41. ON_WM_CTLCOLOR_REFLECT()
  42. ON_WM_SETFOCUS()
  43. ON_WM_KILLFOCUS()
  44. END_MESSAGE_MAP()
  45. BOOL CSymbolEdit::PreTranslateMessage(MSG* pMsg)
  46. {
  47. // TODO: Add your specialized code here and/or call the base class
  48. // Intercept Ctrl + Z (Undo), Ctrl + X (Cut), Ctrl + C (Copy), Ctrl + V (Paste) and Ctrl + A (Select All)
  49. // before CEdit base class gets a hold of them.
  50. if (pMsg->message == WM_KEYDOWN &&
  51. CONTROL_PRESSED)
  52. {
  53. switch (pMsg->wParam)
  54. {
  55. case 'Z':
  56. Undo();
  57. return TRUE;
  58. case 'X':
  59. Cut();
  60. return TRUE;
  61. case 'C':
  62. Copy();
  63. return TRUE;
  64. case 'V':
  65. Paste();
  66. return TRUE;
  67. case 'A':
  68. SetSel(0, -1);
  69. return TRUE;
  70. }
  71. }
  72. switch (pMsg->message)
  73. {
  74. case WM_KEYDOWN:
  75. {
  76. if (pMsg->wParam == VK_RETURN)
  77. {
  78. CWnd *pWnd = GetParent();
  79. if (pWnd)
  80. {
  81. if (g_Opt.m_bFindAsYouType)
  82. {
  83. pWnd->SendMessage(NM_SEARCH_ENTER_PRESSED, 0, 0);
  84. }
  85. else
  86. {
  87. //Send a message to the parent to refill the lb from the search
  88. pWnd->PostMessage(CB_SEARCH, 0, 0);
  89. }
  90. }
  91. return TRUE;
  92. }
  93. else if (pMsg->wParam == VK_DOWN ||
  94. pMsg->wParam == VK_UP ||
  95. pMsg->wParam == VK_F3 ||
  96. pMsg->wParam == VK_PRIOR ||
  97. pMsg->wParam == VK_NEXT)
  98. {
  99. CWnd *pWnd = GetParent();
  100. if (pWnd)
  101. {
  102. pWnd->SendMessage(CB_UPDOWN, pMsg->wParam, pMsg->lParam);
  103. return TRUE;
  104. }
  105. }
  106. break;
  107. }
  108. }
  109. return CEdit::PreTranslateMessage(pMsg);
  110. }
  111. void CSymbolEdit::DestroyIcon()
  112. {
  113. // if icon was loaded internally, destroy it
  114. if (m_bInternalIcon || m_hSymbolIcon != NULL)
  115. ::DestroyIcon(m_hSymbolIcon);
  116. }
  117. void CSymbolEdit::PreSubclassWindow()
  118. {
  119. RecalcLayout();
  120. }
  121. void CSymbolEdit::SetSymbolIcon(HICON hIcon, BOOL redraw)
  122. {
  123. DestroyIcon();
  124. m_hSymbolIcon = hIcon;
  125. // icon was not loaded internally
  126. m_bInternalIcon = false;
  127. RecalcLayout();
  128. if (redraw)
  129. Invalidate(TRUE);
  130. }
  131. void CSymbolEdit::SetSymbolIcon(UINT id, BOOL redraw)
  132. {
  133. DestroyIcon();
  134. m_hSymbolIcon = (HICON)::LoadImage(
  135. AfxGetResourceHandle(),
  136. MAKEINTRESOURCE(id),
  137. IMAGE_ICON,
  138. 16,
  139. 16,
  140. LR_DEFAULTCOLOR | LR_LOADTRANSPARENT);
  141. ASSERT(m_hSymbolIcon != NULL);
  142. // icon was loaded internally
  143. m_bInternalIcon = true;
  144. RecalcLayout();
  145. if (redraw)
  146. Invalidate(TRUE);
  147. }
  148. void CSymbolEdit::SetPromptText(CString text, BOOL redraw)
  149. {
  150. m_strPromptText = text;
  151. if (redraw)
  152. Invalidate(TRUE);
  153. }
  154. void CSymbolEdit::SetPromptText(LPCTSTR szText, BOOL redraw)
  155. {
  156. m_strPromptText = szText;
  157. if (redraw)
  158. Invalidate(TRUE);
  159. }
  160. void CSymbolEdit::SetPromptTextColor(COLORREF color, BOOL redraw)
  161. {
  162. m_colorPromptText = color;
  163. if (redraw)
  164. Invalidate(TRUE);
  165. }
  166. void CSymbolEdit::SetPromptFont(CFont& font, BOOL redraw)
  167. {
  168. LOGFONT lf;
  169. memset(&lf, 0, sizeof(LOGFONT));
  170. font.GetLogFont(&lf);
  171. SetPromptFont(&lf);
  172. if (redraw)
  173. Invalidate(TRUE);
  174. }
  175. void CSymbolEdit::SetPromptFont(const LOGFONT* lpLogFont, BOOL redraw)
  176. {
  177. m_fontPrompt.DeleteObject();
  178. m_fontPrompt.CreateFontIndirect(lpLogFont);
  179. if (redraw)
  180. Invalidate(TRUE);
  181. }
  182. void CSymbolEdit::RecalcLayout()
  183. {
  184. int width = GetSystemMetrics(SM_CXSMICON);
  185. if (m_hSymbolIcon)
  186. {
  187. DWORD dwMargins = GetMargins();
  188. SetMargins(LOWORD(dwMargins), width + 6);
  189. }
  190. else
  191. {
  192. SetMargins(4, 24);
  193. }
  194. }
  195. // CSymbolEdit message handlers
  196. void CSymbolEdit::OnPaint()
  197. {
  198. CPaintDC dc(this);
  199. CRect rect;
  200. GetClientRect(&rect);
  201. DWORD margins = GetMargins();
  202. CRect textRect(rect);
  203. textRect.left += LOWORD(margins);
  204. textRect.right -= HIWORD(margins);
  205. // Clearing the background
  206. dc.FillSolidRect(rect, GetSysColor(COLOR_WINDOW));
  207. if (m_hSymbolIcon)
  208. {
  209. // Drawing the icon
  210. int width = GetSystemMetrics(SM_CXSMICON);
  211. int height = GetSystemMetrics(SM_CYSMICON);
  212. ::DrawIconEx(
  213. dc.m_hDC,
  214. rect.right - width - 1,
  215. 1,
  216. m_hSymbolIcon,
  217. width,
  218. height,
  219. 0,
  220. NULL,
  221. DI_NORMAL);
  222. rect.left += LOWORD(margins) + 1;
  223. rect.right -= (width + 7);
  224. }
  225. else
  226. {
  227. //rect.left += (LOWORD(dwMargins) + 1);
  228. //rect.right -= (HIWORD(dwMargins) + 1);
  229. }
  230. CString text;
  231. GetWindowText(text);
  232. CFont* oldFont = NULL;
  233. //rect.top += 1;
  234. if(this == GetFocus() || text.GetLength() > 0)
  235. {
  236. dc.FillSolidRect(rect, m_editFocusColor);
  237. oldFont = dc.SelectObject(GetFont());
  238. dc.DrawText(text, textRect, DT_SINGLELINE | DT_INTERNAL | DT_EDITCONTROL);
  239. dc.SelectObject(oldFont);
  240. }
  241. else
  242. {
  243. dc.FillSolidRect(rect, m_editNonFocusColor);
  244. if (this != GetFocus() && m_strPromptText.GetLength() > 0)
  245. {
  246. oldFont = dc.SelectObject(&m_fontPrompt);
  247. COLORREF color = dc.GetTextColor();
  248. dc.SetTextColor(m_colorPromptText);
  249. dc.DrawText(m_strPromptText, textRect, DT_LEFT | DT_SINGLELINE | DT_EDITCONTROL);
  250. dc.SetTextColor(color);
  251. dc.SelectObject(oldFont);
  252. }
  253. }
  254. m_searchButton.Draw(&dc, this, rect.right - 22, 4, false, false);
  255. //OutputDebugString(_T("OnPaint"));
  256. }
  257. void CSymbolEdit::OnSize(UINT nType, int cx, int cy)
  258. {
  259. CEdit::OnSize(nType, cx, cy);
  260. RecalcLayout();
  261. }
  262. LRESULT CSymbolEdit::OnSetFont(WPARAM wParam, LPARAM lParam)
  263. {
  264. DefWindowProc(WM_SETFONT, wParam, lParam);
  265. RecalcLayout();
  266. return 0;
  267. }
  268. HBRUSH CSymbolEdit::CtlColor(CDC* pDC, UINT n)
  269. {
  270. if (::GetFocus() == m_hWnd)
  271. {
  272. pDC->SetBkColor(m_editFocusColor);
  273. return m_editFocusBrush;
  274. }
  275. else
  276. {
  277. pDC->SetBkColor(m_editNonFocusColor);
  278. return m_editNonFocusBrush;
  279. }
  280. }
  281. void CSymbolEdit::OnSetFocus(CWnd* pOldWnd)
  282. {
  283. Invalidate(FALSE);
  284. CEdit::OnSetFocus(pOldWnd);
  285. }
  286. void CSymbolEdit::OnKillFocus(CWnd* pNewWnd)
  287. {
  288. Invalidate(FALSE);
  289. CEdit::OnKillFocus(pNewWnd);
  290. }