PropertyGrid.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #pragma once
  2. #include "PropertyGridCombo.h"
  3. #include "PropertyGridInPlaceEdit.h"
  4. #include <map>
  5. #include <vector>
  6. #include <string>
  7. using namespace std;
  8. // CPropertyGrid
  9. #define WM_PG_ITEMCHANGED WM_USER+486
  10. typedef UINT HSECTION;
  11. typedef UINT HITEM;
  12. class ICustomItem;
  13. class CPropertyGrid : public CWnd
  14. {
  15. DECLARE_DYNAMIC(CPropertyGrid)
  16. public:
  17. // display mode
  18. //显示模式;
  19. enum EDisplayMode
  20. {
  21. DM_CATEGORIZED = 0, //分类;
  22. DM_ALPHABETICAL, //按字母排序;
  23. DM_NOSORT //无排序;
  24. };
  25. // editing
  26. //编辑;
  27. enum EEditMode
  28. {
  29. EM_CUSTOM = 0, //自定义;
  30. EM_INPLACE, //就地编辑;
  31. EM_DROPDOWN, //下拉;
  32. EM_MODAL, //模态(对话框);
  33. EM_IMMEDIATE //即时;
  34. };
  35. // 项类型;
  36. enum EItemType
  37. {
  38. IT_CUSTOM = 0, //自定义;
  39. IT_STRING, //字符串;
  40. IT_TEXT, //文本;
  41. IT_INTEGER, //整型;
  42. IT_DOUBLE, //双精度;
  43. IT_COMBO, //下拉框;
  44. IT_BOOLEAN, //布尔;
  45. IT_DATE, //日期;
  46. IT_DATETIME, //日期时间;
  47. IT_FILE, //文件;
  48. IT_FOLDER, //目录;
  49. IT_COLOR, //颜色;
  50. IT_FONT, //字体;
  51. IT_CHECK //复选框;
  52. };
  53. public:
  54. CPropertyGrid();
  55. virtual ~CPropertyGrid();
  56. // customization
  57. // 定制的接口;
  58. bool GetShadeTitles(); // 获取标题阴影状态;
  59. void SetShadeTitles(bool shade_titles); // 设置标题是否有阴影;
  60. bool GetDrawLines(); // 获取行格线状态;
  61. void SetDrawLines(bool draw_lines); // 设置是否有行格线;
  62. bool GetDrawGutter(); // 获取列格线状态;
  63. void SetDrawGutter(bool draw_gutter); // 设置是否有列格线;
  64. bool GetFocusDisabled(); // 获取焦点状态;
  65. void SetFocusDisabled(bool focus_disabled); // 设置是否有焦点;
  66. bool GetBoldModified(); // 获取黑体状态;
  67. void SetBoldModified(bool bold_modified); // 设置是否黑体字体;
  68. bool GetBoldEditables(); // 获取黑体编辑状态;
  69. void SetBoldEditables(bool bold_editables); // 设置是否黑体编辑;
  70. // gutter width
  71. int GetGutterWidth(); // 获取列格线宽度;
  72. void SetGutterWidth(int gutter_width); // 设置列格线宽度;
  73. // custom colors
  74. void SetTextColor(COLORREF clrText);
  75. void SetTitleColor(COLORREF clrText);
  76. void SetBackColor(COLORREF clrBack);
  77. void SetShadeColor(COLORREF clrShade);
  78. void SetFocusColor(COLORREF clrFocus);
  79. void SetHiliteColor(COLORREF clrHilite);
  80. void SetEditableColor(COLORREF clrEditable);
  81. void SetDisabledColor(COLORREF clrDisabled);
  82. // localization
  83. void SetTrueFalseStrings(string strTrue, string strFalse);
  84. void SetOkCancelStrings(string strOk, string strCancel);
  85. void SetDateTimeStrings(string strDate, string strTime);
  86. void SetUndefinedString(string strUndefined);
  87. void SetEmptyString(string strEmpty);
  88. // add a section
  89. HSECTION AddSection(string title, bool collapsed = false, HSECTION after = -1);
  90. // add items
  91. HITEM AddCustomItem(HSECTION, string name, ICustomItem* pItem, bool editable = true, HITEM after = -1);
  92. HITEM AddStringItem(HSECTION section, string name, string value, bool editable = true, HITEM after = -1);
  93. HITEM AddTextItem(HSECTION section, string name, string value, bool editable = true, HITEM after = -1);
  94. HITEM AddIntegerItem(HSECTION section, string name, int value, string format = "", bool editable = true, bool undefined = false, HITEM after = -1);
  95. HITEM AddDoubleItem(HSECTION section, string name, double value, string format = "", bool editable = true, bool undefined = false, HITEM after = -1);
  96. HITEM AddComboItem(HSECTION section, string name, const vector<string>& values, int cur, bool editable = true, bool undefined = false, HITEM after = -1);
  97. HITEM AddBoolItem(HSECTION section, string name, bool value, bool editable = true, bool undefined = false, HITEM after = -1);
  98. HITEM AddDateItem(HSECTION section, string name, COleDateTime value, string format = "", bool editable = true, bool undefined = false, HITEM after = -1);
  99. HITEM AddDateTimeItem(HSECTION section, string name, COleDateTime value, string format = "", bool editable = true, bool undefined = false, HITEM after = -1);
  100. HITEM AddFileItem(HSECTION section, string name, string value, string filter = "", bool editable = true, HITEM after = -1);
  101. HITEM AddFolderItem(HSECTION section, string name, string value, string title = "", bool editable = true, HITEM after = -1);
  102. HITEM AddColorItem(HSECTION section, string name, COLORREF value, bool editable = true, bool undefined = false, HITEM after = -1);
  103. HITEM AddFontItem(HSECTION section, string name, LOGFONT value, bool editable = true, bool undefined = false, HITEM after = -1);
  104. HITEM AddCheckItem(HSECTION section, string name, bool value, bool editable = true, bool undefined = false, HITEM after = -1);
  105. // contents
  106. void ResetContents();
  107. bool RemoveSection(HSECTION hs);
  108. bool RemoveItem(HITEM item);
  109. void ValidateChanges();
  110. // status
  111. int GetNumSections();
  112. int GetSectionSize(HSECTION hs);
  113. // get item value
  114. bool GetItemValue(HITEM item, string& strValue) const;
  115. bool GetItemValue(HITEM item, int& nValue) const;
  116. bool GetItemValue(HITEM item, double& dValue) const;
  117. bool GetItemValue(HITEM item, bool& bValue) const;
  118. bool GetItemValue(HITEM item, COleDateTime& dtValue) const;
  119. bool GetItemValue(HITEM item, COLORREF& clrValue) const;
  120. bool GetItemValue(HITEM item, LOGFONT& lfValue) const;
  121. // set item value
  122. bool SetItemValue(HITEM item, const string strValue);
  123. bool SetItemValue(HITEM item, const int nValue);
  124. bool SetItemValue(HITEM item, const double nValue);
  125. bool SetItemValue(HITEM item, const bool bValue);
  126. bool SetItemValue(HITEM item, const COleDateTime dtValue);
  127. bool SetItemValue(HITEM item, const COLORREF clrValue);
  128. bool SetItemValue(HITEM item, const LOGFONT lfValue);
  129. // for custom items
  130. int GetTextMargin();
  131. CFont* GetFontNormal();
  132. CFont* GetFontBold();
  133. // appearance stuff
  134. void SetDisplayMode(EDisplayMode display_mode);
  135. void ExpandAll(bool expand);
  136. void ExpandSection(HSECTION hs, bool expand);
  137. bool IsSectionCollapsed(HSECTION hs);
  138. protected:
  139. class CItem // 一个项所具有的特性;
  140. {
  141. public:
  142. HITEM m_id; // 项ID;
  143. bool m_editable; // 项可编辑否;
  144. bool m_undefined; // 项是否定义;
  145. EItemType m_type; // 项类型;
  146. string m_name; // 项名称;
  147. vector<string> m_options; // 项字符操作;
  148. /****以下是项框内当前所能表示的值;****/
  149. int m_nValue; // 项的整型表示值;
  150. double m_dValue; // 项的双精度表示值;
  151. string m_strValue; // 项的字符表示内容;
  152. bool m_bValue; // 项是否有值;
  153. COleDateTime m_dtValue; // 项的时间表示内容;
  154. COLORREF m_clrValue; // 项的颜色表示;
  155. LOGFONT m_lfValue; // 项的字体表示;
  156. ICustomItem* m_pCustom; // 项的用户定义表示;
  157. /****以下是项框内过去所能表示的值;****/
  158. bool m_undefined_old; // 是否被定义;
  159. int m_nValue_old; // 旧的整型值;
  160. double m_dValue_old; // 旧的浮点值;
  161. string m_strValue_old; // 旧的字符内容;
  162. bool m_bValue_old; // 过去是否有值;
  163. COleDateTime m_dtValue_old; // 旧的时间值;
  164. COLORREF m_clrValue_old; // 旧的颜色值;
  165. LOGFONT m_lfValue_old; // 旧的字体值;
  166. // 该项只有二列;(可自行添加列数)
  167. CRect m_rcName; // 项列之一:名称框;
  168. CRect m_rcValue; // 项列之一:值框;
  169. bool operator==(const HITEM& item) const;
  170. bool operator==(const string& name) const;
  171. void ValidateChanges();
  172. };
  173. friend bool item_alpha_sort(vector<CPropertyGrid::CItem>::iterator it1, vector<CPropertyGrid::CItem>::iterator it2);
  174. // 一个段的特性;
  175. class CSection
  176. {
  177. public:
  178. HSECTION m_id; // 段ID;
  179. string m_title; // 段名;
  180. bool m_collapsed; // 是否收缩;
  181. vector<CItem> m_items; // 所包含的项;
  182. CRect m_rcSign; // 段标识框(段名框在其内?);
  183. CRect m_rcTitle; // 段名框;
  184. bool operator==(const HSECTION& section) const;
  185. };
  186. // 一个控件所包含的段;
  187. vector<CSection> m_sections;
  188. HSECTION m_focused_section; // 焦点所在的段的ID;
  189. HITEM m_focused_item; // 焦点所在的项的ID;
  190. EDisplayMode m_display_mode; // 显示模式;
  191. // 以下为显示模式开关;
  192. bool m_shade_titles; // 是否显示标题边框阴影;
  193. bool m_draw_lines; // 是否显示行分格线;
  194. bool m_draw_gutter; // 是否显示列分格线;
  195. bool m_focus_disabled; // 是否禁用焦点;
  196. bool m_bold_modified; // 是否可修改;
  197. bool m_bold_editables; // 是否可编辑;
  198. int m_gutter_width; // 列分格线宽;
  199. bool m_resizing_gutter; // 是否再生列分格线;
  200. CPoint m_ptLast; // 最后的点;
  201. CFont m_fntNormal; // 正常字体;
  202. CFont m_fntBold; // 粗体;
  203. int m_line_height;
  204. // 行为动作开关;
  205. CRect m_rect_button; // 按钮框;
  206. CWnd* m_control; // 控件;
  207. bool m_button_pushed; // 按钮是否按下;
  208. bool m_button_depressed; // 按钮是否按住;
  209. bool m_value_clicked; // 值是否单击;
  210. bool m_custom_tracking; // 下拉框是否变动;
  211. HSECTION m_section_id; // 当前段ID;
  212. HITEM m_item_id; // 当前项ID;
  213. string m_strTrue;
  214. string m_strFalse;
  215. string m_strOk;
  216. string m_strCancel;
  217. string m_strDate;
  218. string m_strTime;
  219. string m_strUndefined;
  220. string m_strEmpty;
  221. COLORREF m_clrText; // 文本颜色;
  222. COLORREF m_clrTitle; // 标题颜色;
  223. COLORREF m_clrBack; // 背景颜色;
  224. COLORREF m_clrShade; // 阴影颜色;
  225. COLORREF m_clrFocus; // 焦点选中时颜色;
  226. COLORREF m_clrHilite; // 隐藏时颜色;
  227. COLORREF m_clrEditable; // 可编辑时颜色;
  228. COLORREF m_clrDisabled; // 不可编辑时颜色;
  229. protected:
  230. DECLARE_MESSAGE_MAP()
  231. // init control
  232. void InitControl(); // 初始化该控件;
  233. // drawing
  234. void DrawItem(CDC& dc, int w, int x, int y, vector<CItem>::iterator& it);
  235. // item management
  236. CSection* FindSection(HSECTION hs) const; // 通过ID查找;
  237. CItem* FindItem(HITEM hi) const; // 通过ID查找;
  238. HITEM AddItem(HSECTION hs, EItemType type, string name, void* pValue, bool editable, bool undefined, HITEM after);
  239. // scrolling stuff
  240. CScrollBar m_scrollbar;
  241. bool m_scroll_enabled;
  242. int GetScrollOffset();
  243. void RecalcLayout();
  244. // editing
  245. EEditMode GetEditMode(CItem& item);
  246. void DeleteEditControl();
  247. void EditFocusedItem();
  248. // movement in list
  249. void MoveForward(HSECTION& focused_section, HITEM& focused_item);
  250. // keyboard
  251. void FocusNextItem();
  252. void FocusPrevItem();
  253. CRect AdjustCheckBoxRect(CRect* rc);
  254. protected:
  255. virtual void PreSubclassWindow();
  256. public:
  257. afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  258. afx_msg void OnDestroy();
  259. afx_msg BOOL OnEraseBkgnd(CDC* pDC);
  260. afx_msg void OnPaint();
  261. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  262. afx_msg void OnMouseMove(UINT nHitTest, CPoint point);
  263. afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
  264. afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
  265. afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
  266. afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
  267. afx_msg LRESULT OnComboSelChanged(WPARAM wParam, LPARAM lParam);
  268. afx_msg LRESULT OnEditChanged(WPARAM wParam, LPARAM lParam);
  269. afx_msg LRESULT OnDateChanged(WPARAM wParam, LPARAM lParam);
  270. afx_msg void OnSize(UINT nType, int cx, int cy);
  271. afx_msg UINT OnGetDlgCode();
  272. afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
  273. afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
  274. };