spinctlg.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/generic/spinctlg.h
  3. // Purpose: generic wxSpinCtrl class
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 28.10.99
  7. // Copyright: (c) Vadim Zeitlin
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_GENERIC_SPINCTRL_H_
  11. #define _WX_GENERIC_SPINCTRL_H_
  12. // ----------------------------------------------------------------------------
  13. // wxSpinCtrl is a combination of wxSpinButton and wxTextCtrl, so if
  14. // wxSpinButton is available, this is what we do - but if it isn't, we still
  15. // define wxSpinCtrl class which then has the same appearance as wxTextCtrl but
  16. // the different interface. This allows to write programs using wxSpinCtrl
  17. // without tons of #ifdefs.
  18. // ----------------------------------------------------------------------------
  19. #if wxUSE_SPINBTN
  20. #include "wx/compositewin.h"
  21. class WXDLLIMPEXP_FWD_CORE wxSpinButton;
  22. class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
  23. class wxSpinCtrlTextGeneric; // wxTextCtrl used for the wxSpinCtrlGenericBase
  24. // The !wxUSE_SPINBTN version's GetValue() function conflicts with the
  25. // wxTextCtrl's GetValue() and so you have to input a dummy int value.
  26. #define wxSPINCTRL_GETVALUE_FIX
  27. // ----------------------------------------------------------------------------
  28. // wxSpinCtrlGeneric is a combination of wxTextCtrl and wxSpinButton
  29. //
  30. // This class manages a double valued generic spinctrl through the DoGet/SetXXX
  31. // functions that are made public as Get/SetXXX functions for int or double
  32. // for the wxSpinCtrl and wxSpinCtrlDouble classes respectively to avoid
  33. // function ambiguity.
  34. // ----------------------------------------------------------------------------
  35. class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase
  36. : public wxNavigationEnabled<wxCompositeWindow<wxSpinCtrlBase> >
  37. {
  38. public:
  39. wxSpinCtrlGenericBase() { Init(); }
  40. bool Create(wxWindow *parent,
  41. wxWindowID id = wxID_ANY,
  42. const wxString& value = wxEmptyString,
  43. const wxPoint& pos = wxDefaultPosition,
  44. const wxSize& size = wxDefaultSize,
  45. long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
  46. double min = 0, double max = 100, double initial = 0,
  47. double inc = 1,
  48. const wxString& name = wxT("wxSpinCtrl"));
  49. virtual ~wxSpinCtrlGenericBase();
  50. // accessors
  51. // T GetValue() const
  52. // T GetMin() const
  53. // T GetMax() const
  54. // T GetIncrement() const
  55. virtual bool GetSnapToTicks() const { return m_snap_to_ticks; }
  56. // unsigned GetDigits() const - wxSpinCtrlDouble only
  57. // operations
  58. virtual void SetValue(const wxString& text);
  59. // void SetValue(T val)
  60. // void SetRange(T minVal, T maxVal)
  61. // void SetIncrement(T inc)
  62. virtual void SetSnapToTicks(bool snap_to_ticks);
  63. // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
  64. // Select text in the textctrl
  65. void SetSelection(long from, long to);
  66. // implementation from now on
  67. // forward these functions to all subcontrols
  68. virtual bool Enable(bool enable = true);
  69. virtual bool Show(bool show = true);
  70. #if wxUSE_TOOLTIPS
  71. virtual void DoSetToolTip(wxToolTip *tip);
  72. #endif // wxUSE_TOOLTIPS
  73. virtual bool SetBackgroundColour(const wxColour& colour);
  74. // get the subcontrols
  75. wxTextCtrl *GetText() const { return m_textCtrl; }
  76. wxSpinButton *GetSpinButton() const { return m_spinButton; }
  77. // forwarded events from children windows
  78. void OnSpinButton(wxSpinEvent& event);
  79. void OnTextLostFocus(wxFocusEvent& event);
  80. void OnTextChar(wxKeyEvent& event);
  81. // this window itself is used only as a container for its sub windows so it
  82. // shouldn't accept the focus at all and any attempts to explicitly set
  83. // focus to it should give focus to its text constol part
  84. virtual bool AcceptsFocus() const { return false; }
  85. virtual void SetFocus();
  86. friend class wxSpinCtrlTextGeneric;
  87. protected:
  88. // override the base class virtuals involved into geometry calculations
  89. virtual wxSize DoGetBestSize() const;
  90. virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen = -1) const;
  91. virtual void DoMoveWindow(int x, int y, int width, int height);
  92. #ifdef __WXMSW__
  93. // and, for MSW, enabling this window itself
  94. virtual void DoEnable(bool enable);
  95. #endif // __WXMSW__
  96. enum SendEvent
  97. {
  98. SendEvent_None,
  99. SendEvent_Text
  100. };
  101. // generic double valued functions
  102. double DoGetValue() const { return m_value; }
  103. bool DoSetValue(double val, SendEvent sendEvent);
  104. void DoSetRange(double min_val, double max_val);
  105. void DoSetIncrement(double inc);
  106. // update our value to reflect the text control contents (if it has been
  107. // modified by user, do nothing otherwise)
  108. //
  109. // can also change the text control if its value is invalid
  110. //
  111. // return true if our value has changed
  112. bool SyncSpinToText(SendEvent sendEvent);
  113. // Send the correct event type
  114. virtual void DoSendEvent() = 0;
  115. // Convert the text to/from the corresponding value.
  116. virtual bool DoTextToValue(const wxString& text, double *val) = 0;
  117. virtual wxString DoValueToText(double val) = 0;
  118. // check if the value is in range
  119. bool InRange(double n) const { return (n >= m_min) && (n <= m_max); }
  120. // ensure that the value is in range wrapping it round if necessary
  121. double AdjustToFitInRange(double value) const;
  122. double m_value;
  123. double m_min;
  124. double m_max;
  125. double m_increment;
  126. bool m_snap_to_ticks;
  127. int m_spin_value;
  128. // the subcontrols
  129. wxTextCtrl *m_textCtrl;
  130. wxSpinButton *m_spinButton;
  131. private:
  132. // common part of all ctors
  133. void Init();
  134. // Implement pure virtual function inherited from wxCompositeWindow.
  135. virtual wxWindowList GetCompositeWindowParts() const;
  136. DECLARE_EVENT_TABLE()
  137. };
  138. #else // !wxUSE_SPINBTN
  139. #define wxSPINCTRL_GETVALUE_FIX int = 1
  140. // ----------------------------------------------------------------------------
  141. // wxSpinCtrl is just a text control
  142. // ----------------------------------------------------------------------------
  143. #include "wx/textctrl.h"
  144. class WXDLLIMPEXP_CORE wxSpinCtrlGenericBase : public wxTextCtrl
  145. {
  146. public:
  147. wxSpinCtrlGenericBase() : m_value(0), m_min(0), m_max(100),
  148. m_increment(1), m_snap_to_ticks(false),
  149. m_format(wxT("%g")) { }
  150. bool Create(wxWindow *parent,
  151. wxWindowID id = wxID_ANY,
  152. const wxString& value = wxEmptyString,
  153. const wxPoint& pos = wxDefaultPosition,
  154. const wxSize& size = wxDefaultSize,
  155. long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
  156. double min = 0, double max = 100, double initial = 0,
  157. double inc = 1,
  158. const wxString& name = wxT("wxSpinCtrl"))
  159. {
  160. m_min = min;
  161. m_max = max;
  162. m_value = initial;
  163. m_increment = inc;
  164. bool ok = wxTextCtrl::Create(parent, id, value, pos, size, style,
  165. wxDefaultValidator, name);
  166. DoSetValue(initial, SendEvent_None);
  167. return ok;
  168. }
  169. // accessors
  170. // T GetValue() const
  171. // T GetMin() const
  172. // T GetMax() const
  173. // T GetIncrement() const
  174. virtual bool GetSnapToTicks() const { return m_snap_to_ticks; }
  175. // unsigned GetDigits() const - wxSpinCtrlDouble only
  176. // operations
  177. virtual void SetValue(const wxString& text) { wxTextCtrl::SetValue(text); }
  178. // void SetValue(T val)
  179. // void SetRange(T minVal, T maxVal)
  180. // void SetIncrement(T inc)
  181. virtual void SetSnapToTicks(bool snap_to_ticks)
  182. { m_snap_to_ticks = snap_to_ticks; }
  183. // void SetDigits(unsigned digits) - wxSpinCtrlDouble only
  184. // Select text in the textctrl
  185. //void SetSelection(long from, long to);
  186. protected:
  187. // generic double valued
  188. double DoGetValue() const
  189. {
  190. double n;
  191. if ( (wxSscanf(wxTextCtrl::GetValue(), wxT("%lf"), &n) != 1) )
  192. n = INT_MIN;
  193. return n;
  194. }
  195. bool DoSetValue(double val, SendEvent sendEvent)
  196. {
  197. wxString str(wxString::Format(m_format, val));
  198. switch ( sendEvent )
  199. {
  200. case SendEvent_None:
  201. wxTextCtrl::ChangeValue(str);
  202. break;
  203. case SendEvent_Text:
  204. wxTextCtrl::SetValue(str);
  205. break;
  206. }
  207. return true;
  208. }
  209. void DoSetRange(double min_val, double max_val)
  210. {
  211. m_min = min_val;
  212. m_max = max_val;
  213. }
  214. void DoSetIncrement(double inc) { m_increment = inc; } // Note: unused
  215. double m_value;
  216. double m_min;
  217. double m_max;
  218. double m_increment;
  219. bool m_snap_to_ticks;
  220. wxString m_format;
  221. };
  222. #endif // wxUSE_SPINBTN/!wxUSE_SPINBTN
  223. #if !defined(wxHAS_NATIVE_SPINCTRL)
  224. //-----------------------------------------------------------------------------
  225. // wxSpinCtrl
  226. //-----------------------------------------------------------------------------
  227. class WXDLLIMPEXP_CORE wxSpinCtrl : public wxSpinCtrlGenericBase
  228. {
  229. public:
  230. wxSpinCtrl() { Init(); }
  231. wxSpinCtrl(wxWindow *parent,
  232. wxWindowID id = wxID_ANY,
  233. const wxString& value = wxEmptyString,
  234. const wxPoint& pos = wxDefaultPosition,
  235. const wxSize& size = wxDefaultSize,
  236. long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
  237. int min = 0, int max = 100, int initial = 0,
  238. const wxString& name = wxT("wxSpinCtrl"))
  239. {
  240. Init();
  241. Create(parent, id, value, pos, size, style, min, max, initial, name);
  242. }
  243. bool Create(wxWindow *parent,
  244. wxWindowID id = wxID_ANY,
  245. const wxString& value = wxEmptyString,
  246. const wxPoint& pos = wxDefaultPosition,
  247. const wxSize& size = wxDefaultSize,
  248. long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
  249. int min = 0, int max = 100, int initial = 0,
  250. const wxString& name = wxT("wxSpinCtrl"))
  251. {
  252. return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
  253. style, min, max, initial, 1, name);
  254. }
  255. // accessors
  256. int GetValue(wxSPINCTRL_GETVALUE_FIX) const { return int(DoGetValue()); }
  257. int GetMin() const { return int(m_min); }
  258. int GetMax() const { return int(m_max); }
  259. int GetIncrement() const { return int(m_increment); }
  260. // operations
  261. void SetValue(const wxString& value)
  262. { wxSpinCtrlGenericBase::SetValue(value); }
  263. void SetValue( int value ) { DoSetValue(value, SendEvent_None); }
  264. void SetRange( int minVal, int maxVal ) { DoSetRange(minVal, maxVal); }
  265. void SetIncrement(int inc) { DoSetIncrement(inc); }
  266. virtual int GetBase() const { return m_base; }
  267. virtual bool SetBase(int base);
  268. protected:
  269. virtual void DoSendEvent();
  270. virtual bool DoTextToValue(const wxString& text, double *val);
  271. virtual wxString DoValueToText(double val);
  272. private:
  273. // Common part of all ctors.
  274. void Init()
  275. {
  276. m_base = 10;
  277. }
  278. int m_base;
  279. DECLARE_DYNAMIC_CLASS(wxSpinCtrl)
  280. };
  281. #endif // wxHAS_NATIVE_SPINCTRL
  282. //-----------------------------------------------------------------------------
  283. // wxSpinCtrlDouble
  284. //-----------------------------------------------------------------------------
  285. class WXDLLIMPEXP_CORE wxSpinCtrlDouble : public wxSpinCtrlGenericBase
  286. {
  287. public:
  288. wxSpinCtrlDouble() { Init(); }
  289. wxSpinCtrlDouble(wxWindow *parent,
  290. wxWindowID id = wxID_ANY,
  291. const wxString& value = wxEmptyString,
  292. const wxPoint& pos = wxDefaultPosition,
  293. const wxSize& size = wxDefaultSize,
  294. long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
  295. double min = 0, double max = 100, double initial = 0,
  296. double inc = 1,
  297. const wxString& name = wxT("wxSpinCtrlDouble"))
  298. {
  299. Init();
  300. Create(parent, id, value, pos, size, style,
  301. min, max, initial, inc, name);
  302. }
  303. bool Create(wxWindow *parent,
  304. wxWindowID id = wxID_ANY,
  305. const wxString& value = wxEmptyString,
  306. const wxPoint& pos = wxDefaultPosition,
  307. const wxSize& size = wxDefaultSize,
  308. long style = wxSP_ARROW_KEYS | wxALIGN_RIGHT,
  309. double min = 0, double max = 100, double initial = 0,
  310. double inc = 1,
  311. const wxString& name = wxT("wxSpinCtrlDouble"))
  312. {
  313. return wxSpinCtrlGenericBase::Create(parent, id, value, pos, size,
  314. style, min, max, initial,
  315. inc, name);
  316. }
  317. // accessors
  318. double GetValue(wxSPINCTRL_GETVALUE_FIX) const { return DoGetValue(); }
  319. double GetMin() const { return m_min; }
  320. double GetMax() const { return m_max; }
  321. double GetIncrement() const { return m_increment; }
  322. unsigned GetDigits() const { return m_digits; }
  323. // operations
  324. void SetValue(const wxString& value)
  325. { wxSpinCtrlGenericBase::SetValue(value); }
  326. void SetValue(double value) { DoSetValue(value, SendEvent_None); }
  327. void SetRange(double minVal, double maxVal) { DoSetRange(minVal, maxVal); }
  328. void SetIncrement(double inc) { DoSetIncrement(inc); }
  329. void SetDigits(unsigned digits);
  330. // We don't implement bases support for floating point numbers, this is not
  331. // very useful in practice.
  332. virtual int GetBase() const { return 10; }
  333. virtual bool SetBase(int WXUNUSED(base)) { return 0; }
  334. protected:
  335. virtual void DoSendEvent();
  336. virtual bool DoTextToValue(const wxString& text, double *val);
  337. virtual wxString DoValueToText(double val);
  338. unsigned m_digits;
  339. private:
  340. // Common part of all ctors.
  341. void Init()
  342. {
  343. m_digits = 0;
  344. m_format = wxS("%g");
  345. }
  346. wxString m_format;
  347. DECLARE_DYNAMIC_CLASS(wxSpinCtrlDouble)
  348. };
  349. #endif // _WX_GENERIC_SPINCTRL_H_