]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/textctrl.mm
Implement native OS X ComboBox for OS X Cocoa, and implement wxTextEntry methods...
[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 wxSize wxNSTextViewControl::GetBestSize() const
518 {
519 if (m_textView && [m_textView layoutManager])
520 {
521 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
522 wxSize size = wxSize(rect.size.width, rect.size.height);
523 size.x += [m_textView textContainerInset].width;
524 size.y += [m_textView textContainerInset].height;
525 return size;
526 }
527 }
528
529 // wxNSTextFieldControl
530
531 wxNSTextFieldControl::wxNSTextFieldControl( wxWindow *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
532 {
533 m_textField = (NSTextField*) w;
534 [m_textField setDelegate: w];
535 m_selStart = m_selEnd = 0;
536 m_hasEditor = [w isKindOfClass:[NSTextField class]];
537 }
538
539 wxNSTextFieldControl::~wxNSTextFieldControl()
540 {
541 if (m_textField)
542 [m_textField setDelegate: nil];
543 }
544
545 wxString wxNSTextFieldControl::GetStringValue() const
546 {
547 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
548 }
549
550 void wxNSTextFieldControl::SetStringValue( const wxString &str)
551 {
552 wxMacEditHelper helper(m_textField);
553 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
554 }
555
556 void wxNSTextFieldControl::Copy()
557 {
558 NSText* editor = [m_textField currentEditor];
559 if ( editor )
560 {
561 [editor copy:nil];
562 }
563 }
564
565 void wxNSTextFieldControl::Cut()
566 {
567 NSText* editor = [m_textField currentEditor];
568 if ( editor )
569 {
570 [editor cut:nil];
571 }
572 }
573
574 void wxNSTextFieldControl::Paste()
575 {
576 NSText* editor = [m_textField currentEditor];
577 if ( editor )
578 {
579 [editor paste:nil];
580 }
581 }
582
583 bool wxNSTextFieldControl::CanPaste() const
584 {
585 return true;
586 }
587
588 void wxNSTextFieldControl::SetEditable(bool editable)
589 {
590 [m_textField setEditable:editable];
591 }
592
593 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
594 {
595 NSText* editor = [m_textField currentEditor];
596 if ( editor )
597 {
598 NSRange range = [editor selectedRange];
599 *from = range.location;
600 *to = range.location + range.length;
601 }
602 else
603 {
604 *from = m_selStart;
605 *to = m_selEnd;
606 }
607 }
608
609 void wxNSTextFieldControl::SetSelection( long from , long to )
610 {
611 long textLength = [[m_textField stringValue] length];
612 if ((from == -1) && (to == -1))
613 {
614 from = 0 ;
615 to = textLength ;
616 }
617 else
618 {
619 from = wxMin(textLength,wxMax(from,0)) ;
620 if ( to == -1 )
621 to = textLength;
622 else
623 to = wxMax(0,wxMin(textLength,to)) ;
624 }
625
626 NSText* editor = [m_textField currentEditor];
627 if ( editor )
628 {
629 [editor setSelectedRange:NSMakeRange(from, to-from)];
630 }
631 else
632 {
633 m_selStart = from;
634 m_selEnd = to;
635 }
636 }
637
638 void wxNSTextFieldControl::WriteText(const wxString& str)
639 {
640 NSEvent* formerEvent = m_lastKeyDownEvent;
641 m_lastKeyDownEvent = nil;
642 NSText* editor = [m_textField currentEditor];
643 if ( editor )
644 {
645 wxMacEditHelper helper(m_textField);
646 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
647 }
648 else
649 {
650 wxString val = GetStringValue() ;
651 long start , end ;
652 GetSelection( &start , &end ) ;
653 val.Remove( start , end - start ) ;
654 val.insert( start , str ) ;
655 SetStringValue( val ) ;
656 SetSelection( start + str.length() , start + str.length() ) ;
657 }
658 m_lastKeyDownEvent = formerEvent;
659 }
660
661 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
662 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
663 {
664 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
665 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
666 {
667 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
668 event.SetEventObject( wxpeer );
669 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
670 wxpeer->HandleWindowEvent( event );
671 }
672 }
673
674 //
675 //
676 //
677
678 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
679 wxWindowMac* WXUNUSED(parent),
680 wxWindowID WXUNUSED(id),
681 const wxString& WXUNUSED(str),
682 const wxPoint& pos,
683 const wxSize& size,
684 long style,
685 long WXUNUSED(extraStyle))
686 {
687 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
688 wxWidgetCocoaImpl* c = NULL;
689
690 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
691 {
692 wxNSTextScrollView* v = nil;
693 v = [[wxNSTextScrollView alloc] initWithFrame:r];
694 c = new wxNSTextViewControl( wxpeer, v );
695 }
696 else
697 {
698 NSTextField* v = nil;
699 if ( style & wxTE_PASSWORD )
700 v = [[wxNSSecureTextField alloc] initWithFrame:r];
701 else
702 v = [[wxNSTextField alloc] initWithFrame:r];
703
704 if ( style & wxNO_BORDER )
705 {
706 // FIXME: How can we remove the native control's border?
707 // setBordered is separate from the text ctrl's border.
708 }
709
710 [v setBezeled:NO];
711 [v setBordered:NO];
712
713 c = new wxNSTextFieldControl( wxpeer, v );
714 }
715
716 return c;
717 }
718
719
720 #endif // wxUSE_TEXTCTRL