]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/textctrl.mm
63a2515307fd2630ddedfaf503e2155748d582bb
[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 NSView(EditableView)
53 - (BOOL)isEditable;
54 - (void)setEditable:(BOOL)flag;
55 @end
56
57 class wxMacEditHelper
58 {
59 public :
60 wxMacEditHelper( NSView* textView )
61 {
62 m_textView = textView;
63 m_formerState = YES;
64 if ( textView )
65 {
66 m_formerState = [textView isEditable];
67 [textView setEditable:YES];
68 }
69 }
70
71 ~wxMacEditHelper()
72 {
73 if ( m_textView )
74 [m_textView setEditable:m_formerState];
75 }
76
77 protected :
78 BOOL m_formerState ;
79 NSView* m_textView;
80 } ;
81
82 @implementation wxNSSecureTextField
83
84 + (void)initialize
85 {
86 static BOOL initialized = NO;
87 if (!initialized)
88 {
89 initialized = YES;
90 wxOSXCocoaClassAddWXMethods( self );
91 }
92 }
93
94 - (void)controlTextDidChange:(NSNotification *)aNotification
95 {
96 wxUnusedVar(aNotification);
97 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
98 if ( impl )
99 {
100 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
101 if ( wxpeer ) {
102 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
103 event.SetEventObject( wxpeer );
104 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
105 wxpeer->HandleWindowEvent( event );
106 }
107 }
108 }
109
110 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
111 {
112 wxUnusedVar(aNotification);
113 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
114 if ( impl )
115 {
116 impl->DoNotifyFocusEvent( false, NULL );
117 }
118 }
119
120 @end
121
122 @interface wxNSTextScrollView : NSScrollView
123 {
124 }
125 @end
126
127 @implementation wxNSTextScrollView
128
129 + (void)initialize
130 {
131 static BOOL initialized = NO;
132 if (!initialized)
133 {
134 initialized = YES;
135 wxOSXCocoaClassAddWXMethods( self );
136 }
137 }
138
139 @end
140
141 @implementation wxNSTextFieldEditor
142
143 - (void) keyDown:(NSEvent*) event
144 {
145 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
146 lastKeyDownEvent = event;
147 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
148 [super keyDown:event];
149 lastKeyDownEvent = nil;
150 }
151
152 - (void) keyUp:(NSEvent*) event
153 {
154 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
155 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
156 [super keyUp:event];
157 }
158
159 - (void) flagsChanged:(NSEvent*) event
160 {
161 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
162 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
163 [super flagsChanged:event];
164 }
165
166 - (BOOL) performKeyEquivalent:(NSEvent*) event
167 {
168 BOOL retval = [super performKeyEquivalent:event];
169 return retval;
170 }
171
172 - (void) insertText:(id) str
173 {
174 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
175 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
176 {
177 [super insertText:str];
178 }
179 }
180
181 @end
182
183 @implementation wxNSTextView
184
185 + (void)initialize
186 {
187 static BOOL initialized = NO;
188 if (!initialized)
189 {
190 initialized = YES;
191 wxOSXCocoaClassAddWXMethods( self );
192 }
193 }
194
195 - (void)textDidChange:(NSNotification *)aNotification
196 {
197 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( [aNotification object] );
198 if ( impl )
199 {
200 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
201 if ( wxpeer ) {
202 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
203 event.SetEventObject( wxpeer );
204 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
205 wxpeer->HandleWindowEvent( event );
206 }
207 }
208 }
209
210 @end
211
212 @implementation wxNSTextField
213
214 + (void)initialize
215 {
216 static BOOL initialized = NO;
217 if (!initialized)
218 {
219 initialized = YES;
220 wxOSXCocoaClassAddWXMethods( self );
221 }
222 }
223
224 - (id) initWithFrame:(NSRect) frame
225 {
226 self = [super initWithFrame:frame];
227 fieldEditor = nil;
228 return self;
229 }
230
231 - (void) dealloc
232 {
233 [fieldEditor release];
234 [super dealloc];
235 }
236
237 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
238 {
239 fieldEditor = editor;
240 }
241
242 - (wxNSTextFieldEditor*) fieldEditor
243 {
244 return fieldEditor;
245 }
246
247
248 - (void) setEnabled:(BOOL) flag
249 {
250 [super setEnabled: flag];
251
252 if (![self drawsBackground]) {
253 // Static text is drawn incorrectly when disabled.
254 // For an explanation, see
255 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
256 if (flag) {
257 [self setTextColor: [NSColor controlTextColor]];
258 } else {
259 [self setTextColor: [NSColor secondarySelectedControlColor]];
260 }
261 }
262 }
263
264 - (void)controlTextDidChange:(NSNotification *)aNotification
265 {
266 wxUnusedVar(aNotification);
267 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
268 if ( impl )
269 {
270 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
271 if ( wxpeer ) {
272 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
273 event.SetEventObject( wxpeer );
274 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
275 wxpeer->HandleWindowEvent( event );
276 }
277 }
278 }
279
280 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
281
282 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
283 {
284 wxUnusedVar(textView);
285 wxUnusedVar(control);
286 if (commandSelector == @selector(insertNewline:))
287 {
288 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
289 if ( impl )
290 {
291 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
292 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
293 {
294 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
295 event.SetEventObject( wxpeer );
296 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
297 wxpeer->HandleWindowEvent( event );
298 }
299 }
300 }
301
302 return NO;
303 }
304
305 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
306 {
307 wxUnusedVar(aNotification);
308 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
309 if ( impl )
310 {
311 impl->DoNotifyFocusEvent( false, NULL );
312 }
313 }
314 @end
315
316 // wxNSTextViewControl
317
318 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
319 {
320 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
321 m_scrollView = sv;
322
323 [m_scrollView setHasVerticalScroller:YES];
324 [m_scrollView setHasHorizontalScroller:NO];
325 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
326 NSSize contentSize = [m_scrollView contentSize];
327
328 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
329 contentSize.width, contentSize.height)];
330 m_textView = tv;
331 [tv setVerticallyResizable:YES];
332 [tv setHorizontallyResizable:NO];
333 [tv setAutoresizingMask:NSViewWidthSizable];
334
335 [m_scrollView setDocumentView: tv];
336
337 [tv setDelegate: tv];
338
339 InstallEventHandler(tv);
340 }
341
342 wxNSTextViewControl::~wxNSTextViewControl()
343 {
344 if (m_textView)
345 [m_textView setDelegate: nil];
346 }
347
348 wxString wxNSTextViewControl::GetStringValue() const
349 {
350 if (m_textView)
351 {
352 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
353 wxMacConvertNewlines13To10( &result ) ;
354 return result;
355 }
356 return wxEmptyString;
357 }
358 void wxNSTextViewControl::SetStringValue( const wxString &str)
359 {
360 wxString st = str;
361 wxMacConvertNewlines10To13( &st );
362 wxMacEditHelper helper(m_textView);
363
364 if (m_textView)
365 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
366 }
367
368 void wxNSTextViewControl::Copy()
369 {
370 if (m_textView)
371 [m_textView copy:nil];
372
373 }
374
375 void wxNSTextViewControl::Cut()
376 {
377 if (m_textView)
378 [m_textView cut:nil];
379 }
380
381 void wxNSTextViewControl::Paste()
382 {
383 if (m_textView)
384 [m_textView paste:nil];
385 }
386
387 bool wxNSTextViewControl::CanPaste() const
388 {
389 return true;
390 }
391
392 void wxNSTextViewControl::SetEditable(bool editable)
393 {
394 if (m_textView)
395 [m_textView setEditable: editable];
396 }
397
398 void wxNSTextViewControl::GetSelection( long* from, long* to) const
399 {
400 if (m_textView)
401 {
402 NSRange range = [m_textView selectedRange];
403 *from = range.location;
404 *to = range.location + range.length;
405 }
406 }
407
408 void wxNSTextViewControl::SetSelection( long from , long to )
409 {
410 long textLength = [[m_textView string] length];
411 if ((from == -1) && (to == -1))
412 {
413 from = 0 ;
414 to = textLength ;
415 }
416 else
417 {
418 from = wxMin(textLength,wxMax(from,0)) ;
419 if ( to == -1 )
420 to = textLength;
421 else
422 to = wxMax(0,wxMin(textLength,to)) ;
423 }
424
425 NSRange selrange = NSMakeRange(from, to-from);
426 [m_textView setSelectedRange:selrange];
427 [m_textView scrollRangeToVisible:selrange];
428 }
429
430 void wxNSTextViewControl::WriteText(const wxString& str)
431 {
432 wxString st = str;
433 wxMacConvertNewlines10To13( &st );
434 wxMacEditHelper helper(m_textView);
435 NSEvent* formerEvent = m_lastKeyDownEvent;
436 m_lastKeyDownEvent = nil;
437 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
438 m_lastKeyDownEvent = formerEvent;
439 }
440
441 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
442 {
443 if ([m_textView respondsToSelector:@selector(setFont:)])
444 [m_textView setFont: font.OSXGetNSFont()];
445 }
446
447 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
448 {
449 if (m_textView && position >=0)
450 {
451 NSFont* font = NULL;
452 NSColor* bgcolor = NULL;
453 NSColor* fgcolor = NULL;
454 // NOTE: It appears that other platforms accept GetStyle with the position == length
455 // but that NSTextStorage does not accept length as a valid position.
456 // Therefore we return the default control style in that case.
457 if (position < [[m_textView string] length])
458 {
459 NSTextStorage* storage = [m_textView textStorage];
460 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
461 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
462 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
463 }
464 else
465 {
466 NSDictionary* attrs = [m_textView typingAttributes];
467 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
468 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
469 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
470 }
471
472 if (font)
473 style.SetFont(wxFont(font));
474
475 if (bgcolor)
476 style.SetBackgroundColour(wxColour(bgcolor));
477
478 if (fgcolor)
479 style.SetTextColour(wxColour(fgcolor));
480 return true;
481 }
482
483 return false;
484 }
485
486 void wxNSTextViewControl::SetStyle(long start,
487 long end,
488 const wxTextAttr& style)
489 {
490 if (m_textView) {
491 NSRange range = NSMakeRange(start, end-start);
492 if (start == -1 && end == -1)
493 range = [m_textView selectedRange];
494
495 NSTextStorage* storage = [m_textView textStorage];
496
497 wxFont font = style.GetFont();
498 if (style.HasFont() && font.IsOk())
499 [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range];
500
501 wxColour bgcolor = style.GetBackgroundColour();
502 if (style.HasBackgroundColour() && bgcolor.IsOk())
503 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
504
505 wxColour fgcolor = style.GetTextColour();
506 if (style.HasTextColour() && fgcolor.IsOk())
507 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
508 }
509 }
510
511 void wxNSTextViewControl::CheckSpelling(bool check)
512 {
513 if (m_textView)
514 [m_textView setContinuousSpellCheckingEnabled: check];
515 }
516
517 // wxNSTextFieldControl
518
519 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
520 {
521 m_textField = (NSTextField*) w;
522 [m_textField setDelegate: w];
523 m_selStart = m_selEnd = 0;
524 m_hasEditor = [w isKindOfClass:[NSTextField class]];
525 }
526
527 wxNSTextFieldControl::~wxNSTextFieldControl()
528 {
529 if (m_textField)
530 [m_textField setDelegate: nil];
531 }
532
533 wxString wxNSTextFieldControl::GetStringValue() const
534 {
535 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
536 }
537
538 void wxNSTextFieldControl::SetStringValue( const wxString &str)
539 {
540 wxMacEditHelper helper(m_textField);
541 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
542 }
543
544 void wxNSTextFieldControl::Copy()
545 {
546 NSText* editor = [m_textField currentEditor];
547 if ( editor )
548 {
549 [editor copy:nil];
550 }
551 }
552
553 void wxNSTextFieldControl::Cut()
554 {
555 NSText* editor = [m_textField currentEditor];
556 if ( editor )
557 {
558 [editor cut:nil];
559 }
560 }
561
562 void wxNSTextFieldControl::Paste()
563 {
564 NSText* editor = [m_textField currentEditor];
565 if ( editor )
566 {
567 [editor paste:nil];
568 }
569 }
570
571 bool wxNSTextFieldControl::CanPaste() const
572 {
573 return true;
574 }
575
576 void wxNSTextFieldControl::SetEditable(bool editable)
577 {
578 [m_textField setEditable:editable];
579 }
580
581 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
582 {
583 NSText* editor = [m_textField currentEditor];
584 if ( editor )
585 {
586 NSRange range = [editor selectedRange];
587 *from = range.location;
588 *to = range.location + range.length;
589 }
590 else
591 {
592 *from = m_selStart;
593 *to = m_selEnd;
594 }
595 }
596
597 void wxNSTextFieldControl::SetSelection( long from , long to )
598 {
599 long textLength = [[m_textField stringValue] length];
600 if ((from == -1) && (to == -1))
601 {
602 from = 0 ;
603 to = textLength ;
604 }
605 else
606 {
607 from = wxMin(textLength,wxMax(from,0)) ;
608 if ( to == -1 )
609 to = textLength;
610 else
611 to = wxMax(0,wxMin(textLength,to)) ;
612 }
613
614 NSText* editor = [m_textField currentEditor];
615 if ( editor )
616 {
617 [editor setSelectedRange:NSMakeRange(from, to-from)];
618 }
619 else
620 {
621 m_selStart = from;
622 m_selEnd = to;
623 }
624 }
625
626 void wxNSTextFieldControl::WriteText(const wxString& str)
627 {
628 NSEvent* formerEvent = m_lastKeyDownEvent;
629 m_lastKeyDownEvent = nil;
630 NSText* editor = [m_textField currentEditor];
631 if ( editor )
632 {
633 wxMacEditHelper helper(m_textField);
634 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
635 }
636 else
637 {
638 wxString val = GetStringValue() ;
639 long start , end ;
640 GetSelection( &start , &end ) ;
641 val.Remove( start , end - start ) ;
642 val.insert( start , str ) ;
643 SetStringValue( val ) ;
644 SetSelection( start + str.length() , start + str.length() ) ;
645 }
646 m_lastKeyDownEvent = formerEvent;
647 }
648
649 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
650 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
651 {
652 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
653 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
654 {
655 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
656 event.SetEventObject( wxpeer );
657 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
658 wxpeer->HandleWindowEvent( event );
659 }
660 }
661
662 //
663 //
664 //
665
666 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
667 wxWindowMac* WXUNUSED(parent),
668 wxWindowID WXUNUSED(id),
669 const wxString& WXUNUSED(str),
670 const wxPoint& pos,
671 const wxSize& size,
672 long style,
673 long WXUNUSED(extraStyle))
674 {
675 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
676 wxWidgetCocoaImpl* c = NULL;
677
678 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
679 {
680 wxNSTextScrollView* v = nil;
681 v = [[wxNSTextScrollView alloc] initWithFrame:r];
682 c = new wxNSTextViewControl( wxpeer, v );
683 }
684 else
685 {
686 NSTextField* v = nil;
687 if ( style & wxTE_PASSWORD )
688 v = [[wxNSSecureTextField alloc] initWithFrame:r];
689 else
690 v = [[wxNSTextField alloc] initWithFrame:r];
691
692 if ( style & wxNO_BORDER )
693 {
694 // FIXME: How can we remove the native control's border?
695 // setBordered is separate from the text ctrl's border.
696 }
697
698 [v setBezeled:NO];
699 [v setBordered:NO];
700
701 c = new wxNSTextFieldControl( wxpeer, v );
702 }
703
704 return c;
705 }
706
707
708 #endif // wxUSE_TEXTCTRL