EditWithButton.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // EditWithButton.cpp : implementation file
  2. //
  3. //DISCLAIMER:
  4. //The code in this project is Copyright (C) 2006 by Gautam Jain. You have the right to
  5. //use and distribute the code in any way you see fit as long as this paragraph is included
  6. //with the distribution. No warranties or claims are made as to the validity of the
  7. //information and code contained herein, so use it at your own risk.
  8. #include "stdafx.h"
  9. #include "EditWithButton.h"
  10. #include "cp_main.h"
  11. IMPLEMENT_DYNAMIC(CEditWithButton, CEdit)
  12. CEditWithButton::CEditWithButton()
  13. {
  14. m_iButtonClickedMessageId = NM_CANCEL_SEARCH;
  15. m_bButtonExistsAlways = FALSE;
  16. m_rcEditArea.SetRect(0, 0, 0, 0);
  17. }
  18. CEditWithButton::~CEditWithButton()
  19. {
  20. m_bmpEmptyEdit.DeleteObject();
  21. m_bmpFilledEdit.DeleteObject();
  22. }
  23. BEGIN_MESSAGE_MAP(CEditWithButton, CEdit)
  24. ON_MESSAGE(WM_SETFONT, OnSetFont)
  25. ON_WM_SIZE()
  26. ON_WM_ERASEBKGND()
  27. ON_WM_CHAR()
  28. ON_WM_KEYDOWN()
  29. ON_WM_LBUTTONUP()
  30. ON_WM_SETCURSOR()
  31. ON_WM_CREATE()
  32. END_MESSAGE_MAP()
  33. // CEditWithButton message handlers
  34. void CEditWithButton::PreSubclassWindow()
  35. {
  36. // We must have a multiline edit
  37. // to be able to set the edit rect
  38. ASSERT(GetStyle() & ES_MULTILINE);
  39. ResizeWindow();
  40. }
  41. BOOL CEditWithButton::PreTranslateMessage(MSG* pMsg)
  42. {
  43. // TODO: Add your specialized code here and/or call the base class
  44. // Intercept Ctrl + Z (Undo), Ctrl + X (Cut), Ctrl + C (Copy), Ctrl + V (Paste) and Ctrl + A (Select All)
  45. // before CEdit base class gets a hold of them.
  46. if (pMsg->message == WM_KEYDOWN &&
  47. CONTROL_PRESSED)
  48. {
  49. switch (pMsg->wParam)
  50. {
  51. case 'Z':
  52. Undo();
  53. return TRUE;
  54. case 'X':
  55. Cut();
  56. return TRUE;
  57. case 'C':
  58. Copy();
  59. return TRUE;
  60. case 'V':
  61. Paste();
  62. return TRUE;
  63. case 'A':
  64. SetSel(0, -1);
  65. return TRUE;
  66. }
  67. }
  68. switch(pMsg->message)
  69. {
  70. case WM_KEYDOWN:
  71. {
  72. if(pMsg->wParam == VK_RETURN)
  73. {
  74. CWnd *pWnd = GetParent();
  75. if(pWnd)
  76. {
  77. if(g_Opt.m_bFindAsYouType)
  78. {
  79. pWnd->SendMessage(NM_SEARCH_ENTER_PRESSED, 0, 0);
  80. }
  81. else
  82. {
  83. //Send a message to the parent to refill the lb from the search
  84. pWnd->PostMessage(CB_SEARCH, 0, 0);
  85. }
  86. }
  87. return TRUE;
  88. }
  89. else if (pMsg->wParam == VK_DOWN ||
  90. pMsg->wParam == VK_UP ||
  91. pMsg->wParam == VK_F3 ||
  92. pMsg->wParam == VK_PRIOR ||
  93. pMsg->wParam == VK_NEXT)
  94. {
  95. CWnd *pWnd = GetParent();
  96. if(pWnd)
  97. {
  98. pWnd->SendMessage(CB_UPDOWN, pMsg->wParam, pMsg->lParam);
  99. return TRUE;
  100. }
  101. }
  102. break;
  103. }
  104. }
  105. return CEdit::PreTranslateMessage(pMsg);
  106. }
  107. BOOL CEditWithButton::SetBitmaps(UINT iEmptyEdit, UINT iFilledEdit)
  108. {
  109. BITMAP bmpInfo;
  110. //delete if already loaded.. just in case
  111. m_bmpEmptyEdit.DeleteObject();
  112. m_bmpFilledEdit.DeleteObject();
  113. m_bmpEmptyEdit.LoadBitmap(iEmptyEdit);
  114. m_bmpFilledEdit.LoadBitmap(iFilledEdit);
  115. m_bmpEmptyEdit.GetBitmap(&bmpInfo);
  116. m_sizeEmptyBitmap.SetSize(bmpInfo.bmWidth,bmpInfo.bmHeight);
  117. m_bmpFilledEdit.GetBitmap(&bmpInfo);
  118. m_sizeFilledBitmap.SetSize(bmpInfo.bmWidth,bmpInfo.bmHeight);
  119. return TRUE;
  120. }
  121. //client area
  122. void CEditWithButton::SetButtonArea(CRect rcButtonArea)
  123. {
  124. m_rcButtonArea = rcButtonArea;
  125. }
  126. void CEditWithButton::ResizeWindow()
  127. {
  128. if (!::IsWindow(m_hWnd)) return;
  129. //proceed only if edit area is set
  130. if (m_rcBorder == CRect(0,0,0,0))
  131. return;
  132. CRect r;
  133. GetWindowRect(r);
  134. ScreenToClient(r);
  135. SetWindowPos(&wndTop, 0, 0, r.Width(), r.Height(), SWP_NOMOVE|SWP_NOZORDER);
  136. m_rcEditArea.left = r.left + m_rcBorder.left;
  137. m_rcEditArea.top = r.top + m_rcBorder.top;
  138. m_rcEditArea.right = r.right -= m_rcBorder.right;
  139. m_rcEditArea.bottom = r.bottom -= m_rcBorder.bottom;
  140. SetRect(&m_rcEditArea);
  141. }
  142. //set edit area may be called before creating the edit control
  143. //especially when using the CEdit::Create method
  144. //or after creating the edit control in CEdit::DoDataExchange
  145. //we call ResizeWindow once in SetEditArea and once in PreSubclassWindow
  146. BOOL CEditWithButton::SetBorder(CRect rcEditArea)
  147. {
  148. m_rcBorder = rcEditArea;
  149. ResizeWindow();
  150. return TRUE;
  151. }
  152. void CEditWithButton::SetButtonExistsAlways(BOOL bButtonExistsAlways)
  153. {
  154. m_bButtonExistsAlways = bButtonExistsAlways;
  155. }
  156. BOOL CEditWithButton::OnEraseBkgnd(CDC* pDC)
  157. {
  158. // Get the size of the bitmap
  159. CDC dcMemory;
  160. CSize sizeBitmap;
  161. CBitmap* pOldBitmap = NULL;
  162. int iTextLength = GetWindowTextLength();
  163. CRect size;
  164. GetWindowRect(size);
  165. if (iTextLength == 0)
  166. {
  167. sizeBitmap = m_sizeEmptyBitmap;
  168. }
  169. else
  170. {
  171. sizeBitmap = m_sizeFilledBitmap;
  172. }
  173. // Create an in-memory DC compatible with the
  174. // display DC we're using to paint
  175. dcMemory.CreateCompatibleDC(pDC);
  176. if (iTextLength == 0)
  177. {
  178. // Select the bitmap into the in-memory DC
  179. pOldBitmap = dcMemory.SelectObject(&m_bmpEmptyEdit);
  180. }
  181. else
  182. {
  183. // Select the bitmap into the in-memory DC
  184. pOldBitmap = dcMemory.SelectObject(&m_bmpFilledEdit);
  185. }
  186. // Copy the bits from the in-memory DC into the on-
  187. // screen DC to actually do the painting. Use the centerpoint
  188. // we computed for the target offset.
  189. pDC->BitBlt(0,0, 50, sizeBitmap.cy, &dcMemory,
  190. 0, 0, SRCCOPY);
  191. for(int i = 50; i < size.Width()-50; i++)
  192. {
  193. pDC->BitBlt(i, 0, 1, sizeBitmap.cy, &dcMemory,
  194. 50, 0, SRCCOPY);
  195. }
  196. pDC->BitBlt(size.Width()-50, 0, 50, sizeBitmap.cy, &dcMemory,
  197. sizeBitmap.cx-50, 0, SRCCOPY);
  198. dcMemory.SelectObject(pOldBitmap);
  199. return TRUE;
  200. }
  201. void CEditWithButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  202. {
  203. //this will draw the background again
  204. //so that the button will be drawn if the text exists
  205. InvalidateRect(NULL);
  206. CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  207. }
  208. void CEditWithButton::OnLButtonUp(UINT nFlags, CPoint point)
  209. {
  210. //if the button is clicked then send message to the
  211. //owner.. the owner need not be parent
  212. //you can set the owner using the CWnd::SetOwner method
  213. CRect crClose;
  214. GetWindowRect(crClose);
  215. ScreenToClient(crClose);
  216. int left = crClose.right - (m_sizeFilledBitmap.cx - m_rcButtonArea.left);
  217. int right = crClose.right - (m_sizeFilledBitmap.cx - m_rcButtonArea.right);
  218. crClose.top = m_rcButtonArea.top;
  219. crClose.bottom = m_rcButtonArea.bottom;
  220. crClose.left = left;
  221. crClose.right = right;
  222. if (crClose.PtInRect(point))
  223. {
  224. //it is assumed that when the text is not typed in the
  225. //edit control, the button will not be visible
  226. //but you can override this by setting
  227. //the m_bButtonExistsAlways to TRUE
  228. if ( (GetWindowTextLength() > 0) || m_bButtonExistsAlways)
  229. {
  230. CWnd *pOwner = GetOwner();
  231. if (pOwner)
  232. {
  233. pOwner->SendMessage(m_iButtonClickedMessageId, 0, 0);
  234. }
  235. }
  236. }
  237. CEdit::OnLButtonUp(nFlags, point);
  238. }
  239. //by default, when the mouse moves over the edit control
  240. //the system shows the I-beam cursor. However we want to
  241. //show the arrow cursor when it is over the Non-Edit area
  242. //where the button and icon is displayed
  243. //here is the code to do this
  244. BOOL CEditWithButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  245. {
  246. CPoint pntCursor;
  247. GetCursorPos(&pntCursor);
  248. ScreenToClient(&pntCursor);
  249. CRect edit;
  250. GetWindowRect(edit);
  251. ScreenToClient(edit);
  252. edit.left += m_rcBorder.left;
  253. edit.top += m_rcBorder.top;
  254. edit.right -= m_rcBorder.right;
  255. edit.bottom -= m_rcBorder.bottom;
  256. //if mouse is not in the edit area then
  257. //show arrow cursor
  258. if (!edit.PtInRect(pntCursor))
  259. {
  260. SetCursor(AfxGetApp()->LoadStandardCursor(MAKEINTRESOURCE(IDC_ARROW)));
  261. return TRUE;
  262. }
  263. return CEdit::OnSetCursor(pWnd, nHitTest, message);
  264. }
  265. int CEditWithButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
  266. {
  267. if (CEdit::OnCreate(lpCreateStruct) == -1)
  268. return -1;
  269. ResizeWindow();
  270. return 0;
  271. }
  272. LRESULT CEditWithButton::OnSetFont( WPARAM wParam, LPARAM lParam )
  273. {
  274. DefWindowProc(WM_SETFONT, wParam, lParam);
  275. ResizeWindow();
  276. return 0;
  277. }
  278. void CEditWithButton::OnSize(UINT nType, int cx, int cy)
  279. {
  280. CEdit::OnSize(nType, cx, cy);
  281. ResizeWindow();
  282. }