]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/osx/cocoa/textctrl.mm
Create an NSAutoreleasePool in wxMacWakeUp().
[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@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
265typedef 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
303wxNSTextViewControl::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
327wxNSTextViewControl::~wxNSTextViewControl()
328{
329 if (m_textView)
330 [m_textView setDelegate: nil];
331}
332
333wxString 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}
343void 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
353void wxNSTextViewControl::Copy()
354{
355 if (m_textView)
356 [m_textView copy:nil];
357
358}
359
360void wxNSTextViewControl::Cut()
361{
362 if (m_textView)
363 [m_textView cut:nil];
364}
365
366void wxNSTextViewControl::Paste()
367{
368 if (m_textView)
369 [m_textView paste:nil];
370}
371
372bool wxNSTextViewControl::CanPaste() const
373{
374 return true;
375}
376
377void wxNSTextViewControl::SetEditable(bool editable)
378{
379 if (m_textView)
380 [m_textView setEditable: editable];
381}
382
383void 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
393void 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
415void 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
426void 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
433// wxNSTextFieldControl
434
435wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
436{
437 m_textField = (NSTextField*) w;
438 [m_textField setDelegate: w];
439 m_selStart = m_selEnd = 0;
440 m_hasEditor = [w isKindOfClass:[NSTextField class]];
441}
442
443wxNSTextFieldControl::~wxNSTextFieldControl()
444{
445 if (m_textField)
446 [m_textField setDelegate: nil];
447}
448
449wxString wxNSTextFieldControl::GetStringValue() const
450{
451 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
452}
453
454void wxNSTextFieldControl::SetStringValue( const wxString &str)
455{
456 wxMacEditHelper helper(m_textField);
457 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
458}
459
460void wxNSTextFieldControl::Copy()
461{
462 NSText* editor = [m_textField currentEditor];
463 if ( editor )
464 {
465 [editor copy:nil];
466 }
467}
468
469void wxNSTextFieldControl::Cut()
470{
471 NSText* editor = [m_textField currentEditor];
472 if ( editor )
473 {
474 [editor cut:nil];
475 }
476}
477
478void wxNSTextFieldControl::Paste()
479{
480 NSText* editor = [m_textField currentEditor];
481 if ( editor )
482 {
483 [editor paste:nil];
484 }
485}
486
487bool wxNSTextFieldControl::CanPaste() const
488{
489 return true;
490}
491
492void wxNSTextFieldControl::SetEditable(bool editable)
493{
494 [m_textField setEditable:editable];
495}
496
497void wxNSTextFieldControl::GetSelection( long* from, long* to) const
498{
499 NSText* editor = [m_textField currentEditor];
500 if ( editor )
501 {
502 NSRange range = [editor selectedRange];
503 *from = range.location;
504 *to = range.location + range.length;
505 }
506 else
507 {
508 *from = m_selStart;
509 *to = m_selEnd;
510 }
511}
512
513void wxNSTextFieldControl::SetSelection( long from , long to )
514{
515 long textLength = [[m_textField stringValue] length];
516 if ((from == -1) && (to == -1))
517 {
518 from = 0 ;
519 to = textLength ;
520 }
521 else
522 {
523 from = wxMin(textLength,wxMax(from,0)) ;
524 if ( to == -1 )
525 to = textLength;
526 else
527 to = wxMax(0,wxMin(textLength,to)) ;
528 }
529
530 NSText* editor = [m_textField currentEditor];
531 if ( editor )
532 {
533 [editor setSelectedRange:NSMakeRange(from, to-from)];
534 }
535 else
536 {
537 m_selStart = from;
538 m_selEnd = to;
539 }
540}
541
542void wxNSTextFieldControl::WriteText(const wxString& str)
543{
544 NSEvent* formerEvent = m_lastKeyDownEvent;
545 m_lastKeyDownEvent = nil;
546 NSText* editor = [m_textField currentEditor];
547 if ( editor )
548 {
549 wxMacEditHelper helper(m_textField);
550 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
551 }
552 else
553 {
554 wxString val = GetStringValue() ;
555 long start , end ;
556 GetSelection( &start , &end ) ;
557 val.Remove( start , end - start ) ;
558 val.insert( start , str ) ;
559 SetStringValue( val ) ;
560 SetSelection( start + str.length() , start + str.length() ) ;
561 }
562 m_lastKeyDownEvent = formerEvent;
563}
564
565void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
566 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
567{
568 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
569 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
570 {
571 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
572 event.SetEventObject( wxpeer );
573 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
574 wxpeer->HandleWindowEvent( event );
575 }
576}
577
578//
579//
580//
581
582wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
583 wxWindowMac* WXUNUSED(parent),
584 wxWindowID WXUNUSED(id),
585 const wxString& WXUNUSED(str),
586 const wxPoint& pos,
587 const wxSize& size,
588 long style,
589 long WXUNUSED(extraStyle))
590{
591 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
592 wxWidgetCocoaImpl* c = NULL;
593
594 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
595 {
596 wxNSTextScrollView* v = nil;
597 v = [[wxNSTextScrollView alloc] initWithFrame:r];
598 c = new wxNSTextViewControl( wxpeer, v );
599 }
600 else
601 {
602 NSTextField* v = nil;
603 if ( style & wxTE_PASSWORD )
604 v = [[wxNSSecureTextField alloc] initWithFrame:r];
605 else
606 v = [[wxNSTextField alloc] initWithFrame:r];
607
608 if ( style & wxNO_BORDER )
609 {
610 // FIXME: How can we remove the native control's border?
611 // setBordered is separate from the text ctrl's border.
612 }
613
614 [v setBezeled:NO];
615 [v setBordered:NO];
616
617 c = new wxNSTextFieldControl( wxpeer, v );
618 }
619
620 return c;
621}
622
623
624#endif // wxUSE_TEXTCTRL