]> git.saurik.com Git - wxWidgets.git/blob - src/common/textentrycmn.cpp
a0e31da051ed15c4719ce20d9dd5eb228d5a503d
[wxWidgets.git] / src / common / textentrycmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/textentrycmn.cpp
3 // Purpose: wxTextEntryBase implementation
4 // Author: Vadim Zeitlin
5 // Created: 2007-09-26
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_TEXTCTRL || wxUSE_COMBOBOX
27
28 #ifndef WX_PRECOMP
29 #include "wx/window.h"
30 #include "wx/dataobj.h"
31 #endif //WX_PRECOMP
32
33 #include "wx/textentry.h"
34 #include "wx/clipbrd.h"
35
36 // ----------------------------------------------------------------------------
37 // wxTextEntryHintData
38 // ----------------------------------------------------------------------------
39
40 class WXDLLIMPEXP_CORE wxTextEntryHintData wxBIND_OR_CONNECT_HACK_ONLY_BASE_CLASS
41 {
42 public:
43 wxTextEntryHintData(wxTextEntryBase *entry, wxWindow *win)
44 : m_entry(entry),
45 m_win(win)
46 {
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);
51 }
52
53 // default dtor is ok
54
55 void SetHintString(const wxString& hint)
56 {
57 m_hint = hint;
58
59 if ( ShowsHint() )
60 {
61 // update it immediately
62 m_entry->ChangeValue(hint);
63 }
64 //else: the new hint will be shown later
65 }
66
67 const wxString& GetHintString() const { return m_hint; }
68
69 private:
70 // are we showing the hint right now?
71 bool ShowsHint() const
72 {
73 return m_entry->GetValue() == m_hint;
74 }
75
76 void OnSetFocus(wxFocusEvent& event)
77 {
78 // hide the hint if we were showing it
79 if ( ShowsHint() )
80 {
81 // Clear() would send an event which we don't want, so do it like
82 // this
83 m_entry->ChangeValue(wxString());
84 m_win->SetForegroundColour(m_colFg);
85 }
86
87 event.Skip();
88 }
89
90 void OnKillFocus(wxFocusEvent& event)
91 {
92 // restore the hint if the user didn't do anything in the control
93 if ( m_entry->IsEmpty() )
94 {
95 m_entry->ChangeValue(m_hint);
96
97 m_colFg = m_win->GetForegroundColour();
98 m_win->SetForegroundColour(*wxLIGHT_GREY);
99 }
100
101 event.Skip();
102 }
103
104
105 wxTextEntryBase * const m_entry;
106 wxWindow * const m_win;
107
108 wxColour m_colFg;
109
110 wxString m_hint;
111
112 wxDECLARE_NO_COPY_CLASS(wxTextEntryHintData);
113 };
114
115 // ============================================================================
116 // wxTextEntryBase implementation
117 // ============================================================================
118
119 wxTextEntryBase::~wxTextEntryBase()
120 {
121 delete m_hintData;
122 }
123
124 // ----------------------------------------------------------------------------
125 // text operations
126 // ----------------------------------------------------------------------------
127
128 wxString wxTextEntryBase::GetRange(long from, long to) const
129 {
130 wxString sel;
131 wxString value = GetValue();
132
133 if ( from < to && (long)value.length() >= to )
134 {
135 sel = value.substr(from, to - from);
136 }
137
138 return sel;
139 }
140
141 void wxTextEntryBase::AppendText(const wxString& text)
142 {
143 SetInsertionPointEnd();
144 WriteText(text);
145 }
146
147 void wxTextEntryBase::DoSetValue(const wxString& value, int flags)
148 {
149 EventsSuppressor noeventsIf(this, !(flags & SetValue_SendEvent));
150
151 SelectAll();
152 WriteText(value);
153
154 SetInsertionPoint(0);
155 }
156
157 void wxTextEntryBase::Replace(long from, long to, const wxString& value)
158 {
159 {
160 EventsSuppressor noevents(this);
161 Remove(from, to);
162 }
163
164 SetInsertionPoint(from);
165 WriteText(value);
166 }
167
168 // ----------------------------------------------------------------------------
169 // selection
170 // ----------------------------------------------------------------------------
171
172 bool wxTextEntryBase::HasSelection() const
173 {
174 long from, to;
175 GetSelection(&from, &to);
176
177 return from < to;
178 }
179
180 void wxTextEntryBase::RemoveSelection()
181 {
182 long from, to;
183 GetSelection(& from, & to);
184 if (from != -1 && to != -1)
185 Remove(from, to);
186 }
187
188 wxString wxTextEntryBase::GetStringSelection() const
189 {
190 long from, to;
191 GetSelection(&from, &to);
192
193 return GetRange(from, to);
194 }
195
196 // ----------------------------------------------------------------------------
197 // clipboard
198 // ----------------------------------------------------------------------------
199
200 bool wxTextEntryBase::CanCopy() const
201 {
202 return HasSelection();
203 }
204
205 bool wxTextEntryBase::CanCut() const
206 {
207 return CanCopy() && IsEditable();
208 }
209
210 bool wxTextEntryBase::CanPaste() const
211 {
212 if ( IsEditable() )
213 {
214 #if wxUSE_CLIPBOARD
215 // check if there is any text on the clipboard
216 if ( wxTheClipboard->IsSupported(wxDF_TEXT)
217 #if wxUSE_UNICODE
218 || wxTheClipboard->IsSupported(wxDF_UNICODETEXT)
219 #endif // wxUSE_UNICODE
220 )
221 {
222 return true;
223 }
224 #endif // wxUSE_CLIPBOARD
225 }
226
227 return false;
228 }
229
230 // ----------------------------------------------------------------------------
231 // hints support
232 // ----------------------------------------------------------------------------
233
234 bool wxTextEntryBase::SetHint(const wxString& hint)
235 {
236 if ( !m_hintData )
237 m_hintData = new wxTextEntryHintData(this, GetEditableWindow());
238
239 m_hintData->SetHintString(hint);
240
241 return true;
242 }
243
244 wxString wxTextEntryBase::GetHint() const
245 {
246 return m_hintData ? m_hintData->GetHintString() : wxString();
247 }
248
249 #endif // wxUSE_TEXTCTRL || wxUSE_COMBOBOX