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