1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textentrycmn.cpp
3 // Purpose: wxTextEntryBase implementation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_TEXTCTRL || wxUSE_COMBOBOX
29 #include "wx/window.h"
30 #include "wx/dataobj.h"
33 #include "wx/textentry.h"
34 #include "wx/clipbrd.h"
36 // ----------------------------------------------------------------------------
37 // wxTextEntryHintData
38 // ----------------------------------------------------------------------------
40 class WXDLLIMPEXP_CORE wxTextEntryHintData wxBIND_OR_CONNECT_HACK_ONLY_BASE_CLASS
43 wxTextEntryHintData(wxTextEntryBase
*entry
, wxWindow
*win
)
46 m_text(m_entry
->GetValue())
48 wxBIND_OR_CONNECT_HACK(win
, wxEVT_SET_FOCUS
, wxFocusEventHandler
,
49 wxTextEntryHintData::OnSetFocus
, this);
50 wxBIND_OR_CONNECT_HACK(win
, wxEVT_KILL_FOCUS
, wxFocusEventHandler
,
51 wxTextEntryHintData::OnKillFocus
, this);
52 wxBIND_OR_CONNECT_HACK(win
, wxEVT_COMMAND_TEXT_UPDATED
,
53 wxCommandEventHandler
,
54 wxTextEntryHintData::OnTextChanged
, this);
59 // Get the real text of the control such as it was before we replaced it
61 const wxString
& GetText() const { return m_text
; }
63 // Set the hint to show, shouldn't be empty normally.
65 // This should be called after creating a new wxTextEntryHintData object
66 // and may be called more times in the future.
67 void SetHintString(const wxString
& hint
)
71 if ( !m_win
->HasFocus() )
72 ShowHintIfAppropriate();
73 //else: The new hint will be shown later when we lose focus.
76 const wxString
& GetHintString() const { return m_hint
; }
78 // This is called whenever the text control contents changes.
80 // We call it ourselves when this change generates an event but it's also
81 // necessary to call it explicitly from wxTextEntry::ChangeValue() as it,
82 // by design, does not generate any events.
83 void HandleTextUpdate(const wxString
& text
)
87 // If we're called because of a call to Set or ChangeValue(), the
88 // control may still have the hint text colour, reset it in this case.
89 RestoreTextColourIfNecessary();
93 // Show the hint in the window if we should do it, i.e. if the window
94 // doesn't have any text of its own.
95 void ShowHintIfAppropriate()
97 // Never overwrite existing window text.
98 if ( !m_text
.empty() )
101 // Save the old text colour and set a more inconspicuous one for the
103 m_colFg
= m_win
->GetForegroundColour();
104 m_win
->SetForegroundColour(*wxLIGHT_GREY
);
106 m_entry
->DoSetValue(m_hint
, wxTextEntryBase::SetValue_NoEvent
);
109 // Restore the original text colour if we had changed it to show the hint
110 // and not restored it yet.
111 void RestoreTextColourIfNecessary()
113 if ( m_colFg
.IsOk() )
115 m_win
->SetForegroundColour(m_colFg
);
116 m_colFg
= wxColour();
120 void OnSetFocus(wxFocusEvent
& event
)
122 // If we had been showing the hint before, remove it now and restore
123 // the normal colour.
124 if ( m_text
.empty() )
126 RestoreTextColourIfNecessary();
128 m_entry
->DoSetValue(wxString(), wxTextEntryBase::SetValue_NoEvent
);
134 void OnKillFocus(wxFocusEvent
& event
)
136 // Restore the hint if the user didn't enter anything.
137 ShowHintIfAppropriate();
142 void OnTextChanged(wxCommandEvent
& event
)
144 // Update the stored window text.
146 // Notice that we can't use GetValue() nor wxCommandEvent::GetString()
147 // which uses it internally because this would just forward back to us
148 // so go directly to the private method which returns the real control
150 HandleTextUpdate(m_entry
->DoGetValue());
156 // the text control we're associated with (as its interface and its window)
157 wxTextEntryBase
* const m_entry
;
158 wxWindow
* const m_win
;
160 // the original foreground colour of m_win before we changed it
163 // The hint passed to wxTextEntry::SetHint(), never empty.
166 // The real text of the window.
170 wxDECLARE_NO_COPY_CLASS(wxTextEntryHintData
);
173 // ============================================================================
174 // wxTextEntryBase implementation
175 // ============================================================================
177 wxTextEntryBase::~wxTextEntryBase()
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 wxString
wxTextEntryBase::GetValue() const
188 return m_hintData
? m_hintData
->GetText() : DoGetValue();
191 wxString
wxTextEntryBase::GetRange(long from
, long to
) const
194 wxString value
= GetValue();
196 if ( from
< to
&& (long)value
.length() >= to
)
198 sel
= value
.substr(from
, to
- from
);
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 void wxTextEntryBase::ChangeValue(const wxString
& value
)
210 DoSetValue(value
, SetValue_NoEvent
);
212 // As we didn't generate any events for wxTextEntryHintData to catch,
213 // notify it explicitly about our changed contents.
215 m_hintData
->HandleTextUpdate(value
);
218 void wxTextEntryBase::AppendText(const wxString
& text
)
220 SetInsertionPointEnd();
224 void wxTextEntryBase::DoSetValue(const wxString
& value
, int flags
)
226 EventsSuppressor
noeventsIf(this, !(flags
& SetValue_SendEvent
));
231 SetInsertionPoint(0);
234 void wxTextEntryBase::Replace(long from
, long to
, const wxString
& value
)
237 EventsSuppressor
noevents(this);
241 SetInsertionPoint(from
);
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 bool wxTextEntryBase::HasSelection() const
252 GetSelection(&from
, &to
);
257 void wxTextEntryBase::RemoveSelection()
260 GetSelection(& from
, & to
);
261 if (from
!= -1 && to
!= -1)
265 wxString
wxTextEntryBase::GetStringSelection() const
268 GetSelection(&from
, &to
);
270 return GetRange(from
, to
);
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 bool wxTextEntryBase::CanCopy() const
279 return HasSelection();
282 bool wxTextEntryBase::CanCut() const
284 return CanCopy() && IsEditable();
287 bool wxTextEntryBase::CanPaste() const
292 // check if there is any text on the clipboard
293 if ( wxTheClipboard
->IsSupported(wxDF_TEXT
)
295 || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
)
296 #endif // wxUSE_UNICODE
301 #endif // wxUSE_CLIPBOARD
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 bool wxTextEntryBase::SetHint(const wxString
& hint
)
316 m_hintData
= new wxTextEntryHintData(this, GetEditableWindow());
318 m_hintData
->SetHintString(hint
);
320 else if ( m_hintData
)
322 // Setting empty hint removes any currently set one.
326 //else: Setting empty hint when we don't have any doesn't do anything.
331 wxString
wxTextEntryBase::GetHint() const
333 return m_hintData
? m_hintData
->GetHintString() : wxString();
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 bool wxTextEntryBase::DoSetMargins(const wxPoint
& WXUNUSED(pt
))
345 wxPoint
wxTextEntryBase::DoGetMargins() const
347 return wxPoint(-1, -1);
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
355 bool wxTextEntryBase::SendTextUpdatedEvent(wxWindow
*win
)
357 wxCHECK_MSG( win
, false, "can't send an event without a window" );
359 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId());
361 // do not do this as it could be very inefficient if the text control
362 // contains a lot of text and we're not using ref-counted wxString
363 // implementation -- instead, event.GetString() will query the control for
364 // its current text if needed
365 //event.SetString(win->GetValue());
367 event
.SetEventObject(win
);
368 return win
->HandleWindowEvent(event
);
371 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX