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