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