]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/textctrl.mm
Use the app name, not display name, as debug report name,
[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 if ( textView )
64 {
65 m_formerState = [textView isEditable];
66 [textView setEditable:YES];
67 }
68 }
69
70 ~wxMacEditHelper()
71 {
72 if ( m_textView )
73 [m_textView setEditable:m_formerState];
74 }
75
76 protected :
77 BOOL m_formerState ;
78 NSView* m_textView;
79 } ;
80
81 @implementation wxNSSecureTextField
82
83 + (void)initialize
84 {
85 static BOOL initialized = NO;
86 if (!initialized)
87 {
88 initialized = YES;
89 wxOSXCocoaClassAddWXMethods( self );
90 }
91 }
92
93 - (void)controlTextDidChange:(NSNotification *)aNotification
94 {
95 wxUnusedVar(aNotification);
96 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
97 if ( impl )
98 {
99 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
100 if ( wxpeer ) {
101 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
102 event.SetEventObject( wxpeer );
103 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
104 wxpeer->HandleWindowEvent( event );
105 }
106 }
107 }
108
109 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
110 {
111 wxUnusedVar(aNotification);
112 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
113 if ( impl )
114 {
115 impl->DoNotifyFocusEvent( false, NULL );
116 }
117 }
118
119 @end
120
121 @interface wxNSTextScrollView : NSScrollView
122 {
123 }
124 @end
125
126 @implementation wxNSTextScrollView
127
128 + (void)initialize
129 {
130 static BOOL initialized = NO;
131 if (!initialized)
132 {
133 initialized = YES;
134 wxOSXCocoaClassAddWXMethods( self );
135 }
136 }
137
138 @end
139
140 @implementation wxNSTextFieldEditor
141
142 - (void) keyDown:(NSEvent*) event
143 {
144 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
145 lastKeyDownEvent = event;
146 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
147 [super keyDown:event];
148 lastKeyDownEvent = nil;
149 }
150
151 - (void) keyUp:(NSEvent*) event
152 {
153 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
154 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
155 [super keyUp:event];
156 }
157
158 - (void) flagsChanged:(NSEvent*) event
159 {
160 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
161 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
162 [super flagsChanged:event];
163 }
164
165 - (BOOL) performKeyEquivalent:(NSEvent*) event
166 {
167 BOOL retval = [super performKeyEquivalent:event];
168 return retval;
169 }
170
171 - (void) insertText:(id) str
172 {
173 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
174 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
175 {
176 [super insertText:str];
177 }
178 }
179
180 @end
181
182 @implementation wxNSTextView
183
184 + (void)initialize
185 {
186 static BOOL initialized = NO;
187 if (!initialized)
188 {
189 initialized = YES;
190 wxOSXCocoaClassAddWXMethods( self );
191 }
192 }
193
194 @end
195
196 @implementation wxNSTextField
197
198 + (void)initialize
199 {
200 static BOOL initialized = NO;
201 if (!initialized)
202 {
203 initialized = YES;
204 wxOSXCocoaClassAddWXMethods( self );
205 }
206 }
207
208 - (id) initWithFrame:(NSRect) frame
209 {
210 self = [super initWithFrame:frame];
211 fieldEditor = nil;
212 return self;
213 }
214
215 - (void) dealloc
216 {
217 [fieldEditor release];
218 [super dealloc];
219 }
220
221 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
222 {
223 fieldEditor = editor;
224 }
225
226 - (wxNSTextFieldEditor*) fieldEditor
227 {
228 return fieldEditor;
229 }
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 {
254 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
255 if ( wxpeer ) {
256 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
257 event.SetEventObject( wxpeer );
258 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
259 wxpeer->HandleWindowEvent( event );
260 }
261 }
262 }
263
264 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
265
266 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
267 {
268 wxUnusedVar(textView);
269 wxUnusedVar(control);
270 if (commandSelector == @selector(insertNewline:))
271 {
272 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
273 if ( impl )
274 {
275 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
276 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
277 {
278 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
279 event.SetEventObject( wxpeer );
280 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
281 wxpeer->HandleWindowEvent( event );
282 }
283 }
284 }
285
286 return NO;
287 }
288
289 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
290 {
291 wxUnusedVar(aNotification);
292 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
293 if ( impl )
294 {
295 impl->DoNotifyFocusEvent( false, NULL );
296 }
297 }
298 @end
299
300 // wxNSTextViewControl
301
302 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
303 {
304 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
305 m_scrollView = sv;
306
307 [m_scrollView setHasVerticalScroller:YES];
308 [m_scrollView setHasHorizontalScroller:NO];
309 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
310 NSSize contentSize = [m_scrollView contentSize];
311
312 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
313 contentSize.width, contentSize.height)];
314 m_textView = tv;
315 [tv setVerticallyResizable:YES];
316 [tv setHorizontallyResizable:NO];
317 [tv setAutoresizingMask:NSViewWidthSizable];
318
319 [m_scrollView setDocumentView: tv];
320
321 [tv setDelegate: w];
322
323 InstallEventHandler(tv);
324 }
325
326 wxNSTextViewControl::~wxNSTextViewControl()
327 {
328 if (m_textView)
329 [m_textView setDelegate: nil];
330 }
331
332 wxString wxNSTextViewControl::GetStringValue() const
333 {
334 if (m_textView)
335 {
336 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
337 wxString result = cf.AsString(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
421 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
422 }
423
424 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
425 {
426 if ([m_textView respondsToSelector:@selector(setFont:)])
427 [m_textView setFont: font.OSXGetNSFont()];
428 }
429
430
431 // wxNSTextFieldControl
432
433 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
434 {
435 m_textField = (NSTextField*) w;
436 [m_textField setDelegate: w];
437 m_selStart = m_selEnd = 0;
438 m_hasEditor = [w isKindOfClass:[NSTextField class]];
439 }
440
441 wxNSTextFieldControl::~wxNSTextFieldControl()
442 {
443 if (m_textField)
444 [m_textField setDelegate: nil];
445 }
446
447 wxString wxNSTextFieldControl::GetStringValue() const
448 {
449 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
450 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
451 }
452
453 void wxNSTextFieldControl::SetStringValue( const wxString &str)
454 {
455 wxMacEditHelper helper(m_textField);
456 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
457 }
458
459 void wxNSTextFieldControl::Copy()
460 {
461 NSText* editor = [m_textField currentEditor];
462 if ( editor )
463 {
464 [editor copy:nil];
465 }
466 }
467
468 void wxNSTextFieldControl::Cut()
469 {
470 NSText* editor = [m_textField currentEditor];
471 if ( editor )
472 {
473 [editor cut:nil];
474 }
475 }
476
477 void wxNSTextFieldControl::Paste()
478 {
479 NSText* editor = [m_textField currentEditor];
480 if ( editor )
481 {
482 [editor paste:nil];
483 }
484 }
485
486 bool wxNSTextFieldControl::CanPaste() const
487 {
488 return true;
489 }
490
491 void wxNSTextFieldControl::SetEditable(bool editable)
492 {
493 [m_textField setEditable:editable];
494 }
495
496 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
497 {
498 NSText* editor = [m_textField currentEditor];
499 if ( editor )
500 {
501 NSRange range = [editor selectedRange];
502 *from = range.location;
503 *to = range.location + range.length;
504 }
505 else
506 {
507 *from = m_selStart;
508 *to = m_selEnd;
509 }
510 }
511
512 void wxNSTextFieldControl::SetSelection( long from , long to )
513 {
514 long textLength = [[m_textField stringValue] length];
515 if ((from == -1) && (to == -1))
516 {
517 from = 0 ;
518 to = textLength ;
519 }
520 else
521 {
522 from = wxMin(textLength,wxMax(from,0)) ;
523 if ( to == -1 )
524 to = textLength;
525 else
526 to = wxMax(0,wxMin(textLength,to)) ;
527 }
528
529 NSText* editor = [m_textField currentEditor];
530 if ( editor )
531 {
532 [editor setSelectedRange:NSMakeRange(from, to-from)];
533 }
534 else
535 {
536 m_selStart = from;
537 m_selEnd = to;
538 }
539 }
540
541 void wxNSTextFieldControl::WriteText(const wxString& str)
542 {
543 NSText* editor = [m_textField currentEditor];
544 if ( editor )
545 {
546 wxMacEditHelper helper(m_textField);
547 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
548 }
549 else
550 {
551 wxString val = GetStringValue() ;
552 long start , end ;
553 GetSelection( &start , &end ) ;
554 val.Remove( start , end - start ) ;
555 val.insert( start , str ) ;
556 SetStringValue( val ) ;
557 SetSelection( start + str.length() , start + str.length() ) ;
558 }
559 }
560
561 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
562 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
563 {
564 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
565 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
566 {
567 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
568 event.SetEventObject( wxpeer );
569 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
570 wxpeer->HandleWindowEvent( event );
571 }
572 }
573
574 //
575 //
576 //
577
578 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
579 wxWindowMac* WXUNUSED(parent),
580 wxWindowID WXUNUSED(id),
581 const wxString& str,
582 const wxPoint& pos,
583 const wxSize& size,
584 long style,
585 long WXUNUSED(extraStyle))
586 {
587 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
588 wxWidgetCocoaImpl* c = NULL;
589
590 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
591 {
592 wxNSTextScrollView* v = nil;
593 v = [[wxNSTextScrollView alloc] initWithFrame:r];
594 c = new wxNSTextViewControl( wxpeer, v );
595 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
596 }
597 else
598 {
599 NSTextField* v = nil;
600 if ( style & wxTE_PASSWORD )
601 v = [[wxNSSecureTextField alloc] initWithFrame:r];
602 else
603 v = [[wxNSTextField alloc] initWithFrame:r];
604
605 if ( style & wxNO_BORDER )
606 {
607 // FIXME: How can we remove the native control's border?
608 // setBordered is separate from the text ctrl's border.
609 }
610
611 [v setBezeled:NO];
612 [v setBordered:NO];
613
614 c = new wxNSTextFieldControl( wxpeer, v );
615 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
616 }
617
618 return c;
619 }
620
621
622 #endif // wxUSE_TEXTCTRL