control.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/control.h
  3. // Purpose: wxControl common interface
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 26.07.99
  7. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_CONTROL_H_BASE_
  11. #define _WX_CONTROL_H_BASE_
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #include "wx/defs.h"
  16. #if wxUSE_CONTROLS
  17. #include "wx/window.h" // base class
  18. extern WXDLLIMPEXP_DATA_CORE(const char) wxControlNameStr[];
  19. // ----------------------------------------------------------------------------
  20. // Ellipsize() constants
  21. // ----------------------------------------------------------------------------
  22. enum wxEllipsizeFlags
  23. {
  24. wxELLIPSIZE_FLAGS_NONE = 0,
  25. wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS = 1,
  26. wxELLIPSIZE_FLAGS_EXPAND_TABS = 2,
  27. wxELLIPSIZE_FLAGS_DEFAULT = wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS |
  28. wxELLIPSIZE_FLAGS_EXPAND_TABS
  29. };
  30. // NB: Don't change the order of these values, they're the same as in
  31. // PangoEllipsizeMode enum.
  32. enum wxEllipsizeMode
  33. {
  34. wxELLIPSIZE_NONE,
  35. wxELLIPSIZE_START,
  36. wxELLIPSIZE_MIDDLE,
  37. wxELLIPSIZE_END
  38. };
  39. // ----------------------------------------------------------------------------
  40. // wxControl is the base class for all controls
  41. // ----------------------------------------------------------------------------
  42. class WXDLLIMPEXP_CORE wxControlBase : public wxWindow
  43. {
  44. public:
  45. wxControlBase() { }
  46. virtual ~wxControlBase();
  47. // Create() function adds the validator parameter
  48. bool Create(wxWindow *parent, wxWindowID id,
  49. const wxPoint& pos = wxDefaultPosition,
  50. const wxSize& size = wxDefaultSize,
  51. long style = 0,
  52. const wxValidator& validator = wxDefaultValidator,
  53. const wxString& name = wxControlNameStr);
  54. // get the control alignment (left/right/centre, top/bottom/centre)
  55. int GetAlignment() const { return m_windowStyle & wxALIGN_MASK; }
  56. // set label with mnemonics
  57. virtual void SetLabel(const wxString& label)
  58. {
  59. m_labelOrig = label;
  60. InvalidateBestSize();
  61. wxWindow::SetLabel(label);
  62. }
  63. // return the original string, as it was passed to SetLabel()
  64. // (i.e. with wx-style mnemonics)
  65. virtual wxString GetLabel() const { return m_labelOrig; }
  66. // set label text (mnemonics will be escaped)
  67. virtual void SetLabelText(const wxString& text)
  68. {
  69. SetLabel(EscapeMnemonics(text));
  70. }
  71. // get just the text of the label, without mnemonic characters ('&')
  72. virtual wxString GetLabelText() const { return GetLabelText(GetLabel()); }
  73. #if wxUSE_MARKUP
  74. // Set the label with markup (and mnemonics). Markup is a simple subset of
  75. // HTML with tags such as <b>, <i> and <span>. By default it is not
  76. // supported i.e. all the markup is simply stripped and SetLabel() is
  77. // called but some controls in some ports do support this already and in
  78. // the future most of them should.
  79. //
  80. // Notice that, being HTML-like, markup also supports XML entities so '<'
  81. // should be encoded as "&lt;" and so on, a bare '<' in the input will
  82. // likely result in an error. As an exception, a bare '&' is allowed and
  83. // indicates that the next character is a mnemonic. To insert a literal '&'
  84. // in the control you need to use "&amp;" in the input string.
  85. //
  86. // Returns true if the label was set, even if the markup in it was ignored.
  87. // False is only returned if we failed to parse the label.
  88. bool SetLabelMarkup(const wxString& markup)
  89. {
  90. return DoSetLabelMarkup(markup);
  91. }
  92. #endif // wxUSE_MARKUP
  93. // controls by default inherit the colours of their parents, if a
  94. // particular control class doesn't want to do it, it can override
  95. // ShouldInheritColours() to return false
  96. virtual bool ShouldInheritColours() const { return true; }
  97. // WARNING: this doesn't work for all controls nor all platforms!
  98. //
  99. // simulates the event of given type (i.e. wxButton::Command() is just as
  100. // if the button was clicked)
  101. virtual void Command(wxCommandEvent &event);
  102. virtual bool SetFont(const wxFont& font);
  103. // wxControl-specific processing after processing the update event
  104. virtual void DoUpdateWindowUI(wxUpdateUIEvent& event);
  105. wxSize GetSizeFromTextSize(int xlen, int ylen = -1) const
  106. { return DoGetSizeFromTextSize(xlen, ylen); }
  107. wxSize GetSizeFromTextSize(const wxSize& tsize) const
  108. { return DoGetSizeFromTextSize(tsize.x, tsize.y); }
  109. // static utilities for mnemonics char (&) handling
  110. // ------------------------------------------------
  111. // returns the given string without mnemonic characters ('&')
  112. static wxString GetLabelText(const wxString& label);
  113. // returns the given string without mnemonic characters ('&')
  114. // this function is identic to GetLabelText() and is provided for clarity
  115. // and for symmetry with the wxStaticText::RemoveMarkup() function.
  116. static wxString RemoveMnemonics(const wxString& str);
  117. // escapes (by doubling them) the mnemonics
  118. static wxString EscapeMnemonics(const wxString& str);
  119. // miscellaneous static utilities
  120. // ------------------------------
  121. // replaces parts of the given (multiline) string with an ellipsis if needed
  122. static wxString Ellipsize(const wxString& label, const wxDC& dc,
  123. wxEllipsizeMode mode, int maxWidth,
  124. int flags = wxELLIPSIZE_FLAGS_DEFAULT);
  125. // return the accel index in the string or -1 if none and puts the modified
  126. // string into second parameter if non NULL
  127. static int FindAccelIndex(const wxString& label,
  128. wxString *labelOnly = NULL);
  129. // this is a helper for the derived class GetClassDefaultAttributes()
  130. // implementation: it returns the right colours for the classes which
  131. // contain something else (e.g. wxListBox, wxTextCtrl, ...) instead of
  132. // being simple controls (such as wxButton, wxCheckBox, ...)
  133. static wxVisualAttributes
  134. GetCompositeControlsDefaultAttributes(wxWindowVariant variant);
  135. protected:
  136. // choose the default border for this window
  137. virtual wxBorder GetDefaultBorder() const;
  138. // creates the control (calls wxWindowBase::CreateBase inside) and adds it
  139. // to the list of parents children
  140. bool CreateControl(wxWindowBase *parent,
  141. wxWindowID id,
  142. const wxPoint& pos,
  143. const wxSize& size,
  144. long style,
  145. const wxValidator& validator,
  146. const wxString& name);
  147. #if wxUSE_MARKUP
  148. // This function may be overridden in the derived classes to implement
  149. // support for labels with markup. The base class version simply strips the
  150. // markup and calls SetLabel() with the remaining text.
  151. virtual bool DoSetLabelMarkup(const wxString& markup);
  152. #endif // wxUSE_MARKUP
  153. // override this to return the total control's size from a string size
  154. virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen = -1) const;
  155. // initialize the common fields of wxCommandEvent
  156. void InitCommandEvent(wxCommandEvent& event) const;
  157. // Ellipsize() helper:
  158. static wxString DoEllipsizeSingleLine(const wxString& label, const wxDC& dc,
  159. wxEllipsizeMode mode, int maxWidth,
  160. int replacementWidth);
  161. #if wxUSE_MARKUP
  162. // Remove markup from the given string, returns empty string on error i.e.
  163. // if markup was syntactically invalid.
  164. static wxString RemoveMarkup(const wxString& markup);
  165. #endif // wxUSE_MARKUP
  166. // this field contains the label in wx format, i.e. with '&' mnemonics,
  167. // as it was passed to the last SetLabel() call
  168. wxString m_labelOrig;
  169. wxDECLARE_NO_COPY_CLASS(wxControlBase);
  170. };
  171. // ----------------------------------------------------------------------------
  172. // include platform-dependent wxControl declarations
  173. // ----------------------------------------------------------------------------
  174. #if defined(__WXUNIVERSAL__)
  175. #include "wx/univ/control.h"
  176. #elif defined(__WXMSW__)
  177. #include "wx/msw/control.h"
  178. #elif defined(__WXMOTIF__)
  179. #include "wx/motif/control.h"
  180. #elif defined(__WXGTK20__)
  181. #include "wx/gtk/control.h"
  182. #elif defined(__WXGTK__)
  183. #include "wx/gtk1/control.h"
  184. #elif defined(__WXMAC__)
  185. #include "wx/osx/control.h"
  186. #elif defined(__WXCOCOA__)
  187. #include "wx/cocoa/control.h"
  188. #elif defined(__WXPM__)
  189. #include "wx/os2/control.h"
  190. #endif
  191. #endif // wxUSE_CONTROLS
  192. #endif
  193. // _WX_CONTROL_H_BASE_