]>
Commit | Line | Data |
---|---|---|
c84030e0 KO |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/textentry_osx.cpp | |
3 | // Purpose: wxTextEntry | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: Kevin Ollivier | |
6 | // Created: 1998-01-01 | |
b5b208a1 | 7 | // RCS-ID: $Id$ |
c84030e0 KO |
8 | // Copyright: (c) Stefan Csomor |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #if wxUSE_TEXTCTRL | |
15 | ||
16 | #include "wx/textctrl.h" | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/intl.h" | |
20 | #include "wx/app.h" | |
21 | #include "wx/utils.h" | |
22 | #include "wx/dc.h" | |
23 | #include "wx/button.h" | |
24 | #include "wx/menu.h" | |
25 | #include "wx/settings.h" | |
26 | #include "wx/msgdlg.h" | |
27 | #include "wx/toplevel.h" | |
28 | #endif | |
29 | ||
30 | #ifdef __DARWIN__ | |
31 | #include <sys/types.h> | |
32 | #include <sys/stat.h> | |
33 | #else | |
34 | #include <stat.h> | |
35 | #endif | |
36 | ||
37 | #if wxUSE_STD_IOSTREAM | |
38 | #if wxUSE_IOSTREAMH | |
39 | #include <fstream.h> | |
40 | #else | |
41 | #include <fstream> | |
42 | #endif | |
43 | #endif | |
44 | ||
45 | #include "wx/filefn.h" | |
46 | #include "wx/sysopt.h" | |
47 | #include "wx/thread.h" | |
c729f16f | 48 | #include "wx/textcompleter.h" |
c84030e0 KO |
49 | |
50 | #include "wx/osx/private.h" | |
51 | ||
c729f16f VZ |
52 | wxTextEntry::wxTextEntry() |
53 | { | |
54 | m_completer = NULL; | |
55 | m_editable = true; | |
56 | m_maxLength = 0; | |
57 | } | |
58 | ||
59 | wxTextEntry::~wxTextEntry() | |
60 | { | |
61 | delete m_completer; | |
62 | } | |
63 | ||
c84030e0 KO |
64 | wxString wxTextEntry::DoGetValue() const |
65 | { | |
da8eb5f5 VZ |
66 | wxCHECK_MSG( GetTextPeer(), wxString(), "Must create the control first" ); |
67 | ||
c84030e0 KO |
68 | return GetTextPeer()->GetStringValue() ; |
69 | } | |
70 | ||
71 | void wxTextEntry::GetSelection(long* from, long* to) const | |
72 | { | |
da8eb5f5 VZ |
73 | wxCHECK_RET( GetTextPeer(), "Must create the control first" ); |
74 | ||
c84030e0 KO |
75 | GetTextPeer()->GetSelection( from , to ) ; |
76 | } | |
77 | ||
78 | void wxTextEntry::SetMaxLength(unsigned long len) | |
79 | { | |
a39815bd SC |
80 | if ( GetTextPeer()->CanClipMaxLength() ) |
81 | GetTextPeer()->SetMaxLength(len); | |
c84030e0 KO |
82 | m_maxLength = len ; |
83 | } | |
84 | ||
85 | // Clipboard operations | |
86 | ||
87 | void wxTextEntry::Copy() | |
88 | { | |
da8eb5f5 VZ |
89 | wxCHECK_RET( GetTextPeer(), "Must create the control first" ); |
90 | ||
c84030e0 KO |
91 | if (CanCopy()) |
92 | GetTextPeer()->Copy() ; | |
93 | } | |
94 | ||
95 | void wxTextEntry::Cut() | |
96 | { | |
da8eb5f5 VZ |
97 | wxCHECK_RET( GetTextPeer(), "Must create the control first" ); |
98 | ||
c84030e0 KO |
99 | if (CanCut()) |
100 | GetTextPeer()->Cut() ; | |
101 | } | |
102 | ||
103 | void wxTextEntry::Paste() | |
104 | { | |
da8eb5f5 VZ |
105 | wxCHECK_RET( GetTextPeer(), "Must create the control first" ); |
106 | ||
c84030e0 KO |
107 | if (CanPaste()) |
108 | GetTextPeer()->Paste() ; | |
109 | } | |
110 | ||
111 | bool wxTextEntry::CanCopy() const | |
112 | { | |
113 | // Can copy if there's a selection | |
114 | long from, to; | |
115 | GetSelection( &from, &to ); | |
116 | ||
117 | return (from != to); | |
118 | } | |
119 | ||
120 | bool wxTextEntry::CanCut() const | |
121 | { | |
122 | if ( !IsEditable() ) | |
123 | return false; | |
124 | ||
125 | // Can cut if there's a selection | |
126 | long from, to; | |
127 | GetSelection( &from, &to ); | |
128 | ||
129 | return (from != to); | |
130 | } | |
131 | ||
132 | bool wxTextEntry::CanPaste() const | |
133 | { | |
134 | if (!IsEditable()) | |
135 | return false; | |
136 | ||
da8eb5f5 VZ |
137 | wxCHECK_MSG( GetTextPeer(), false, "Must create the control first" ); |
138 | ||
c84030e0 KO |
139 | return GetTextPeer()->CanPaste() ; |
140 | } | |
141 | ||
142 | void wxTextEntry::SetEditable(bool editable) | |
143 | { | |
da8eb5f5 VZ |
144 | if ( editable == m_editable ) |
145 | return; | |
146 | ||
147 | m_editable = editable ; | |
148 | ||
149 | wxCHECK_RET( GetTextPeer(), "Must create the control first" ); | |
150 | GetTextPeer()->SetEditable( editable ) ; | |
c84030e0 KO |
151 | } |
152 | ||
153 | void wxTextEntry::SetInsertionPoint(long pos) | |
154 | { | |
155 | SetSelection( pos , pos ) ; | |
156 | } | |
157 | ||
158 | void wxTextEntry::SetInsertionPointEnd() | |
159 | { | |
160 | long pos = GetLastPosition(); | |
161 | SetInsertionPoint( pos ); | |
162 | } | |
163 | ||
164 | long wxTextEntry::GetInsertionPoint() const | |
165 | { | |
166 | long begin, end ; | |
167 | GetSelection( &begin , &end ) ; | |
168 | ||
169 | return begin ; | |
170 | } | |
171 | ||
172 | wxTextPos wxTextEntry::GetLastPosition() const | |
173 | { | |
da8eb5f5 VZ |
174 | wxCHECK_MSG( GetTextPeer(), -1, "Must create the control first" ); |
175 | ||
c84030e0 KO |
176 | return GetTextPeer()->GetLastPosition() ; |
177 | } | |
178 | ||
179 | void wxTextEntry::Remove(long from, long to) | |
180 | { | |
da8eb5f5 VZ |
181 | wxCHECK_RET( GetTextPeer(), "Must create the control first" ); |
182 | ||
4275201b SC |
183 | { |
184 | EventsSuppressor noevents(this); | |
185 | GetTextPeer()->Remove( from , to ); | |
186 | } | |
ce00f59b | 187 | |
e567904a | 188 | SendTextUpdatedEventIfAllowed(); |
c84030e0 KO |
189 | } |
190 | ||
191 | void wxTextEntry::SetSelection(long from, long to) | |
192 | { | |
da8eb5f5 VZ |
193 | wxCHECK_RET( GetTextPeer(), "Must create the control first" ); |
194 | ||
c84030e0 KO |
195 | GetTextPeer()->SetSelection( from , to ) ; |
196 | } | |
197 | ||
198 | void wxTextEntry::WriteText(const wxString& str) | |
199 | { | |
da8eb5f5 VZ |
200 | wxCHECK_RET( GetTextPeer(), "Must create the control first" ); |
201 | ||
4275201b SC |
202 | { |
203 | EventsSuppressor noevents(this); | |
204 | GetTextPeer()->WriteText( str ); | |
205 | } | |
e567904a VZ |
206 | |
207 | SendTextUpdatedEventIfAllowed(); | |
c84030e0 KO |
208 | } |
209 | ||
210 | void wxTextEntry::Clear() | |
211 | { | |
da8eb5f5 VZ |
212 | wxCHECK_RET( GetTextPeer(), "Must create the control first" ); |
213 | ||
4275201b SC |
214 | { |
215 | EventsSuppressor noevents(this); | |
216 | GetTextPeer()->Clear(); | |
217 | } | |
e567904a VZ |
218 | |
219 | SendTextUpdatedEventIfAllowed(); | |
c84030e0 KO |
220 | } |
221 | ||
222 | bool wxTextEntry::IsEditable() const | |
223 | { | |
224 | return m_editable ; | |
225 | } | |
226 | ||
053f5a55 SC |
227 | bool wxTextEntry::SendMaxLenEvent() |
228 | { | |
229 | wxWindow *win = GetEditableWindow(); | |
230 | wxCHECK_MSG( win, false, "can't send an event without a window" ); | |
231 | ||
232 | wxCommandEvent event(wxEVT_TEXT_MAXLEN, win->GetId()); | |
233 | ||
234 | // do not do this as it could be very inefficient if the text control | |
235 | // contains a lot of text and we're not using ref-counted wxString | |
236 | // implementation -- instead, event.GetString() will query the control for | |
237 | // its current text if needed | |
238 | //event.SetString(win->GetValue()); | |
239 | ||
240 | event.SetEventObject(win); | |
241 | return win->HandleWindowEvent(event); | |
242 | } | |
243 | ||
c84030e0 KO |
244 | // ---------------------------------------------------------------------------- |
245 | // Undo/redo | |
246 | // ---------------------------------------------------------------------------- | |
247 | ||
248 | void wxTextEntry::Undo() | |
249 | { | |
da8eb5f5 VZ |
250 | wxCHECK_RET( GetTextPeer(), "Must create the control first" ); |
251 | ||
c84030e0 KO |
252 | if (CanUndo()) |
253 | GetTextPeer()->Undo() ; | |
254 | } | |
255 | ||
256 | void wxTextEntry::Redo() | |
257 | { | |
da8eb5f5 VZ |
258 | wxCHECK_RET( GetTextPeer(), "Must create the control first" ); |
259 | ||
c84030e0 KO |
260 | if (CanRedo()) |
261 | GetTextPeer()->Redo() ; | |
262 | } | |
263 | ||
264 | bool wxTextEntry::CanUndo() const | |
265 | { | |
266 | if ( !IsEditable() ) | |
267 | return false ; | |
268 | ||
da8eb5f5 VZ |
269 | wxCHECK_MSG( GetTextPeer(), false, "Must create the control first" ); |
270 | ||
c84030e0 KO |
271 | return GetTextPeer()->CanUndo() ; |
272 | } | |
273 | ||
274 | bool wxTextEntry::CanRedo() const | |
275 | { | |
276 | if ( !IsEditable() ) | |
277 | return false ; | |
278 | ||
da8eb5f5 VZ |
279 | wxCHECK_MSG( GetTextPeer(), false, "Must create the control first" ); |
280 | ||
c84030e0 KO |
281 | return GetTextPeer()->CanRedo() ; |
282 | } | |
283 | ||
284 | wxTextWidgetImpl * wxTextEntry::GetTextPeer() const | |
285 | { | |
005ac806 VZ |
286 | wxWindow * const win = const_cast<wxTextEntry *>(this)->GetEditableWindow(); |
287 | ||
288 | return win ? dynamic_cast<wxTextWidgetImpl *>(win->GetPeer()) : NULL; | |
c84030e0 | 289 | } |
005ac806 | 290 | |
c729f16f VZ |
291 | // ---------------------------------------------------------------------------- |
292 | // Auto-completion | |
293 | // ---------------------------------------------------------------------------- | |
294 | ||
295 | bool wxTextEntry::DoAutoCompleteStrings(const wxArrayString& choices) | |
296 | { | |
297 | wxTextCompleterFixed * const completer = new wxTextCompleterFixed; | |
298 | completer->SetCompletions(choices); | |
299 | ||
300 | return DoAutoCompleteCustom(completer); | |
301 | } | |
302 | ||
303 | bool wxTextEntry::DoAutoCompleteCustom(wxTextCompleter *completer) | |
304 | { | |
305 | m_completer = completer; | |
306 | ||
307 | return true; | |
308 | } | |
309 | ||
c84030e0 | 310 | #endif // wxUSE_TEXTCTRL |