SPPPropertyGridInplaceEdit.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /********************************************
  2. ** 工作室:S&P工作室
  3. ** 作者 :张东斌
  4. ** 日期 :2007年6月
  5. *********************************************/
  6. #include "stdafx.h"
  7. #include "SPVC80Helpers.h"
  8. #include "SPPropertyGridInplaceEdit.h"
  9. #include "SPPropertyGridInplaceButton.h"
  10. #include "SPPropertyGridInplaceList.h"
  11. #include "SPPropertyGridItem.h"
  12. #include "SPPropertyGridInplaceEdit.h"
  13. #include "SPPropertyGrid.h"
  14. #include "SPPropertyGridDefines.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CSPPropertyGridInplaceEdit
  22. CSPPropertyGridInplaceEdit::CSPPropertyGridInplaceEdit() : m_pItem( 0 ) , m_pGrid( 0 )
  23. {
  24. m_clrBack = 0;
  25. }
  26. CSPPropertyGridInplaceEdit::~CSPPropertyGridInplaceEdit()
  27. {
  28. }
  29. IMPLEMENT_DYNAMIC( CSPPropertyGridInplaceEdit , CEdit )
  30. BEGIN_MESSAGE_MAP(CSPPropertyGridInplaceEdit, CXTMaskEditT<CEdit>)
  31. ON_MASKEDIT_REFLECT()
  32. //{{AFX_MSG_MAP(CSPPropertyGridInplaceEdit)
  33. ON_WM_CTLCOLOR_REFLECT()
  34. ON_CONTROL_REFLECT(EN_KILLFOCUS, OnEnKillfocus)
  35. ON_CONTROL_REFLECT(EN_SETFOCUS, OnEnSetfocus)
  36. ON_CONTROL_REFLECT(EN_CHANGE, OnEnChange)
  37. ON_WM_LBUTTONDBLCLK()
  38. ON_WM_KEYDOWN()
  39. ON_WM_GETDLGCODE()
  40. ON_WM_CHAR()
  41. ON_WM_SYSKEYDOWN()
  42. //}}AFX_MSG_MAP
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CSPPropertyGridInplaceEdit message handlers
  46. void CSPPropertyGridInplaceEdit::SetValue( CString strValue )
  47. {
  48. m_strValue = strValue;
  49. }
  50. void CSPPropertyGridInplaceEdit::HideWindow()
  51. {
  52. if ( m_hWnd )
  53. {
  54. ShowWindow( SW_HIDE );
  55. }
  56. }
  57. void CSPPropertyGridInplaceEdit::Create( CSPPropertyGridItem * pItem , CRect rect )
  58. {
  59. ASSERT( pItem && pItem->GetGrid() );
  60. m_pGrid = pItem->GetGrid();
  61. m_pItem = pItem;
  62. if ( m_hWnd && ( m_pItem->m_bPassword || ( !m_pItem->m_bPassword && ( ( GetStyle() & ES_PASSWORD ) != 0 ) ) ) )
  63. {
  64. DestroyWindow();
  65. }
  66. if ( !m_hWnd )
  67. {
  68. CEdit::Create( WS_CHILD | ES_AUTOHSCROLL | ( m_pItem->m_bPassword ? ES_PASSWORD : 0 ),rect,m_pGrid,0 );
  69. if ( m_pGrid->GetExStyle() & WS_EX_LAYOUTRTL )
  70. {
  71. ModifyStyleEx( 0,WS_EX_LAYOUTRTL );
  72. }
  73. }
  74. if ( m_pItem->m_bPassword )
  75. {
  76. CEdit::SetPasswordChar( pItem->m_chPrompt );
  77. }
  78. SetFont( m_pGrid->GetFont() );
  79. SetWindowText( m_strValue );
  80. SetWindowPos( 0,rect.left,rect.top,rect.Width(),rect.Height(),SWP_NOZORDER | SWP_SHOWWINDOW );
  81. SetMargins( m_pGrid->GetExStyle() & WS_EX_LAYOUTRTL ? 2 : 3,0 );
  82. SetUseMask( m_pItem->m_bUseMask );
  83. if ( m_pItem->m_bUseMask )
  84. {
  85. SetEditMask( m_pItem->m_strMask,m_pItem->m_strLiteral,m_pItem->m_strValue );
  86. SetPromptChar( m_pItem->m_chPrompt );
  87. }
  88. }
  89. HBRUSH CSPPropertyGridInplaceEdit::CtlColor( CDC * pDC , UINT /*nCtlColor*/ )
  90. {
  91. CSPPropertyGridView * pGrid = ( CSPPropertyGridView * ) m_pGrid;
  92. pDC->SetTextColor( GetStyle() & ES_READONLY ? pGrid->m_clrReadOnlyFore : pGrid->m_clrFore );
  93. COLORREF clr = pGrid->m_clrBack;
  94. if ( clr != m_clrBack || !m_brBack.GetSafeHandle() )
  95. {
  96. m_brBack.DeleteObject();
  97. m_brBack.CreateSolidBrush( clr );
  98. m_clrBack = clr;
  99. }
  100. pDC->SetBkColor( m_clrBack );
  101. return m_brBack;
  102. }
  103. void CSPPropertyGridInplaceEdit::OnEnSetfocus()
  104. {
  105. if ( !m_pGrid )
  106. {
  107. return;
  108. }
  109. m_pGrid->Invalidate( FALSE );
  110. #ifdef SP_SITENOTIFY_ONFOCUS
  111. SP_SITENOTIFY_ONFOCUS( this,m_pGrid,TRUE )
  112. #endif
  113. }
  114. void CSPPropertyGridInplaceEdit::OnEnKillfocus()
  115. {
  116. if ( !m_pGrid )
  117. {
  118. return;
  119. }
  120. if ( m_pItem )
  121. {
  122. m_pItem->OnValidateEdit();
  123. if ( m_pGrid )
  124. m_pGrid->Invalidate( FALSE );
  125. }
  126. #ifdef SP_SITENOTIFY_ONFOCUS
  127. SP_SITENOTIFY_ONFOCUS( this,m_pGrid,FALSE )
  128. #endif
  129. }
  130. void CSPPropertyGridInplaceEdit::OnEnChange()
  131. {
  132. ASSERT( m_pItem );
  133. CSPPropertyGrid * pCtrl = ( CSPPropertyGrid * ) m_pGrid->GetParent();
  134. ASSERT_KINDOF( CSPPropertyGrid,pCtrl );
  135. CWnd * pOwner = pCtrl->GetOwner();
  136. ASSERT( pOwner );
  137. pOwner->SendMessage( SPWM_PROPERTYGRID_NOTIFY,SP_PGN_EDIT_CHANGED,( LPARAM )this );
  138. }
  139. UINT CSPPropertyGridInplaceEdit::OnGetDlgCode()
  140. {
  141. return DLGC_WANTALLKEYS;
  142. }
  143. void CSPPropertyGridInplaceEdit::OnChar( UINT nChar , UINT nRepCnt , UINT nFlags )
  144. {
  145. if ( nChar == VK_TAB )
  146. {
  147. return;
  148. }
  149. if ( nChar == VK_ESCAPE || nChar == VK_RETURN )
  150. {
  151. m_pGrid->SetFocus();
  152. return;
  153. }
  154. if ( m_pItem && !m_pItem->GetReadOnly() && m_pItem->GetConstraintEdit() )
  155. {
  156. CSPPropertyGridItemConstraints *pList = m_pItem->GetConstraints();
  157. if ( !pList->IsEmpty() )
  158. {
  159. CString str;
  160. GetWindowText( str );
  161. int nIndex = pList->FindConstraint( str );
  162. int nIndexStart = nIndex == -1 ? pList->GetCount() - 1 : nIndex;
  163. CString strSeach ( ( TCHAR ) nChar );
  164. do
  165. {
  166. nIndex = nIndex < pList->GetCount() - 1 ? nIndex + 1 : 0;
  167. CString str = pList->GetAt( nIndex );
  168. if ( strSeach.CompareNoCase( str.Left( 1 ) ) == 0 )
  169. {
  170. SetWindowText( str );
  171. pList->SetCurrent( nIndex );
  172. SetSel( 0,-1 );
  173. return;
  174. }
  175. }
  176. while ( nIndex != nIndexStart );
  177. return;
  178. }
  179. }
  180. CXTMaskEditT< CEdit > ::OnChar ( nChar,nRepCnt,nFlags );
  181. }
  182. void CSPPropertyGridInplaceEdit::OnKeyDown( UINT nChar , UINT nRepCnt , UINT nFlags )
  183. {
  184. if ( nChar == VK_TAB && m_pGrid )
  185. {
  186. ( ( CSPPropertyGrid * ) m_pGrid->GetParent() )->OnNavigate( SPGridUIInplaceEdit,GetKeyState( VK_SHIFT ) >= 0,m_pItem );
  187. return;
  188. }
  189. else if ( nChar == VK_ESCAPE )
  190. {
  191. SetWindowText( m_strValue );
  192. return ;
  193. }
  194. else if ( nChar == VK_RETURN )
  195. {
  196. return ;
  197. }
  198. else if ( nChar == VK_F4 )
  199. {
  200. if ( m_pItem && m_pItem->GetFlags() & SPGridItemHasComboButton )
  201. {
  202. m_pItem->OnInplaceButtonDown();
  203. }
  204. }
  205. else if ( nChar == VK_DOWN || nChar == VK_UP )
  206. {
  207. if ( m_pItem != NULL && !m_pItem->GetReadOnly() )
  208. {
  209. if ( SelectConstraint( nChar == VK_DOWN ? +1 : -1,FALSE ) )
  210. {
  211. SetSel( 0,-1 );
  212. return ;
  213. }
  214. }
  215. }
  216. CXTMaskEditT< CEdit > ::OnKeyDown ( nChar,nRepCnt,nFlags );
  217. }
  218. void CSPPropertyGridInplaceEdit::OnSysKeyDown( UINT nChar , UINT nRepCnt , UINT nFlags )
  219. {
  220. if ( nChar == VK_DOWN || nChar == VK_UP )
  221. {
  222. if ( m_pItem && m_pItem->GetFlags() & SPGridItemHasComboButton )
  223. {
  224. m_pItem->OnInplaceButtonDown();
  225. }
  226. }
  227. CXTMaskEditT< CEdit > ::OnSysKeyDown ( nChar,nRepCnt,nFlags );
  228. }
  229. BOOL CSPPropertyGridInplaceEdit::SelectConstraint( int nDirection , BOOL bCircle )
  230. {
  231. CSPPropertyGridItemConstraints *pList = m_pItem->GetConstraints();
  232. if ( pList->IsEmpty() )
  233. {
  234. return FALSE;
  235. }
  236. CString str;
  237. GetWindowText( str );
  238. int nIndex = pList->FindConstraint( str );
  239. if ( nIndex == -1 )
  240. {
  241. return FALSE;
  242. }
  243. nIndex += nDirection;
  244. if ( nIndex >= pList->GetCount() )
  245. {
  246. nIndex = bCircle ? 0 : ( ULONG ) pList->GetCount() - 1;
  247. }
  248. if ( nIndex < 0 )
  249. {
  250. nIndex = bCircle ? ( ULONG ) pList->GetCount() - 1 : 0;
  251. }
  252. SetWindowText( pList->GetAt( nIndex ) );
  253. pList->SetCurrent( nIndex );
  254. return TRUE;
  255. }
  256. void CSPPropertyGridInplaceEdit::OnLButtonDblClk( UINT nFlags , CPoint point )
  257. {
  258. if ( m_pItem != NULL && !m_pItem->GetReadOnly() && SelectConstraint( +1,TRUE ) )
  259. {
  260. m_pGrid->SetFocus();
  261. return;
  262. }
  263. CXTMaskEditT< CEdit > ::OnLButtonDblClk ( nFlags,point );
  264. }
  265. void CSPPropertyGridInplaceEdit::DestroyItem()
  266. {
  267. // reset variables to defaults.
  268. m_pItem = NULL;
  269. m_pGrid = NULL;
  270. m_strValue.Empty();
  271. m_brBack.DeleteObject();
  272. // destroy the window.
  273. //DestroyWindow( );
  274. if ( ::IsWindow( m_hWnd ) )
  275. {
  276. ShowWindow( SW_HIDE );
  277. }
  278. }
  279. BOOL CSPPropertyGridInplaceEdit::PreTranslateMessage( MSG * pMsg )
  280. {
  281. if ( pMsg->message == WM_KEYDOWN && IsDialogMessage( pMsg ) )
  282. {
  283. return TRUE;
  284. }
  285. return CXTMaskEditT< CEdit >::PreTranslateMessage( pMsg );
  286. }