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