OptionTreeSpinnerEdit.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 6k
Category:

Graph program

Development Platform:

Visual C++

  1. // COptionTree
  2. //
  3. // License
  4. // -------
  5. // This code is provided "as is" with no expressed or implied warranty.
  6. // 
  7. // You may use this code in a commercial product with or without acknowledgement.
  8. // However you may not sell this code or any modification of this code, this includes 
  9. // commercial libraries and anything else for profit.
  10. //
  11. // I would appreciate a notification of any bugs or bug fixes to help the control grow.
  12. //
  13. // History:
  14. // --------
  15. // See License.txt for full history information.
  16. //
  17. //
  18. // Copyright (c) 1999-2002 
  19. // ComputerSmarts.net 
  20. // mattrmiller@computersmarts.net
  21. #include "stdafx.h"
  22. #include "OptionTreeSpinnerEdit.h"
  23. // Added Headers
  24. #include "OptionTree.h"
  25. #ifdef _DEBUG
  26. #define new DEBUG_NEW
  27. #undef THIS_FILE
  28. static char THIS_FILE[] = __FILE__;
  29. #endif
  30. /////////////////////////////////////////////////////////////////////////////
  31. // COptionTreeSpinnerEdit
  32. COptionTreeSpinnerEdit::COptionTreeSpinnerEdit()
  33. {
  34. // Initialize variables
  35. m_otSpinnerButton = NULL;
  36. }
  37. COptionTreeSpinnerEdit::~COptionTreeSpinnerEdit()
  38. {
  39. }
  40. BEGIN_MESSAGE_MAP(COptionTreeSpinnerEdit, CEdit)
  41. //{{AFX_MSG_MAP(COptionTreeSpinnerEdit)
  42. ON_WM_KEYDOWN()
  43. ON_WM_CREATE()
  44. ON_WM_CONTEXTMENU()
  45. ON_WM_KILLFOCUS()
  46. ON_WM_SETFOCUS()
  47. ON_CONTROL_REFLECT(EN_CHANGE, OnTextChange)
  48. ON_WM_CHAR()
  49. //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51. /////////////////////////////////////////////////////////////////////////////
  52. // COptionTreeSpinnerEdit message handlers
  53. void COptionTreeSpinnerEdit::SetOwnerSpinner(COptionTreeSpinnerButton *otSpinnerButton)
  54. {
  55. // Save pointer
  56. m_otSpinnerButton = otSpinnerButton;
  57. }
  58. void COptionTreeSpinnerEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  59. {
  60. // Find what key was presed
  61. if (nChar == VK_UP)
  62. {
  63. m_otSpinnerButton->SendMessage(OT_NOTIFY_UP, 0, 0);
  64. }
  65. else if (nChar == VK_DOWN)
  66. {
  67. m_otSpinnerButton->SendMessage(OT_NOTIFY_DOWN, 0, 0);
  68. }
  69. CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
  70. }
  71. BOOL COptionTreeSpinnerEdit::OnCommand(WPARAM wParam, LPARAM lParam) 
  72. {
  73. // User edit
  74. if(m_otSpinnerButton != NULL)
  75. {
  76. // -- No user edit
  77. if (m_otSpinnerButton->GetOption(OT_EDIT_USEREDIT) == FALSE)
  78. {
  79. // -- -- No paste or cut
  80. if (LOWORD(wParam) == WM_PASTE || LOWORD(wParam) == WM_CUT)
  81. {
  82. return FALSE;
  83. }
  84. }
  85. // -- User edit
  86. else
  87. {
  88. switch (LOWORD(wParam))
  89. {
  90. case EM_UNDO:
  91. case WM_CUT:
  92. case WM_COPY:
  93. case WM_CLEAR:
  94. case WM_PASTE:
  95. return SendMessage(LOWORD(wParam));
  96. case OT_MES_NSELECTALL:
  97. return SendMessage(EM_SETSEL, 0, -1);
  98. default:
  99. return CEdit::OnCommand(wParam, lParam);
  100. }
  101. }
  102. }
  103. return FALSE;
  104. }
  105. int COptionTreeSpinnerEdit::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  106. {
  107. // Create edit
  108. if (CEdit::OnCreate(lpCreateStruct) == -1)
  109. {
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. void COptionTreeSpinnerEdit::OnContextMenu(CWnd* pWnd, CPoint point) 
  115. {
  116.     // Declare variables
  117. CMenu mMenu;
  118. BOOL bReadOnly;
  119. DWORD dwFlags;
  120. int nSelStart, nSelEnd;
  121. int nLen;
  122. // Make sure we have options
  123. if (m_otSpinnerButton == NULL)
  124. {
  125. return;
  126. }
  127.     // Crete menu
  128. mMenu.CreatePopupMenu();
  129.     // Get read only
  130. bReadOnly = GetStyle() & ES_READONLY;
  131. // Get selection
  132. GetSel(nSelStart, nSelEnd);
  133. // Get window length
  134. nLen = GetWindowTextLength();
  135. // Insert Undo
  136. if (m_otSpinnerButton->GetOption(OT_EDIT_USEREDIT) == TRUE)
  137. {
  138. dwFlags = CanUndo() && !bReadOnly ? 0 : MF_GRAYED | MF_DISABLED;
  139. }
  140. else
  141. {
  142. dwFlags = MF_GRAYED | MF_DISABLED;
  143. }
  144.     mMenu.InsertMenu(0, MF_BYPOSITION | dwFlags, EM_UNDO, OT_MES_UNDO);
  145. // Insert seperator
  146.     mMenu.InsertMenu(1, MF_BYPOSITION | MF_SEPARATOR);
  147.     // Insert copy
  148. if (nSelStart == nSelEnd)
  149. {
  150. dwFlags = MF_GRAYED | MF_DISABLED;
  151. }
  152. else
  153. {
  154. dwFlags = 0;
  155. }
  156.     mMenu.InsertMenu(2, MF_BYPOSITION | dwFlags, WM_COPY, OT_MES_COPY);
  157. // Insert cut and clear
  158. if (m_otSpinnerButton->GetOption(OT_EDIT_USEREDIT) == TRUE)
  159. {
  160. if (nSelStart == nSelEnd || bReadOnly == TRUE)
  161. {
  162. dwFlags = MF_GRAYED | MF_DISABLED;
  163. }
  164. else
  165. {
  166. dwFlags = 0;
  167. }
  168. }
  169. else
  170. {
  171. dwFlags = MF_GRAYED | MF_DISABLED;
  172. }
  173.     mMenu.InsertMenu(2, MF_BYPOSITION | dwFlags, WM_CUT, OT_MES_CUT);
  174.     mMenu.InsertMenu(4, MF_BYPOSITION | dwFlags, WM_CLEAR, OT_MES_DELETE);
  175.     // Insert paste
  176. if (m_otSpinnerButton->GetOption(OT_EDIT_USEREDIT) == TRUE)
  177. {
  178. if (IsClipboardFormatAvailable(CF_TEXT) == FALSE || bReadOnly == TRUE)
  179. {
  180. dwFlags = MF_GRAYED | MF_DISABLED;
  181. }
  182. else
  183. {
  184. dwFlags = 0;
  185. }
  186. }
  187. else
  188. {
  189. dwFlags = MF_GRAYED | MF_DISABLED;
  190. }
  191.     mMenu.InsertMenu(4, MF_BYPOSITION | dwFlags, WM_PASTE, OT_MES_PASTE);
  192. // Insert seperator
  193.     mMenu.InsertMenu(6, MF_BYPOSITION | MF_SEPARATOR);
  194.     // Insert select all
  195.     mMenu.InsertMenu(7, MF_BYPOSITION | dwFlags, OT_MES_NSELECTALL, OT_MES_SELECTALL);
  196.     // Show menu
  197. mMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, point.x, point.y, this);
  198. }
  199. void COptionTreeSpinnerEdit::OnKillFocus(CWnd* pNewWnd) 
  200. {
  201. // Make sure we have options
  202. if (m_otSpinnerButton != NULL)
  203. {
  204. // -- If new focus is not parent then sent message to item to commit
  205. if (pNewWnd->GetSafeHwnd() != m_otSpinnerButton->GetSafeHwnd())
  206. {
  207. ::PostMessage(m_otSpinnerButton->GetSafeHwnd(), OT_NOTIFY_COMMITCHANGES, NULL, NULL);
  208. }
  209. }
  210. CEdit::OnKillFocus(pNewWnd);
  211. }
  212. void COptionTreeSpinnerEdit::OnSetFocus(CWnd* pOldWnd) 
  213. {
  214. CEdit::OnSetFocus(pOldWnd);
  215. }
  216. void COptionTreeSpinnerEdit::OnTextChange() 
  217. {
  218. // Force spinner button redraw
  219. ::PostMessage(m_otSpinnerButton->GetSafeHwnd(), OT_NOTIFY_FORCEREDRAW, NULL, NULL);
  220. }
  221. BOOL COptionTreeSpinnerEdit::PreTranslateMessage(MSG* pMsg) 
  222. {
  223. return CEdit::PreTranslateMessage(pMsg);
  224. }
  225. void COptionTreeSpinnerEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  226. {
  227. // Disallow user edit
  228. if(m_otSpinnerButton != NULL)
  229. {
  230. // -- No user edit
  231. if (m_otSpinnerButton->GetOption(OT_EDIT_USEREDIT) == FALSE)
  232. {
  233. return;
  234. }
  235. }
  236. CEdit::OnChar(nChar, nRepCnt, nFlags);
  237. }