SPPropertyGridItem.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /********************************************
  2. ** 工作室:S&P工作室
  3. ** 作者 :张东斌
  4. ** 日期 :2007年6月
  5. *********************************************/
  6. #if !defined(__SPPROPERTYGRIDITEM_H__)
  7. #define __SPPROPERTYGRIDITEM_H__
  8. #if _MSC_VER > 1000
  9. #pragma once
  10. #endif // _MSC_VER > 1000
  11. // class forwards.
  12. class CSPPropertyGridView;
  13. class CSPPropertyGridItem;
  14. //-----------------------------------------------------------------------
  15. // Summary:
  16. // This constant is used to determine the "gutter" width of the property
  17. // grid. This is the area on the left of the grid that the
  18. // expand buttons are drawn in.
  19. // Remarks:
  20. // This is useful when drawing owner drawn controls such as buttons
  21. // and drop-down menus.
  22. // Example:
  23. // The following example illustrates using SP_PGI_EXPAND_BORDER:
  24. // <code>
  25. // void CDelphiGridPage::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  26. // {
  27. // // code snip ...
  28. //
  29. // CRect rcText(rc);
  30. // rcText.left = max(1, pItem->GetIndent()) * SP_PGI_EXPAND_BORDER + 3;
  31. // rcText.right = rcCaption.right - 1;
  32. // rcText.bottom -= 1;
  33. // dc.DrawText( pItem->GetCaption(), rcText, DT_SINGLELINE|DT_VCENTER);
  34. // }
  35. // </code>
  36. //-----------------------------------------------------------------------
  37. const int SP_PGI_EXPAND_BORDER = 14;
  38. //-----------------------------------------------------------------------
  39. // Summary:
  40. // Flags that indicate what styles to set for the grid item.
  41. // Example:
  42. // <code>pItem->SetFlags(SPGridItemHasEdit|SPGridItemHasComboButton);</code>
  43. // See Also: CSPPropertyGridItem::SetFlags, OnInplaceButtonDown
  44. //
  45. // <KEYWORDS SPGridItemHasEdit, SPGridItemHasExpandButton, SPGridItemHasComboButton>
  46. //-----------------------------------------------------------------------
  47. enum SPPropertyGridItemFlags
  48. {
  49. SPGridItemHasEdit = 1 , // Item has an edit control.
  50. SPGridItemHasExpandButton = 2 , // Item has an expand button.
  51. SPGridItemHasComboButton = 4 // Item has a combo button.
  52. };
  53. //===========================================================================
  54. // Summary:
  55. // CSPPropertyGridItems is a CList derived class. It represents the items
  56. // collection with some internal functionality.
  57. //===========================================================================
  58. class CSPPropertyGridItems : public CCmdTarget
  59. {
  60. public:
  61. //-----------------------------------------------------------------------
  62. // Summary:
  63. // Call this member function to find an item.
  64. // Parameters:
  65. // strCaption - Caption for the item to find.
  66. // nID - Identifier for the item to find.
  67. // Returns:
  68. // A pointer to a CSPPropertyGridItem object.
  69. //-----------------------------------------------------------------------
  70. CSPPropertyGridItem * FindItem( UINT nID );
  71. CSPPropertyGridItem * FindItem( CString strCaption ); // <COMBINE CSPPropertyGridItems::FindItem@UINT>
  72. //-----------------------------------------------------------------------
  73. // Summary:
  74. // Call this member to determine the index of a CSPPropertyGridItem.
  75. // Parameters:
  76. // pItem - Pointer to the CSPPropertyGridItem you want to know
  77. // the index of.
  78. // Returns:
  79. // The index of the item within the collection of items, -1 if
  80. // the item is not found.
  81. //-----------------------------------------------------------------------
  82. int Find( CSPPropertyGridItem * pItem );
  83. //-----------------------------------------------------------------------
  84. // Summary:
  85. // Call this member to clear all the items in a list.
  86. //-----------------------------------------------------------------------
  87. void Clear();
  88. //-----------------------------------------------------------------------
  89. // Summary:
  90. // Call this member to get count of the items in a list.
  91. // Returns:
  92. // Count of the items in a list.
  93. //-----------------------------------------------------------------------
  94. long GetCount();
  95. //-----------------------------------------------------------------------
  96. // Summary:
  97. // Call this member to determine if any items have been added
  98. // to the property grid.
  99. // Returns:
  100. // TRUE if there are no items in the grid, FALSE if at least
  101. // one item has been added to the grid.
  102. //-----------------------------------------------------------------------
  103. BOOL IsEmpty();
  104. //-----------------------------------------------------------------------
  105. // Summary:
  106. // Call this member to get an item by its index.
  107. // Parameters:
  108. // nIndex - Item's index.
  109. // Returns:
  110. // A pointer to a CSPPropertyGridItem object.
  111. //-----------------------------------------------------------------------
  112. CSPPropertyGridItem * GetAt( int nIndex );
  113. //-----------------------------------------------------------------------
  114. // Summary:
  115. // Call this member to remove an item by its index.
  116. // Parameters:
  117. // nIndex - Item's index.
  118. //-----------------------------------------------------------------------
  119. void RemoveAt( int nIndex );
  120. protected:
  121. //-------------------------------------------------------------------------
  122. // Summary:
  123. // Constructs a CSPPropertyGridItems object.
  124. //-------------------------------------------------------------------------
  125. CSPPropertyGridItems();
  126. //-----------------------------------------------------------------------
  127. // Summary:
  128. // This member adds a new item to the end (tail) of the
  129. // Property Grid's collection of items. The collection can be empty
  130. // before the operation.
  131. // Parameters:
  132. // pItem - Pointer to the CSPPropertyGridItem to add to the collection.
  133. //-----------------------------------------------------------------------
  134. void AddTail( CSPPropertyGridItem * pItem );
  135. //-----------------------------------------------------------------------
  136. // Summary:
  137. // This member is called to insert a CSPPropertyGridItem object
  138. // into the collections of items at a specific location specified
  139. // by nIndex.
  140. // Parameters:
  141. // nIndex - Position in the collection of items to insert pItem.
  142. // pItem - Pointer to the CSPPropertyGridItem to add to the collection.
  143. //-----------------------------------------------------------------------
  144. void InsertAt( int nIndex , CSPPropertyGridItem * pItem );
  145. //-----------------------------------------------------------------------
  146. // Summary:
  147. // This member adds a collection of items to the end (tail) of the
  148. // Property Grid's collection of items. The collection can be empty
  149. // before the operation.
  150. // Parameters:
  151. // pItems - Pointer to the collection of CSPPropertyGridItems to
  152. // add to the collection.
  153. //-----------------------------------------------------------------------
  154. void AddTail( CSPPropertyGridItems * pItems );
  155. //-------------------------------------------------------------------------
  156. // Summary:
  157. // This member sorts the collection of items in alphabetical order.
  158. // Remarks:
  159. // This method uses Visual C++ run-time library (MSVCRT)
  160. // implementation of the quick-sort function, qsort, for sorting
  161. // stored CSPPropertyGridItem objects.
  162. //-------------------------------------------------------------------------
  163. void Sort();
  164. private:
  165. static int _cdecl CompareFunc( const CSPPropertyGridItem ** ppItem1 , const CSPPropertyGridItem ** ppItem2 );
  166. private:
  167. CArray< CSPPropertyGridItem*, CSPPropertyGridItem * > m_arrItems;
  168. CSPPropertyGridView * m_pGrid;
  169. friend class CSPPropertyGridItem;
  170. friend class CSPPropertyGridView;
  171. };
  172. class CSPPropertyGridItemConstraint : public CCmdTarget
  173. {
  174. public:
  175. CSPPropertyGridItemConstraint();
  176. int GetIndex();
  177. public:
  178. CString m_strConstraint; // Caption text of constraint. This is the
  179. // text displayed for this constraint.
  180. DWORD_PTR m_dwData; // The 32-bit value associated with the item.
  181. protected:
  182. int m_nIndex; // Index of constraint.
  183. friend class CSPPropertyGridItemConstraints;
  184. };
  185. class CSPPropertyGridItemConstraints : public CCmdTarget
  186. {
  187. protected:
  188. CSPPropertyGridItemConstraints( CSPPropertyGridItem * pItem );
  189. ~CSPPropertyGridItemConstraints();
  190. public:
  191. CSPPropertyGridItemConstraint * AddConstraint( CString str , DWORD dwData = 0 );
  192. int GetCount();
  193. BOOL IsEmpty();
  194. void RemoveAll();
  195. void RemoveAt( int nIndex );
  196. void SetCurrent( int nIndex );
  197. int GetCurrent();
  198. int FindConstraint( CString str );
  199. int FindConstraint( DWORD dwData ); // <COMBINE CSPPropertyGridItemConstraints::FindConstraint@CString>
  200. CString GetAt( int nIndex );
  201. CSPPropertyGridItemConstraint * GetConstraintAt( int nIndex );
  202. void Sort();
  203. private:
  204. static int _cdecl CompareFunc( const CSPPropertyGridItemConstraint ** ppConstraint1 , const CSPPropertyGridItemConstraint ** ppConstraint2 );
  205. private:
  206. CArray< CSPPropertyGridItemConstraint*, CSPPropertyGridItemConstraint * > m_arrConstraints;
  207. int m_nCurrent;
  208. CSPPropertyGridItem * m_pItem;
  209. friend class CSPPropertyGridItem;
  210. };
  211. class CSPPropertyGridItem : public CCmdTarget
  212. {
  213. public:
  214. CSPPropertyGridItem( CString strCaption , LPCTSTR strValue = NULL , CString * pBindString = NULL );
  215. CSPPropertyGridItem( UINT nID , LPCTSTR strValue = NULL , CString * pBindString = NULL ); // <COMBINE CSPPropertyGridItem::CSPPropertyGridItem@CString@LPCTSTR@CString*>
  216. virtual ~CSPPropertyGridItem();
  217. public:
  218. void SetCaption( CString strCaption );
  219. void SetPrompt( CString strCaption );
  220. void SetDescription( LPCTSTR lpszDescription );
  221. void SetReadOnly( BOOL bReadOnly );
  222. void SetID( UINT nID );
  223. void SetFlags( UINT nFlags );
  224. CString GetCaption() const;
  225. CString GetDescription();
  226. BOOL GetReadOnly();
  227. UINT GetID();
  228. CString GetValue();
  229. UINT GetFlags();
  230. BOOL IsCategory();
  231. CSPPropertyGridItem * AddChildItem( CSPPropertyGridItem * pItem );
  232. CSPPropertyGridItem * InsertChildItem( int nIndex , CSPPropertyGridItem * pItem );
  233. BOOL HasChilds();
  234. CSPPropertyGridItems * GetChilds();
  235. BOOL HasParent( CSPPropertyGridItem * pParent );
  236. CSPPropertyGridItemConstraints * GetConstraints();
  237. void SetConstraintEdit( BOOL bConstraintEdit );
  238. BOOL GetConstraintEdit();
  239. void Expand();
  240. void Collapse();
  241. void Select();
  242. CRect GetItemRect();
  243. virtual CRect GetValueRect();
  244. virtual void SetValue( CString strValue );
  245. virtual void OnValueChanged( CString strValue );
  246. CSPPropertyGridView * GetGrid();
  247. BOOL IsVisible() const;
  248. BOOL IsExpanded() const;
  249. CSPPropertyGridItem * GetParentItem();
  250. int GetIndent();
  251. virtual BOOL OnDrawItemValue( CDC & dc , CRect rcValue );
  252. virtual void MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct );
  253. BOOL IsSelected();
  254. void BindToString( CString * pBindString );
  255. void Remove();
  256. void SetMask( LPCTSTR strMask , LPCTSTR strLiteral , TCHAR chPrompt = _T( '_' ) );
  257. void SetPasswordMask( TCHAR chMask = _T( '*' ) );
  258. CString GetMaskedText();
  259. void SetMaskedText( LPCTSTR lpszMaskedText );
  260. virtual CString GetViewValue();
  261. void SetItemData( DWORD_PTR dwData );
  262. DWORD_PTR GetItemData();
  263. virtual CSPPropertyGridInplaceEdit &GetInplaceEdit();
  264. protected:
  265. virtual void SetVisible( BOOL bVisible );
  266. virtual void OnSelect();
  267. virtual void OnDeselect();
  268. virtual BOOL OnChar( UINT nChar );
  269. virtual void OnLButtonDblClk();
  270. virtual void OnRButtonDown();
  271. virtual BOOL OnLButtonDown( UINT nFlags , CPoint point );
  272. virtual void OnAddChildItem();
  273. virtual void OnInplaceButtonDown();
  274. virtual CSPPropertyGridInplaceButton&GetInplaceButton();
  275. virtual CSPPropertyGridInplaceList &GetInplaceList();
  276. virtual void OnValidateEdit();
  277. virtual void SetEditText( CString str );
  278. virtual void OnBeforeInsert();
  279. virtual void OnConstraintsChanged();
  280. protected:
  281. BOOL m_bConstraintEdit; // TRUE to constraint edit.
  282. int m_nIndex; // Index of the item.
  283. int m_nIndent; // Indent of the item.
  284. UINT m_nID; // Identifier of the item.
  285. UINT m_nFlags; // Item's flags.
  286. BOOL m_bReadOnly; // TRUE to disable item's edition.
  287. BOOL m_bVisible; // Visibility of the item.
  288. BOOL m_bCategory; // TRUE if the item is category.
  289. BOOL m_bExpanded; // TRUE if item is expanded.
  290. CString m_strValue; // Value of the item.
  291. CString m_strCaption; // Caption of the item.
  292. CString m_strDescription; // Description of the item.
  293. CSPPropertyGridItem * m_pParent; // Parent item.
  294. CSPPropertyGridView * m_pGrid; // Parent grid class.
  295. CSPPropertyGridItems * m_pChilds; // Child items.
  296. CSPPropertyGridItemConstraints * m_pConstraints; // Item's constraints.
  297. CString * m_pBindString; // Binded string.
  298. CString m_strFormat; // Format of the double value.
  299. CString m_strMask; // String to use as edit mask.
  300. CString m_strLiteral; // String to use as literal. This is the same as the edit mask, but all mask characters are replaced with m_chPrompt.
  301. TCHAR m_chPrompt; // Character used as a space holder for a character. This is used in m_strLiteral.
  302. bool m_bUseMask; // TRUE to use and edit mask to display item data.
  303. BOOL m_bPassword; // TRUE to use a password mask. If TRUE, then each character will be replaced with an asterisk (*).
  304. DWORD_PTR m_dwData; // The 32-bit value associated with the item
  305. private:
  306. void Init();
  307. void Refresh( BOOL bInvalidate = TRUE );
  308. friend class CSPPropertyGridItems;
  309. friend class CSPPropertyGridView;
  310. friend class CSPPropertyGridInplaceEdit;
  311. friend class CSPPropertyGridInplaceButton;
  312. DECLARE_DYNAMIC( CSPPropertyGridItem )
  313. friend class CSPPropertyGridItemConstraints;
  314. friend class CSPPropertyGrid;
  315. };
  316. class CSPPropertyGridItemCategory : public CSPPropertyGridItem
  317. {
  318. public:
  319. CSPPropertyGridItemCategory( CString strCaption );
  320. CSPPropertyGridItemCategory( UINT nID ); // <COMBINE CSPPropertyGridItemCategory::CSPPropertyGridItemCategory@CString>
  321. };
  322. //////////////////////////////////////////////////////////////////////
  323. AFX_INLINE long CSPPropertyGridItems::GetCount()
  324. {
  325. return ( long ) m_arrItems.GetSize();
  326. }
  327. AFX_INLINE BOOL CSPPropertyGridItems::IsEmpty()
  328. {
  329. return GetCount() == 0;
  330. }
  331. AFX_INLINE void CSPPropertyGridItems::AddTail( CSPPropertyGridItem * pItem )
  332. {
  333. m_arrItems.Add( pItem );
  334. }
  335. AFX_INLINE void CSPPropertyGridItems::InsertAt( int nIndex , CSPPropertyGridItem * pItem )
  336. {
  337. m_arrItems.InsertAt( nIndex,pItem );
  338. }
  339. AFX_INLINE BOOL CSPPropertyGridItem::HasChilds()
  340. {
  341. return !m_pChilds->IsEmpty();
  342. }
  343. AFX_INLINE void CSPPropertyGridItem::SetCaption( CString strCaption )
  344. {
  345. m_strCaption = strCaption;
  346. }
  347. AFX_INLINE CString CSPPropertyGridItem::GetCaption() const
  348. {
  349. return m_strCaption;
  350. }
  351. AFX_INLINE CString CSPPropertyGridItem::GetDescription()
  352. {
  353. return m_strDescription;
  354. }
  355. AFX_INLINE BOOL CSPPropertyGridItem::GetReadOnly()
  356. {
  357. return m_bReadOnly;
  358. }
  359. AFX_INLINE void CSPPropertyGridItem::SetID( UINT nID )
  360. {
  361. m_nID = nID;
  362. }
  363. AFX_INLINE UINT CSPPropertyGridItem::GetID()
  364. {
  365. return m_nID;
  366. }
  367. AFX_INLINE CString CSPPropertyGridItem::GetValue()
  368. {
  369. return m_strValue;
  370. }
  371. AFX_INLINE BOOL CSPPropertyGridItem::IsCategory()
  372. {
  373. return m_bCategory;
  374. }
  375. AFX_INLINE CSPPropertyGridView * CSPPropertyGridItem::GetGrid()
  376. {
  377. return m_pGrid;
  378. }
  379. AFX_INLINE BOOL CSPPropertyGridItem::OnDrawItemValue( CDC & , CRect )
  380. {
  381. return FALSE;
  382. }
  383. AFX_INLINE void CSPPropertyGridItem::OnAddChildItem()
  384. {
  385. }
  386. AFX_INLINE void CSPPropertyGridItem::SetFlags( UINT nFlags )
  387. {
  388. ASSERT( !m_bCategory );
  389. ASSERT( ( nFlags & ( SPGridItemHasComboButton | SPGridItemHasExpandButton ) ) != ( SPGridItemHasComboButton | SPGridItemHasExpandButton ) );
  390. m_nFlags = nFlags;
  391. }
  392. AFX_INLINE UINT CSPPropertyGridItem::GetFlags()
  393. {
  394. return m_nFlags;
  395. }
  396. AFX_INLINE BOOL CSPPropertyGridItem::IsVisible() const
  397. {
  398. return m_bVisible;
  399. }
  400. AFX_INLINE BOOL CSPPropertyGridItem::IsExpanded() const
  401. {
  402. return m_bExpanded;
  403. }
  404. AFX_INLINE CSPPropertyGridItem * CSPPropertyGridItem::GetParentItem()
  405. {
  406. return m_pParent;
  407. }
  408. AFX_INLINE CSPPropertyGridItems * CSPPropertyGridItem::GetChilds()
  409. {
  410. return m_pChilds;
  411. }
  412. AFX_INLINE int CSPPropertyGridItem::GetIndent()
  413. {
  414. return m_nIndent;
  415. }
  416. AFX_INLINE void CSPPropertyGridItem::SetConstraintEdit( BOOL bConstraintEdit )
  417. {
  418. m_bConstraintEdit = bConstraintEdit;
  419. }
  420. AFX_INLINE BOOL CSPPropertyGridItem::GetConstraintEdit()
  421. {
  422. return m_bConstraintEdit;
  423. }
  424. AFX_INLINE void CSPPropertyGridItem::SetPasswordMask( TCHAR chMask )
  425. {
  426. m_bPassword = TRUE;
  427. m_chPrompt = chMask;
  428. }
  429. AFX_INLINE void CSPPropertyGridItem::OnRButtonDown()
  430. {
  431. }
  432. AFX_INLINE void CSPPropertyGridItem::OnConstraintsChanged()
  433. {
  434. }
  435. AFX_INLINE void CSPPropertyGridItem::MeasureItem( LPMEASUREITEMSTRUCT /*lpMeasureItemStruct*/ )
  436. {
  437. }
  438. AFX_INLINE void CSPPropertyGridItem::SetItemData( DWORD_PTR dwData )
  439. {
  440. m_dwData = dwData;
  441. }
  442. AFX_INLINE DWORD_PTR CSPPropertyGridItem::GetItemData()
  443. {
  444. return m_dwData;
  445. }
  446. AFX_INLINE void CSPPropertyGridItem::SetVisible( BOOL bVisible )
  447. {
  448. m_bVisible = bVisible;
  449. }
  450. AFX_INLINE CSPPropertyGridItemConstraints * CSPPropertyGridItem::GetConstraints()
  451. {
  452. return m_pConstraints;
  453. }
  454. AFX_INLINE int CSPPropertyGridItemConstraints::GetCount()
  455. {
  456. return ( int ) m_arrConstraints.GetSize();
  457. }
  458. AFX_INLINE BOOL CSPPropertyGridItemConstraints::IsEmpty()
  459. {
  460. return GetCount() == 0;
  461. }
  462. AFX_INLINE void CSPPropertyGridItemConstraints::SetCurrent( int nIndex )
  463. {
  464. m_nCurrent = nIndex;
  465. }
  466. AFX_INLINE int CSPPropertyGridItemConstraints::GetCurrent()
  467. {
  468. return m_nCurrent;
  469. }
  470. AFX_INLINE int CSPPropertyGridItemConstraint::GetIndex()
  471. {
  472. return m_nIndex;
  473. }
  474. #endif