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