SymbolEdit.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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_searchButton.LoadStdImageDPI(Search_16, Search_20, Search_24, Search_32, _T("PNG"));
  29. m_closeButton.LoadStdImageDPI(search_close_16, Search_20, Search_24, Search_28, Search_32, _T("PNG"));
  30. }
  31. CSymbolEdit::~CSymbolEdit()
  32. {
  33. DestroyIcon();
  34. }
  35. BEGIN_MESSAGE_MAP(CSymbolEdit, CEdit)
  36. ON_WM_PAINT()
  37. ON_MESSAGE(WM_SETFONT, OnSetFont)
  38. ON_WM_CTLCOLOR_REFLECT()
  39. ON_WM_SETFOCUS()
  40. ON_WM_KILLFOCUS()
  41. ON_WM_SETCURSOR()
  42. ON_WM_LBUTTONUP()
  43. ON_WM_LBUTTONDOWN()
  44. ON_WM_MOUSEMOVE()
  45. END_MESSAGE_MAP()
  46. BOOL CSymbolEdit::PreTranslateMessage(MSG* pMsg)
  47. {
  48. // TODO: Add your specialized code here and/or call the base class
  49. // Intercept Ctrl + Z (Undo), Ctrl + X (Cut), Ctrl + C (Copy), Ctrl + V (Paste) and Ctrl + A (Select All)
  50. // before CEdit base class gets a hold of them.
  51. if (pMsg->message == WM_KEYDOWN &&
  52. CONTROL_PRESSED)
  53. {
  54. switch (pMsg->wParam)
  55. {
  56. case 'Z':
  57. Undo();
  58. return TRUE;
  59. case 'X':
  60. Cut();
  61. return TRUE;
  62. case 'C':
  63. Copy();
  64. return TRUE;
  65. case 'V':
  66. Paste();
  67. return TRUE;
  68. case 'A':
  69. SetSel(0, -1);
  70. return TRUE;
  71. }
  72. }
  73. switch (pMsg->message)
  74. {
  75. case WM_KEYDOWN:
  76. {
  77. if (pMsg->wParam == VK_RETURN)
  78. {
  79. CWnd *pWnd = GetParent();
  80. if (pWnd)
  81. {
  82. if (g_Opt.m_bFindAsYouType)
  83. {
  84. pWnd->SendMessage(NM_SEARCH_ENTER_PRESSED, 0, 0);
  85. }
  86. else
  87. {
  88. //Send a message to the parent to refill the lb from the search
  89. pWnd->PostMessage(CB_SEARCH, 0, 0);
  90. }
  91. }
  92. return TRUE;
  93. }
  94. else if (pMsg->wParam == VK_DOWN ||
  95. pMsg->wParam == VK_UP ||
  96. pMsg->wParam == VK_F3 ||
  97. pMsg->wParam == VK_PRIOR ||
  98. pMsg->wParam == VK_NEXT)
  99. {
  100. CWnd *pWnd = GetParent();
  101. if (pWnd)
  102. {
  103. pWnd->SendMessage(CB_UPDOWN, pMsg->wParam, pMsg->lParam);
  104. return TRUE;
  105. }
  106. }
  107. break;
  108. }
  109. }
  110. return CEdit::PreTranslateMessage(pMsg);
  111. }
  112. void CSymbolEdit::DestroyIcon()
  113. {
  114. // if icon was loaded internally, destroy it
  115. if (m_bInternalIcon || m_hSymbolIcon != NULL)
  116. ::DestroyIcon(m_hSymbolIcon);
  117. }
  118. void CSymbolEdit::PreSubclassWindow()
  119. {
  120. RecalcLayout();
  121. }
  122. void CSymbolEdit::SetSymbolIcon(HICON hIcon, BOOL redraw)
  123. {
  124. DestroyIcon();
  125. m_hSymbolIcon = hIcon;
  126. // icon was not loaded internally
  127. m_bInternalIcon = false;
  128. RecalcLayout();
  129. if (redraw)
  130. Invalidate(TRUE);
  131. }
  132. void CSymbolEdit::SetSymbolIcon(UINT id, BOOL redraw)
  133. {
  134. DestroyIcon();
  135. m_hSymbolIcon = (HICON)::LoadImage(
  136. AfxGetResourceHandle(),
  137. MAKEINTRESOURCE(id),
  138. IMAGE_ICON,
  139. 16,
  140. 16,
  141. LR_DEFAULTCOLOR | LR_LOADTRANSPARENT);
  142. ASSERT(m_hSymbolIcon != NULL);
  143. // icon was loaded internally
  144. m_bInternalIcon = true;
  145. RecalcLayout();
  146. if (redraw)
  147. Invalidate(TRUE);
  148. }
  149. void CSymbolEdit::SetPromptText(CString text, BOOL redraw)
  150. {
  151. m_strPromptText = text;
  152. if (redraw)
  153. Invalidate(TRUE);
  154. }
  155. void CSymbolEdit::SetPromptText(LPCTSTR szText, BOOL redraw)
  156. {
  157. m_strPromptText = szText;
  158. if (redraw)
  159. Invalidate(TRUE);
  160. }
  161. void CSymbolEdit::SetPromptTextColor(COLORREF color, BOOL redraw)
  162. {
  163. m_colorPromptText = color;
  164. if (redraw)
  165. Invalidate(TRUE);
  166. }
  167. void CSymbolEdit::SetPromptFont(CFont& font, BOOL redraw)
  168. {
  169. LOGFONT lf;
  170. memset(&lf, 0, sizeof(LOGFONT));
  171. font.GetLogFont(&lf);
  172. SetPromptFont(&lf);
  173. if (redraw)
  174. Invalidate(TRUE);
  175. }
  176. void CSymbolEdit::SetPromptFont(const LOGFONT* lpLogFont, BOOL redraw)
  177. {
  178. m_fontPrompt.DeleteObject();
  179. m_fontPrompt.CreateFontIndirect(lpLogFont);
  180. if (redraw)
  181. Invalidate(TRUE);
  182. }
  183. void CSymbolEdit::RecalcLayout()
  184. {
  185. int width = GetSystemMetrics(SM_CXSMICON);
  186. if (m_hSymbolIcon)
  187. {
  188. DWORD dwMargins = GetMargins();
  189. SetMargins(LOWORD(dwMargins), width + 6);
  190. }
  191. else
  192. {
  193. SetMargins(4, 24);
  194. }
  195. }
  196. // CSymbolEdit message handlers
  197. void CSymbolEdit::OnPaint()
  198. {
  199. CPaintDC dc(this);
  200. CRect rect;
  201. GetClientRect(&rect);
  202. DWORD margins = GetMargins();
  203. CRect textRect(rect);
  204. textRect.left += LOWORD(margins);
  205. textRect.right -= HIWORD(margins);
  206. // Clearing the background
  207. dc.FillSolidRect(rect, GetSysColor(COLOR_WINDOW));
  208. if (m_hSymbolIcon)
  209. {
  210. // Drawing the icon
  211. int width = GetSystemMetrics(SM_CXSMICON);
  212. int height = GetSystemMetrics(SM_CYSMICON);
  213. ::DrawIconEx(
  214. dc.m_hDC,
  215. rect.right - width - 1,
  216. 1,
  217. m_hSymbolIcon,
  218. width,
  219. height,
  220. 0,
  221. NULL,
  222. DI_NORMAL);
  223. rect.left += LOWORD(margins) + 1;
  224. rect.right -= (width + 7);
  225. }
  226. else
  227. {
  228. //rect.left += (LOWORD(dwMargins) + 1);
  229. //rect.right -= (HIWORD(dwMargins) + 1);
  230. }
  231. CString text;
  232. GetWindowText(text);
  233. CFont* oldFont = NULL;
  234. //rect.top += 1;
  235. if(this == GetFocus() || text.GetLength() > 0)
  236. {
  237. dc.FillSolidRect(rect, g_Opt.m_Theme.SearchTextBoxFocusBG());
  238. oldFont = dc.SelectObject(GetFont());
  239. COLORREF oldColor = dc.GetTextColor();
  240. dc.SetTextColor(g_Opt.m_Theme.SearchTextBoxFocusText());
  241. dc.DrawText(text, textRect, DT_SINGLELINE | DT_INTERNAL | DT_EDITCONTROL);
  242. dc.SelectObject(oldFont);
  243. dc.SetTextColor(oldColor);
  244. }
  245. else
  246. {
  247. dc.FillSolidRect(rect, g_Opt.m_Theme.MainWindowBG());
  248. }
  249. if (text.GetLength() == 0 && m_strPromptText.GetLength() > 0)
  250. {
  251. oldFont = dc.SelectObject(&m_fontPrompt);
  252. COLORREF color = dc.GetTextColor();
  253. dc.SetTextColor(m_colorPromptText);
  254. dc.DrawText(m_strPromptText, textRect, DT_LEFT | DT_SINGLELINE | DT_EDITCONTROL | DT_VCENTER);
  255. dc.SetTextColor(color);
  256. dc.SelectObject(oldFont);
  257. }
  258. if (text.GetLength() > 0)
  259. {
  260. m_closeButtonRect.SetRect(rect.right - 22, 0, rect.right, rect.bottom);
  261. m_closeButton.Draw(&dc, this, m_closeButtonRect.left, 4, m_mouseHoveringOverClose, m_mouseDownOnClose);
  262. }
  263. else
  264. {
  265. m_closeButtonRect.SetRect(0, 0, 0, 0);
  266. //m_searchButton.Draw(&dc, this, rect.right - 22, 4, false, false);
  267. }
  268. //OutputDebugString(_T("OnPaint"));
  269. }
  270. void CSymbolEdit::OnSize(UINT nType, int cx, int cy)
  271. {
  272. CEdit::OnSize(nType, cx, cy);
  273. RecalcLayout();
  274. }
  275. LRESULT CSymbolEdit::OnSetFont(WPARAM wParam, LPARAM lParam)
  276. {
  277. DefWindowProc(WM_SETFONT, wParam, lParam);
  278. RecalcLayout();
  279. return 0;
  280. }
  281. HBRUSH CSymbolEdit::CtlColor(CDC* pDC, UINT n)
  282. {
  283. if (::GetFocus() == m_hWnd)
  284. {
  285. pDC->SetBkColor(RGB(255, 255, 255));
  286. return CreateSolidBrush(RGB(255, 255, 255));
  287. }
  288. else
  289. {
  290. pDC->SetBkColor(g_Opt.m_Theme.MainWindowBG());
  291. return CreateSolidBrush(g_Opt.m_Theme.MainWindowBG());
  292. }
  293. }
  294. void CSymbolEdit::OnSetFocus(CWnd* pOldWnd)
  295. {
  296. Invalidate(FALSE);
  297. CEdit::OnSetFocus(pOldWnd);
  298. }
  299. void CSymbolEdit::OnKillFocus(CWnd* pNewWnd)
  300. {
  301. Invalidate(FALSE);
  302. CEdit::OnKillFocus(pNewWnd);
  303. }
  304. BOOL CSymbolEdit::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  305. {
  306. CPoint pntCursor;
  307. GetCursorPos(&pntCursor);
  308. ScreenToClient(&pntCursor);
  309. if(m_closeButtonRect.PtInRect(pntCursor))
  310. {
  311. HCURSOR h = ::LoadCursor(NULL, IDC_ARROW);
  312. ::SetCursor(h);
  313. return TRUE;
  314. }
  315. return CEdit::OnSetCursor(pWnd, nHitTest, message);
  316. }
  317. void CSymbolEdit::OnLButtonUp(UINT nFlags, CPoint point)
  318. {
  319. if (m_mouseDownOnClose)
  320. {
  321. ReleaseCapture();
  322. InvalidateRect(m_closeButtonRect);
  323. }
  324. m_mouseDownOnClose = false;
  325. if (m_closeButtonRect.PtInRect(point))
  326. {
  327. if ((GetWindowTextLength() > 0))
  328. {
  329. CWnd *pOwner = GetOwner();
  330. if (pOwner)
  331. {
  332. pOwner->SendMessage(NM_CANCEL_SEARCH, 0, 0);
  333. }
  334. }
  335. }
  336. CEdit::OnLButtonUp(nFlags, point);
  337. }
  338. void CSymbolEdit::OnLButtonDown(UINT nFlags, CPoint point)
  339. {
  340. if (m_closeButtonRect.PtInRect(point))
  341. {
  342. m_mouseDownOnClose = true;
  343. SetCapture();
  344. InvalidateRect(m_closeButtonRect);
  345. }
  346. else
  347. {
  348. m_mouseDownOnClose = false;
  349. }
  350. CEdit::OnLButtonDown(nFlags, point);
  351. }
  352. void CSymbolEdit::OnMouseMove(UINT nFlags, CPoint point)
  353. {
  354. if (m_closeButtonRect.PtInRect(point))
  355. {
  356. if (m_mouseHoveringOverClose == false)
  357. {
  358. m_mouseHoveringOverClose = true;
  359. InvalidateRect(m_closeButtonRect);
  360. }
  361. }
  362. else if(m_mouseHoveringOverClose)
  363. {
  364. m_mouseHoveringOverClose = false;
  365. InvalidateRect(m_closeButtonRect);
  366. }
  367. CEdit::OnMouseMove(nFlags, point);
  368. }