SymbolEdit.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. #define RANGE_START 3000
  9. #define CLEAR_LIST 3050
  10. #define LIST_MAX_COUNT 50
  11. IMPLEMENT_DYNAMIC(CSymbolEdit, CEdit)
  12. CSymbolEdit::CSymbolEdit() :
  13. m_hSymbolIcon(NULL),
  14. m_bInternalIcon(false),
  15. m_colorPromptText(RGB(127, 127, 127))
  16. {
  17. m_fontPrompt.CreateFont(
  18. 16, // nHeight
  19. 0, // nWidth
  20. 0, // nEscapement
  21. 0, // nOrientation
  22. FW_NORMAL, // nWeight
  23. TRUE, // bItalic
  24. FALSE, // bUnderline
  25. 0, // cStrikeOut
  26. DEFAULT_CHARSET, // nCharSet
  27. OUT_DEFAULT_PRECIS, // nOutPrecision
  28. CLIP_DEFAULT_PRECIS, // nClipPrecision
  29. DEFAULT_QUALITY, // nQuality
  30. DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
  31. _T("Calibri"));
  32. m_mouseDownOnSearches = false;
  33. m_mouseHoveringOverSearches = false;
  34. m_mouseDownOnClose = false;
  35. m_mouseHoveringOverClose = false;
  36. //m_searchButton.LoadStdImageDPI(Search_16, Search_20, Search_24, Search_32, _T("PNG"));
  37. m_closeButton.LoadStdImageDPI(search_close_16, Search_20, Search_24, Search_28, Search_32, _T("PNG"));
  38. m_searchesButton.LoadStdImageDPI(down_16, down_20, down_24, down_28, down_32, _T("PNG"));
  39. }
  40. CSymbolEdit::~CSymbolEdit()
  41. {
  42. DestroyIcon();
  43. }
  44. BEGIN_MESSAGE_MAP(CSymbolEdit, CEdit)
  45. ON_WM_PAINT()
  46. ON_MESSAGE(WM_SETFONT, OnSetFont)
  47. //ON_MESSAGE(WM_EXITMENULOOP, OnMenuExit)
  48. ON_WM_CTLCOLOR_REFLECT()
  49. ON_WM_SETFOCUS()
  50. ON_WM_KILLFOCUS()
  51. ON_WM_SETCURSOR()
  52. ON_WM_LBUTTONUP()
  53. ON_WM_LBUTTONDOWN()
  54. ON_WM_MOUSEMOVE()
  55. ON_COMMAND_RANGE(RANGE_START, (RANGE_START+ LIST_MAX_COUNT), OnSelectSearchString)
  56. ON_WM_EXITSIZEMOVE()
  57. END_MESSAGE_MAP()
  58. BOOL CSymbolEdit::PreTranslateMessage(MSG* pMsg)
  59. {
  60. // TODO: Add your specialized code here and/or call the base class
  61. // Intercept Ctrl + Z (Undo), Ctrl + X (Cut), Ctrl + C (Copy), Ctrl + V (Paste) and Ctrl + A (Select All)
  62. // before CEdit base class gets a hold of them.
  63. if (pMsg->message == WM_KEYDOWN &&
  64. CONTROL_PRESSED)
  65. {
  66. switch (pMsg->wParam)
  67. {
  68. case 'Z':
  69. Undo();
  70. return TRUE;
  71. case 'X':
  72. Cut();
  73. return TRUE;
  74. case 'C':
  75. Copy();
  76. return TRUE;
  77. case 'V':
  78. Paste();
  79. return TRUE;
  80. case 'A':
  81. SetSel(0, -1);
  82. return TRUE;
  83. }
  84. }
  85. switch (pMsg->message)
  86. {
  87. case WM_KEYDOWN:
  88. {
  89. if (pMsg->wParam == VK_RETURN)
  90. {
  91. CWnd *pWnd = GetParent();
  92. if (pWnd)
  93. {
  94. if (g_Opt.m_bFindAsYouType)
  95. {
  96. pWnd->SendMessage(NM_SEARCH_ENTER_PRESSED, 0, 0);
  97. }
  98. else
  99. {
  100. //Send a message to the parent to refill the lb from the search
  101. pWnd->PostMessage(CB_SEARCH, 0, 0);
  102. }
  103. AddToSearchHistory();
  104. }
  105. return TRUE;
  106. }
  107. else if (pMsg->wParam == VK_DOWN &&
  108. ((GetKeyState(VK_CONTROL) & 0x8000) || ((GetKeyState(VK_CONTROL) & 0x8000) && (GetKeyState(VK_SHIFT) & 0x8000))))
  109. {
  110. if (ShowSearchHistoryMenu())
  111. {
  112. return TRUE;
  113. }
  114. }
  115. else if (pMsg->wParam == VK_DOWN ||
  116. pMsg->wParam == VK_UP ||
  117. pMsg->wParam == VK_PRIOR ||
  118. pMsg->wParam == VK_NEXT)
  119. {
  120. CWnd *pWnd = GetParent();
  121. if (pWnd)
  122. {
  123. pWnd->SendMessage(CB_UPDOWN, pMsg->wParam, pMsg->lParam);
  124. return TRUE;
  125. }
  126. }
  127. else if (pMsg->wParam == VK_DELETE)
  128. {
  129. int startChar;
  130. int endChar;
  131. this->GetSel(startChar, endChar);
  132. CString cs;
  133. this->GetWindowText(cs);
  134. //if selection is at the end then forward this on to the parent to delete the selected clip
  135. if(startChar == cs.GetLength() &&
  136. endChar == cs.GetLength())
  137. {
  138. CWnd *pWnd = GetParent();
  139. if (pWnd)
  140. {
  141. pWnd->SendMessage(NM_DELETE, pMsg->wParam, pMsg->lParam);
  142. return TRUE;
  143. }
  144. }
  145. }
  146. break;
  147. }
  148. }
  149. return CEdit::PreTranslateMessage(pMsg);
  150. }
  151. void CSymbolEdit::AddToSearchHistory()
  152. {
  153. CString cs;
  154. this->GetWindowText(cs);
  155. if (cs != _T(""))
  156. {
  157. if (m_searches.GetCount() >= LIST_MAX_COUNT)
  158. {
  159. m_searches.RemoveAt(0);
  160. }
  161. bool existing = false;
  162. int count = m_searches.GetCount();
  163. for (int i = 0; i < count; i++)
  164. {
  165. if (m_searches[i] == cs)
  166. {
  167. m_searches.RemoveAt(i);
  168. m_searches.Add(cs);
  169. existing = true;
  170. break;
  171. }
  172. }
  173. if (existing == false)
  174. {
  175. m_searches.Add(cs);
  176. }
  177. }
  178. }
  179. bool CSymbolEdit::ShowSearchHistoryMenu()
  180. {
  181. if (m_searches.GetCount() == 0)
  182. {
  183. return false;
  184. }
  185. CMenu cmPopUp;
  186. cmPopUp.CreatePopupMenu();
  187. int count = min(m_searches.GetCount(), LIST_MAX_COUNT);
  188. for (int i = count-1; i >= 0; i--)
  189. {
  190. cmPopUp.AppendMenuW(MF_STRING, (RANGE_START + i), m_searches[i]);
  191. }
  192. cmPopUp.AppendMenu(MF_SEPARATOR);
  193. cmPopUp.AppendMenuW(MF_STRING, CLEAR_LIST, _T("Clear List"));
  194. CRect windowRect;
  195. this->GetWindowRect(&windowRect);
  196. POINT pp;
  197. GetCursorPos(&pp);
  198. POINT x = this->GetCaretPos();
  199. ClientToScreen(&x);
  200. x.y += windowRect.Height();
  201. cmPopUp.TrackPopupMenu(TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON, x.x, x.y, this, NULL);
  202. Invalidate();
  203. return true;
  204. }
  205. void CSymbolEdit::DestroyIcon()
  206. {
  207. // if icon was loaded internally, destroy it
  208. if (m_bInternalIcon || m_hSymbolIcon != NULL)
  209. ::DestroyIcon(m_hSymbolIcon);
  210. }
  211. void CSymbolEdit::PreSubclassWindow()
  212. {
  213. RecalcLayout();
  214. }
  215. void CSymbolEdit::SetSymbolIcon(HICON hIcon, BOOL redraw)
  216. {
  217. DestroyIcon();
  218. m_hSymbolIcon = hIcon;
  219. // icon was not loaded internally
  220. m_bInternalIcon = false;
  221. RecalcLayout();
  222. if (redraw)
  223. Invalidate(TRUE);
  224. }
  225. void CSymbolEdit::SetSymbolIcon(UINT id, BOOL redraw)
  226. {
  227. DestroyIcon();
  228. m_hSymbolIcon = (HICON)::LoadImage(
  229. AfxGetResourceHandle(),
  230. MAKEINTRESOURCE(id),
  231. IMAGE_ICON,
  232. 16,
  233. 16,
  234. LR_DEFAULTCOLOR | LR_LOADTRANSPARENT);
  235. ASSERT(m_hSymbolIcon != NULL);
  236. // icon was loaded internally
  237. m_bInternalIcon = true;
  238. RecalcLayout();
  239. if (redraw)
  240. Invalidate(TRUE);
  241. }
  242. void CSymbolEdit::SetPromptText(CString text, BOOL redraw)
  243. {
  244. m_strPromptText = text;
  245. if (redraw)
  246. Invalidate(TRUE);
  247. }
  248. void CSymbolEdit::SetPromptText(LPCTSTR szText, BOOL redraw)
  249. {
  250. m_strPromptText = szText;
  251. if (redraw)
  252. Invalidate(TRUE);
  253. }
  254. void CSymbolEdit::SetPromptTextColor(COLORREF color, BOOL redraw)
  255. {
  256. m_colorPromptText = color;
  257. if (redraw)
  258. Invalidate(TRUE);
  259. }
  260. void CSymbolEdit::SetPromptFont(CFont& font, BOOL redraw)
  261. {
  262. LOGFONT lf;
  263. memset(&lf, 0, sizeof(LOGFONT));
  264. font.GetLogFont(&lf);
  265. SetPromptFont(&lf);
  266. if (redraw)
  267. Invalidate(TRUE);
  268. }
  269. void CSymbolEdit::SetPromptFont(const LOGFONT* lpLogFont, BOOL redraw)
  270. {
  271. m_fontPrompt.DeleteObject();
  272. m_fontPrompt.CreateFontIndirect(lpLogFont);
  273. if (redraw)
  274. Invalidate(TRUE);
  275. }
  276. void CSymbolEdit::RecalcLayout()
  277. {
  278. int width = GetSystemMetrics(SM_CXSMICON);
  279. if (m_hSymbolIcon)
  280. {
  281. DWORD dwMargins = GetMargins();
  282. SetMargins(LOWORD(dwMargins), width + 6);
  283. }
  284. else
  285. {
  286. SetMargins(4, theApp.m_metrics.ScaleX(34));
  287. }
  288. }
  289. // CSymbolEdit message handlers
  290. void CSymbolEdit::OnPaint()
  291. {
  292. CPaintDC dc(this);
  293. CRect rect;
  294. GetClientRect(&rect);
  295. DWORD margins = GetMargins();
  296. CRect textRect(rect);
  297. textRect.left += LOWORD(margins);
  298. textRect.right -= HIWORD(margins);
  299. // Clearing the background
  300. dc.FillSolidRect(rect, GetSysColor(COLOR_WINDOW));
  301. if (m_hSymbolIcon)
  302. {
  303. // Drawing the icon
  304. int width = GetSystemMetrics(SM_CXSMICON);
  305. int height = GetSystemMetrics(SM_CYSMICON);
  306. ::DrawIconEx(
  307. dc.m_hDC,
  308. rect.right - width - 1,
  309. 1,
  310. m_hSymbolIcon,
  311. width,
  312. height,
  313. 0,
  314. NULL,
  315. DI_NORMAL);
  316. rect.left += LOWORD(margins) + 1;
  317. rect.right -= (width + 7);
  318. }
  319. else
  320. {
  321. //rect.left += (LOWORD(dwMargins) + 1);
  322. //rect.right -= (HIWORD(dwMargins) + 1);
  323. }
  324. CString text;
  325. GetWindowText(text);
  326. CFont* oldFont = NULL;
  327. //rect.top += 1;
  328. if(this == GetFocus() || text.GetLength() > 0)
  329. {
  330. dc.FillSolidRect(rect, g_Opt.m_Theme.SearchTextBoxFocusBG());
  331. oldFont = dc.SelectObject(GetFont());
  332. COLORREF oldColor = dc.GetTextColor();
  333. dc.SetTextColor(g_Opt.m_Theme.SearchTextBoxFocusText());
  334. dc.DrawText(text, textRect, DT_SINGLELINE | DT_INTERNAL | DT_EDITCONTROL);
  335. dc.SelectObject(oldFont);
  336. dc.SetTextColor(oldColor);
  337. }
  338. else
  339. {
  340. dc.FillSolidRect(rect, g_Opt.m_Theme.MainWindowBG());
  341. }
  342. if (text.GetLength() == 0 && m_strPromptText.GetLength() > 0)
  343. {
  344. //if we aren't showing the close icon, then use the full space
  345. textRect.right += theApp.m_metrics.ScaleX(16);
  346. //textRect.right -= LOWORD(margins);
  347. oldFont = dc.SelectObject(&m_fontPrompt);
  348. COLORREF color = dc.GetTextColor();
  349. dc.SetTextColor(m_colorPromptText);
  350. dc.DrawText(m_strPromptText, textRect, DT_LEFT | DT_SINGLELINE | DT_EDITCONTROL | DT_VCENTER);
  351. dc.SetTextColor(color);
  352. dc.SelectObject(oldFont);
  353. }
  354. int right = rect.right;
  355. if ((text.GetLength() > 0 || this == GetFocus()))
  356. {
  357. m_searchesButtonRect.SetRect(rect.right - theApp.m_metrics.ScaleX(18), 0, rect.right, rect.bottom);
  358. right = rect.right - theApp.m_metrics.ScaleX(18);
  359. m_searchesButton.Draw(&dc, this, m_searchesButtonRect.left, 4, m_mouseHoveringOverSearches, m_mouseDownOnSearches);
  360. }
  361. else
  362. {
  363. m_searchesButtonRect.SetRect(0, 0, 0, 0);
  364. //m_searchButton.Draw(&dc, this, rect.right - 22, 4, false, false);
  365. }
  366. if (text.GetLength() > 0)
  367. {
  368. OutputDebugString(_T("showing close button\n"));
  369. m_closeButtonRect.SetRect(right - theApp.m_metrics.ScaleX(16), 0, right, rect.bottom);
  370. m_closeButton.Draw(&dc, this, m_closeButtonRect.left, 4, m_mouseHoveringOverClose, m_mouseDownOnClose);
  371. }
  372. else
  373. {
  374. OutputDebugString(_T("not showing close button\n"));
  375. m_closeButtonRect.SetRect(0, 0, 0, 0);
  376. //m_searchButton.Draw(&dc, this, rect.right - 22, 4, false, false);
  377. }
  378. //OutputDebugString(_T("OnPaint"));
  379. }
  380. void CSymbolEdit::OnSize(UINT nType, int cx, int cy)
  381. {
  382. CEdit::OnSize(nType, cx, cy);
  383. RecalcLayout();
  384. }
  385. LRESULT CSymbolEdit::OnSetFont(WPARAM wParam, LPARAM lParam)
  386. {
  387. DefWindowProc(WM_SETFONT, wParam, lParam);
  388. RecalcLayout();
  389. return 0;
  390. }
  391. HBRUSH CSymbolEdit::CtlColor(CDC* pDC, UINT n)
  392. {
  393. if (::GetFocus() == m_hWnd)
  394. {
  395. pDC->SetBkColor(RGB(255, 255, 255));
  396. return CreateSolidBrush(RGB(255, 255, 255));
  397. }
  398. else
  399. {
  400. pDC->SetBkColor(g_Opt.m_Theme.MainWindowBG());
  401. return CreateSolidBrush(g_Opt.m_Theme.MainWindowBG());
  402. }
  403. }
  404. void CSymbolEdit::OnSetFocus(CWnd* pOldWnd)
  405. {
  406. Invalidate(FALSE);
  407. CEdit::OnSetFocus(pOldWnd);
  408. CWnd *pWnd = GetParent();
  409. if (pWnd)
  410. {
  411. if (g_Opt.m_bFindAsYouType)
  412. {
  413. pWnd->SendMessage(NM_FOCUS_ON_SEARCH, 0, 0);
  414. }
  415. }
  416. }
  417. void CSymbolEdit::OnKillFocus(CWnd* pNewWnd)
  418. {
  419. AddToSearchHistory();
  420. Invalidate(FALSE);
  421. CEdit::OnKillFocus(pNewWnd);
  422. }
  423. BOOL CSymbolEdit::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  424. {
  425. CPoint pntCursor;
  426. GetCursorPos(&pntCursor);
  427. ScreenToClient(&pntCursor);
  428. if(m_closeButtonRect.PtInRect(pntCursor))
  429. {
  430. HCURSOR h = ::LoadCursor(NULL, IDC_ARROW);
  431. ::SetCursor(h);
  432. return TRUE;
  433. }
  434. if (m_searchesButtonRect.PtInRect(pntCursor))
  435. {
  436. HCURSOR h = ::LoadCursor(NULL, IDC_ARROW);
  437. ::SetCursor(h);
  438. return TRUE;
  439. }
  440. return CEdit::OnSetCursor(pWnd, nHitTest, message);
  441. }
  442. void CSymbolEdit::OnLButtonUp(UINT nFlags, CPoint point)
  443. {
  444. if (m_mouseDownOnClose)
  445. {
  446. ReleaseCapture();
  447. InvalidateRect(m_closeButtonRect);
  448. }
  449. if (m_mouseDownOnSearches)
  450. {
  451. ReleaseCapture();
  452. InvalidateRect(m_searchesButtonRect);
  453. }
  454. m_mouseDownOnClose = false;
  455. m_mouseDownOnSearches = false;
  456. if (m_closeButtonRect.PtInRect(point))
  457. {
  458. if ((GetWindowTextLength() > 0))
  459. {
  460. CWnd *pOwner = GetOwner();
  461. if (pOwner)
  462. {
  463. pOwner->SendMessage(NM_CANCEL_SEARCH, 0, 0);
  464. }
  465. }
  466. }
  467. if (m_searchesButtonRect.PtInRect(point))
  468. {
  469. this->ShowSearchHistoryMenu();
  470. }
  471. CEdit::OnLButtonUp(nFlags, point);
  472. }
  473. void CSymbolEdit::OnLButtonDown(UINT nFlags, CPoint point)
  474. {
  475. if (m_closeButtonRect.PtInRect(point))
  476. {
  477. m_mouseDownOnClose = true;
  478. SetCapture();
  479. InvalidateRect(m_closeButtonRect);
  480. }
  481. else
  482. {
  483. m_mouseDownOnClose = false;
  484. }
  485. if (m_searchesButtonRect.PtInRect(point))
  486. {
  487. m_mouseDownOnSearches = true;
  488. SetCapture();
  489. InvalidateRect(m_searchesButtonRect);
  490. }
  491. else
  492. {
  493. m_mouseDownOnSearches = false;
  494. }
  495. CEdit::OnLButtonDown(nFlags, point);
  496. }
  497. void CSymbolEdit::OnMouseMove(UINT nFlags, CPoint point)
  498. {
  499. if (m_closeButtonRect.PtInRect(point))
  500. {
  501. if (m_mouseHoveringOverClose == false)
  502. {
  503. m_mouseHoveringOverClose = true;
  504. InvalidateRect(m_closeButtonRect);
  505. }
  506. }
  507. else if(m_mouseHoveringOverClose)
  508. {
  509. m_mouseHoveringOverClose = false;
  510. InvalidateRect(m_closeButtonRect);
  511. }
  512. if (m_searchesButtonRect.PtInRect(point))
  513. {
  514. if (m_mouseHoveringOverSearches == false)
  515. {
  516. m_mouseHoveringOverSearches = true;
  517. InvalidateRect(m_searchesButtonRect);
  518. }
  519. }
  520. else if (m_mouseHoveringOverSearches)
  521. {
  522. m_mouseHoveringOverSearches = false;
  523. InvalidateRect(m_searchesButtonRect);
  524. }
  525. CEdit::OnMouseMove(nFlags, point);
  526. }
  527. void CSymbolEdit::OnSelectSearchString(UINT idIn)
  528. {
  529. int index = idIn - RANGE_START;
  530. if (idIn == CLEAR_LIST)
  531. {
  532. m_searches.RemoveAll();
  533. }
  534. else if (index >= 0 &&
  535. index < m_searches.GetCount())
  536. {
  537. CString cs = m_searches[index];
  538. this->SetWindowTextW(cs);
  539. this->SetFocus();
  540. this->SetSel(-1);
  541. this->Invalidate();
  542. m_searches.RemoveAt(index);
  543. m_searches.Add(cs);
  544. }
  545. }