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