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