From 135b23b266fffceb26b29d832c7d25db99038d0d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 2 Mar 2009 13:31:29 +0000 Subject: [PATCH] added wxTextEntry::DoGetValue() to allow returning empty string if the control currently contains hint text git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59265 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/generic/srchctlg.h | 6 +++--- include/wx/gtk/textentry.h | 4 +++- include/wx/motif/textentry.h | 3 ++- include/wx/msw/textentry.h | 2 ++ include/wx/os2/textentry.h | 3 ++- include/wx/textentry.h | 3 ++- src/common/textentrycmn.cpp | 39 ++++++++++++++++++++++++++--------- src/generic/srchctlg.cpp | 7 +------ src/gtk/textentry.cpp | 2 +- src/motif/textentry.cpp | 2 +- src/msw/textctrl.cpp | 7 ++++--- src/msw/textentry.cpp | 2 +- src/os2/textentry.cpp | 2 +- 13 files changed, 52 insertions(+), 30 deletions(-) diff --git a/include/wx/generic/srchctlg.h b/include/wx/generic/srchctlg.h index 407783f957..d71fdcc3ae 100644 --- a/include/wx/generic/srchctlg.h +++ b/include/wx/generic/srchctlg.h @@ -69,9 +69,6 @@ public: // accessors // --------- - virtual wxString GetValue() const; - virtual void SetValue(const wxString& value); - virtual wxString GetRange(long from, long to) const; virtual int GetLineLength(long lineNo) const; @@ -204,6 +201,9 @@ public: protected: virtual void DoSetValue(const wxString& value, int flags); + virtual wxString DoGetValue() const; + virtual void SetValue(const wxString& value); + virtual bool DoLoadFile(const wxString& file, int fileType); virtual bool DoSaveFile(const wxString& file, int fileType); diff --git a/include/wx/gtk/textentry.h b/include/wx/gtk/textentry.h index 91db71ae11..deab5219c4 100644 --- a/include/wx/gtk/textentry.h +++ b/include/wx/gtk/textentry.h @@ -24,7 +24,6 @@ public: // implement wxTextEntryBase pure virtual methods virtual void WriteText(const wxString& text); - virtual wxString GetValue() const; virtual void Remove(long from, long to); virtual void Copy(); @@ -53,6 +52,9 @@ public: // implementation only from now on void SendMaxLenEvent(); +protected: + virtual wxString DoGetValue() const; + private: // implement this to return the associated GtkEntry or another widget // implementing GtkEditable diff --git a/include/wx/motif/textentry.h b/include/wx/motif/textentry.h index 5761116e0a..5a3a9577cb 100644 --- a/include/wx/motif/textentry.h +++ b/include/wx/motif/textentry.h @@ -22,7 +22,6 @@ public: // implement wxTextEntryBase pure virtual methods virtual void WriteText(const wxString& text); - virtual wxString GetValue() const; virtual void Replace(long from, long to, const wxString& value); virtual void Remove(long from, long to); @@ -46,6 +45,8 @@ public: virtual void SetEditable(bool editable); protected: + virtual wxString DoGetValue() const; + // translate wx text position (which may be -1 meaning "last one") to a // valid Motif text position long GetMotifPos(long pos) const; diff --git a/include/wx/msw/textentry.h b/include/wx/msw/textentry.h index 8f86d191b9..11141000a9 100644 --- a/include/wx/msw/textentry.h +++ b/include/wx/msw/textentry.h @@ -59,6 +59,8 @@ public: #endif // wxUSE_UXTHEME protected: + virtual wxString DoGetValue() const; + // this is really a hook for multiline text controls as the single line // ones don't need to ever scroll to show the selection but having it here // allows us to put Remove() in the base class diff --git a/include/wx/os2/textentry.h b/include/wx/os2/textentry.h index 33bb2320ce..52d22bf463 100644 --- a/include/wx/os2/textentry.h +++ b/include/wx/os2/textentry.h @@ -22,7 +22,6 @@ public: // implement wxTextEntryBase pure virtual methods virtual void WriteText(const wxString& text); - virtual wxString GetValue() const; virtual void Remove(long from, long to); virtual void Copy(); @@ -48,6 +47,8 @@ public: virtual void SetMaxLength(unsigned long len); protected: + virtual wxString DoGetValue() const; + // this is really a hook for multiline text controls as the single line // ones don't need to ever scroll to show the selection but having it here // allows us to put Remove() in the base class diff --git a/include/wx/textentry.h b/include/wx/textentry.h index 1194b61f90..c0c116a433 100644 --- a/include/wx/textentry.h +++ b/include/wx/textentry.h @@ -44,7 +44,7 @@ public: virtual void WriteText(const wxString& text) = 0; virtual void AppendText(const wxString& text); - virtual wxString GetValue() const = 0; + virtual wxString GetValue() const; virtual wxString GetRange(long from, long to) const; bool IsEmpty() const { return GetLastPosition() <= 0; } @@ -152,6 +152,7 @@ protected: }; virtual void DoSetValue(const wxString& value, int flags); + virtual wxString DoGetValue() const = 0; // class which should be used to temporarily disable text change events // diff --git a/src/common/textentrycmn.cpp b/src/common/textentrycmn.cpp index a0e31da051..cb81ea0779 100644 --- a/src/common/textentrycmn.cpp +++ b/src/common/textentrycmn.cpp @@ -48,15 +48,21 @@ public: wxTextEntryHintData::OnSetFocus, this); wxBIND_OR_CONNECT_HACK(win, wxEVT_KILL_FOCUS, wxFocusEventHandler, wxTextEntryHintData::OnKillFocus, this); + + // we don't have any hint yet + m_showsHint = false; } // default dtor is ok + // are we showing the hint right now? + bool ShowsHint() const { return m_showsHint; } + void SetHintString(const wxString& hint) { m_hint = hint; - if ( ShowsHint() ) + if ( m_showsHint ) { // update it immediately m_entry->ChangeValue(hint); @@ -67,21 +73,17 @@ public: const wxString& GetHintString() const { return m_hint; } private: - // are we showing the hint right now? - bool ShowsHint() const - { - return m_entry->GetValue() == m_hint; - } - void OnSetFocus(wxFocusEvent& event) { // hide the hint if we were showing it - if ( ShowsHint() ) + if ( m_showsHint ) { // Clear() would send an event which we don't want, so do it like // this m_entry->ChangeValue(wxString()); m_win->SetForegroundColour(m_colFg); + + m_showsHint = false; } event.Skip(); @@ -96,19 +98,27 @@ private: m_colFg = m_win->GetForegroundColour(); m_win->SetForegroundColour(*wxLIGHT_GREY); + + m_showsHint = true; } 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() wxString m_hint; + // true if we're currently showing it, for this we must be empty and not + // have focus + bool m_showsHint; + wxDECLARE_NO_COPY_CLASS(wxTextEntryHintData); }; @@ -122,9 +132,14 @@ wxTextEntryBase::~wxTextEntryBase() } // ---------------------------------------------------------------------------- -// text operations +// text accessors // ---------------------------------------------------------------------------- +wxString wxTextEntryBase::GetValue() const +{ + return m_hintData && m_hintData->ShowsHint() ? wxString() : DoGetValue(); +} + wxString wxTextEntryBase::GetRange(long from, long to) const { wxString sel; @@ -138,6 +153,10 @@ wxString wxTextEntryBase::GetRange(long from, long to) const return sel; } +// ---------------------------------------------------------------------------- +// text operations +// ---------------------------------------------------------------------------- + void wxTextEntryBase::AppendText(const wxString& text) { SetInsertionPointEnd(); diff --git a/src/generic/srchctlg.cpp b/src/generic/srchctlg.cpp index e520c7dd24..494ae1537f 100644 --- a/src/generic/srchctlg.cpp +++ b/src/generic/srchctlg.cpp @@ -577,7 +577,7 @@ void wxSearchCtrl::LayoutControls(int x, int y, int width, int height) // accessors // --------- -wxString wxSearchCtrl::GetValue() const +wxString wxSearchCtrl::DoGetValue() const { wxString value = m_text->GetValue(); if (value == m_text->GetDescriptiveText()) @@ -585,11 +585,6 @@ wxString wxSearchCtrl::GetValue() const else return value; } -void wxSearchCtrl::SetValue(const wxString& value) -{ - m_text->SetValue(value); -} - wxString wxSearchCtrl::GetRange(long from, long to) const { return m_text->GetRange(from, to); diff --git a/src/gtk/textentry.cpp b/src/gtk/textentry.cpp index 99d0f12943..7c9b9e8dee 100644 --- a/src/gtk/textentry.cpp +++ b/src/gtk/textentry.cpp @@ -103,7 +103,7 @@ void wxTextEntry::WriteText(const wxString& value) gtk_editable_set_position(edit, len); } -wxString wxTextEntry::GetValue() const +wxString wxTextEntry::DoGetValue() const { const wxGtkString value(gtk_editable_get_chars(GetEditable(), 0, -1)); diff --git a/src/motif/textentry.cpp b/src/motif/textentry.cpp index 9ba70b8df3..d2b4cb27f6 100644 --- a/src/motif/textentry.cpp +++ b/src/motif/textentry.cpp @@ -59,7 +59,7 @@ long wxTextEntry::GetMotifPos(long pos) const // operations on control text // ---------------------------------------------------------------------------- -wxString wxTextEntry::GetValue() const +wxString wxTextEntry::DoGetValue() const { wxString str; diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index 3b9129f0a4..f394ce44e2 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -858,8 +858,9 @@ wxString wxTextCtrl::GetRange(long from, long to) const else #endif // wxUSE_RICHEDIT { - // retrieve all text - str = wxGetWindowText(GetHWND()); + // retrieve all text: wxTextEntry method works even for multiline + // controls and must be used for single line ones to account for hints + str = wxTextEntry::GetValue(); // need only a range? if ( from < to ) @@ -882,7 +883,7 @@ void wxTextCtrl::DoSetValue(const wxString& value, int flags) // comparing it with the old one (chances are that it will be different // anyhow, this comparison is there to avoid flicker for small single-line // edit controls mostly) - if ( (value.length() > 0x400) || (value != GetValue()) ) + if ( (value.length() > 0x400) || (value != DoGetValue()) ) { DoWriteText(value, flags /* doesn't include SelectionOnly here */); diff --git a/src/msw/textentry.cpp b/src/msw/textentry.cpp index 968f913fd1..b39db7f12c 100644 --- a/src/msw/textentry.cpp +++ b/src/msw/textentry.cpp @@ -184,7 +184,7 @@ void wxTextEntry::WriteText(const wxString& text) ::SendMessage(GetEditHwnd(), EM_REPLACESEL, 0, (LPARAM)text.wx_str()); } -wxString wxTextEntry::GetValue() const +wxString wxTextEntry::DoGetValue() const { return wxGetWindowText(GetEditHWND()); } diff --git a/src/os2/textentry.cpp b/src/os2/textentry.cpp index 817390980d..9190bc8ee5 100644 --- a/src/os2/textentry.cpp +++ b/src/os2/textentry.cpp @@ -61,7 +61,7 @@ void wxTextEntry::WriteText(const wxString& text) ::WinSetWindowText(GetEditHwnd(), text.c_str()); } -wxString wxTextEntry::GetValue() const +wxString wxTextEntry::DoGetValue() const { return wxGetWindowText(GetEditHWND()); } -- 2.45.2