SymbolEdit.cpp 9.1 KB

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