+// ----------------------------------------------------------------------------
+// wxTextEntryHintData
+// ----------------------------------------------------------------------------
+
+class WXDLLIMPEXP_CORE wxTextEntryHintData wxBIND_OR_CONNECT_HACK_ONLY_BASE_CLASS
+{
+public:
+ wxTextEntryHintData(wxTextEntryBase *entry, wxWindow *win)
+ : m_entry(entry),
+ m_win(win),
+ m_text(m_entry->GetValue())
+ {
+ wxBIND_OR_CONNECT_HACK(win, wxEVT_SET_FOCUS, wxFocusEventHandler,
+ wxTextEntryHintData::OnSetFocus, this);
+ wxBIND_OR_CONNECT_HACK(win, wxEVT_KILL_FOCUS, wxFocusEventHandler,
+ wxTextEntryHintData::OnKillFocus, this);
+ wxBIND_OR_CONNECT_HACK(win, wxEVT_COMMAND_TEXT_UPDATED,
+ wxCommandEventHandler,
+ wxTextEntryHintData::OnTextChanged, this);
+ }
+
+ // default dtor is ok
+
+ // Get the real text of the control such as it was before we replaced it
+ // with the hint.
+ const wxString& GetText() const { return m_text; }
+
+ // Set the hint to show, shouldn't be empty normally.
+ //
+ // This should be called after creating a new wxTextEntryHintData object
+ // and may be called more times in the future.
+ void SetHintString(const wxString& hint)
+ {
+ m_hint = hint;
+
+ if ( !m_win->HasFocus() )
+ ShowHintIfAppropriate();
+ //else: The new hint will be shown later when we lose focus.
+ }
+
+ const wxString& GetHintString() const { return m_hint; }
+
+ // This is called whenever the text control contents changes.
+ //
+ // We call it ourselves when this change generates an event but it's also
+ // necessary to call it explicitly from wxTextEntry::ChangeValue() as it,
+ // by design, does not generate any events.
+ void HandleTextUpdate(const wxString& text)
+ {
+ m_text = text;
+
+ // If we're called because of a call to Set or ChangeValue(), the
+ // control may still have the hint text colour, reset it in this case.
+ RestoreTextColourIfNecessary();
+ }
+
+private:
+ // Show the hint in the window if we should do it, i.e. if the window
+ // doesn't have any text of its own.
+ void ShowHintIfAppropriate()
+ {
+ // Never overwrite existing window text.
+ if ( !m_text.empty() )
+ return;
+
+ // Save the old text colour and set a more inconspicuous one for the
+ // hint.
+ m_colFg = m_win->GetForegroundColour();
+ m_win->SetForegroundColour(*wxLIGHT_GREY);
+
+ m_entry->DoSetValue(m_hint, wxTextEntryBase::SetValue_NoEvent);
+ }
+
+ // Restore the original text colour if we had changed it to show the hint
+ // and not restored it yet.
+ void RestoreTextColourIfNecessary()
+ {
+ if ( m_colFg.IsOk() )
+ {
+ m_win->SetForegroundColour(m_colFg);
+ m_colFg = wxColour();
+ }
+ }
+
+ void OnSetFocus(wxFocusEvent& event)
+ {
+ // If we had been showing the hint before, remove it now and restore
+ // the normal colour.
+ if ( m_text.empty() )
+ {
+ RestoreTextColourIfNecessary();
+
+ m_entry->DoSetValue(wxString(), wxTextEntryBase::SetValue_NoEvent);
+ }
+
+ event.Skip();
+ }
+
+ void OnKillFocus(wxFocusEvent& event)
+ {
+ // Restore the hint if the user didn't enter anything.
+ ShowHintIfAppropriate();
+
+ event.Skip();
+ }
+
+ void OnTextChanged(wxCommandEvent& event)
+ {
+ // Update the stored window text.
+ //
+ // Notice that we can't use GetValue() nor wxCommandEvent::GetString()
+ // which uses it internally because this would just forward back to us
+ // so go directly to the private method which returns the real control
+ // contents.
+ HandleTextUpdate(m_entry->DoGetValue());
+
+ event.Skip();
+ }
+
+
+ // the text control we're associated with (as its interface and its window)
+ wxTextEntryBase * const m_entry;
+ wxWindow * const m_win;
+
+ // the original foreground colour of m_win before we changed it
+ wxColour m_colFg;
+
+ // The hint passed to wxTextEntry::SetHint(), never empty.
+ wxString m_hint;
+
+ // The real text of the window.
+ wxString m_text;
+
+
+ wxDECLARE_NO_COPY_CLASS(wxTextEntryHintData);
+};
+