]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/textctrl.mm
Remove no longer necessary re-entrancy check, fix function prototype definition for...
[wxWidgets.git] / src / osx / cocoa / textctrl.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/textctrl.mm
3 // Purpose: wxTextCtrl
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
6 // Created: 1998-01-01
7 // RCS-ID: $Id: textctrl.cpp 54820 2008-07-29 20:04:11Z SC $
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"
48
49 #include "wx/osx/private.h"
50 #include "wx/osx/cocoa/private/textimpl.h"
51
52 @interface wxNSSecureTextField : NSSecureTextField
53 {
54 wxWidgetCocoaImpl* impl;
55 }
56
57 - (void) setImplementation:(wxWidgetCocoaImpl*) item;
58 - (wxWidgetCocoaImpl*) implementation;
59 @end
60
61 @implementation wxNSSecureTextField
62
63 + (void)initialize
64 {
65 static BOOL initialized = NO;
66 if (!initialized)
67 {
68 initialized = YES;
69 wxOSXCocoaClassAddWXMethods( self );
70 }
71 }
72
73 - (wxWidgetCocoaImpl*) implementation
74 {
75 return impl;
76 }
77
78 - (void) setImplementation:(wxWidgetCocoaImpl*) item
79 {
80 impl = item;
81 }
82
83 - (void)controlTextDidChange:(NSNotification *)aNotification
84 {
85 if ( impl )
86 {
87 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
88 if ( wxpeer ) {
89 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
90 event.SetEventObject( wxpeer );
91 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
92 wxpeer->HandleWindowEvent( event );
93 }
94 }
95 }
96
97 @end
98
99 @interface wxNSTextView : NSScrollView
100 {
101 wxWidgetCocoaImpl* impl;
102 }
103
104 - (void) setImplementation:(wxWidgetCocoaImpl*) item;
105 - (wxWidgetCocoaImpl*) implementation;
106 @end
107
108 @implementation wxNSTextView
109
110 + (void)initialize
111 {
112 static BOOL initialized = NO;
113 if (!initialized)
114 {
115 initialized = YES;
116 wxOSXCocoaClassAddWXMethods( self );
117 }
118 }
119
120 - (wxWidgetCocoaImpl*) implementation
121 {
122 return impl;
123 }
124
125 - (void) setImplementation:(wxWidgetCocoaImpl*) item
126 {
127 impl = item;
128 }
129
130
131 - (void)controlTextDidChange:(NSNotification *)aNotification
132 {
133 if ( impl )
134 {
135 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
136 if ( wxpeer ) {
137 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
138 event.SetEventObject( wxpeer );
139 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
140 wxpeer->HandleWindowEvent( event );
141 }
142 }
143 }
144 @end
145
146 @implementation wxNSTextField
147
148 + (void)initialize
149 {
150 static BOOL initialized = NO;
151 if (!initialized)
152 {
153 initialized = YES;
154 wxOSXCocoaClassAddWXMethods( self );
155 }
156 }
157
158 - (wxWidgetCocoaImpl*) implementation
159 {
160 return impl;
161 }
162
163 - (void) setImplementation:(wxWidgetCocoaImpl*) item
164 {
165 impl = item;
166 }
167
168
169 - (void)controlTextDidChange:(NSNotification *)aNotification
170 {
171 if ( impl )
172 {
173 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
174 if ( wxpeer ) {
175 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
176 event.SetEventObject( wxpeer );
177 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
178 wxpeer->HandleWindowEvent( event );
179 }
180 }
181 }
182
183 /*
184 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
185 {
186 if ( impl )
187 {
188 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
189 if ( wxpeer ) {
190 wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId());
191 event.SetEventObject( wxpeer );
192 event.SetWindow( wxpeer );
193 wxpeer->HandleWindowEvent( event );
194 }
195 }
196 }
197 */
198 @end
199
200 // wxNSTextViewControl
201
202 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
203 {
204 m_scrollView = (NSScrollView*) w;
205 [w setImplementation: this];
206
207 [m_scrollView setHasVerticalScroller:YES];
208 [m_scrollView setHasHorizontalScroller:NO];
209 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
210 NSSize contentSize = [m_scrollView contentSize];
211
212 m_textView = [[NSTextView alloc] initWithFrame: NSMakeRect(0, 0,
213 contentSize.width, contentSize.height)];
214 [m_textView setVerticallyResizable:YES];
215 [m_textView setHorizontallyResizable:NO];
216 [m_textView setAutoresizingMask:NSViewWidthSizable];
217
218 [m_scrollView setDocumentView: m_textView];
219
220 [m_textView setDelegate: w];
221 }
222
223 wxNSTextViewControl::~wxNSTextViewControl()
224 {
225 if (m_textView)
226 [m_textView setDelegate: nil];
227 }
228
229 wxString wxNSTextViewControl::GetStringValue() const
230 {
231 if (m_textView)
232 {
233 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
234 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
235 }
236 return wxEmptyString;
237 }
238 void wxNSTextViewControl::SetStringValue( const wxString &str)
239 {
240 if (m_textView)
241 [m_textView setString: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
242 }
243 void wxNSTextViewControl::Copy()
244 {
245 if (m_textView)
246 [m_textView copy:nil];
247
248 }
249
250 void wxNSTextViewControl::Cut()
251 {
252 if (m_textView)
253 [m_textView cut:nil];
254 }
255
256 void wxNSTextViewControl::Paste()
257 {
258 if (m_textView)
259 [m_textView paste:nil];
260 }
261
262 bool wxNSTextViewControl::CanPaste() const
263 {
264 return true;
265 }
266
267 void wxNSTextViewControl::SetEditable(bool editable)
268 {
269 if (m_textView)
270 [m_textView setEditable: editable];
271 }
272
273 void wxNSTextViewControl::GetSelection( long* from, long* to) const
274 {
275 if (m_textView)
276 {
277 NSRange range = [m_textView selectedRange];
278 *from = range.location;
279 *to = range.location + range.length;
280 }
281 }
282
283 void wxNSTextViewControl::SetSelection( long from , long to )
284 {
285 [m_textView setSelectedRange:NSMakeRange(from, to-from)];
286 }
287
288 void wxNSTextViewControl::WriteText(const wxString& str)
289 {
290 // temp hack to get logging working early
291 wxString former = GetStringValue();
292 SetStringValue( former + str );
293 }
294
295 // wxNSTextFieldControl
296
297 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
298 {
299 m_textField = (NSTextField*) w;
300 [m_textField setDelegate: w];
301 }
302
303 wxNSTextFieldControl::~wxNSTextFieldControl()
304 {
305 if (m_textField)
306 [m_textField setDelegate: nil];
307 }
308
309 wxString wxNSTextFieldControl::GetStringValue() const
310 {
311 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
312 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
313 }
314 void wxNSTextFieldControl::SetStringValue( const wxString &str)
315 {
316 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
317 }
318 void wxNSTextFieldControl::Copy()
319 {
320 NSText* editor = [m_textField currentEditor];
321 if ( editor )
322 {
323 [editor copy:nil];
324 }
325 }
326
327 void wxNSTextFieldControl::Cut()
328 {
329 NSText* editor = [m_textField currentEditor];
330 if ( editor )
331 {
332 [editor cut:nil];
333 }
334 }
335
336 void wxNSTextFieldControl::Paste()
337 {
338 NSText* editor = [m_textField currentEditor];
339 if ( editor )
340 {
341 [editor paste:nil];
342 }
343 }
344
345 bool wxNSTextFieldControl::CanPaste() const
346 {
347 return true;
348 }
349
350 void wxNSTextFieldControl::SetEditable(bool editable)
351 {
352 [m_textField setEditable:editable];
353 }
354
355 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
356 {
357 NSText* editor = [m_textField currentEditor];
358 if ( editor )
359 {
360 NSRange range = [editor selectedRange];
361 *from = range.location;
362 *to = range.location + range.length;
363 }
364 }
365
366 void wxNSTextFieldControl::SetSelection( long from , long to )
367 {
368 NSText* editor = [m_textField currentEditor];
369 if ( editor )
370 {
371 [editor setSelectedRange:NSMakeRange(from, to-from)];
372 }
373 }
374
375 void wxNSTextFieldControl::WriteText(const wxString& str)
376 {
377 // temp hack to get logging working early
378 wxString former = GetStringValue();
379 SetStringValue( former + str );
380 }
381
382 void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender)
383 {
384 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
385 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
386 {
387 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
388 event.SetEventObject( wxpeer );
389 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
390 wxpeer->HandleWindowEvent( event );
391 }
392 }
393
394 //
395 //
396 //
397
398 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
399 wxWindowMac* parent,
400 wxWindowID id,
401 const wxString& str,
402 const wxPoint& pos,
403 const wxSize& size,
404 long style,
405 long extraStyle)
406 {
407 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
408 NSTextField* v = nil;
409 wxWidgetCocoaImpl* c = NULL;
410
411 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
412 {
413 v = [[wxNSTextView alloc] initWithFrame:r];
414 c = new wxNSTextViewControl( wxpeer, v );
415 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
416 }
417 else
418 {
419 if ( style & wxTE_PASSWORD )
420 v = [[wxNSSecureTextField alloc] initWithFrame:r];
421 else
422 v = [[wxNSTextField alloc] initWithFrame:r];
423
424 if ( style & wxNO_BORDER )
425 {
426 // FIXME: How can we remove the native control's border?
427 // setBordered is separate from the text ctrl's border.
428 }
429
430 [v setBezeled:NO];
431 [v setBordered:NO];
432
433 c = new wxNSTextFieldControl( wxpeer, v );
434 [v setImplementation: c];
435 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
436 }
437
438 return c;
439 }
440
441
442 #endif // wxUSE_TEXTCTRL