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