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