]> git.saurik.com Git - wxWidgets.git/blame - include/wx/textentry.h
Use wxWindowMSW instead of wxWindow to fix wxUniv/MSW compilation.
[wxWidgets.git] / include / wx / textentry.h
CommitLineData
0ec1179b
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: wx/textentry.h
3// Purpose: declares wxTextEntry interface defining a simple text entry
4// Author: Vadim Zeitlin
5// Created: 2007-09-24
6// RCS-ID: $Id$
7// Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11#ifndef _WX_TEXTENTRY_H_
12#define _WX_TEXTENTRY_H_
13
14// wxTextPos is the position in the text (currently it's hardly used anywhere
15// and should probably be replaced with int anyhow)
16typedef long wxTextPos;
17
b4782e57 18class WXDLLIMPEXP_FWD_BASE wxArrayString;
ea98f11c 19class WXDLLIMPEXP_FWD_CORE wxTextCompleter;
63f7d502 20class WXDLLIMPEXP_FWD_CORE wxTextEntryHintData;
a95f37bc 21class WXDLLIMPEXP_FWD_CORE wxWindow;
b4782e57 22
4516928a
VZ
23#include "wx/gdicmn.h" // for wxPoint
24
0ec1179b
VZ
25// ----------------------------------------------------------------------------
26// wxTextEntryBase
27// ----------------------------------------------------------------------------
28
29class WXDLLIMPEXP_CORE wxTextEntryBase
30{
31public:
63f7d502
VZ
32 wxTextEntryBase() { m_eventsBlock = 0; m_hintData = NULL; }
33 virtual ~wxTextEntryBase();
0ec1179b
VZ
34
35
36 // accessing the value
37 // -------------------
38
39 // SetValue() generates a text change event, ChangeValue() doesn't
40 virtual void SetValue(const wxString& value)
41 { DoSetValue(value, SetValue_SendEvent); }
7bfc104b 42 virtual void ChangeValue(const wxString& value);
0ec1179b
VZ
43
44 // writing text inserts it at the current position replacing any current
45 // selection, appending always inserts it at the end and doesn't remove any
46 // existing text (but it will reset the selection if there is any)
47 virtual void WriteText(const wxString& text) = 0;
48 virtual void AppendText(const wxString& text);
49
135b23b2 50 virtual wxString GetValue() const;
0ec1179b 51 virtual wxString GetRange(long from, long to) const;
a7507230 52 bool IsEmpty() const { return GetLastPosition() <= 0; }
0ec1179b
VZ
53
54
55 // editing operations
56 // ------------------
57
58 virtual void Replace(long from, long to, const wxString& value);
59 virtual void Remove(long from, long to) = 0;
60 virtual void Clear() { SetValue(wxString()); }
5a25f858 61 void RemoveSelection();
0ec1179b
VZ
62
63
64 // clipboard operations
65 // --------------------
66
67 virtual void Copy() = 0;
68 virtual void Cut() = 0;
69 virtual void Paste() = 0;
70
71 virtual bool CanCopy() const;
72 virtual bool CanCut() const;
73 virtual bool CanPaste() const;
74
75 // undo/redo
76 // ---------
77
78 virtual void Undo() = 0;
79 virtual void Redo() = 0;
80
81 virtual bool CanUndo() const = 0;
82 virtual bool CanRedo() const = 0;
83
84
85 // insertion point
86 // ---------------
87
88 // note that moving insertion point removes any current selection
89 virtual void SetInsertionPoint(long pos) = 0;
90 virtual void SetInsertionPointEnd() { SetInsertionPoint(-1); }
91 virtual long GetInsertionPoint() const = 0;
92 virtual long GetLastPosition() const = 0;
93
94
95 // selection
96 // ---------
97
98 virtual void SetSelection(long from, long to) = 0;
e0721133 99 virtual void SelectAll() { SetSelection(-1, -1); }
0ec1179b
VZ
100 virtual void GetSelection(long *from, long *to) const = 0;
101 bool HasSelection() const;
102 virtual wxString GetStringSelection() const;
103
978c6e41 104
ecaed0bc
VZ
105 // auto-completion
106 // ---------------
107
59396417
VZ
108 // these functions allow to auto-complete the text already entered into the
109 // control using either the given fixed list of strings, the paths from the
ea98f11c 110 // file system or an arbitrary user-defined completer
59396417
VZ
111 //
112 // they all return true if completion was enabled or false on error (most
113 // commonly meaning that this functionality is not available under the
114 // current platform)
115
574479e8
VZ
116 bool AutoComplete(const wxArrayString& choices)
117 { return DoAutoCompleteStrings(choices); }
59396417 118
574479e8
VZ
119 bool AutoCompleteFileNames()
120 { return DoAutoCompleteFileNames(); }
ecaed0bc 121
ea98f11c
VZ
122 // notice that we take ownership of the pointer and will delete it
123 //
124 // if the pointer is NULL auto-completion is disabled
125 bool AutoComplete(wxTextCompleter *completer)
126 { return DoAutoCompleteCustom(completer); }
127
0ec1179b
VZ
128
129 // status
130 // ------
ecaed0bc 131
0ec1179b
VZ
132 virtual bool IsEditable() const = 0;
133 virtual void SetEditable(bool editable) = 0;
134
135
136 // set the max number of characters which may be entered in a single line
137 // text control
138 virtual void SetMaxLength(unsigned long WXUNUSED(len)) { }
139
140
63f7d502
VZ
141 // hints
142 // -----
143
144 // hint is the (usually greyed out) text shown in the control as long as
145 // it's empty and doesn't have focus, it is typically used in controls used
146 // for searching to let the user know what is supposed to be entered there
147
148 virtual bool SetHint(const wxString& hint);
149 virtual wxString GetHint() const;
150
151
0847e36e
JS
152 // margins
153 // -------
154
155 // margins are the empty space between borders of control and the text
156 // itself. When setting margin, use value -1 to indicate that specific
157 // margin should not be changed.
158
159 bool SetMargins(const wxPoint& pt)
160 { return DoSetMargins(pt); }
161 bool SetMargins(wxCoord left, wxCoord top = -1)
162 { return DoSetMargins(wxPoint(left, top)); }
163 wxPoint GetMargins() const
164 { return DoGetMargins(); }
165
50135807 166
356fd0b5
VZ
167 // implementation only
168 // -------------------
50135807
VZ
169
170 // generate the wxEVT_COMMAND_TEXT_UPDATED event for GetEditableWindow(),
171 // like SetValue() does and return true if the event was processed
172 //
173 // NB: this is public for wxRichTextCtrl use only right now, do not call it
174 static bool SendTextUpdatedEvent(wxWindow *win);
175
e793ec14
VZ
176 // generate the wxEVT_COMMAND_TEXT_UPDATED event for this window
177 bool SendTextUpdatedEvent()
178 {
179 return SendTextUpdatedEvent(GetEditableWindow());
180 }
181
182
4275201b
SC
183 // generate the wxEVT_COMMAND_TEXT_UPDATED event for this window if the
184 // events are not currently disabled
185 void SendTextUpdatedEventIfAllowed()
186 {
187 if ( EventsAllowed() )
188 SendTextUpdatedEvent();
189 }
190
356fd0b5
VZ
191 // this function is provided solely for the purpose of forwarding text
192 // change notifications state from one control to another, e.g. it can be
193 // used by a wxComboBox which derives from wxTextEntry if it delegates all
194 // of its methods to another wxTextCtrl
195 void ForwardEnableTextChangedEvents(bool enable)
196 {
197 // it's important to call the functions which update m_eventsBlock here
198 // and not just our own EnableTextChangedEvents() because our state
199 // (i.e. the result of EventsAllowed()) must change as well
200 if ( enable )
201 ResumeTextChangedEvents();
202 else
203 SuppressTextChangedEvents();
204 }
205
0ec1179b
VZ
206protected:
207 // flags for DoSetValue(): common part of SetValue() and ChangeValue() and
208 // also used to implement WriteText() in wxMSW
209 enum
210 {
211 SetValue_NoEvent = 0,
212 SetValue_SendEvent = 1,
213 SetValue_SelectionOnly = 2
214 };
215
216 virtual void DoSetValue(const wxString& value, int flags);
135b23b2 217 virtual wxString DoGetValue() const = 0;
0ec1179b 218
4ba5f250
VZ
219 // override this to return the associated window, it will be used for event
220 // generation and also by generic hints implementation
221 virtual wxWindow *GetEditableWindow() = 0;
222
0847e36e
JS
223 // margins functions
224 virtual bool DoSetMargins(const wxPoint& pt);
225 virtual wxPoint DoGetMargins() const;
226
574479e8
VZ
227 // the derived classes should override these virtual methods to implement
228 // auto-completion, they do the same thing as their public counterparts but
229 // have different names to allow overriding just one of them without hiding
230 // the other one(s)
231 virtual bool DoAutoCompleteStrings(const wxArrayString& WXUNUSED(choices))
232 { return false; }
233 virtual bool DoAutoCompleteFileNames() { return false; }
ea98f11c 234 virtual bool DoAutoCompleteCustom(wxTextCompleter *completer);
574479e8 235
4ba5f250 236
0ec1179b
VZ
237 // class which should be used to temporarily disable text change events
238 //
239 // if suppress argument in ctor is false, nothing is done
240 class EventsSuppressor
241 {
242 public:
243 EventsSuppressor(wxTextEntryBase *text, bool suppress = true)
3b49331b
VZ
244 : m_text(text),
245 m_suppress(suppress)
0ec1179b 246 {
0ec1179b 247 if ( m_suppress )
0ec1179b 248 m_text->SuppressTextChangedEvents();
0ec1179b
VZ
249 }
250
251 ~EventsSuppressor()
252 {
253 if ( m_suppress )
254 m_text->ResumeTextChangedEvents();
255 }
256
257 private:
258 wxTextEntryBase *m_text;
259 bool m_suppress;
260 };
3b49331b 261
558820fd 262 friend class EventsSuppressor;
0ec1179b 263
0ec1179b
VZ
264private:
265 // suppress or resume the text changed events generation: don't use these
266 // functions directly, use EventsSuppressor class above instead
267 void SuppressTextChangedEvents()
268 {
269 if ( !m_eventsBlock++ )
270 EnableTextChangedEvents(false);
271 }
272
273 void ResumeTextChangedEvents()
274 {
275 if ( !--m_eventsBlock )
276 EnableTextChangedEvents(true);
277 }
278
279
280 // this must be overridden in the derived classes if our implementation of
281 // SetValue() or Replace() is used to disable (and enable back) generation
282 // of the text changed events
283 //
284 // initially the generation of the events is enabled
285 virtual void EnableTextChangedEvents(bool WXUNUSED(enable)) { }
286
50135807
VZ
287 // return true if the events are currently not suppressed
288 bool EventsAllowed() const { return m_eventsBlock == 0; }
289
290
0ec1179b
VZ
291 // if this counter is non-null, events are blocked
292 unsigned m_eventsBlock;
63f7d502
VZ
293
294 // hint-related stuff, only allocated if/when SetHint() is used
295 wxTextEntryHintData *m_hintData;
a7aeddac
VZ
296
297 // It needs to call our Do{Get,Set}Value() to work with the real control
298 // contents.
299 friend class wxTextEntryHintData;
0ec1179b
VZ
300};
301
2978a784
VZ
302#ifdef __WXUNIVERSAL__
303 // TODO: we need to use wxTextEntryDelegate here, but for now just prevent
304 // the GTK/MSW classes from being used in wxUniv build
305 class WXDLLIMPEXP_CORE wxTextEntry : public wxTextEntryBase
306 {
307 };
308#elif defined(__WXGTK20__)
0ec1179b 309 #include "wx/gtk/textentry.h"
c84030e0
KO
310#elif defined(__WXMAC__)
311 #include "wx/osx/textentry.h"
fa2f57be
VZ
312#elif defined(__WXMSW__)
313 #include "wx/msw/textentry.h"
978c6e41
VZ
314#elif defined(__WXMOTIF__)
315 #include "wx/motif/textentry.h"
72cb72bf
SN
316#elif defined(__WXPM__)
317 #include "wx/os2/textentry.h"
0ec1179b
VZ
318#else
319 // no platform-specific implementation of wxTextEntry yet
320 class WXDLLIMPEXP_CORE wxTextEntry : public wxTextEntryBase
321 {
322 };
323#endif
324
325#endif // _WX_TEXTENTRY_H_
326