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