]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/textctrl.mm
32717d37d09e01c85cbd54f6151e68aefb400985
[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 }
55 @end
56
57 @implementation wxNSSecureTextField
58
59 + (void)initialize
60 {
61 static BOOL initialized = NO;
62 if (!initialized)
63 {
64 initialized = YES;
65 wxOSXCocoaClassAddWXMethods( self );
66 }
67 }
68
69 - (void)controlTextDidChange:(NSNotification *)aNotification
70 {
71 wxUnusedVar(aNotification);
72 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
73 if ( impl )
74 {
75 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
76 if ( wxpeer ) {
77 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
78 event.SetEventObject( wxpeer );
79 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
80 wxpeer->HandleWindowEvent( event );
81 }
82 }
83 }
84
85 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
86 {
87 wxUnusedVar(aNotification);
88 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
89 if ( impl )
90 {
91 impl->DoNotifyFocusEvent( false, NULL );
92 }
93 }
94
95 @end
96
97 @interface wxNSTextScrollView : NSScrollView
98 {
99 }
100 @end
101
102 @interface wxNSTextView : NSTextView
103 {
104 wxNSTextScrollView* scrollView;
105 }
106
107 - (void)setScrollView: (wxNSTextScrollView *) sv;
108 - (wxNSTextScrollView*) scrollView;
109
110 @end
111
112 @implementation wxNSTextScrollView
113
114 + (void)initialize
115 {
116 static BOOL initialized = NO;
117 if (!initialized)
118 {
119 initialized = YES;
120 wxOSXCocoaClassAddWXMethods( self );
121 }
122 }
123
124 - (void)textDidChange:(NSNotification *)aNotification
125 {
126 wxUnusedVar(aNotification);
127 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
128 if ( impl )
129 {
130 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
131 if ( wxpeer ) {
132 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
133 event.SetEventObject( wxpeer );
134 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
135 wxpeer->HandleWindowEvent( event );
136 }
137 }
138 }
139
140 - (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)commandSelector
141 {
142 wxUnusedVar(aTextView);
143 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
144 if ( impl )
145 {
146 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
147 if (commandSelector == @selector(insertNewline:))
148 {
149 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
150 {
151 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
152 event.SetEventObject( wxpeer );
153 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
154 wxpeer->HandleWindowEvent( event );
155 }
156 }
157 }
158
159 return NO;
160 }
161
162 - (void)textDidEndEditing:(NSNotification *)aNotification
163 {
164 wxUnusedVar(aNotification);
165 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
166 if ( impl )
167 {
168 impl->DoNotifyFocusEvent( false, NULL );
169 }
170 }
171 @end
172
173 @implementation wxNSTextView
174
175 - (BOOL) becomeFirstResponder
176 {
177 BOOL val = [super becomeFirstResponder];
178
179 if ( val )
180 {
181 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( scrollView );
182 if (impl )
183 impl->DoNotifyFocusEvent( true, NULL );
184
185 }
186 return val;
187 }
188
189 - (void)setScrollView: (wxNSTextScrollView *) sv
190 {
191 scrollView = sv;
192 }
193
194 - (wxNSTextScrollView*) scrollView
195 {
196 return scrollView;
197 }
198
199 @end
200
201 @implementation wxNSTextField
202
203 + (void)initialize
204 {
205 static BOOL initialized = NO;
206 if (!initialized)
207 {
208 initialized = YES;
209 wxOSXCocoaClassAddWXMethods( self );
210 }
211 }
212
213 - (void) setEnabled:(BOOL) flag
214 {
215 [super setEnabled: flag];
216
217 if (![self drawsBackground]) {
218 // Static text is drawn incorrectly when disabled.
219 // For an explanation, see
220 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
221 if (flag) {
222 [self setTextColor: [NSColor controlTextColor]];
223 } else {
224 [self setTextColor: [NSColor secondarySelectedControlColor]];
225 }
226 }
227 }
228
229 - (void)controlTextDidChange:(NSNotification *)aNotification
230 {
231 wxUnusedVar(aNotification);
232 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
233 if ( impl )
234 {
235 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
236 if ( wxpeer ) {
237 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
238 event.SetEventObject( wxpeer );
239 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
240 wxpeer->HandleWindowEvent( event );
241 }
242 }
243 }
244
245 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
246
247 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
248 {
249 wxUnusedVar(textView);
250 wxUnusedVar(control);
251 if (commandSelector == @selector(insertNewline:))
252 {
253 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
254 if ( impl )
255 {
256 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
257 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
258 {
259 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
260 event.SetEventObject( wxpeer );
261 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
262 wxpeer->HandleWindowEvent( event );
263 }
264 }
265 }
266
267 return NO;
268 }
269
270 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
271 {
272 wxUnusedVar(aNotification);
273 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
274 if ( impl )
275 {
276 impl->DoNotifyFocusEvent( false, NULL );
277 }
278 }
279 @end
280
281 // wxNSTextViewControl
282
283 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
284 {
285 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
286 m_scrollView = sv;
287
288 [m_scrollView setHasVerticalScroller:YES];
289 [m_scrollView setHasHorizontalScroller:NO];
290 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
291 NSSize contentSize = [m_scrollView contentSize];
292
293 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
294 contentSize.width, contentSize.height)];
295 m_textView = tv;
296 [tv setVerticallyResizable:YES];
297 [tv setHorizontallyResizable:NO];
298 [tv setAutoresizingMask:NSViewWidthSizable];
299
300 [m_scrollView setDocumentView: tv];
301
302 [tv setDelegate: w];
303 [tv setScrollView:sv];
304 }
305
306 wxNSTextViewControl::~wxNSTextViewControl()
307 {
308 if (m_textView)
309 [m_textView setDelegate: nil];
310 }
311
312 wxString wxNSTextViewControl::GetStringValue() const
313 {
314 if (m_textView)
315 {
316 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
317 wxString result = cf.AsString(m_wxPeer->GetFont().GetEncoding());
318 wxMacConvertNewlines13To10( &result ) ;
319 return result;
320 }
321 return wxEmptyString;
322 }
323 void wxNSTextViewControl::SetStringValue( const wxString &str)
324 {
325 wxString st = str;
326 wxMacConvertNewlines10To13( &st );
327
328 if (m_textView)
329 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
330 }
331 void wxNSTextViewControl::Copy()
332 {
333 if (m_textView)
334 [m_textView copy:nil];
335
336 }
337
338 void wxNSTextViewControl::Cut()
339 {
340 if (m_textView)
341 [m_textView cut:nil];
342 }
343
344 void wxNSTextViewControl::Paste()
345 {
346 if (m_textView)
347 [m_textView paste:nil];
348 }
349
350 bool wxNSTextViewControl::CanPaste() const
351 {
352 return true;
353 }
354
355 void wxNSTextViewControl::SetEditable(bool editable)
356 {
357 if (m_textView)
358 [m_textView setEditable: editable];
359 }
360
361 void wxNSTextViewControl::GetSelection( long* from, long* to) const
362 {
363 if (m_textView)
364 {
365 NSRange range = [m_textView selectedRange];
366 *from = range.location;
367 *to = range.location + range.length;
368 }
369 }
370
371 void wxNSTextViewControl::SetSelection( long from , long to )
372 {
373 long textLength = [[m_textView string] length];
374 if ((from == -1) && (to == -1))
375 {
376 from = 0 ;
377 to = textLength ;
378 }
379 else
380 {
381 from = wxMin(textLength,wxMax(from,0)) ;
382 if ( to == -1 )
383 to = textLength;
384 else
385 to = wxMax(0,wxMin(textLength,to)) ;
386 }
387
388 NSRange selrange = NSMakeRange(from, to-from);
389 [m_textView setSelectedRange:selrange];
390 [m_textView scrollRangeToVisible:selrange];
391 }
392
393 void wxNSTextViewControl::WriteText(const wxString& str)
394 {
395 wxString st = str;
396 wxMacConvertNewlines10To13( &st );
397
398 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
399 }
400
401 // wxNSTextFieldControl
402
403 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
404 {
405 m_textField = (NSTextField*) w;
406 [m_textField setDelegate: w];
407 m_selStart = m_selEnd = 0;
408 }
409
410 wxNSTextFieldControl::~wxNSTextFieldControl()
411 {
412 if (m_textField)
413 [m_textField setDelegate: nil];
414 }
415
416 wxString wxNSTextFieldControl::GetStringValue() const
417 {
418 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
419 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
420 }
421
422 void wxNSTextFieldControl::SetStringValue( const wxString &str)
423 {
424 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
425 }
426
427 void wxNSTextFieldControl::Copy()
428 {
429 NSText* editor = [m_textField currentEditor];
430 if ( editor )
431 {
432 [editor copy:nil];
433 }
434 }
435
436 void wxNSTextFieldControl::Cut()
437 {
438 NSText* editor = [m_textField currentEditor];
439 if ( editor )
440 {
441 [editor cut:nil];
442 }
443 }
444
445 void wxNSTextFieldControl::Paste()
446 {
447 NSText* editor = [m_textField currentEditor];
448 if ( editor )
449 {
450 [editor paste:nil];
451 }
452 }
453
454 bool wxNSTextFieldControl::CanPaste() const
455 {
456 return true;
457 }
458
459 void wxNSTextFieldControl::SetEditable(bool editable)
460 {
461 [m_textField setEditable:editable];
462 }
463
464 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
465 {
466 NSText* editor = [m_textField currentEditor];
467 if ( editor )
468 {
469 NSRange range = [editor selectedRange];
470 *from = range.location;
471 *to = range.location + range.length;
472 }
473 else
474 {
475 *from = m_selStart;
476 *to = m_selEnd;
477 }
478 }
479
480 void wxNSTextFieldControl::SetSelection( long from , long to )
481 {
482 long textLength = [[m_textField stringValue] length];
483 if ((from == -1) && (to == -1))
484 {
485 from = 0 ;
486 to = textLength ;
487 }
488 else
489 {
490 from = wxMin(textLength,wxMax(from,0)) ;
491 if ( to == -1 )
492 to = textLength;
493 else
494 to = wxMax(0,wxMin(textLength,to)) ;
495 }
496
497 NSText* editor = [m_textField currentEditor];
498 if ( editor )
499 {
500 [editor setSelectedRange:NSMakeRange(from, to-from)];
501 }
502 else
503 {
504 m_selStart = from;
505 m_selEnd = to;
506 }
507 }
508
509 void wxNSTextFieldControl::WriteText(const wxString& str)
510 {
511 NSText* editor = [m_textField currentEditor];
512 if ( editor )
513 {
514 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
515 }
516 else
517 {
518 wxString val = GetStringValue() ;
519 long start , end ;
520 GetSelection( &start , &end ) ;
521 val.Remove( start , end - start ) ;
522 val.insert( start , str ) ;
523 SetStringValue( val ) ;
524 SetSelection( start + str.length() , start + str.length() ) ;
525 }
526 }
527
528 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
529 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
530 {
531 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
532 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
533 {
534 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
535 event.SetEventObject( wxpeer );
536 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
537 wxpeer->HandleWindowEvent( event );
538 }
539 }
540
541 //
542 //
543 //
544
545 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
546 wxWindowMac* WXUNUSED(parent),
547 wxWindowID WXUNUSED(id),
548 const wxString& str,
549 const wxPoint& pos,
550 const wxSize& size,
551 long style,
552 long WXUNUSED(extraStyle))
553 {
554 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
555 wxWidgetCocoaImpl* c = NULL;
556
557 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
558 {
559 wxNSTextScrollView* v = nil;
560 v = [[wxNSTextScrollView alloc] initWithFrame:r];
561 c = new wxNSTextViewControl( wxpeer, v );
562 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
563 }
564 else
565 {
566 NSTextField* v = nil;
567 if ( style & wxTE_PASSWORD )
568 v = [[wxNSSecureTextField alloc] initWithFrame:r];
569 else
570 v = [[wxNSTextField alloc] initWithFrame:r];
571
572 if ( style & wxNO_BORDER )
573 {
574 // FIXME: How can we remove the native control's border?
575 // setBordered is separate from the text ctrl's border.
576 }
577
578 [v setBezeled:NO];
579 [v setBordered:NO];
580
581 c = new wxNSTextFieldControl( wxpeer, v );
582 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
583 }
584
585 return c;
586 }
587
588
589 #endif // wxUSE_TEXTCTRL