grideditors.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/generic/grideditors.h
  3. // Purpose: wxGridCellEditorEvtHandler and wxGrid editors
  4. // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn)
  5. // Modified by: Santiago Palacios
  6. // Created: 1/08/1999
  7. // Copyright: (c) Michael Bedward
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_GENERIC_GRID_EDITORS_H_
  11. #define _WX_GENERIC_GRID_EDITORS_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_GRID
  14. #include "wx/scopedptr.h"
  15. class wxGridCellEditorEvtHandler : public wxEvtHandler
  16. {
  17. public:
  18. wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor)
  19. : m_grid(grid),
  20. m_editor(editor),
  21. m_inSetFocus(false)
  22. {
  23. }
  24. void OnKillFocus(wxFocusEvent& event);
  25. void OnKeyDown(wxKeyEvent& event);
  26. void OnChar(wxKeyEvent& event);
  27. void SetInSetFocus(bool inSetFocus) { m_inSetFocus = inSetFocus; }
  28. private:
  29. wxGrid *m_grid;
  30. wxGridCellEditor *m_editor;
  31. // Work around the fact that a focus kill event can be sent to
  32. // a combobox within a set focus event.
  33. bool m_inSetFocus;
  34. DECLARE_EVENT_TABLE()
  35. DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler)
  36. wxDECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler);
  37. };
  38. #if wxUSE_TEXTCTRL
  39. // the editor for string/text data
  40. class WXDLLIMPEXP_ADV wxGridCellTextEditor : public wxGridCellEditor
  41. {
  42. public:
  43. wxEXPLICIT wxGridCellTextEditor(size_t maxChars = 0);
  44. virtual void Create(wxWindow* parent,
  45. wxWindowID id,
  46. wxEvtHandler* evtHandler);
  47. virtual void SetSize(const wxRect& rect);
  48. virtual void PaintBackground(wxDC& dc,
  49. const wxRect& rectCell,
  50. const wxGridCellAttr& attr);
  51. virtual bool IsAcceptedKey(wxKeyEvent& event);
  52. virtual void BeginEdit(int row, int col, wxGrid* grid);
  53. virtual bool EndEdit(int row, int col, const wxGrid* grid,
  54. const wxString& oldval, wxString *newval);
  55. virtual void ApplyEdit(int row, int col, wxGrid* grid);
  56. virtual void Reset();
  57. virtual void StartingKey(wxKeyEvent& event);
  58. virtual void HandleReturn(wxKeyEvent& event);
  59. // parameters string format is "max_width"
  60. virtual void SetParameters(const wxString& params);
  61. #if wxUSE_VALIDATORS
  62. virtual void SetValidator(const wxValidator& validator);
  63. #endif
  64. virtual wxGridCellEditor *Clone() const;
  65. // added GetValue so we can get the value which is in the control
  66. virtual wxString GetValue() const;
  67. protected:
  68. wxTextCtrl *Text() const { return (wxTextCtrl *)m_control; }
  69. // parts of our virtual functions reused by the derived classes
  70. void DoCreate(wxWindow* parent, wxWindowID id, wxEvtHandler* evtHandler,
  71. long style = 0);
  72. void DoBeginEdit(const wxString& startValue);
  73. void DoReset(const wxString& startValue);
  74. private:
  75. size_t m_maxChars; // max number of chars allowed
  76. #if wxUSE_VALIDATORS
  77. wxScopedPtr<wxValidator> m_validator;
  78. #endif
  79. wxString m_value;
  80. wxDECLARE_NO_COPY_CLASS(wxGridCellTextEditor);
  81. };
  82. // the editor for numeric (long) data
  83. class WXDLLIMPEXP_ADV wxGridCellNumberEditor : public wxGridCellTextEditor
  84. {
  85. public:
  86. // allows to specify the range - if min == max == -1, no range checking is
  87. // done
  88. wxGridCellNumberEditor(int min = -1, int max = -1);
  89. virtual void Create(wxWindow* parent,
  90. wxWindowID id,
  91. wxEvtHandler* evtHandler);
  92. virtual bool IsAcceptedKey(wxKeyEvent& event);
  93. virtual void BeginEdit(int row, int col, wxGrid* grid);
  94. virtual bool EndEdit(int row, int col, const wxGrid* grid,
  95. const wxString& oldval, wxString *newval);
  96. virtual void ApplyEdit(int row, int col, wxGrid* grid);
  97. virtual void Reset();
  98. virtual void StartingKey(wxKeyEvent& event);
  99. // parameters string format is "min,max"
  100. virtual void SetParameters(const wxString& params);
  101. virtual wxGridCellEditor *Clone() const
  102. { return new wxGridCellNumberEditor(m_min, m_max); }
  103. // added GetValue so we can get the value which is in the control
  104. virtual wxString GetValue() const;
  105. protected:
  106. #if wxUSE_SPINCTRL
  107. wxSpinCtrl *Spin() const { return (wxSpinCtrl *)m_control; }
  108. #endif
  109. // if HasRange(), we use wxSpinCtrl - otherwise wxTextCtrl
  110. bool HasRange() const
  111. {
  112. #if wxUSE_SPINCTRL
  113. return m_min != m_max;
  114. #else
  115. return false;
  116. #endif
  117. }
  118. // string representation of our value
  119. wxString GetString() const
  120. { return wxString::Format(wxT("%ld"), m_value); }
  121. private:
  122. int m_min,
  123. m_max;
  124. long m_value;
  125. wxDECLARE_NO_COPY_CLASS(wxGridCellNumberEditor);
  126. };
  127. enum wxGridCellFloatFormat
  128. {
  129. // Decimal floating point (%f)
  130. wxGRID_FLOAT_FORMAT_FIXED = 0x0010,
  131. // Scientific notation (mantise/exponent) using e character (%e)
  132. wxGRID_FLOAT_FORMAT_SCIENTIFIC = 0x0020,
  133. // Use the shorter of %e or %f (%g)
  134. wxGRID_FLOAT_FORMAT_COMPACT = 0x0040,
  135. // To use in combination with one of the above formats (%F/%E/%G)
  136. wxGRID_FLOAT_FORMAT_UPPER = 0x0080,
  137. // Format used by default.
  138. wxGRID_FLOAT_FORMAT_DEFAULT = wxGRID_FLOAT_FORMAT_FIXED,
  139. // A mask to extract format from the combination of flags.
  140. wxGRID_FLOAT_FORMAT_MASK = wxGRID_FLOAT_FORMAT_FIXED |
  141. wxGRID_FLOAT_FORMAT_SCIENTIFIC |
  142. wxGRID_FLOAT_FORMAT_COMPACT |
  143. wxGRID_FLOAT_FORMAT_UPPER
  144. };
  145. // the editor for floating point numbers (double) data
  146. class WXDLLIMPEXP_ADV wxGridCellFloatEditor : public wxGridCellTextEditor
  147. {
  148. public:
  149. wxGridCellFloatEditor(int width = -1,
  150. int precision = -1,
  151. int format = wxGRID_FLOAT_FORMAT_DEFAULT);
  152. virtual void Create(wxWindow* parent,
  153. wxWindowID id,
  154. wxEvtHandler* evtHandler);
  155. virtual bool IsAcceptedKey(wxKeyEvent& event);
  156. virtual void BeginEdit(int row, int col, wxGrid* grid);
  157. virtual bool EndEdit(int row, int col, const wxGrid* grid,
  158. const wxString& oldval, wxString *newval);
  159. virtual void ApplyEdit(int row, int col, wxGrid* grid);
  160. virtual void Reset();
  161. virtual void StartingKey(wxKeyEvent& event);
  162. virtual wxGridCellEditor *Clone() const
  163. { return new wxGridCellFloatEditor(m_width, m_precision); }
  164. // parameters string format is "width[,precision[,format]]"
  165. // format to choose beween f|e|g|E|G (f is used by default)
  166. virtual void SetParameters(const wxString& params);
  167. protected:
  168. // string representation of our value
  169. wxString GetString();
  170. private:
  171. int m_width,
  172. m_precision;
  173. double m_value;
  174. int m_style;
  175. wxString m_format;
  176. wxDECLARE_NO_COPY_CLASS(wxGridCellFloatEditor);
  177. };
  178. #endif // wxUSE_TEXTCTRL
  179. #if wxUSE_CHECKBOX
  180. // the editor for boolean data
  181. class WXDLLIMPEXP_ADV wxGridCellBoolEditor : public wxGridCellEditor
  182. {
  183. public:
  184. wxGridCellBoolEditor() { }
  185. virtual void Create(wxWindow* parent,
  186. wxWindowID id,
  187. wxEvtHandler* evtHandler);
  188. virtual void SetSize(const wxRect& rect);
  189. virtual void Show(bool show, wxGridCellAttr *attr = NULL);
  190. virtual bool IsAcceptedKey(wxKeyEvent& event);
  191. virtual void BeginEdit(int row, int col, wxGrid* grid);
  192. virtual bool EndEdit(int row, int col, const wxGrid* grid,
  193. const wxString& oldval, wxString *newval);
  194. virtual void ApplyEdit(int row, int col, wxGrid* grid);
  195. virtual void Reset();
  196. virtual void StartingClick();
  197. virtual void StartingKey(wxKeyEvent& event);
  198. virtual wxGridCellEditor *Clone() const
  199. { return new wxGridCellBoolEditor; }
  200. // added GetValue so we can get the value which is in the control, see
  201. // also UseStringValues()
  202. virtual wxString GetValue() const;
  203. // set the string values returned by GetValue() for the true and false
  204. // states, respectively
  205. static void UseStringValues(const wxString& valueTrue = wxT("1"),
  206. const wxString& valueFalse = wxEmptyString);
  207. // return true if the given string is equal to the string representation of
  208. // true value which we currently use
  209. static bool IsTrueValue(const wxString& value);
  210. protected:
  211. wxCheckBox *CBox() const { return (wxCheckBox *)m_control; }
  212. private:
  213. bool m_value;
  214. static wxString ms_stringValues[2];
  215. wxDECLARE_NO_COPY_CLASS(wxGridCellBoolEditor);
  216. };
  217. #endif // wxUSE_CHECKBOX
  218. #if wxUSE_COMBOBOX
  219. // the editor for string data allowing to choose from the list of strings
  220. class WXDLLIMPEXP_ADV wxGridCellChoiceEditor : public wxGridCellEditor
  221. {
  222. public:
  223. // if !allowOthers, user can't type a string not in choices array
  224. wxGridCellChoiceEditor(size_t count = 0,
  225. const wxString choices[] = NULL,
  226. bool allowOthers = false);
  227. wxGridCellChoiceEditor(const wxArrayString& choices,
  228. bool allowOthers = false);
  229. virtual void Create(wxWindow* parent,
  230. wxWindowID id,
  231. wxEvtHandler* evtHandler);
  232. virtual void SetSize(const wxRect& rect);
  233. virtual void PaintBackground(wxDC& dc,
  234. const wxRect& rectCell,
  235. const wxGridCellAttr& attr);
  236. virtual void BeginEdit(int row, int col, wxGrid* grid);
  237. virtual bool EndEdit(int row, int col, const wxGrid* grid,
  238. const wxString& oldval, wxString *newval);
  239. virtual void ApplyEdit(int row, int col, wxGrid* grid);
  240. virtual void Reset();
  241. // parameters string format is "item1[,item2[...,itemN]]"
  242. virtual void SetParameters(const wxString& params);
  243. virtual wxGridCellEditor *Clone() const;
  244. // added GetValue so we can get the value which is in the control
  245. virtual wxString GetValue() const;
  246. protected:
  247. wxComboBox *Combo() const { return (wxComboBox *)m_control; }
  248. wxString m_value;
  249. wxArrayString m_choices;
  250. bool m_allowOthers;
  251. wxDECLARE_NO_COPY_CLASS(wxGridCellChoiceEditor);
  252. };
  253. #endif // wxUSE_COMBOBOX
  254. #if wxUSE_COMBOBOX
  255. class WXDLLIMPEXP_ADV wxGridCellEnumEditor : public wxGridCellChoiceEditor
  256. {
  257. public:
  258. wxGridCellEnumEditor( const wxString& choices = wxEmptyString );
  259. virtual ~wxGridCellEnumEditor() {}
  260. virtual wxGridCellEditor* Clone() const;
  261. virtual void BeginEdit(int row, int col, wxGrid* grid);
  262. virtual bool EndEdit(int row, int col, const wxGrid* grid,
  263. const wxString& oldval, wxString *newval);
  264. virtual void ApplyEdit(int row, int col, wxGrid* grid);
  265. private:
  266. long m_index;
  267. wxDECLARE_NO_COPY_CLASS(wxGridCellEnumEditor);
  268. };
  269. #endif // wxUSE_COMBOBOX
  270. class WXDLLIMPEXP_ADV wxGridCellAutoWrapStringEditor : public wxGridCellTextEditor
  271. {
  272. public:
  273. wxGridCellAutoWrapStringEditor() : wxGridCellTextEditor() { }
  274. virtual void Create(wxWindow* parent,
  275. wxWindowID id,
  276. wxEvtHandler* evtHandler);
  277. virtual wxGridCellEditor *Clone() const
  278. { return new wxGridCellAutoWrapStringEditor; }
  279. wxDECLARE_NO_COPY_CLASS(wxGridCellAutoWrapStringEditor);
  280. };
  281. #endif // wxUSE_GRID
  282. #endif // _WX_GENERIC_GRID_EDITORS_H_