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. // Clearing the background
  202. dc.FillSolidRect(rect, GetSysColor(COLOR_WINDOW));
  203. DWORD margins = GetMargins();
  204. if (m_hSymbolIcon)
  205. {
  206. // Drawing the icon
  207. int width = GetSystemMetrics(SM_CXSMICON);
  208. int height = GetSystemMetrics(SM_CYSMICON);
  209. ::DrawIconEx(
  210. dc.m_hDC,
  211. rect.right - width - 1,
  212. 1,
  213. m_hSymbolIcon,
  214. width,
  215. height,
  216. 0,
  217. NULL,
  218. DI_NORMAL);
  219. rect.left += LOWORD(margins) + 1;
  220. rect.right -= (width + 7);
  221. }
  222. else
  223. {
  224. //rect.left += (LOWORD(dwMargins) + 1);
  225. //rect.right -= (HIWORD(dwMargins) + 1);
  226. }
  227. CString text;
  228. GetWindowText(text);
  229. CFont* oldFont = NULL;
  230. //rect.top += 1;
  231. if(this == GetFocus() || text.GetLength() > 0)
  232. {
  233. dc.FillSolidRect(rect, m_editFocusColor);
  234. oldFont = dc.SelectObject(GetFont());
  235. rect.left += LOWORD(margins);
  236. rect.right -= HIWORD(margins);
  237. dc.DrawText(text, rect, DT_SINGLELINE | DT_INTERNAL | DT_EDITCONTROL);
  238. dc.SelectObject(oldFont);
  239. }
  240. else
  241. {
  242. dc.FillSolidRect(rect, m_editNonFocusColor);
  243. if (this != GetFocus() && m_strPromptText.GetLength() > 0)
  244. {
  245. oldFont = dc.SelectObject(&m_fontPrompt);
  246. COLORREF color = dc.GetTextColor();
  247. dc.SetTextColor(m_colorPromptText);
  248. rect.left += LOWORD(margins);
  249. rect.right -= HIWORD(margins);
  250. dc.DrawText(m_strPromptText, rect, DT_LEFT | DT_SINGLELINE | DT_EDITCONTROL);
  251. dc.SetTextColor(color);
  252. dc.SelectObject(oldFont);
  253. }
  254. }
  255. m_searchButton.Draw(&dc, this, rect.right - 22, 4, false, false);
  256. //OutputDebugString(_T("OnPaint"));
  257. }
  258. void CSymbolEdit::OnSize(UINT nType, int cx, int cy)
  259. {
  260. CEdit::OnSize(nType, cx, cy);
  261. RecalcLayout();
  262. }
  263. LRESULT CSymbolEdit::OnSetFont(WPARAM wParam, LPARAM lParam)
  264. {
  265. DefWindowProc(WM_SETFONT, wParam, lParam);
  266. RecalcLayout();
  267. return 0;
  268. }
  269. HBRUSH CSymbolEdit::CtlColor(CDC* pDC, UINT n)
  270. {
  271. if (::GetFocus() == m_hWnd)
  272. {
  273. pDC->SetBkColor(m_editFocusColor);
  274. return m_editFocusBrush;
  275. }
  276. else
  277. {
  278. pDC->SetBkColor(m_editNonFocusColor);
  279. return m_editNonFocusBrush;
  280. }
  281. }
  282. void CSymbolEdit::OnSetFocus(CWnd* pOldWnd)
  283. {
  284. Invalidate(FALSE);
  285. CEdit::OnSetFocus(pOldWnd);
  286. }
  287. void CSymbolEdit::OnKillFocus(CWnd* pNewWnd)
  288. {
  289. Invalidate(FALSE);
  290. CEdit::OnKillFocus(pNewWnd);
  291. }