]>
Commit | Line | Data |
---|---|---|
dbeddfb9 SC |
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 | |
dbeddfb9 SC |
7 | // Copyright: (c) Stefan Csomor |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #if wxUSE_TEXTCTRL | |
14 | ||
15 | #include "wx/textctrl.h" | |
16 | ||
17 | #ifndef WX_PRECOMP | |
18 | #include "wx/intl.h" | |
19 | #include "wx/app.h" | |
20 | #include "wx/utils.h" | |
21 | #include "wx/dc.h" | |
22 | #include "wx/button.h" | |
23 | #include "wx/menu.h" | |
24 | #include "wx/settings.h" | |
25 | #include "wx/msgdlg.h" | |
26 | #include "wx/toplevel.h" | |
27 | #endif | |
28 | ||
29 | #ifdef __DARWIN__ | |
30 | #include <sys/types.h> | |
31 | #include <sys/stat.h> | |
32 | #else | |
33 | #include <stat.h> | |
34 | #endif | |
35 | ||
36 | #if wxUSE_STD_IOSTREAM | |
37 | #if wxUSE_IOSTREAMH | |
38 | #include <fstream.h> | |
39 | #else | |
40 | #include <fstream> | |
41 | #endif | |
42 | #endif | |
43 | ||
44 | #include "wx/filefn.h" | |
45 | #include "wx/sysopt.h" | |
46 | #include "wx/thread.h" | |
c729f16f | 47 | #include "wx/textcompleter.h" |
dbeddfb9 SC |
48 | |
49 | #include "wx/osx/private.h" | |
1e181c7a | 50 | #include "wx/osx/cocoa/private/textimpl.h" |
dbeddfb9 | 51 | |
03647350 | 52 | @interface NSView(EditableView) |
5f65ba36 SC |
53 | - (BOOL)isEditable; |
54 | - (void)setEditable:(BOOL)flag; | |
816b2941 SC |
55 | - (BOOL)isSelectable; |
56 | - (void)setSelectable:(BOOL)flag; | |
5f65ba36 SC |
57 | @end |
58 | ||
66fc8b0e VZ |
59 | // An object of this class is created before the text is modified |
60 | // programmatically and destroyed as soon as this is done. It does several | |
61 | // things, like ensuring that the control is editable to allow setting its text | |
62 | // at all and eating any unwanted focus loss events from textDidEndEditing: | |
63 | // which don't really correspond to focus change. | |
5f65ba36 SC |
64 | class wxMacEditHelper |
65 | { | |
66 | public : | |
67 | wxMacEditHelper( NSView* textView ) | |
68 | { | |
66fc8b0e VZ |
69 | m_viewPreviouslyEdited = ms_viewCurrentlyEdited; |
70 | ms_viewCurrentlyEdited = | |
63bcc669 | 71 | m_textView = textView; |
816b2941 | 72 | m_formerEditable = YES; |
5f65ba36 SC |
73 | if ( textView ) |
74 | { | |
816b2941 SC |
75 | m_formerEditable = [textView isEditable]; |
76 | m_formerSelectable = [textView isSelectable]; | |
5f65ba36 SC |
77 | [textView setEditable:YES]; |
78 | } | |
79 | } | |
80 | ||
81 | ~wxMacEditHelper() | |
82 | { | |
83 | if ( m_textView ) | |
816b2941 SC |
84 | { |
85 | [m_textView setEditable:m_formerEditable]; | |
86 | [m_textView setSelectable:m_formerSelectable]; | |
87 | } | |
66fc8b0e VZ |
88 | |
89 | ms_viewCurrentlyEdited = m_viewPreviouslyEdited; | |
5f65ba36 SC |
90 | } |
91 | ||
66fc8b0e VZ |
92 | // Returns the last view we were instantiated for or NULL. |
93 | static NSView *GetCurrentlyEditedView() { return ms_viewCurrentlyEdited; } | |
94 | ||
5f65ba36 | 95 | protected : |
816b2941 SC |
96 | BOOL m_formerEditable ; |
97 | BOOL m_formerSelectable; | |
5f65ba36 | 98 | NSView* m_textView; |
66fc8b0e VZ |
99 | |
100 | // The original value of ms_viewCurrentlyEdited when this object was | |
101 | // created. | |
102 | NSView* m_viewPreviouslyEdited; | |
103 | ||
104 | static NSView* ms_viewCurrentlyEdited; | |
5f65ba36 SC |
105 | } ; |
106 | ||
66fc8b0e VZ |
107 | NSView* wxMacEditHelper::ms_viewCurrentlyEdited = nil; |
108 | ||
9c71750d SC |
109 | // a minimal NSFormatter that just avoids getting too long entries |
110 | @interface wxMaximumLengthFormatter : NSFormatter | |
111 | { | |
112 | int maxLength; | |
053f5a55 | 113 | wxTextEntry* field; |
9c71750d SC |
114 | } |
115 | ||
116 | @end | |
117 | ||
118 | @implementation wxMaximumLengthFormatter | |
119 | ||
120 | - (id)init | |
121 | { | |
6e884381 | 122 | self = [super init]; |
9c71750d SC |
123 | maxLength = 0; |
124 | return self; | |
125 | } | |
126 | ||
127 | - (void) setMaxLength:(int) maxlen | |
128 | { | |
129 | maxLength = maxlen; | |
130 | } | |
131 | ||
132 | - (NSString *)stringForObjectValue:(id)anObject | |
133 | { | |
134 | if(![anObject isKindOfClass:[NSString class]]) | |
135 | return nil; | |
136 | return [NSString stringWithString:anObject]; | |
137 | } | |
138 | ||
139 | - (BOOL)getObjectValue:(id *)obj forString:(NSString *)string errorDescription:(NSString **)error | |
140 | { | |
141 | *obj = [NSString stringWithString:string]; | |
142 | return YES; | |
143 | } | |
144 | ||
145 | - (BOOL)isPartialStringValid:(NSString **)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr | |
146 | originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error | |
147 | { | |
148 | int len = [*partialStringPtr length]; | |
149 | if ( maxLength > 0 && len > maxLength ) | |
150 | { | |
053f5a55 | 151 | field->SendMaxLenEvent(); |
9c71750d SC |
152 | return NO; |
153 | } | |
154 | return YES; | |
155 | } | |
156 | ||
053f5a55 SC |
157 | - (void) setTextEntry:(wxTextEntry*) tf |
158 | { | |
159 | field = tf; | |
160 | } | |
161 | ||
9c71750d SC |
162 | @end |
163 | ||
03647350 | 164 | @implementation wxNSSecureTextField |
dbeddfb9 | 165 | |
4dd9fdf8 SC |
166 | + (void)initialize |
167 | { | |
168 | static BOOL initialized = NO; | |
03647350 | 169 | if (!initialized) |
4dd9fdf8 SC |
170 | { |
171 | initialized = YES; | |
172 | wxOSXCocoaClassAddWXMethods( self ); | |
173 | } | |
174 | } | |
dbeddfb9 | 175 | |
4d23a0d3 KO |
176 | - (void)controlTextDidChange:(NSNotification *)aNotification |
177 | { | |
6331c8c0 SC |
178 | wxUnusedVar(aNotification); |
179 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
4d23a0d3 | 180 | if ( impl ) |
75a2c6a1 | 181 | impl->controlTextDidChange(); |
4d23a0d3 KO |
182 | } |
183 | ||
6d109846 SC |
184 | - (void)controlTextDidEndEditing:(NSNotification *)aNotification |
185 | { | |
186 | wxUnusedVar(aNotification); | |
187 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
188 | if ( impl ) | |
189 | { | |
60d66be3 | 190 | NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder(); |
e17d84e1 | 191 | NSView* otherView = wxOSXGetViewFromResponder(responder); |
60d66be3 | 192 | |
e17d84e1 | 193 | wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView); |
60d66be3 | 194 | impl->DoNotifyFocusEvent( false, otherWindow ); |
6d109846 SC |
195 | } |
196 | } | |
197 | ||
a2ce6469 SC |
198 | - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector |
199 | { | |
200 | wxUnusedVar(textView); | |
201 | ||
202 | BOOL handled = NO; | |
203 | ||
204 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( control ); | |
205 | if ( impl ) | |
206 | { | |
207 | wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); | |
208 | if ( wxpeer ) | |
209 | { | |
210 | if (commandSelector == @selector(insertNewline:)) | |
211 | { | |
212 | wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(wxpeer), wxTopLevelWindow); | |
213 | if ( tlw && tlw->GetDefaultItem() ) | |
214 | { | |
215 | wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); | |
216 | if ( def && def->IsEnabled() ) | |
217 | { | |
ce7fe42e | 218 | wxCommandEvent event(wxEVT_BUTTON, def->GetId() ); |
a2ce6469 SC |
219 | event.SetEventObject(def); |
220 | def->Command(event); | |
221 | handled = YES; | |
222 | } | |
223 | } | |
224 | } | |
225 | } | |
226 | } | |
227 | ||
228 | return handled; | |
229 | } | |
230 | ||
e32090ba SC |
231 | @end |
232 | ||
1087c6c1 | 233 | @interface wxNSTextScrollView : NSScrollView |
4d23a0d3 | 234 | { |
4d23a0d3 | 235 | } |
1087c6c1 SC |
236 | @end |
237 | ||
1087c6c1 | 238 | @implementation wxNSTextScrollView |
c824c165 KO |
239 | |
240 | + (void)initialize | |
241 | { | |
242 | static BOOL initialized = NO; | |
03647350 VZ |
243 | if (!initialized) |
244 | { | |
c824c165 KO |
245 | initialized = YES; |
246 | wxOSXCocoaClassAddWXMethods( self ); | |
247 | } | |
248 | } | |
249 | ||
7cb2a241 SC |
250 | @end |
251 | ||
252 | @implementation wxNSTextFieldEditor | |
253 | ||
254 | - (void) keyDown:(NSEvent*) event | |
4d23a0d3 | 255 | { |
9e55f38d | 256 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 SC |
257 | lastKeyDownEvent = event; |
258 | if ( impl == NULL || !impl->DoHandleKeyEvent(event) ) | |
259 | [super keyDown:event]; | |
260 | lastKeyDownEvent = nil; | |
4d23a0d3 | 261 | } |
533e2ae1 | 262 | |
7cb2a241 | 263 | - (void) keyUp:(NSEvent*) event |
533e2ae1 | 264 | { |
9e55f38d | 265 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 SC |
266 | if ( impl == NULL || !impl->DoHandleKeyEvent(event) ) |
267 | [super keyUp:event]; | |
533e2ae1 | 268 | } |
1087c6c1 | 269 | |
7cb2a241 | 270 | - (void) flagsChanged:(NSEvent*) event |
1087c6c1 | 271 | { |
9e55f38d | 272 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 SC |
273 | if ( impl == NULL || !impl->DoHandleKeyEvent(event) ) |
274 | [super flagsChanged:event]; | |
1087c6c1 | 275 | } |
1087c6c1 | 276 | |
7cb2a241 SC |
277 | - (BOOL) performKeyEquivalent:(NSEvent*) event |
278 | { | |
279 | BOOL retval = [super performKeyEquivalent:event]; | |
280 | return retval; | |
281 | } | |
1087c6c1 | 282 | |
7cb2a241 | 283 | - (void) insertText:(id) str |
1087c6c1 | 284 | { |
9e55f38d | 285 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 | 286 | if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) ) |
1087c6c1 | 287 | { |
7cb2a241 | 288 | [super insertText:str]; |
1087c6c1 | 289 | } |
1087c6c1 SC |
290 | } |
291 | ||
7cb2a241 SC |
292 | @end |
293 | ||
294 | @implementation wxNSTextView | |
1087c6c1 | 295 | |
7cb2a241 | 296 | + (void)initialize |
1087c6c1 | 297 | { |
7cb2a241 | 298 | static BOOL initialized = NO; |
03647350 | 299 | if (!initialized) |
7cb2a241 SC |
300 | { |
301 | initialized = YES; | |
302 | wxOSXCocoaClassAddWXMethods( self ); | |
303 | } | |
1087c6c1 SC |
304 | } |
305 | ||
b9cf2753 KO |
306 | - (void)textDidChange:(NSNotification *)aNotification |
307 | { | |
75a2c6a1 KO |
308 | wxUnusedVar(aNotification); |
309 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
b9cf2753 | 310 | if ( impl ) |
75a2c6a1 | 311 | impl->controlTextDidChange(); |
b9cf2753 KO |
312 | } |
313 | ||
816b2941 SC |
314 | - (void) setEnabled:(BOOL) flag |
315 | { | |
316 | // from Technical Q&A QA1461 | |
317 | if (flag) { | |
318 | [self setTextColor: [NSColor controlTextColor]]; | |
319 | ||
320 | } else { | |
321 | [self setTextColor: [NSColor disabledControlTextColor]]; | |
322 | } | |
323 | ||
324 | [self setSelectable: flag]; | |
325 | [self setEditable: flag]; | |
326 | } | |
327 | ||
328 | - (BOOL) isEnabled | |
329 | { | |
330 | return [self isEditable]; | |
331 | } | |
332 | ||
9159a257 SC |
333 | - (void)textDidEndEditing:(NSNotification *)aNotification |
334 | { | |
335 | wxUnusedVar(aNotification); | |
66fc8b0e VZ |
336 | |
337 | if ( self == wxMacEditHelper::GetCurrentlyEditedView() ) | |
338 | { | |
339 | // This notification is generated as the result of calling our own | |
340 | // wxTextCtrl method (e.g. WriteText()) and doesn't correspond to any | |
341 | // real focus loss event so skip generating it. | |
342 | return; | |
343 | } | |
344 | ||
9159a257 SC |
345 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); |
346 | if ( impl ) | |
347 | { | |
60d66be3 | 348 | NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder(); |
e17d84e1 | 349 | NSView* otherView = wxOSXGetViewFromResponder(responder); |
60d66be3 | 350 | |
e17d84e1 | 351 | wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView); |
60d66be3 | 352 | impl->DoNotifyFocusEvent( false, otherWindow ); |
9159a257 SC |
353 | } |
354 | } | |
355 | ||
c824c165 KO |
356 | @end |
357 | ||
e32090ba | 358 | @implementation wxNSTextField |
ffad7b0d | 359 | |
e32090ba | 360 | + (void)initialize |
dbeddfb9 | 361 | { |
e32090ba | 362 | static BOOL initialized = NO; |
03647350 | 363 | if (!initialized) |
e32090ba SC |
364 | { |
365 | initialized = YES; | |
366 | wxOSXCocoaClassAddWXMethods( self ); | |
367 | } | |
dbeddfb9 | 368 | } |
e32090ba | 369 | |
7cb2a241 SC |
370 | - (id) initWithFrame:(NSRect) frame |
371 | { | |
372 | self = [super initWithFrame:frame]; | |
373 | fieldEditor = nil; | |
374 | return self; | |
375 | } | |
376 | ||
af665c2d SC |
377 | - (void) dealloc |
378 | { | |
379 | [fieldEditor release]; | |
380 | [super dealloc]; | |
381 | } | |
382 | ||
7cb2a241 SC |
383 | - (void) setFieldEditor:(wxNSTextFieldEditor*) editor |
384 | { | |
c2a22141 SC |
385 | if ( editor != fieldEditor ) |
386 | { | |
387 | [editor retain]; | |
388 | [fieldEditor release]; | |
389 | fieldEditor = editor; | |
390 | } | |
7cb2a241 SC |
391 | } |
392 | ||
393 | - (wxNSTextFieldEditor*) fieldEditor | |
394 | { | |
395 | return fieldEditor; | |
396 | } | |
397 | ||
c3e433b1 SC |
398 | - (void) setEnabled:(BOOL) flag |
399 | { | |
400 | [super setEnabled: flag]; | |
401 | ||
402 | if (![self drawsBackground]) { | |
403 | // Static text is drawn incorrectly when disabled. | |
404 | // For an explanation, see | |
405 | // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028 | |
406 | if (flag) { | |
407 | [self setTextColor: [NSColor controlTextColor]]; | |
408 | } else { | |
409 | [self setTextColor: [NSColor secondarySelectedControlColor]]; | |
410 | } | |
411 | } | |
412 | } | |
4d23a0d3 | 413 | |
ffad7b0d SC |
414 | - (void)controlTextDidChange:(NSNotification *)aNotification |
415 | { | |
6331c8c0 SC |
416 | wxUnusedVar(aNotification); |
417 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
ffad7b0d | 418 | if ( impl ) |
75a2c6a1 | 419 | impl->controlTextDidChange(); |
ffad7b0d | 420 | } |
dbeddfb9 | 421 | |
00ba1af9 | 422 | - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words |
b4bdde87 | 423 | forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger*)index |
00ba1af9 SC |
424 | { |
425 | NSMutableArray* matches = NULL; | |
00ba1af9 | 426 | |
c729f16f VZ |
427 | wxTextWidgetImpl* impl = (wxNSTextFieldControl * ) wxWidgetImpl::FindFromWXWidget( self ); |
428 | wxTextEntry * const entry = impl->GetTextEntry(); | |
429 | wxTextCompleter * const completer = entry->OSXGetCompleter(); | |
430 | if ( completer ) | |
431 | { | |
432 | const wxString prefix = entry->GetValue(); | |
433 | if ( completer->Start(prefix) ) | |
434 | { | |
435 | const wxString | |
436 | wordStart = wxCFStringRef::AsString( | |
437 | [[textView string] substringWithRange:charRange] | |
438 | ); | |
439 | ||
440 | matches = [NSMutableArray array]; | |
441 | for ( ;; ) | |
442 | { | |
443 | const wxString s = completer->GetNext(); | |
444 | if ( s.empty() ) | |
445 | break; | |
446 | ||
447 | // Normally the completer should return only the strings | |
448 | // starting with the prefix, but there could be exceptions | |
449 | // and, for compatibility with MSW which simply ignores all | |
450 | // entries that don't match the current text control contents, | |
451 | // we ignore them as well. Besides, our own wxTextCompleterFixed | |
452 | // doesn't respect this rule and, moreover, we need to extract | |
453 | // just the rest of the string anyhow. | |
454 | wxString completion; | |
455 | if ( s.StartsWith(prefix, &completion) ) | |
456 | { | |
457 | // We discarded the entire prefix above but actually we | |
458 | // should include the part of it that consists of the | |
459 | // beginning of the current word, otherwise it would be | |
460 | // lost when completion is accepted as OS X supposes that | |
461 | // our matches do start with the "partial word range" | |
462 | // passed to us. | |
463 | const wxCFStringRef fullWord(wordStart + completion); | |
464 | [matches addObject: fullWord.AsNSString()]; | |
465 | } | |
466 | } | |
467 | } | |
468 | } | |
469 | ||
00ba1af9 SC |
470 | return matches; |
471 | } | |
472 | ||
76b1a2c2 SC |
473 | - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector |
474 | { | |
475 | wxUnusedVar(textView); | |
476 | wxUnusedVar(control); | |
477 | ||
478 | BOOL handled = NO; | |
479 | ||
480 | // send back key events wx' common code knows how to handle | |
481 | ||
482 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
483 | if ( impl ) | |
484 | { | |
485 | wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); | |
486 | if ( wxpeer ) | |
487 | { | |
488 | if (commandSelector == @selector(insertNewline:)) | |
489 | { | |
490 | [textView insertNewlineIgnoringFieldEditor:self]; | |
491 | handled = YES; | |
492 | } | |
493 | else if ( commandSelector == @selector(insertTab:)) | |
494 | { | |
495 | [textView insertTabIgnoringFieldEditor:self]; | |
496 | handled = YES; | |
497 | } | |
498 | else if ( commandSelector == @selector(insertBacktab:)) | |
499 | { | |
500 | [textView insertTabIgnoringFieldEditor:self]; | |
501 | handled = YES; | |
502 | } | |
503 | } | |
504 | } | |
505 | ||
506 | return handled; | |
507 | } | |
508 | ||
ffad7b0d SC |
509 | - (void)controlTextDidEndEditing:(NSNotification *)aNotification |
510 | { | |
6d109846 SC |
511 | wxUnusedVar(aNotification); |
512 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
ffad7b0d SC |
513 | if ( impl ) |
514 | { | |
8e64b8fe SC |
515 | wxNSTextFieldControl* timpl = dynamic_cast<wxNSTextFieldControl*>(impl); |
516 | if ( fieldEditor ) | |
517 | { | |
518 | NSRange range = [fieldEditor selectedRange]; | |
519 | timpl->SetInternalSelection(range.location, range.location + range.length); | |
520 | } | |
521 | ||
60d66be3 | 522 | NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder(); |
e17d84e1 | 523 | NSView* otherView = wxOSXGetViewFromResponder(responder); |
60d66be3 | 524 | |
e17d84e1 | 525 | wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView); |
60d66be3 | 526 | impl->DoNotifyFocusEvent( false, otherWindow ); |
ffad7b0d SC |
527 | } |
528 | } | |
dbeddfb9 SC |
529 | @end |
530 | ||
c824c165 KO |
531 | // wxNSTextViewControl |
532 | ||
c072b9ec VZ |
533 | wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) |
534 | : wxWidgetCocoaImpl(wxPeer, w), | |
535 | wxTextWidgetImpl(wxPeer) | |
c824c165 | 536 | { |
1087c6c1 SC |
537 | wxNSTextScrollView* sv = (wxNSTextScrollView*) w; |
538 | m_scrollView = sv; | |
03647350 | 539 | |
c824c165 KO |
540 | [m_scrollView setHasVerticalScroller:YES]; |
541 | [m_scrollView setHasHorizontalScroller:NO]; | |
1e70e497 SC |
542 | // TODO Remove if no regression, this was causing automatic resizes of multi-line textfields when the tlw changed |
543 | // [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; | |
c824c165 | 544 | NSSize contentSize = [m_scrollView contentSize]; |
03647350 | 545 | |
1087c6c1 | 546 | wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0, |
c824c165 | 547 | contentSize.width, contentSize.height)]; |
1087c6c1 SC |
548 | m_textView = tv; |
549 | [tv setVerticallyResizable:YES]; | |
550 | [tv setHorizontallyResizable:NO]; | |
551 | [tv setAutoresizingMask:NSViewWidthSizable]; | |
03647350 | 552 | |
1087c6c1 | 553 | [m_scrollView setDocumentView: tv]; |
c824c165 | 554 | |
b9cf2753 | 555 | [tv setDelegate: tv]; |
03647350 VZ |
556 | |
557 | InstallEventHandler(tv); | |
c824c165 KO |
558 | } |
559 | ||
560 | wxNSTextViewControl::~wxNSTextViewControl() | |
561 | { | |
562 | if (m_textView) | |
563 | [m_textView setDelegate: nil]; | |
564 | } | |
565 | ||
78a17075 KO |
566 | bool wxNSTextViewControl::CanFocus() const |
567 | { | |
81533a3a SC |
568 | // since this doesn't work (return false), we hardcode |
569 | // if (m_textView) | |
570 | // return [m_textView canBecomeKeyView]; | |
571 | return true; | |
78a17075 KO |
572 | } |
573 | ||
03647350 | 574 | wxString wxNSTextViewControl::GetStringValue() const |
c824c165 | 575 | { |
03647350 | 576 | if (m_textView) |
c824c165 | 577 | { |
f66ecdc4 | 578 | wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding()); |
50b5e38d SC |
579 | wxMacConvertNewlines13To10( &result ) ; |
580 | return result; | |
c824c165 KO |
581 | } |
582 | return wxEmptyString; | |
583 | } | |
03647350 | 584 | void wxNSTextViewControl::SetStringValue( const wxString &str) |
c824c165 | 585 | { |
50b5e38d SC |
586 | wxString st = str; |
587 | wxMacConvertNewlines10To13( &st ); | |
5f65ba36 | 588 | wxMacEditHelper helper(m_textView); |
50b5e38d | 589 | |
c824c165 | 590 | if (m_textView) |
50b5e38d | 591 | [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
c824c165 | 592 | } |
7cb2a241 | 593 | |
03647350 | 594 | void wxNSTextViewControl::Copy() |
c824c165 KO |
595 | { |
596 | if (m_textView) | |
597 | [m_textView copy:nil]; | |
598 | ||
599 | } | |
600 | ||
03647350 | 601 | void wxNSTextViewControl::Cut() |
c824c165 KO |
602 | { |
603 | if (m_textView) | |
604 | [m_textView cut:nil]; | |
605 | } | |
606 | ||
03647350 | 607 | void wxNSTextViewControl::Paste() |
c824c165 KO |
608 | { |
609 | if (m_textView) | |
610 | [m_textView paste:nil]; | |
611 | } | |
612 | ||
03647350 VZ |
613 | bool wxNSTextViewControl::CanPaste() const |
614 | { | |
c824c165 KO |
615 | return true; |
616 | } | |
617 | ||
03647350 | 618 | void wxNSTextViewControl::SetEditable(bool editable) |
c824c165 KO |
619 | { |
620 | if (m_textView) | |
621 | [m_textView setEditable: editable]; | |
622 | } | |
623 | ||
03647350 | 624 | void wxNSTextViewControl::GetSelection( long* from, long* to) const |
c824c165 KO |
625 | { |
626 | if (m_textView) | |
627 | { | |
628 | NSRange range = [m_textView selectedRange]; | |
629 | *from = range.location; | |
630 | *to = range.location + range.length; | |
631 | } | |
632 | } | |
633 | ||
634 | void wxNSTextViewControl::SetSelection( long from , long to ) | |
635 | { | |
50b5e38d SC |
636 | long textLength = [[m_textView string] length]; |
637 | if ((from == -1) && (to == -1)) | |
638 | { | |
639 | from = 0 ; | |
640 | to = textLength ; | |
641 | } | |
642 | else | |
643 | { | |
644 | from = wxMin(textLength,wxMax(from,0)) ; | |
645 | if ( to == -1 ) | |
646 | to = textLength; | |
647 | else | |
648 | to = wxMax(0,wxMin(textLength,to)) ; | |
649 | } | |
650 | ||
2d6aa919 KO |
651 | NSRange selrange = NSMakeRange(from, to-from); |
652 | [m_textView setSelectedRange:selrange]; | |
653 | [m_textView scrollRangeToVisible:selrange]; | |
c824c165 KO |
654 | } |
655 | ||
03647350 | 656 | void wxNSTextViewControl::WriteText(const wxString& str) |
c824c165 | 657 | { |
50b5e38d SC |
658 | wxString st = str; |
659 | wxMacConvertNewlines10To13( &st ); | |
5f65ba36 | 660 | wxMacEditHelper helper(m_textView); |
715824d5 SC |
661 | NSEvent* formerEvent = m_lastKeyDownEvent; |
662 | m_lastKeyDownEvent = nil; | |
50b5e38d | 663 | [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
715824d5 | 664 | m_lastKeyDownEvent = formerEvent; |
c824c165 KO |
665 | } |
666 | ||
63bcc669 | 667 | void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) ) |
f95dd972 SC |
668 | { |
669 | if ([m_textView respondsToSelector:@selector(setFont:)]) | |
670 | [m_textView setFont: font.OSXGetNSFont()]; | |
671 | } | |
672 | ||
16671f22 KO |
673 | bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style) |
674 | { | |
b9cf2753 KO |
675 | if (m_textView && position >=0) |
676 | { | |
677 | NSFont* font = NULL; | |
678 | NSColor* bgcolor = NULL; | |
679 | NSColor* fgcolor = NULL; | |
680 | // NOTE: It appears that other platforms accept GetStyle with the position == length | |
681 | // but that NSTextStorage does not accept length as a valid position. | |
682 | // Therefore we return the default control style in that case. | |
0f54a3be | 683 | if (position < (long) [[m_textView string] length]) |
b9cf2753 KO |
684 | { |
685 | NSTextStorage* storage = [m_textView textStorage]; | |
686 | font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease]; | |
687 | bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease]; | |
688 | fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease]; | |
689 | } | |
690 | else | |
691 | { | |
692 | NSDictionary* attrs = [m_textView typingAttributes]; | |
693 | font = [[attrs objectForKey:NSFontAttributeName] autorelease]; | |
694 | bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease]; | |
695 | fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease]; | |
696 | } | |
697 | ||
16671f22 KO |
698 | if (font) |
699 | style.SetFont(wxFont(font)); | |
b9cf2753 | 700 | |
16671f22 KO |
701 | if (bgcolor) |
702 | style.SetBackgroundColour(wxColour(bgcolor)); | |
b9cf2753 KO |
703 | |
704 | if (fgcolor) | |
705 | style.SetTextColour(wxColour(fgcolor)); | |
16671f22 KO |
706 | return true; |
707 | } | |
708 | ||
709 | return false; | |
710 | } | |
711 | ||
712 | void wxNSTextViewControl::SetStyle(long start, | |
713 | long end, | |
714 | const wxTextAttr& style) | |
715 | { | |
1659164a VZ |
716 | if ( !m_textView ) |
717 | return; | |
718 | ||
719 | if ( start == -1 && end == -1 ) | |
720 | { | |
721 | NSMutableDictionary* const | |
722 | attrs = [NSMutableDictionary dictionaryWithCapacity:3]; | |
723 | if ( style.HasFont() ) | |
724 | [attrs setValue:style.GetFont().OSXGetNSFont() forKey:NSFontAttributeName]; | |
725 | if ( style.HasBackgroundColour() ) | |
726 | [attrs setValue:style.GetBackgroundColour().OSXGetNSColor() forKey:NSBackgroundColorAttributeName]; | |
727 | if ( style.HasTextColour() ) | |
728 | [attrs setValue:style.GetTextColour().OSXGetNSColor() forKey:NSForegroundColorAttributeName]; | |
729 | ||
730 | [m_textView setTypingAttributes:attrs]; | |
731 | } | |
732 | else // Set the attributes just for this range. | |
733 | { | |
16671f22 | 734 | NSRange range = NSMakeRange(start, end-start); |
b9cf2753 | 735 | |
16671f22 | 736 | NSTextStorage* storage = [m_textView textStorage]; |
dfc9124f VZ |
737 | if ( style.HasFont() ) |
738 | [storage addAttribute:NSFontAttributeName value:style.GetFont().OSXGetNSFont() range:range]; | |
739 | ||
740 | if ( style.HasBackgroundColour() ) | |
741 | [storage addAttribute:NSBackgroundColorAttributeName value:style.GetBackgroundColour().OSXGetNSColor() range:range]; | |
1659164a | 742 | |
dfc9124f VZ |
743 | if ( style.HasTextColour() ) |
744 | [storage addAttribute:NSForegroundColorAttributeName value:style.GetTextColour().OSXGetNSColor() range:range]; | |
16671f22 KO |
745 | } |
746 | } | |
f95dd972 | 747 | |
b9cf2753 KO |
748 | void wxNSTextViewControl::CheckSpelling(bool check) |
749 | { | |
750 | if (m_textView) | |
751 | [m_textView setContinuousSpellCheckingEnabled: check]; | |
752 | } | |
753 | ||
9ab7ff53 KO |
754 | wxSize wxNSTextViewControl::GetBestSize() const |
755 | { | |
756 | if (m_textView && [m_textView layoutManager]) | |
757 | { | |
758 | NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]]; | |
4a49fa24 VZ |
759 | return wxSize((int)(rect.size.width + [m_textView textContainerInset].width), |
760 | (int)(rect.size.height + [m_textView textContainerInset].height)); | |
9ab7ff53 | 761 | } |
c8fdb345 | 762 | return wxSize(0,0); |
9ab7ff53 KO |
763 | } |
764 | ||
c824c165 KO |
765 | // wxNSTextFieldControl |
766 | ||
c072b9ec VZ |
767 | wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w ) |
768 | : wxWidgetCocoaImpl(text, w), | |
769 | wxTextWidgetImpl(text) | |
770 | { | |
c8eab84f | 771 | Init(w); |
c072b9ec VZ |
772 | } |
773 | ||
774 | wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer, | |
775 | wxTextEntry *entry, | |
776 | WXWidget w) | |
777 | : wxWidgetCocoaImpl(wxPeer, w), | |
778 | wxTextWidgetImpl(entry) | |
779 | { | |
c8eab84f | 780 | Init(w); |
c072b9ec VZ |
781 | } |
782 | ||
783 | void wxNSTextFieldControl::Init(WXWidget w) | |
1e181c7a | 784 | { |
c2a22141 | 785 | NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>)*) w; |
c8fdb345 SC |
786 | m_textField = tf; |
787 | [m_textField setDelegate: tf]; | |
50b5e38d | 788 | m_selStart = m_selEnd = 0; |
7cb2a241 | 789 | m_hasEditor = [w isKindOfClass:[NSTextField class]]; |
1e181c7a SC |
790 | } |
791 | ||
792 | wxNSTextFieldControl::~wxNSTextFieldControl() | |
793 | { | |
c824c165 KO |
794 | if (m_textField) |
795 | [m_textField setDelegate: nil]; | |
1e181c7a SC |
796 | } |
797 | ||
03647350 | 798 | wxString wxNSTextFieldControl::GetStringValue() const |
1e181c7a | 799 | { |
f66ecdc4 | 800 | return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding()); |
1e181c7a | 801 | } |
50b5e38d | 802 | |
03647350 | 803 | void wxNSTextFieldControl::SetStringValue( const wxString &str) |
1e181c7a | 804 | { |
5f65ba36 | 805 | wxMacEditHelper helper(m_textField); |
e32090ba | 806 | [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
1e181c7a | 807 | } |
50b5e38d | 808 | |
9c71750d SC |
809 | void wxNSTextFieldControl::SetMaxLength(unsigned long len) |
810 | { | |
811 | wxMaximumLengthFormatter* formatter = [[[wxMaximumLengthFormatter alloc] init] autorelease]; | |
812 | [formatter setMaxLength:len]; | |
053f5a55 | 813 | [formatter setTextEntry:GetTextEntry()]; |
9c71750d SC |
814 | [m_textField setFormatter:formatter]; |
815 | } | |
816 | ||
03647350 | 817 | void wxNSTextFieldControl::Copy() |
1e181c7a | 818 | { |
e32090ba SC |
819 | NSText* editor = [m_textField currentEditor]; |
820 | if ( editor ) | |
821 | { | |
822 | [editor copy:nil]; | |
823 | } | |
1e181c7a SC |
824 | } |
825 | ||
03647350 | 826 | void wxNSTextFieldControl::Cut() |
1e181c7a | 827 | { |
e32090ba SC |
828 | NSText* editor = [m_textField currentEditor]; |
829 | if ( editor ) | |
830 | { | |
831 | [editor cut:nil]; | |
832 | } | |
1e181c7a SC |
833 | } |
834 | ||
03647350 | 835 | void wxNSTextFieldControl::Paste() |
1e181c7a | 836 | { |
e32090ba SC |
837 | NSText* editor = [m_textField currentEditor]; |
838 | if ( editor ) | |
839 | { | |
840 | [editor paste:nil]; | |
841 | } | |
1e181c7a SC |
842 | } |
843 | ||
03647350 VZ |
844 | bool wxNSTextFieldControl::CanPaste() const |
845 | { | |
e32090ba | 846 | return true; |
1e181c7a SC |
847 | } |
848 | ||
03647350 | 849 | void wxNSTextFieldControl::SetEditable(bool editable) |
1e181c7a | 850 | { |
e32090ba | 851 | [m_textField setEditable:editable]; |
1e181c7a SC |
852 | } |
853 | ||
03647350 | 854 | void wxNSTextFieldControl::GetSelection( long* from, long* to) const |
1e181c7a | 855 | { |
e32090ba SC |
856 | NSText* editor = [m_textField currentEditor]; |
857 | if ( editor ) | |
858 | { | |
859 | NSRange range = [editor selectedRange]; | |
860 | *from = range.location; | |
861 | *to = range.location + range.length; | |
862 | } | |
50b5e38d SC |
863 | else |
864 | { | |
865 | *from = m_selStart; | |
866 | *to = m_selEnd; | |
867 | } | |
1e181c7a SC |
868 | } |
869 | ||
870 | void wxNSTextFieldControl::SetSelection( long from , long to ) | |
871 | { | |
50b5e38d SC |
872 | long textLength = [[m_textField stringValue] length]; |
873 | if ((from == -1) && (to == -1)) | |
874 | { | |
875 | from = 0 ; | |
876 | to = textLength ; | |
877 | } | |
878 | else | |
879 | { | |
880 | from = wxMin(textLength,wxMax(from,0)) ; | |
881 | if ( to == -1 ) | |
882 | to = textLength; | |
883 | else | |
884 | to = wxMax(0,wxMin(textLength,to)) ; | |
885 | } | |
886 | ||
e32090ba SC |
887 | NSText* editor = [m_textField currentEditor]; |
888 | if ( editor ) | |
889 | { | |
890 | [editor setSelectedRange:NSMakeRange(from, to-from)]; | |
891 | } | |
8e64b8fe SC |
892 | |
893 | // the editor might still be in existence, but we might be already passed our 'focus lost' storage | |
894 | // of the selection, so make sure we copy this | |
895 | m_selStart = from; | |
896 | m_selEnd = to; | |
1e181c7a SC |
897 | } |
898 | ||
03647350 | 899 | void wxNSTextFieldControl::WriteText(const wxString& str) |
1e181c7a | 900 | { |
715824d5 SC |
901 | NSEvent* formerEvent = m_lastKeyDownEvent; |
902 | m_lastKeyDownEvent = nil; | |
50b5e38d SC |
903 | NSText* editor = [m_textField currentEditor]; |
904 | if ( editor ) | |
905 | { | |
5f65ba36 | 906 | wxMacEditHelper helper(m_textField); |
b5b98611 SC |
907 | BOOL hasUndo = [editor respondsToSelector:@selector(setAllowsUndo:)]; |
908 | if ( hasUndo ) | |
529f35ff | 909 | [(NSTextView*)editor setAllowsUndo:NO]; |
50b5e38d | 910 | [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
b5b98611 | 911 | if ( hasUndo ) |
529f35ff | 912 | [(NSTextView*)editor setAllowsUndo:YES]; |
50b5e38d SC |
913 | } |
914 | else | |
915 | { | |
916 | wxString val = GetStringValue() ; | |
917 | long start , end ; | |
918 | GetSelection( &start , &end ) ; | |
919 | val.Remove( start , end - start ) ; | |
920 | val.insert( start , str ) ; | |
921 | SetStringValue( val ) ; | |
922 | SetSelection( start + str.length() , start + str.length() ) ; | |
923 | } | |
715824d5 | 924 | m_lastKeyDownEvent = formerEvent; |
1e181c7a | 925 | } |
dbeddfb9 | 926 | |
03647350 | 927 | void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf), |
6331c8c0 | 928 | void* WXUNUSED(_cmd), void *WXUNUSED(sender)) |
e32090ba SC |
929 | { |
930 | wxWindow* wxpeer = (wxWindow*) GetWXPeer(); | |
03647350 | 931 | if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) ) |
e32090ba | 932 | { |
ce7fe42e | 933 | wxCommandEvent event(wxEVT_TEXT_ENTER, wxpeer->GetId()); |
e32090ba | 934 | event.SetEventObject( wxpeer ); |
c072b9ec | 935 | event.SetString( GetTextEntry()->GetValue() ); |
e32090ba SC |
936 | wxpeer->HandleWindowEvent( event ); |
937 | } | |
938 | } | |
939 | ||
8e64b8fe SC |
940 | void wxNSTextFieldControl::SetInternalSelection( long from , long to ) |
941 | { | |
942 | m_selStart = from; | |
943 | m_selEnd = to; | |
944 | } | |
945 | ||
1e70e497 SC |
946 | // as becoming first responder on a window - triggers a resign on the same control, we have to avoid |
947 | // the resign notification writing back native selection values before we can set our own | |
948 | ||
949 | static WXWidget s_widgetBecomingFirstResponder = nil; | |
950 | ||
951 | bool wxNSTextFieldControl::becomeFirstResponder(WXWidget slf, void *_cmd) | |
952 | { | |
953 | s_widgetBecomingFirstResponder = slf; | |
954 | bool retval = wxWidgetCocoaImpl::becomeFirstResponder(slf, _cmd); | |
955 | s_widgetBecomingFirstResponder = nil; | |
956 | if ( retval ) | |
957 | { | |
958 | NSText* editor = [m_textField currentEditor]; | |
959 | if ( editor ) | |
8e64b8fe SC |
960 | { |
961 | long textLength = [[m_textField stringValue] length]; | |
962 | m_selStart = wxMin(textLength,wxMax(m_selStart,0)) ; | |
963 | m_selEnd = wxMax(0,wxMin(textLength,m_selEnd)) ; | |
964 | ||
1e70e497 | 965 | [editor setSelectedRange:NSMakeRange(m_selStart, m_selEnd-m_selStart)]; |
8e64b8fe | 966 | } |
1e70e497 SC |
967 | } |
968 | return retval; | |
969 | } | |
970 | ||
971 | bool wxNSTextFieldControl::resignFirstResponder(WXWidget slf, void *_cmd) | |
972 | { | |
973 | if ( slf != s_widgetBecomingFirstResponder ) | |
974 | { | |
975 | NSText* editor = [m_textField currentEditor]; | |
976 | if ( editor ) | |
977 | { | |
978 | NSRange range = [editor selectedRange]; | |
979 | m_selStart = range.location; | |
980 | m_selEnd = range.location + range.length; | |
981 | } | |
982 | } | |
983 | return wxWidgetCocoaImpl::resignFirstResponder(slf, _cmd); | |
984 | } | |
985 | ||
99eb484a SC |
986 | bool wxNSTextFieldControl::SetHint(const wxString& hint) |
987 | { | |
988 | wxCFStringRef hintstring(hint); | |
989 | [[m_textField cell] setPlaceholderString:hintstring.AsNSString()]; | |
990 | return true; | |
991 | } | |
992 | ||
e32090ba SC |
993 | // |
994 | // | |
995 | // | |
996 | ||
03647350 VZ |
997 | wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer, |
998 | wxWindowMac* WXUNUSED(parent), | |
999 | wxWindowID WXUNUSED(id), | |
63bcc669 | 1000 | const wxString& WXUNUSED(str), |
03647350 | 1001 | const wxPoint& pos, |
dbeddfb9 | 1002 | const wxSize& size, |
03647350 | 1003 | long style, |
6331c8c0 | 1004 | long WXUNUSED(extraStyle)) |
dbeddfb9 | 1005 | { |
dbeddfb9 | 1006 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
c824c165 | 1007 | wxWidgetCocoaImpl* c = NULL; |
03647350 | 1008 | |
98c90969 | 1009 | if ( style & wxTE_MULTILINE ) |
b15f9375 | 1010 | { |
1087c6c1 SC |
1011 | wxNSTextScrollView* v = nil; |
1012 | v = [[wxNSTextScrollView alloc] initWithFrame:r]; | |
c824c165 | 1013 | c = new wxNSTextViewControl( wxpeer, v ); |
58cab5e2 | 1014 | c->SetNeedsFocusRect( true ); |
c824c165 | 1015 | } |
03647350 | 1016 | else |
c824c165 | 1017 | { |
6331c8c0 | 1018 | NSTextField* v = nil; |
c824c165 KO |
1019 | if ( style & wxTE_PASSWORD ) |
1020 | v = [[wxNSSecureTextField alloc] initWithFrame:r]; | |
1021 | else | |
1022 | v = [[wxNSTextField alloc] initWithFrame:r]; | |
03647350 | 1023 | |
d68a2a24 SC |
1024 | if ( style & wxTE_RIGHT) |
1025 | { | |
1026 | [v setAlignment:NSRightTextAlignment]; | |
1027 | } | |
1028 | else if ( style & wxTE_CENTRE) | |
1029 | { | |
1030 | [v setAlignment:NSCenterTextAlignment]; | |
1031 | } | |
58cab5e2 | 1032 | |
3f16e51b SC |
1033 | NSTextFieldCell* cell = [v cell]; |
1034 | [cell setScrollable:YES]; | |
1035 | // TODO: Remove if we definitely are sure, it's not needed | |
1036 | // as setting scrolling to yes, should turn off any wrapping | |
1037 | // [cell setLineBreakMode:NSLineBreakByClipping]; | |
03647350 | 1038 | |
c072b9ec | 1039 | c = new wxNSTextFieldControl( wxpeer, wxpeer, v ); |
58cab5e2 SC |
1040 | |
1041 | if ( (style & wxNO_BORDER) || (style & wxSIMPLE_BORDER) ) | |
1042 | { | |
1043 | // under 10.7 the textcontrol can draw its own focus | |
1044 | // even if no border is shown, on previous systems | |
1045 | // we have to emulate this | |
1046 | [v setBezeled:NO]; | |
1047 | [v setBordered:NO]; | |
1048 | if ( UMAGetSystemVersion() < 0x1070 ) | |
1049 | c->SetNeedsFocusRect( true ); | |
1050 | } | |
1051 | else | |
1052 | { | |
1053 | // use native border | |
1054 | c->SetNeedsFrame(false); | |
1055 | } | |
b15f9375 | 1056 | } |
03647350 | 1057 | |
dbeddfb9 SC |
1058 | return c; |
1059 | } | |
1060 | ||
1061 | ||
1062 | #endif // wxUSE_TEXTCTRL |