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