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
)
47 wxBIND_OR_CONNECT_HACK(win
, wxEVT_SET_FOCUS
, wxFocusEventHandler
,
48 wxTextEntryHintData::OnSetFocus
, this);
49 wxBIND_OR_CONNECT_HACK(win
, wxEVT_KILL_FOCUS
, wxFocusEventHandler
,
50 wxTextEntryHintData::OnKillFocus
, this);
55 void SetHintString(const wxString
& hint
)
61 // update it immediately
62 m_entry
->ChangeValue(hint
);
64 //else: the new hint will be shown later
67 const wxString
& GetHintString() const { return m_hint
; }
70 // are we showing the hint right now?
71 bool ShowsHint() const
73 return m_entry
->GetValue() == m_hint
;
76 void OnSetFocus(wxFocusEvent
& event
)
78 // hide the hint if we were showing it
81 // Clear() would send an event which we don't want, so do it like
83 m_entry
->ChangeValue(wxString());
84 m_win
->SetForegroundColour(m_colFg
);
90 void OnKillFocus(wxFocusEvent
& event
)
92 // restore the hint if the user didn't do anything in the control
93 if ( m_entry
->IsEmpty() )
95 m_entry
->ChangeValue(m_hint
);
97 m_colFg
= m_win
->GetForegroundColour();
98 m_win
->SetForegroundColour(*wxLIGHT_GREY
);
105 wxTextEntryBase
* const m_entry
;
106 wxWindow
* const m_win
;
112 wxDECLARE_NO_COPY_CLASS(wxTextEntryHintData
);
115 // ============================================================================
116 // wxTextEntryBase implementation
117 // ============================================================================
119 wxTextEntryBase::~wxTextEntryBase()
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 wxString
wxTextEntryBase::GetRange(long from
, long to
) const
131 wxString value
= GetValue();
133 if ( from
< to
&& (long)value
.length() >= to
)
135 sel
= value
.substr(from
, to
- from
);
141 void wxTextEntryBase::AppendText(const wxString
& text
)
143 SetInsertionPointEnd();
147 void wxTextEntryBase::DoSetValue(const wxString
& value
, int flags
)
149 EventsSuppressor
noeventsIf(this, !(flags
& SetValue_SendEvent
));
154 SetInsertionPoint(0);
157 void wxTextEntryBase::Replace(long from
, long to
, const wxString
& value
)
160 EventsSuppressor
noevents(this);
164 SetInsertionPoint(from
);
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 bool wxTextEntryBase::HasSelection() const
175 GetSelection(&from
, &to
);
180 void wxTextEntryBase::RemoveSelection()
183 GetSelection(& from
, & to
);
184 if (from
!= -1 && to
!= -1)
188 wxString
wxTextEntryBase::GetStringSelection() const
191 GetSelection(&from
, &to
);
193 return GetRange(from
, to
);
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
200 bool wxTextEntryBase::CanCopy() const
202 return HasSelection();
205 bool wxTextEntryBase::CanCut() const
207 return CanCopy() && IsEditable();
210 bool wxTextEntryBase::CanPaste() const
215 // check if there is any text on the clipboard
216 if ( wxTheClipboard
->IsSupported(wxDF_TEXT
)
218 || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
)
219 #endif // wxUSE_UNICODE
224 #endif // wxUSE_CLIPBOARD
230 // ----------------------------------------------------------------------------
232 // ----------------------------------------------------------------------------
234 bool wxTextEntryBase::SetHint(const wxString
& hint
)
237 m_hintData
= new wxTextEntryHintData(this, GetEditableWindow());
239 m_hintData
->SetHintString(hint
);
244 wxString
wxTextEntryBase::GetHint() const
246 return m_hintData
? m_hintData
->GetHintString() : wxString();
249 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX