EditWithButton.cpp 7.4 KB

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