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);
52 // we don't have any hint yet
58 // are we showing the hint right now?
59 bool ShowsHint() const { return m_showsHint
; }
61 void SetHintString(const wxString
& hint
)
67 // update it immediately
68 m_entry
->ChangeValue(hint
);
70 //else: the new hint will be shown later
73 const wxString
& GetHintString() const { return 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
);
92 void OnKillFocus(wxFocusEvent
& event
)
94 // restore the hint if the user didn't do anything in the control
95 if ( m_entry
->IsEmpty() )
97 m_entry
->ChangeValue(m_hint
);
99 m_colFg
= m_win
->GetForegroundColour();
100 m_win
->SetForegroundColour(*wxLIGHT_GREY
);
108 // the text control we're associated with (as its interface and its window)
109 wxTextEntryBase
* const m_entry
;
110 wxWindow
* const m_win
;
112 // the original foreground colour of m_win before we changed it
115 // the hint passed to wxTextEntry::SetHint()
118 // true if we're currently showing it, for this we must be empty and not
122 wxDECLARE_NO_COPY_CLASS(wxTextEntryHintData
);
125 // ============================================================================
126 // wxTextEntryBase implementation
127 // ============================================================================
129 wxTextEntryBase::~wxTextEntryBase()
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 wxString
wxTextEntryBase::GetValue() const
140 return m_hintData
&& m_hintData
->ShowsHint() ? wxString() : DoGetValue();
143 wxString
wxTextEntryBase::GetRange(long from
, long to
) const
146 wxString value
= GetValue();
148 if ( from
< to
&& (long)value
.length() >= to
)
150 sel
= value
.substr(from
, to
- from
);
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 void wxTextEntryBase::AppendText(const wxString
& text
)
162 SetInsertionPointEnd();
166 void wxTextEntryBase::DoSetValue(const wxString
& value
, int flags
)
168 EventsSuppressor
noeventsIf(this, !(flags
& SetValue_SendEvent
));
173 SetInsertionPoint(0);
176 void wxTextEntryBase::Replace(long from
, long to
, const wxString
& value
)
179 EventsSuppressor
noevents(this);
183 SetInsertionPoint(from
);
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 bool wxTextEntryBase::HasSelection() const
194 GetSelection(&from
, &to
);
199 void wxTextEntryBase::RemoveSelection()
202 GetSelection(& from
, & to
);
203 if (from
!= -1 && to
!= -1)
207 wxString
wxTextEntryBase::GetStringSelection() const
210 GetSelection(&from
, &to
);
212 return GetRange(from
, to
);
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
219 bool wxTextEntryBase::CanCopy() const
221 return HasSelection();
224 bool wxTextEntryBase::CanCut() const
226 return CanCopy() && IsEditable();
229 bool wxTextEntryBase::CanPaste() const
234 // check if there is any text on the clipboard
235 if ( wxTheClipboard
->IsSupported(wxDF_TEXT
)
237 || wxTheClipboard
->IsSupported(wxDF_UNICODETEXT
)
238 #endif // wxUSE_UNICODE
243 #endif // wxUSE_CLIPBOARD
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 bool wxTextEntryBase::SetHint(const wxString
& hint
)
256 m_hintData
= new wxTextEntryHintData(this, GetEditableWindow());
258 m_hintData
->SetHintString(hint
);
263 wxString
wxTextEntryBase::GetHint() const
265 return m_hintData
? m_hintData
->GetHintString() : wxString();
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 bool wxTextEntryBase::DoSetMargins(const wxPoint
& WXUNUSED(pt
))
277 wxPoint
wxTextEntryBase::DoGetMargins() const
279 return wxPoint(-1, -1);
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
287 bool wxTextEntryBase::SendTextUpdatedEvent(wxWindow
*win
)
289 wxCHECK_MSG( win
, false, "can't send an event without a window" );
291 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, win
->GetId());
293 // do not do this as it could be very inefficient if the text control
294 // contains a lot of text and we're not using ref-counted wxString
295 // implementation -- instead, event.GetString() will query the control for
296 // its current text if needed
297 //event.SetString(win->GetValue());
299 event
.SetEventObject(win
);
300 return win
->HandleWindowEvent(event
);
303 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX