checkbox.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/checkbox.h
  3. // Purpose: wxCheckBox class interface
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 07.09.00
  7. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_CHECKBOX_H_BASE_
  11. #define _WX_CHECKBOX_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_CHECKBOX
  14. #include "wx/control.h"
  15. /*
  16. * wxCheckBox style flags
  17. * (Using wxCHK_* because wxCB_* is used by wxComboBox).
  18. * Determine whether to use a 3-state or 2-state
  19. * checkbox. 3-state enables to differentiate
  20. * between 'unchecked', 'checked' and 'undetermined'.
  21. *
  22. * In addition to the styles here it is also possible to specify just 0 which
  23. * is treated the same as wxCHK_2STATE for compatibility (but using explicit
  24. * flag is preferred).
  25. */
  26. #define wxCHK_2STATE 0x4000
  27. #define wxCHK_3STATE 0x1000
  28. /*
  29. * If this style is set the user can set the checkbox to the
  30. * undetermined state. If not set the undetermined set can only
  31. * be set programmatically.
  32. * This style can only be used with 3 state checkboxes.
  33. */
  34. #define wxCHK_ALLOW_3RD_STATE_FOR_USER 0x2000
  35. extern WXDLLIMPEXP_DATA_CORE(const char) wxCheckBoxNameStr[];
  36. // ----------------------------------------------------------------------------
  37. // wxCheckBox: a control which shows a label and a box which may be checked
  38. // ----------------------------------------------------------------------------
  39. class WXDLLIMPEXP_CORE wxCheckBoxBase : public wxControl
  40. {
  41. public:
  42. wxCheckBoxBase() { }
  43. // set/get the checked status of the listbox
  44. virtual void SetValue(bool value) = 0;
  45. virtual bool GetValue() const = 0;
  46. bool IsChecked() const
  47. {
  48. wxASSERT_MSG( !Is3State(), wxT("Calling IsChecked() doesn't make sense for")
  49. wxT(" a three state checkbox, Use Get3StateValue() instead") );
  50. return GetValue();
  51. }
  52. wxCheckBoxState Get3StateValue() const
  53. {
  54. wxCheckBoxState state = DoGet3StateValue();
  55. if ( state == wxCHK_UNDETERMINED && !Is3State() )
  56. {
  57. // Undetermined state with a 2-state checkbox??
  58. wxFAIL_MSG( wxT("DoGet3StateValue() says the 2-state checkbox is ")
  59. wxT("in an undetermined/third state") );
  60. state = wxCHK_UNCHECKED;
  61. }
  62. return state;
  63. }
  64. void Set3StateValue(wxCheckBoxState state)
  65. {
  66. if ( state == wxCHK_UNDETERMINED && !Is3State() )
  67. {
  68. wxFAIL_MSG(wxT("Setting a 2-state checkbox to undetermined state"));
  69. state = wxCHK_UNCHECKED;
  70. }
  71. DoSet3StateValue(state);
  72. }
  73. bool Is3State() const { return HasFlag(wxCHK_3STATE); }
  74. bool Is3rdStateAllowedForUser() const
  75. {
  76. return HasFlag(wxCHK_ALLOW_3RD_STATE_FOR_USER);
  77. }
  78. virtual bool HasTransparentBackground() { return true; }
  79. // wxCheckBox-specific processing after processing the update event
  80. virtual void DoUpdateWindowUI(wxUpdateUIEvent& event)
  81. {
  82. wxControl::DoUpdateWindowUI(event);
  83. if ( event.GetSetChecked() )
  84. SetValue(event.GetChecked());
  85. }
  86. protected:
  87. // choose the default border for this window
  88. virtual wxBorder GetDefaultBorder() const { return wxBORDER_NONE; }
  89. virtual void DoSet3StateValue(wxCheckBoxState WXUNUSED(state)) { wxFAIL; }
  90. virtual wxCheckBoxState DoGet3StateValue() const
  91. {
  92. wxFAIL;
  93. return wxCHK_UNCHECKED;
  94. }
  95. // Helper function to be called from derived classes Create()
  96. // implementations: it checks that the style doesn't contain any
  97. // incompatible bits and modifies it to be sane if it does.
  98. static void WXValidateStyle(long *stylePtr)
  99. {
  100. long& style = *stylePtr;
  101. if ( !(style & (wxCHK_2STATE | wxCHK_3STATE)) )
  102. {
  103. // For compatibility we use absence of style flags as wxCHK_2STATE
  104. // because wxCHK_2STATE used to have the value of 0 and some
  105. // existing code uses 0 instead of it. Moreover, some code even
  106. // uses some non-0 style, e.g. wxBORDER_XXX, but doesn't specify
  107. // neither wxCHK_2STATE nor wxCHK_3STATE -- to avoid breaking it,
  108. // assume (much more common) 2 state checkbox by default.
  109. style |= wxCHK_2STATE;
  110. }
  111. if ( style & wxCHK_3STATE )
  112. {
  113. if ( style & wxCHK_2STATE )
  114. {
  115. wxFAIL_MSG( "wxCHK_2STATE and wxCHK_3STATE can't be used "
  116. "together" );
  117. style &= ~wxCHK_3STATE;
  118. }
  119. }
  120. else // No wxCHK_3STATE
  121. {
  122. if ( style & wxCHK_ALLOW_3RD_STATE_FOR_USER )
  123. {
  124. wxFAIL_MSG( "wxCHK_ALLOW_3RD_STATE_FOR_USER doesn't make sense "
  125. "without wxCHK_3STATE" );
  126. style &= ~wxCHK_ALLOW_3RD_STATE_FOR_USER;
  127. }
  128. }
  129. }
  130. private:
  131. wxDECLARE_NO_COPY_CLASS(wxCheckBoxBase);
  132. };
  133. // Most ports support 3 state checkboxes so define this by default.
  134. #define wxHAS_3STATE_CHECKBOX
  135. #if defined(__WXUNIVERSAL__)
  136. #include "wx/univ/checkbox.h"
  137. #elif defined(__WXMSW__)
  138. #include "wx/msw/checkbox.h"
  139. #elif defined(__WXMOTIF__)
  140. #include "wx/motif/checkbox.h"
  141. #elif defined(__WXGTK20__)
  142. #include "wx/gtk/checkbox.h"
  143. #elif defined(__WXGTK__)
  144. #undef wxHAS_3STATE_CHECKBOX
  145. #include "wx/gtk1/checkbox.h"
  146. #elif defined(__WXMAC__)
  147. #include "wx/osx/checkbox.h"
  148. #elif defined(__WXCOCOA__)
  149. #include "wx/cocoa/checkbox.h"
  150. #elif defined(__WXPM__)
  151. #undef wxHAS_3STATE_CHECKBOX
  152. #include "wx/os2/checkbox.h"
  153. #endif
  154. #endif // wxUSE_CHECKBOX
  155. #endif // _WX_CHECKBOX_H_BASE_