]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/osx/cocoa/textctrl.mm
nschars is never used for the wxEVT_CHAR case, so don't assign it a different value...
[wxWidgets.git] / src / osx / cocoa / textctrl.mm
... / ...
CommitLineData
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
57class wxMacEditHelper
58{
59public :
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
77protected :
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
280typedef 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
318wxNSTextViewControl::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
342wxNSTextViewControl::~wxNSTextViewControl()
343{
344 if (m_textView)
345 [m_textView setDelegate: nil];
346}
347
348wxString 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}
358void 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
368void wxNSTextViewControl::Copy()
369{
370 if (m_textView)
371 [m_textView copy:nil];
372
373}
374
375void wxNSTextViewControl::Cut()
376{
377 if (m_textView)
378 [m_textView cut:nil];
379}
380
381void wxNSTextViewControl::Paste()
382{
383 if (m_textView)
384 [m_textView paste:nil];
385}
386
387bool wxNSTextViewControl::CanPaste() const
388{
389 return true;
390}
391
392void wxNSTextViewControl::SetEditable(bool editable)
393{
394 if (m_textView)
395 [m_textView setEditable: editable];
396}
397
398void 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
408void 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
430void 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
441void 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
447bool 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
486void 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
511void wxNSTextViewControl::CheckSpelling(bool check)
512{
513 if (m_textView)
514 [m_textView setContinuousSpellCheckingEnabled: check];
515}
516
517wxSize 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 return wxSize(0,0);
528}
529
530// wxNSTextFieldControl
531
532wxNSTextFieldControl::wxNSTextFieldControl( wxWindow *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
533{
534 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField*) w;
535 m_textField = tf;
536 [m_textField setDelegate: tf];
537 m_selStart = m_selEnd = 0;
538 m_hasEditor = [w isKindOfClass:[NSTextField class]];
539}
540
541wxNSTextFieldControl::~wxNSTextFieldControl()
542{
543 if (m_textField)
544 [m_textField setDelegate: nil];
545}
546
547wxString wxNSTextFieldControl::GetStringValue() const
548{
549 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
550}
551
552void wxNSTextFieldControl::SetStringValue( const wxString &str)
553{
554 wxMacEditHelper helper(m_textField);
555 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
556}
557
558void wxNSTextFieldControl::Copy()
559{
560 NSText* editor = [m_textField currentEditor];
561 if ( editor )
562 {
563 [editor copy:nil];
564 }
565}
566
567void wxNSTextFieldControl::Cut()
568{
569 NSText* editor = [m_textField currentEditor];
570 if ( editor )
571 {
572 [editor cut:nil];
573 }
574}
575
576void wxNSTextFieldControl::Paste()
577{
578 NSText* editor = [m_textField currentEditor];
579 if ( editor )
580 {
581 [editor paste:nil];
582 }
583}
584
585bool wxNSTextFieldControl::CanPaste() const
586{
587 return true;
588}
589
590void wxNSTextFieldControl::SetEditable(bool editable)
591{
592 [m_textField setEditable:editable];
593}
594
595void wxNSTextFieldControl::GetSelection( long* from, long* to) const
596{
597 NSText* editor = [m_textField currentEditor];
598 if ( editor )
599 {
600 NSRange range = [editor selectedRange];
601 *from = range.location;
602 *to = range.location + range.length;
603 }
604 else
605 {
606 *from = m_selStart;
607 *to = m_selEnd;
608 }
609}
610
611void wxNSTextFieldControl::SetSelection( long from , long to )
612{
613 long textLength = [[m_textField stringValue] length];
614 if ((from == -1) && (to == -1))
615 {
616 from = 0 ;
617 to = textLength ;
618 }
619 else
620 {
621 from = wxMin(textLength,wxMax(from,0)) ;
622 if ( to == -1 )
623 to = textLength;
624 else
625 to = wxMax(0,wxMin(textLength,to)) ;
626 }
627
628 NSText* editor = [m_textField currentEditor];
629 if ( editor )
630 {
631 [editor setSelectedRange:NSMakeRange(from, to-from)];
632 }
633 else
634 {
635 m_selStart = from;
636 m_selEnd = to;
637 }
638}
639
640void wxNSTextFieldControl::WriteText(const wxString& str)
641{
642 NSEvent* formerEvent = m_lastKeyDownEvent;
643 m_lastKeyDownEvent = nil;
644 NSText* editor = [m_textField currentEditor];
645 if ( editor )
646 {
647 wxMacEditHelper helper(m_textField);
648 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
649 }
650 else
651 {
652 wxString val = GetStringValue() ;
653 long start , end ;
654 GetSelection( &start , &end ) ;
655 val.Remove( start , end - start ) ;
656 val.insert( start , str ) ;
657 SetStringValue( val ) ;
658 SetSelection( start + str.length() , start + str.length() ) ;
659 }
660 m_lastKeyDownEvent = formerEvent;
661}
662
663void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
664 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
665{
666 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
667 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
668 {
669 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
670 event.SetEventObject( wxpeer );
671 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
672 wxpeer->HandleWindowEvent( event );
673 }
674}
675
676//
677//
678//
679
680wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
681 wxWindowMac* WXUNUSED(parent),
682 wxWindowID WXUNUSED(id),
683 const wxString& WXUNUSED(str),
684 const wxPoint& pos,
685 const wxSize& size,
686 long style,
687 long WXUNUSED(extraStyle))
688{
689 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
690 wxWidgetCocoaImpl* c = NULL;
691
692 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
693 {
694 wxNSTextScrollView* v = nil;
695 v = [[wxNSTextScrollView alloc] initWithFrame:r];
696 c = new wxNSTextViewControl( wxpeer, v );
697 }
698 else
699 {
700 NSTextField* v = nil;
701 if ( style & wxTE_PASSWORD )
702 v = [[wxNSSecureTextField alloc] initWithFrame:r];
703 else
704 v = [[wxNSTextField alloc] initWithFrame:r];
705
706 if ( style & wxNO_BORDER )
707 {
708 // FIXME: How can we remove the native control's border?
709 // setBordered is separate from the text ctrl's border.
710 }
711
712 [v setBezeled:NO];
713 [v setBordered:NO];
714
715 c = new wxNSTextFieldControl( wxpeer, v );
716 }
717
718 return c;
719}
720
721
722#endif // wxUSE_TEXTCTRL