PropertyGridItem.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #pragma once
  2. #include <afxtempl.h>
  3. class CPropertyGridView;
  4. class CPropertyGridItem;
  5. const int PGI_EXPAND_BORDER = 14;
  6. // Flags that indicate what styles to set for the grid item.
  7. //
  8. enum PG_PROPERTYGRID_ITEMFLAGS
  9. {
  10. // Item has edit control.
  11. pgitemHasEdit = 1,
  12. // Item has expand button.
  13. pgitemHasExpandButton = 2,
  14. // Item has combo button.
  15. pgitemHasComboButton = 4
  16. };
  17. //////////////////////////////////////////////////////////////////////
  18. // CPGListItems is a CList derived class. It represents items' collection
  19. // with some internal functionality.
  20. //
  21. class CPGListItems : public CList<CPropertyGridItem*, CPropertyGridItem*>
  22. {
  23. public:
  24. // Call this member function to sort items alphabetical
  25. //
  26. void Sort();
  27. // Call this member function to find item by caption.
  28. // Returns the pointer to the Item object.
  29. //
  30. CPropertyGridItem* FindItem(
  31. // Caption for the item to find
  32. CString strCaption);
  33. // Call this member function to find item by identifier.
  34. // Returns the pointer to the Item object.
  35. //
  36. CPropertyGridItem* FindItem(
  37. // Identifier for the item to find
  38. UINT nID);
  39. };
  40. //////////////////////////////////////////////////////////////////////
  41. // CPGItemConstraints is a CList derived class. It represents item
  42. // constraints' collection.
  43. //
  44. class CPGItemConstraints : public CStringList
  45. {
  46. public:
  47. // Call this member function to add new item's constraint.
  48. // Returns the integer value that represents the index of the added constraint.
  49. //
  50. int AddConstraint(
  51. // new constraint
  52. CString str);
  53. // Call this member function to find constraint in the list.
  54. // Returns the zero-based index of the finded constraint; -1 if the constraint is not found.
  55. //
  56. int FindConstraint(
  57. // A constraint to search for
  58. CString str);
  59. };
  60. //////////////////////////////////////////////////////////////////////
  61. // CPropertyGridItem is the base class representing an item of the
  62. // Property Grid Control
  63. //
  64. class CPropertyGridItem
  65. {
  66. public:
  67. // Constructs a CPropertyGridItem object.
  68. //
  69. CPropertyGridItem(
  70. // Caption of the item.
  71. CString strCaption,
  72. // Initial value
  73. LPCTSTR strValue = NULL,
  74. // 属性项的最大字符数,0表示无限制
  75. // 只有字符串属性项可以使用该限制,其余属性项必须设为0
  76. // 另外要注意,在ANSI模式下,一个汉字占两个字符
  77. UINT nMaxLength = 0);
  78. // Constructs a CPropertyGridItem object.
  79. //
  80. CPropertyGridItem(
  81. // Identifier of the item.
  82. UINT nID,
  83. // Initial value
  84. LPCTSTR strValue = NULL,
  85. // 属性项的最大字符数,0表示无限制
  86. // 只有字符串属性项可以使用该限制,其余属性项必须设为0
  87. // 另外要注意,在ANSI模式下,一个汉字占两个字符
  88. UINT nMaxLength = 0);
  89. // NB: class CPropertyGridItem hasn't default constructor.
  90. // Identifier of second constructor can be linked with
  91. // STRINGTABLE resource with same id in such form "Caption\nDescription".
  92. // Destroys a CPropertyGridItem object, handles cleanup and de-allocation.
  93. //
  94. virtual ~CPropertyGridItem(void);
  95. public:
  96. // Call this method to set the caption of the item.
  97. //
  98. void SetCaption(
  99. // The new caption of the item.
  100. CString strCaption);
  101. // Call this method to set the description of the item.
  102. //
  103. void SetDescription(
  104. // The new description of the item.
  105. CString strDescription);
  106. // Calls this method to set the read-only state of the item
  107. //
  108. void SetReadOnly(
  109. // Specifies whether to set or remove the read-only state of the item.
  110. // A value of TRUE sets the state to read-only; a value of FALSE sets the state to read/write.
  111. BOOL bReadOnly);
  112. // Calls this method to set the identifier of the item
  113. //
  114. void SetID(
  115. // The new identifier of the item.
  116. UINT m_nID);
  117. // This member function is called to set a specific flag for the item.
  118. //
  119. void SetFlags(
  120. // Flag to set. It can be one or more of the following:
  121. //
  122. // pgitemHasEdit - Item has edit control.
  123. // pgitemHasExpandButton - Item has expand button.
  124. // pgitemHasComboButton - Item has combo button.
  125. //
  126. UINT nFlags);
  127. // Call this method to get the caption of the item.
  128. //
  129. CString GetCaption();
  130. // Call this method to get the description of the item.
  131. //
  132. CString GetDescription();
  133. // Call this method to get the read-only state of the item.
  134. //
  135. BOOL GetReadOnly();
  136. // Call this method to get the identifier of the item.
  137. //
  138. UINT GetID();
  139. // Call this method to get current value of the item.
  140. //
  141. CString GetValue();
  142. // Call this method to get flags of the item.
  143. //
  144. UINT GetFlags();
  145. // The IsCategory function determines whether the item is a category.
  146. //
  147. BOOL IsCategory();
  148. // Call this method to add child item.
  149. //
  150. CPropertyGridItem* AddChildItem(
  151. // The item to be added as a child.
  152. CPropertyGridItem* pItem);
  153. // Gets the position of the head element of child's list.
  154. //
  155. POSITION GetFirstChild() const;
  156. // Gets the child item identified by rPosition, then sets rPosition to the POSITION value of the next entry in the list.
  157. //
  158. CPropertyGridItem* GetNextChild(
  159. // A reference to a POSITION value returned by a previous GetNextChild, GetFirstChild, or other member function call.
  160. POSITION& rPosition) const;
  161. // The HasChilds function determines whether the item has children.
  162. //
  163. BOOL HasChilds();
  164. // The HasParent function determines whether the item has pParent item as parent.
  165. // Returns TRUE if pParent is parent of the item.
  166. BOOL HasParent(
  167. // item to be tested.
  168. CPropertyGridItem* pParent);
  169. // Call this method to get constraints's list
  170. //
  171. // Example:
  172. //
  173. // CPGItemConstraints* pList = pItem->GetConstraints();
  174. //
  175. CPGItemConstraints* GetConstraints();
  176. // Call this method to expand the item.
  177. //
  178. void Expand();
  179. // Call this method to collapse the item.
  180. //
  181. void Collapse();
  182. // Call this method to select the item.
  183. //
  184. void Select();
  185. // Retrieves the bounding rectangle of the item
  186. //
  187. CRect GetItemRect();
  188. // Retrieves the bounding rectangle of the item's value text
  189. // Override this member function to change it.
  190. //
  191. virtual CRect GetValueRect();
  192. // Call this method to change item's value
  193. // Override this method to add new functionality.
  194. // You should call the base class version of this function from your override.
  195. //
  196. virtual void SetValue(
  197. // New value of the item
  198. CString strValue);
  199. // This method is called when item's value is changed. Override this function if needed.
  200. //
  201. virtual void OnValueChanged(
  202. // New value of the item
  203. CString strValue);
  204. // Returns the parent window.
  205. //
  206. CWnd* GetGrid();
  207. // Call this member function to determine if the item is visible. Returns
  208. // TRUE if the item is visible, otherwise returns FALSE.
  209. //
  210. BOOL IsVisible() const;
  211. // Call this member function to determine if the item is expanded. Returns
  212. // TRUE if the item is expanded, otherwise returns FALSE.
  213. //
  214. BOOL IsExpanded() const;
  215. // Call this member function to retreive the parent property grid item. Returns
  216. // a CPropertyGridItem pointer that represents the parent item, can be NULL.
  217. //
  218. CPropertyGridItem* GetParentItem();
  219. // 设置一个用户定义的32位数据
  220. //
  221. void SetItemData(DWORD dwItemData)
  222. { m_dwItemData = dwItemData; }
  223. // 获取用户定义的32位数据
  224. //
  225. DWORD GetItemData()
  226. { return m_dwItemData; }
  227. // 把一个字符串对象绑定到属性项
  228. void BindToString(CString* pBindString)
  229. {
  230. m_pBindString = pBindString;
  231. if (m_pBindString)
  232. SetValue(*m_pBindString);
  233. }
  234. protected:
  235. // This method is called when item is drawing. Override this function if needed.
  236. // Returns TRUE if item is self-drawing.
  237. //
  238. virtual BOOL OnDrawItemValue(
  239. // Reference to the device context to be used for rendering an image of the item.
  240. CDC& dc,
  241. // Bounding rect of the item
  242. CRect rcValue);
  243. // This method is called when item is selecting.
  244. //
  245. virtual void OnSelect();
  246. // This method is called when item is deselecting.
  247. //
  248. virtual void OnDeselect();
  249. // This method is called when key is pressed.
  250. //
  251. virtual BOOL OnChar(
  252. // Contains the character code value of the key.
  253. UINT nChar);
  254. // This method is called when the user double-clicks the left mouse button.
  255. //
  256. virtual void OnLButtonDblClk();
  257. // This method is called when the user presses the left mouse button.
  258. //
  259. virtual BOOL OnLButtonDown(
  260. // Indicates whether various virtual keys are down (see Visual C++ documentation)
  261. UINT nFlags,
  262. // Specifies the x- and y-coordinate of the cursor.
  263. CPoint point);
  264. // This method is called when item is added to the parent.
  265. //
  266. virtual void OnAddChildItem();
  267. // This method is called when the user presses inplace button.
  268. // Override the method to show item-specific dialog.
  269. //
  270. virtual void OnInplaceButtonDown();
  271. virtual void OnValidateEdit();
  272. // 根据字符数限制调整属性值
  273. CString AdjustValue(CString strValue);
  274. protected:
  275. int m_nIndex;
  276. int m_nIndent;
  277. UINT m_nID;
  278. UINT m_nFlags;
  279. BOOL m_bReadOnly;
  280. BOOL m_bVisible;
  281. BOOL m_bCategory;
  282. BOOL m_bExpanded;
  283. CString m_strValue;
  284. CString m_strCaption;
  285. CString m_strDescription;
  286. CPropertyGridItem* m_pParent;
  287. CPropertyGridView* m_pGrid;
  288. CPGListItems m_lstChilds;
  289. CPGItemConstraints m_lstContraints;
  290. CPropertyGridInplaceEdit& GetInplaceEdit();
  291. CPropertyGridInplaceButton& GetInplaceButton();
  292. CPropertyGridInplaceList& GetInplaceList();
  293. DWORD m_dwItemData; // 一个用户定义的32位数据
  294. CString* m_pBindString; // 绑定到属性项的字符串对象
  295. UINT m_nMaxLength; // 属性项的最大字符数,0表示无限制
  296. // 只有字符串属性项可以使用该限制,其余属性项必须设为0
  297. // 另外要注意,在ANSI模式下,一个汉字占两个字符
  298. public:
  299. void Init();
  300. virtual void SetEditText(CString str);
  301. friend class CPGListItems;
  302. friend class CPropertyGridView;
  303. friend class CPropertyGridInplaceEdit;
  304. friend class CPropertyGridInplaceButton;
  305. };
  306. //////////////////////////////////////////////////////////////////////
  307. AFX_INLINE POSITION CPropertyGridItem::GetFirstChild() const {
  308. return m_lstChilds.GetHeadPosition();
  309. }
  310. AFX_INLINE CPropertyGridItem* CPropertyGridItem::GetNextChild(POSITION& rPosition) const {
  311. return m_lstChilds.GetNext(rPosition);
  312. }
  313. AFX_INLINE BOOL CPropertyGridItem::HasChilds() {
  314. return !m_lstChilds.IsEmpty();
  315. }
  316. AFX_INLINE void CPropertyGridItem::SetCaption(CString strCaption) {
  317. m_strCaption = strCaption;
  318. }
  319. AFX_INLINE CString CPropertyGridItem::GetCaption() {
  320. return m_strCaption;
  321. }
  322. AFX_INLINE void CPropertyGridItem::SetDescription(CString strDescription) {
  323. m_strDescription = strDescription;
  324. }
  325. AFX_INLINE CString CPropertyGridItem::GetDescription() {
  326. return m_strDescription;
  327. }
  328. AFX_INLINE BOOL CPropertyGridItem::GetReadOnly() {
  329. return m_bReadOnly;
  330. }
  331. AFX_INLINE void CPropertyGridItem::SetID(UINT nID) {
  332. m_nID = nID;
  333. }
  334. AFX_INLINE UINT CPropertyGridItem::GetID() {
  335. return m_nID;
  336. }
  337. AFX_INLINE CString CPropertyGridItem::GetValue() {
  338. return m_strValue;
  339. }
  340. AFX_INLINE BOOL CPropertyGridItem::IsCategory() {
  341. return m_bCategory;
  342. }
  343. AFX_INLINE CWnd* CPropertyGridItem::GetGrid() {
  344. return (CWnd*)m_pGrid;
  345. }
  346. AFX_INLINE BOOL CPropertyGridItem::OnDrawItemValue(CDC&, CRect) {
  347. return FALSE;
  348. }
  349. AFX_INLINE void CPropertyGridItem::OnAddChildItem() {
  350. }
  351. AFX_INLINE void CPropertyGridItem::SetFlags(UINT nFlags) {
  352. ASSERT(!m_bCategory); ASSERT((nFlags & (pgitemHasComboButton | pgitemHasExpandButton)) != (pgitemHasComboButton | pgitemHasExpandButton)); m_nFlags = nFlags;
  353. }
  354. AFX_INLINE UINT CPropertyGridItem::GetFlags() {
  355. return m_nFlags;
  356. }
  357. AFX_INLINE BOOL CPropertyGridItem::IsVisible() const {
  358. return m_bVisible;
  359. }
  360. AFX_INLINE BOOL CPropertyGridItem::IsExpanded() const {
  361. return m_bExpanded;
  362. }
  363. AFX_INLINE CPropertyGridItem* CPropertyGridItem::GetParentItem() {
  364. return m_pParent;
  365. }
  366. //////////////////////////////////////////////////////////////////////
  367. AFX_INLINE int CPGItemConstraints::AddConstraint(CString str){
  368. AddTail(str); return (int)GetCount() - 1;
  369. }
  370. AFX_INLINE CPGItemConstraints* CPropertyGridItem::GetConstraints(){
  371. return &m_lstContraints;
  372. }