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