]>
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$ | |
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 | #include "wx/textcompleter.h" | |
49 | ||
50 | #include "wx/osx/private.h" | |
51 | #include "wx/osx/cocoa/private/textimpl.h" | |
52 | ||
53 | @interface NSView(EditableView) | |
54 | - (BOOL)isEditable; | |
55 | - (void)setEditable:(BOOL)flag; | |
56 | - (BOOL)isSelectable; | |
57 | - (void)setSelectable:(BOOL)flag; | |
58 | @end | |
59 | ||
60 | class wxMacEditHelper | |
61 | { | |
62 | public : | |
63 | wxMacEditHelper( NSView* textView ) | |
64 | { | |
65 | m_textView = textView; | |
66 | m_formerEditable = YES; | |
67 | if ( textView ) | |
68 | { | |
69 | m_formerEditable = [textView isEditable]; | |
70 | m_formerSelectable = [textView isSelectable]; | |
71 | [textView setEditable:YES]; | |
72 | } | |
73 | } | |
74 | ||
75 | ~wxMacEditHelper() | |
76 | { | |
77 | if ( m_textView ) | |
78 | { | |
79 | [m_textView setEditable:m_formerEditable]; | |
80 | [m_textView setSelectable:m_formerSelectable]; | |
81 | } | |
82 | } | |
83 | ||
84 | protected : | |
85 | BOOL m_formerEditable ; | |
86 | BOOL m_formerSelectable; | |
87 | NSView* m_textView; | |
88 | } ; | |
89 | ||
90 | @implementation wxNSSecureTextField | |
91 | ||
92 | + (void)initialize | |
93 | { | |
94 | static BOOL initialized = NO; | |
95 | if (!initialized) | |
96 | { | |
97 | initialized = YES; | |
98 | wxOSXCocoaClassAddWXMethods( self ); | |
99 | } | |
100 | } | |
101 | ||
102 | - (void)controlTextDidChange:(NSNotification *)aNotification | |
103 | { | |
104 | wxUnusedVar(aNotification); | |
105 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
106 | if ( impl ) | |
107 | impl->controlTextDidChange(); | |
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 | - (void)textDidChange:(NSNotification *)aNotification | |
196 | { | |
197 | wxUnusedVar(aNotification); | |
198 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
199 | if ( impl ) | |
200 | impl->controlTextDidChange(); | |
201 | } | |
202 | ||
203 | - (void) setEnabled:(BOOL) flag | |
204 | { | |
205 | // from Technical Q&A QA1461 | |
206 | if (flag) { | |
207 | [self setTextColor: [NSColor controlTextColor]]; | |
208 | ||
209 | } else { | |
210 | [self setTextColor: [NSColor disabledControlTextColor]]; | |
211 | } | |
212 | ||
213 | [self setSelectable: flag]; | |
214 | [self setEditable: flag]; | |
215 | } | |
216 | ||
217 | - (BOOL) isEnabled | |
218 | { | |
219 | return [self isEditable]; | |
220 | } | |
221 | ||
222 | - (void)textDidEndEditing:(NSNotification *)aNotification | |
223 | { | |
224 | wxUnusedVar(aNotification); | |
225 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
226 | if ( impl ) | |
227 | { | |
228 | impl->DoNotifyFocusEvent( false, NULL ); | |
229 | } | |
230 | } | |
231 | ||
232 | @end | |
233 | ||
234 | @implementation wxNSTextField | |
235 | ||
236 | + (void)initialize | |
237 | { | |
238 | static BOOL initialized = NO; | |
239 | if (!initialized) | |
240 | { | |
241 | initialized = YES; | |
242 | wxOSXCocoaClassAddWXMethods( self ); | |
243 | } | |
244 | } | |
245 | ||
246 | - (id) initWithFrame:(NSRect) frame | |
247 | { | |
248 | self = [super initWithFrame:frame]; | |
249 | fieldEditor = nil; | |
250 | return self; | |
251 | } | |
252 | ||
253 | - (void) dealloc | |
254 | { | |
255 | [fieldEditor release]; | |
256 | [super dealloc]; | |
257 | } | |
258 | ||
259 | - (void) setFieldEditor:(wxNSTextFieldEditor*) editor | |
260 | { | |
261 | if ( editor != fieldEditor ) | |
262 | { | |
263 | [editor retain]; | |
264 | [fieldEditor release]; | |
265 | fieldEditor = editor; | |
266 | } | |
267 | } | |
268 | ||
269 | - (wxNSTextFieldEditor*) fieldEditor | |
270 | { | |
271 | return fieldEditor; | |
272 | } | |
273 | ||
274 | - (void) setEnabled:(BOOL) flag | |
275 | { | |
276 | [super setEnabled: flag]; | |
277 | ||
278 | if (![self drawsBackground]) { | |
279 | // Static text is drawn incorrectly when disabled. | |
280 | // For an explanation, see | |
281 | // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028 | |
282 | if (flag) { | |
283 | [self setTextColor: [NSColor controlTextColor]]; | |
284 | } else { | |
285 | [self setTextColor: [NSColor secondarySelectedControlColor]]; | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
290 | - (void)controlTextDidChange:(NSNotification *)aNotification | |
291 | { | |
292 | wxUnusedVar(aNotification); | |
293 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
294 | if ( impl ) | |
295 | impl->controlTextDidChange(); | |
296 | } | |
297 | ||
298 | - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words | |
299 | forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int*)index | |
300 | { | |
301 | NSMutableArray* matches = NULL; | |
302 | ||
303 | wxTextWidgetImpl* impl = (wxNSTextFieldControl * ) wxWidgetImpl::FindFromWXWidget( self ); | |
304 | wxTextEntry * const entry = impl->GetTextEntry(); | |
305 | wxTextCompleter * const completer = entry->OSXGetCompleter(); | |
306 | if ( completer ) | |
307 | { | |
308 | const wxString prefix = entry->GetValue(); | |
309 | if ( completer->Start(prefix) ) | |
310 | { | |
311 | const wxString | |
312 | wordStart = wxCFStringRef::AsString( | |
313 | [[textView string] substringWithRange:charRange] | |
314 | ); | |
315 | ||
316 | matches = [NSMutableArray array]; | |
317 | for ( ;; ) | |
318 | { | |
319 | const wxString s = completer->GetNext(); | |
320 | if ( s.empty() ) | |
321 | break; | |
322 | ||
323 | // Normally the completer should return only the strings | |
324 | // starting with the prefix, but there could be exceptions | |
325 | // and, for compatibility with MSW which simply ignores all | |
326 | // entries that don't match the current text control contents, | |
327 | // we ignore them as well. Besides, our own wxTextCompleterFixed | |
328 | // doesn't respect this rule and, moreover, we need to extract | |
329 | // just the rest of the string anyhow. | |
330 | wxString completion; | |
331 | if ( s.StartsWith(prefix, &completion) ) | |
332 | { | |
333 | // We discarded the entire prefix above but actually we | |
334 | // should include the part of it that consists of the | |
335 | // beginning of the current word, otherwise it would be | |
336 | // lost when completion is accepted as OS X supposes that | |
337 | // our matches do start with the "partial word range" | |
338 | // passed to us. | |
339 | const wxCFStringRef fullWord(wordStart + completion); | |
340 | [matches addObject: fullWord.AsNSString()]; | |
341 | } | |
342 | } | |
343 | } | |
344 | } | |
345 | ||
346 | return matches; | |
347 | } | |
348 | ||
349 | - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector | |
350 | { | |
351 | wxUnusedVar(textView); | |
352 | wxUnusedVar(control); | |
353 | ||
354 | BOOL handled = NO; | |
355 | ||
356 | // send back key events wx' common code knows how to handle | |
357 | ||
358 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
359 | if ( impl ) | |
360 | { | |
361 | wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); | |
362 | if ( wxpeer ) | |
363 | { | |
364 | if (commandSelector == @selector(insertNewline:)) | |
365 | { | |
366 | [textView insertNewlineIgnoringFieldEditor:self]; | |
367 | handled = YES; | |
368 | } | |
369 | else if ( commandSelector == @selector(insertTab:)) | |
370 | { | |
371 | [textView insertTabIgnoringFieldEditor:self]; | |
372 | handled = YES; | |
373 | } | |
374 | else if ( commandSelector == @selector(insertBacktab:)) | |
375 | { | |
376 | [textView insertTabIgnoringFieldEditor:self]; | |
377 | handled = YES; | |
378 | } | |
379 | } | |
380 | } | |
381 | ||
382 | return handled; | |
383 | } | |
384 | ||
385 | - (void)controlTextDidEndEditing:(NSNotification *)aNotification | |
386 | { | |
387 | wxUnusedVar(aNotification); | |
388 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
389 | if ( impl ) | |
390 | { | |
391 | impl->DoNotifyFocusEvent( false, NULL ); | |
392 | } | |
393 | } | |
394 | @end | |
395 | ||
396 | // wxNSTextViewControl | |
397 | ||
398 | wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) | |
399 | : wxWidgetCocoaImpl(wxPeer, w), | |
400 | wxTextWidgetImpl(wxPeer) | |
401 | { | |
402 | wxNSTextScrollView* sv = (wxNSTextScrollView*) w; | |
403 | m_scrollView = sv; | |
404 | ||
405 | [m_scrollView setHasVerticalScroller:YES]; | |
406 | [m_scrollView setHasHorizontalScroller:NO]; | |
407 | [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; | |
408 | NSSize contentSize = [m_scrollView contentSize]; | |
409 | ||
410 | wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0, | |
411 | contentSize.width, contentSize.height)]; | |
412 | m_textView = tv; | |
413 | [tv setVerticallyResizable:YES]; | |
414 | [tv setHorizontallyResizable:NO]; | |
415 | [tv setAutoresizingMask:NSViewWidthSizable]; | |
416 | ||
417 | [m_scrollView setDocumentView: tv]; | |
418 | ||
419 | [tv setDelegate: tv]; | |
420 | ||
421 | InstallEventHandler(tv); | |
422 | } | |
423 | ||
424 | wxNSTextViewControl::~wxNSTextViewControl() | |
425 | { | |
426 | if (m_textView) | |
427 | [m_textView setDelegate: nil]; | |
428 | } | |
429 | ||
430 | bool wxNSTextViewControl::CanFocus() const | |
431 | { | |
432 | // since this doesn't work (return false), we hardcode | |
433 | // if (m_textView) | |
434 | // return [m_textView canBecomeKeyView]; | |
435 | return true; | |
436 | } | |
437 | ||
438 | wxString wxNSTextViewControl::GetStringValue() const | |
439 | { | |
440 | if (m_textView) | |
441 | { | |
442 | wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding()); | |
443 | wxMacConvertNewlines13To10( &result ) ; | |
444 | return result; | |
445 | } | |
446 | return wxEmptyString; | |
447 | } | |
448 | void wxNSTextViewControl::SetStringValue( const wxString &str) | |
449 | { | |
450 | wxString st = str; | |
451 | wxMacConvertNewlines10To13( &st ); | |
452 | wxMacEditHelper helper(m_textView); | |
453 | ||
454 | if (m_textView) | |
455 | [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; | |
456 | } | |
457 | ||
458 | void wxNSTextViewControl::Copy() | |
459 | { | |
460 | if (m_textView) | |
461 | [m_textView copy:nil]; | |
462 | ||
463 | } | |
464 | ||
465 | void wxNSTextViewControl::Cut() | |
466 | { | |
467 | if (m_textView) | |
468 | [m_textView cut:nil]; | |
469 | } | |
470 | ||
471 | void wxNSTextViewControl::Paste() | |
472 | { | |
473 | if (m_textView) | |
474 | [m_textView paste:nil]; | |
475 | } | |
476 | ||
477 | bool wxNSTextViewControl::CanPaste() const | |
478 | { | |
479 | return true; | |
480 | } | |
481 | ||
482 | void wxNSTextViewControl::SetEditable(bool editable) | |
483 | { | |
484 | if (m_textView) | |
485 | [m_textView setEditable: editable]; | |
486 | } | |
487 | ||
488 | void wxNSTextViewControl::GetSelection( long* from, long* to) const | |
489 | { | |
490 | if (m_textView) | |
491 | { | |
492 | NSRange range = [m_textView selectedRange]; | |
493 | *from = range.location; | |
494 | *to = range.location + range.length; | |
495 | } | |
496 | } | |
497 | ||
498 | void wxNSTextViewControl::SetSelection( long from , long to ) | |
499 | { | |
500 | long textLength = [[m_textView string] length]; | |
501 | if ((from == -1) && (to == -1)) | |
502 | { | |
503 | from = 0 ; | |
504 | to = textLength ; | |
505 | } | |
506 | else | |
507 | { | |
508 | from = wxMin(textLength,wxMax(from,0)) ; | |
509 | if ( to == -1 ) | |
510 | to = textLength; | |
511 | else | |
512 | to = wxMax(0,wxMin(textLength,to)) ; | |
513 | } | |
514 | ||
515 | NSRange selrange = NSMakeRange(from, to-from); | |
516 | [m_textView setSelectedRange:selrange]; | |
517 | [m_textView scrollRangeToVisible:selrange]; | |
518 | } | |
519 | ||
520 | void wxNSTextViewControl::WriteText(const wxString& str) | |
521 | { | |
522 | wxString st = str; | |
523 | wxMacConvertNewlines10To13( &st ); | |
524 | wxMacEditHelper helper(m_textView); | |
525 | NSEvent* formerEvent = m_lastKeyDownEvent; | |
526 | m_lastKeyDownEvent = nil; | |
527 | [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; | |
528 | m_lastKeyDownEvent = formerEvent; | |
529 | } | |
530 | ||
531 | void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) ) | |
532 | { | |
533 | if ([m_textView respondsToSelector:@selector(setFont:)]) | |
534 | [m_textView setFont: font.OSXGetNSFont()]; | |
535 | } | |
536 | ||
537 | bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style) | |
538 | { | |
539 | if (m_textView && position >=0) | |
540 | { | |
541 | NSFont* font = NULL; | |
542 | NSColor* bgcolor = NULL; | |
543 | NSColor* fgcolor = NULL; | |
544 | // NOTE: It appears that other platforms accept GetStyle with the position == length | |
545 | // but that NSTextStorage does not accept length as a valid position. | |
546 | // Therefore we return the default control style in that case. | |
547 | if (position < (long) [[m_textView string] length]) | |
548 | { | |
549 | NSTextStorage* storage = [m_textView textStorage]; | |
550 | font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease]; | |
551 | bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease]; | |
552 | fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease]; | |
553 | } | |
554 | else | |
555 | { | |
556 | NSDictionary* attrs = [m_textView typingAttributes]; | |
557 | font = [[attrs objectForKey:NSFontAttributeName] autorelease]; | |
558 | bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease]; | |
559 | fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease]; | |
560 | } | |
561 | ||
562 | if (font) | |
563 | style.SetFont(wxFont(font)); | |
564 | ||
565 | if (bgcolor) | |
566 | style.SetBackgroundColour(wxColour(bgcolor)); | |
567 | ||
568 | if (fgcolor) | |
569 | style.SetTextColour(wxColour(fgcolor)); | |
570 | return true; | |
571 | } | |
572 | ||
573 | return false; | |
574 | } | |
575 | ||
576 | void wxNSTextViewControl::SetStyle(long start, | |
577 | long end, | |
578 | const wxTextAttr& style) | |
579 | { | |
580 | if (m_textView) { | |
581 | NSRange range = NSMakeRange(start, end-start); | |
582 | if (start == -1 && end == -1) | |
583 | range = [m_textView selectedRange]; | |
584 | ||
585 | NSTextStorage* storage = [m_textView textStorage]; | |
586 | ||
587 | wxFont font = style.GetFont(); | |
588 | if (style.HasFont() && font.IsOk()) | |
589 | [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range]; | |
590 | ||
591 | wxColour bgcolor = style.GetBackgroundColour(); | |
592 | if (style.HasBackgroundColour() && bgcolor.IsOk()) | |
593 | [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range]; | |
594 | ||
595 | wxColour fgcolor = style.GetTextColour(); | |
596 | if (style.HasTextColour() && fgcolor.IsOk()) | |
597 | [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range]; | |
598 | } | |
599 | } | |
600 | ||
601 | void wxNSTextViewControl::CheckSpelling(bool check) | |
602 | { | |
603 | if (m_textView) | |
604 | [m_textView setContinuousSpellCheckingEnabled: check]; | |
605 | } | |
606 | ||
607 | wxSize wxNSTextViewControl::GetBestSize() const | |
608 | { | |
609 | if (m_textView && [m_textView layoutManager]) | |
610 | { | |
611 | NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]]; | |
612 | return wxSize((int)(rect.size.width + [m_textView textContainerInset].width), | |
613 | (int)(rect.size.height + [m_textView textContainerInset].height)); | |
614 | } | |
615 | return wxSize(0,0); | |
616 | } | |
617 | ||
618 | // wxNSTextFieldControl | |
619 | ||
620 | wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w ) | |
621 | : wxWidgetCocoaImpl(text, w), | |
622 | wxTextWidgetImpl(text) | |
623 | { | |
624 | Init(w); | |
625 | } | |
626 | ||
627 | wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer, | |
628 | wxTextEntry *entry, | |
629 | WXWidget w) | |
630 | : wxWidgetCocoaImpl(wxPeer, w), | |
631 | wxTextWidgetImpl(entry) | |
632 | { | |
633 | Init(w); | |
634 | } | |
635 | ||
636 | void wxNSTextFieldControl::Init(WXWidget w) | |
637 | { | |
638 | NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>)*) w; | |
639 | m_textField = tf; | |
640 | [m_textField setDelegate: tf]; | |
641 | m_selStart = m_selEnd = 0; | |
642 | m_hasEditor = [w isKindOfClass:[NSTextField class]]; | |
643 | } | |
644 | ||
645 | wxNSTextFieldControl::~wxNSTextFieldControl() | |
646 | { | |
647 | if (m_textField) | |
648 | [m_textField setDelegate: nil]; | |
649 | } | |
650 | ||
651 | wxString wxNSTextFieldControl::GetStringValue() const | |
652 | { | |
653 | return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding()); | |
654 | } | |
655 | ||
656 | void wxNSTextFieldControl::SetStringValue( const wxString &str) | |
657 | { | |
658 | wxMacEditHelper helper(m_textField); | |
659 | [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; | |
660 | } | |
661 | ||
662 | void wxNSTextFieldControl::Copy() | |
663 | { | |
664 | NSText* editor = [m_textField currentEditor]; | |
665 | if ( editor ) | |
666 | { | |
667 | [editor copy:nil]; | |
668 | } | |
669 | } | |
670 | ||
671 | void wxNSTextFieldControl::Cut() | |
672 | { | |
673 | NSText* editor = [m_textField currentEditor]; | |
674 | if ( editor ) | |
675 | { | |
676 | [editor cut:nil]; | |
677 | } | |
678 | } | |
679 | ||
680 | void wxNSTextFieldControl::Paste() | |
681 | { | |
682 | NSText* editor = [m_textField currentEditor]; | |
683 | if ( editor ) | |
684 | { | |
685 | [editor paste:nil]; | |
686 | } | |
687 | } | |
688 | ||
689 | bool wxNSTextFieldControl::CanPaste() const | |
690 | { | |
691 | return true; | |
692 | } | |
693 | ||
694 | void wxNSTextFieldControl::SetEditable(bool editable) | |
695 | { | |
696 | [m_textField setEditable:editable]; | |
697 | } | |
698 | ||
699 | void wxNSTextFieldControl::GetSelection( long* from, long* to) const | |
700 | { | |
701 | NSText* editor = [m_textField currentEditor]; | |
702 | if ( editor ) | |
703 | { | |
704 | NSRange range = [editor selectedRange]; | |
705 | *from = range.location; | |
706 | *to = range.location + range.length; | |
707 | } | |
708 | else | |
709 | { | |
710 | *from = m_selStart; | |
711 | *to = m_selEnd; | |
712 | } | |
713 | } | |
714 | ||
715 | void wxNSTextFieldControl::SetSelection( long from , long to ) | |
716 | { | |
717 | long textLength = [[m_textField stringValue] length]; | |
718 | if ((from == -1) && (to == -1)) | |
719 | { | |
720 | from = 0 ; | |
721 | to = textLength ; | |
722 | } | |
723 | else | |
724 | { | |
725 | from = wxMin(textLength,wxMax(from,0)) ; | |
726 | if ( to == -1 ) | |
727 | to = textLength; | |
728 | else | |
729 | to = wxMax(0,wxMin(textLength,to)) ; | |
730 | } | |
731 | ||
732 | NSText* editor = [m_textField currentEditor]; | |
733 | if ( editor ) | |
734 | { | |
735 | [editor setSelectedRange:NSMakeRange(from, to-from)]; | |
736 | } | |
737 | else | |
738 | { | |
739 | m_selStart = from; | |
740 | m_selEnd = to; | |
741 | } | |
742 | } | |
743 | ||
744 | void wxNSTextFieldControl::WriteText(const wxString& str) | |
745 | { | |
746 | NSEvent* formerEvent = m_lastKeyDownEvent; | |
747 | m_lastKeyDownEvent = nil; | |
748 | NSText* editor = [m_textField currentEditor]; | |
749 | if ( editor ) | |
750 | { | |
751 | wxMacEditHelper helper(m_textField); | |
752 | [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; | |
753 | } | |
754 | else | |
755 | { | |
756 | wxString val = GetStringValue() ; | |
757 | long start , end ; | |
758 | GetSelection( &start , &end ) ; | |
759 | val.Remove( start , end - start ) ; | |
760 | val.insert( start , str ) ; | |
761 | SetStringValue( val ) ; | |
762 | SetSelection( start + str.length() , start + str.length() ) ; | |
763 | } | |
764 | m_lastKeyDownEvent = formerEvent; | |
765 | } | |
766 | ||
767 | void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf), | |
768 | void* WXUNUSED(_cmd), void *WXUNUSED(sender)) | |
769 | { | |
770 | wxWindow* wxpeer = (wxWindow*) GetWXPeer(); | |
771 | if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) ) | |
772 | { | |
773 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId()); | |
774 | event.SetEventObject( wxpeer ); | |
775 | event.SetString( GetTextEntry()->GetValue() ); | |
776 | wxpeer->HandleWindowEvent( event ); | |
777 | } | |
778 | } | |
779 | ||
780 | bool wxNSTextFieldControl::SetHint(const wxString& hint) | |
781 | { | |
782 | wxCFStringRef hintstring(hint); | |
783 | [[m_textField cell] setPlaceholderString:hintstring.AsNSString()]; | |
784 | return true; | |
785 | } | |
786 | ||
787 | // | |
788 | // | |
789 | // | |
790 | ||
791 | wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer, | |
792 | wxWindowMac* WXUNUSED(parent), | |
793 | wxWindowID WXUNUSED(id), | |
794 | const wxString& WXUNUSED(str), | |
795 | const wxPoint& pos, | |
796 | const wxSize& size, | |
797 | long style, | |
798 | long WXUNUSED(extraStyle)) | |
799 | { | |
800 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; | |
801 | wxWidgetCocoaImpl* c = NULL; | |
802 | ||
803 | if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 ) | |
804 | { | |
805 | wxNSTextScrollView* v = nil; | |
806 | v = [[wxNSTextScrollView alloc] initWithFrame:r]; | |
807 | c = new wxNSTextViewControl( wxpeer, v ); | |
808 | } | |
809 | else | |
810 | { | |
811 | NSTextField* v = nil; | |
812 | if ( style & wxTE_PASSWORD ) | |
813 | v = [[wxNSSecureTextField alloc] initWithFrame:r]; | |
814 | else | |
815 | v = [[wxNSTextField alloc] initWithFrame:r]; | |
816 | ||
817 | if ( style & wxNO_BORDER ) | |
818 | { | |
819 | // FIXME: How can we remove the native control's border? | |
820 | // setBordered is separate from the text ctrl's border. | |
821 | } | |
822 | ||
823 | NSTextFieldCell* cell = [v cell]; | |
824 | [cell setScrollable:YES]; | |
825 | // TODO: Remove if we definitely are sure, it's not needed | |
826 | // as setting scrolling to yes, should turn off any wrapping | |
827 | // [cell setLineBreakMode:NSLineBreakByClipping]; | |
828 | ||
829 | [v setBezeled:NO]; | |
830 | [v setBordered:NO]; | |
831 | ||
832 | c = new wxNSTextFieldControl( wxpeer, wxpeer, v ); | |
833 | } | |
834 | c->SetNeedsFocusRect( true ); | |
835 | ||
836 | return c; | |
837 | } | |
838 | ||
839 | ||
840 | #endif // wxUSE_TEXTCTRL |