1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/textentry.h
3 // Purpose: declares wxTextEntry interface defining a simple text entry
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_TEXTENTRY_H_
12 #define _WX_TEXTENTRY_H_
14 // wxTextPos is the position in the text (currently it's hardly used anywhere
15 // and should probably be replaced with int anyhow)
16 typedef long wxTextPos
;
18 class WXDLLIMPEXP_FWD_BASE wxArrayString
;
19 class WXDLLIMPEXP_FWD_CORE wxTextEntryHintData
;
20 class WXDLLIMPEXP_FWD_CORE wxWindow
;
22 #include "wx/gdicmn.h" // for wxPoint
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
28 class WXDLLIMPEXP_CORE wxTextEntryBase
31 wxTextEntryBase() { m_eventsBlock
= 0; m_hintData
= NULL
; }
32 virtual ~wxTextEntryBase();
35 // accessing the value
36 // -------------------
38 // SetValue() generates a text change event, ChangeValue() doesn't
39 virtual void SetValue(const wxString
& value
)
40 { DoSetValue(value
, SetValue_SendEvent
); }
41 virtual void ChangeValue(const wxString
& value
);
43 // writing text inserts it at the current position replacing any current
44 // selection, appending always inserts it at the end and doesn't remove any
45 // existing text (but it will reset the selection if there is any)
46 virtual void WriteText(const wxString
& text
) = 0;
47 virtual void AppendText(const wxString
& text
);
49 virtual wxString
GetValue() const;
50 virtual wxString
GetRange(long from
, long to
) const;
51 bool IsEmpty() const { return GetLastPosition() <= 0; }
57 virtual void Replace(long from
, long to
, const wxString
& value
);
58 virtual void Remove(long from
, long to
) = 0;
59 virtual void Clear() { SetValue(wxString()); }
60 void RemoveSelection();
63 // clipboard operations
64 // --------------------
66 virtual void Copy() = 0;
67 virtual void Cut() = 0;
68 virtual void Paste() = 0;
70 virtual bool CanCopy() const;
71 virtual bool CanCut() const;
72 virtual bool CanPaste() const;
77 virtual void Undo() = 0;
78 virtual void Redo() = 0;
80 virtual bool CanUndo() const = 0;
81 virtual bool CanRedo() const = 0;
87 // note that moving insertion point removes any current selection
88 virtual void SetInsertionPoint(long pos
) = 0;
89 virtual void SetInsertionPointEnd() { SetInsertionPoint(-1); }
90 virtual long GetInsertionPoint() const = 0;
91 virtual long GetLastPosition() const = 0;
97 virtual void SetSelection(long from
, long to
) = 0;
98 virtual void SelectAll() { SetSelection(-1, -1); }
99 virtual void GetSelection(long *from
, long *to
) const = 0;
100 bool HasSelection() const;
101 virtual wxString
GetStringSelection() const;
107 // these functions allow to auto-complete the text already entered into the
108 // control using either the given fixed list of strings, the paths from the
109 // file system or, in the future, an arbitrary user-defined completer
111 // they all return true if completion was enabled or false on error (most
112 // commonly meaning that this functionality is not available under the
115 bool AutoComplete(const wxArrayString
& choices
)
116 { return DoAutoCompleteStrings(choices
); }
118 bool AutoCompleteFileNames()
119 { return DoAutoCompleteFileNames(); }
125 virtual bool IsEditable() const = 0;
126 virtual void SetEditable(bool editable
) = 0;
129 // set the max number of characters which may be entered in a single line
131 virtual void SetMaxLength(unsigned long WXUNUSED(len
)) { }
137 // hint is the (usually greyed out) text shown in the control as long as
138 // it's empty and doesn't have focus, it is typically used in controls used
139 // for searching to let the user know what is supposed to be entered there
141 virtual bool SetHint(const wxString
& hint
);
142 virtual wxString
GetHint() const;
148 // margins are the empty space between borders of control and the text
149 // itself. When setting margin, use value -1 to indicate that specific
150 // margin should not be changed.
152 bool SetMargins(const wxPoint
& pt
)
153 { return DoSetMargins(pt
); }
154 bool SetMargins(wxCoord left
, wxCoord top
= -1)
155 { return DoSetMargins(wxPoint(left
, top
)); }
156 wxPoint
GetMargins() const
157 { return DoGetMargins(); }
160 // implementation only
161 // -------------------
163 // generate the wxEVT_COMMAND_TEXT_UPDATED event for GetEditableWindow(),
164 // like SetValue() does and return true if the event was processed
166 // NB: this is public for wxRichTextCtrl use only right now, do not call it
167 static bool SendTextUpdatedEvent(wxWindow
*win
);
169 // generate the wxEVT_COMMAND_TEXT_UPDATED event for this window
170 bool SendTextUpdatedEvent()
172 return SendTextUpdatedEvent(GetEditableWindow());
176 // generate the wxEVT_COMMAND_TEXT_UPDATED event for this window if the
177 // events are not currently disabled
178 void SendTextUpdatedEventIfAllowed()
180 if ( EventsAllowed() )
181 SendTextUpdatedEvent();
184 // this function is provided solely for the purpose of forwarding text
185 // change notifications state from one control to another, e.g. it can be
186 // used by a wxComboBox which derives from wxTextEntry if it delegates all
187 // of its methods to another wxTextCtrl
188 void ForwardEnableTextChangedEvents(bool enable
)
190 // it's important to call the functions which update m_eventsBlock here
191 // and not just our own EnableTextChangedEvents() because our state
192 // (i.e. the result of EventsAllowed()) must change as well
194 ResumeTextChangedEvents();
196 SuppressTextChangedEvents();
200 // flags for DoSetValue(): common part of SetValue() and ChangeValue() and
201 // also used to implement WriteText() in wxMSW
204 SetValue_NoEvent
= 0,
205 SetValue_SendEvent
= 1,
206 SetValue_SelectionOnly
= 2
209 virtual void DoSetValue(const wxString
& value
, int flags
);
210 virtual wxString
DoGetValue() const = 0;
212 // override this to return the associated window, it will be used for event
213 // generation and also by generic hints implementation
214 virtual wxWindow
*GetEditableWindow() = 0;
217 virtual bool DoSetMargins(const wxPoint
& pt
);
218 virtual wxPoint
DoGetMargins() const;
220 // the derived classes should override these virtual methods to implement
221 // auto-completion, they do the same thing as their public counterparts but
222 // have different names to allow overriding just one of them without hiding
224 virtual bool DoAutoCompleteStrings(const wxArrayString
& WXUNUSED(choices
))
226 virtual bool DoAutoCompleteFileNames() { return false; }
229 // class which should be used to temporarily disable text change events
231 // if suppress argument in ctor is false, nothing is done
232 class EventsSuppressor
235 EventsSuppressor(wxTextEntryBase
*text
, bool suppress
= true)
240 m_text
->SuppressTextChangedEvents();
246 m_text
->ResumeTextChangedEvents();
250 wxTextEntryBase
*m_text
;
254 friend class EventsSuppressor
;
257 // suppress or resume the text changed events generation: don't use these
258 // functions directly, use EventsSuppressor class above instead
259 void SuppressTextChangedEvents()
261 if ( !m_eventsBlock
++ )
262 EnableTextChangedEvents(false);
265 void ResumeTextChangedEvents()
267 if ( !--m_eventsBlock
)
268 EnableTextChangedEvents(true);
272 // this must be overridden in the derived classes if our implementation of
273 // SetValue() or Replace() is used to disable (and enable back) generation
274 // of the text changed events
276 // initially the generation of the events is enabled
277 virtual void EnableTextChangedEvents(bool WXUNUSED(enable
)) { }
279 // return true if the events are currently not suppressed
280 bool EventsAllowed() const { return m_eventsBlock
== 0; }
283 // if this counter is non-null, events are blocked
284 unsigned m_eventsBlock
;
286 // hint-related stuff, only allocated if/when SetHint() is used
287 wxTextEntryHintData
*m_hintData
;
289 // It needs to call our Do{Get,Set}Value() to work with the real control
291 friend class wxTextEntryHintData
;
294 #ifdef __WXUNIVERSAL__
295 // TODO: we need to use wxTextEntryDelegate here, but for now just prevent
296 // the GTK/MSW classes from being used in wxUniv build
297 class WXDLLIMPEXP_CORE wxTextEntry
: public wxTextEntryBase
300 #elif defined(__WXGTK20__)
301 #include "wx/gtk/textentry.h"
302 #elif defined(__WXMAC__)
303 #include "wx/osx/textentry.h"
304 #elif defined(__WXMSW__)
305 #include "wx/msw/textentry.h"
306 #elif defined(__WXMOTIF__)
307 #include "wx/motif/textentry.h"
308 #elif defined(__WXPM__)
309 #include "wx/os2/textentry.h"
311 // no platform-specific implementation of wxTextEntry yet
312 class WXDLLIMPEXP_CORE wxTextEntry
: public wxTextEntryBase
317 #endif // _WX_TEXTENTRY_H_