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