EditWithButton.cpp 7.9 KB

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