SymbolEdit.cpp 6.5 KB

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