]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/textctrl.mm
move wxGridCellRenderer::Draw before other derived classes implementations (no real...
[wxWidgets.git] / src / osx / cocoa / textctrl.mm
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"
50 #include "wx/osx/cocoa/private/textimpl.h"
51
52 @interface wxNSSecureTextField : NSSecureTextField
53 {
54 wxWidgetCocoaImpl* impl;
55 }
56
57 - (void) setImplementation:(wxWidgetCocoaImpl*) item;
58 - (wxWidgetCocoaImpl*) implementation;
59 @end
60
61 @implementation wxNSSecureTextField
62
63 + (void)initialize
64 {
65 static BOOL initialized = NO;
66 if (!initialized)
67 {
68 initialized = YES;
69 wxOSXCocoaClassAddWXMethods( self );
70 }
71 }
72
73 - (wxWidgetCocoaImpl*) implementation
74 {
75 return impl;
76 }
77
78 - (void) setImplementation:(wxWidgetCocoaImpl*) item
79 {
80 impl = item;
81 }
82
83 - (void)controlTextDidChange:(NSNotification *)aNotification
84 {
85 if ( impl )
86 {
87 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
88 if ( wxpeer ) {
89 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
90 event.SetEventObject( wxpeer );
91 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
92 wxpeer->HandleWindowEvent( event );
93 }
94 }
95 }
96
97 @end
98
99 @interface wxNSTextView : NSScrollView
100 {
101 wxWidgetCocoaImpl* impl;
102 }
103
104 - (void) setImplementation:(wxWidgetCocoaImpl*) item;
105 - (wxWidgetCocoaImpl*) implementation;
106 @end
107
108 @implementation wxNSTextView
109
110 + (void)initialize
111 {
112 static BOOL initialized = NO;
113 if (!initialized)
114 {
115 initialized = YES;
116 wxOSXCocoaClassAddWXMethods( self );
117 }
118 }
119
120 - (wxWidgetCocoaImpl*) implementation
121 {
122 return impl;
123 }
124
125 - (void) setImplementation:(wxWidgetCocoaImpl*) item
126 {
127 impl = item;
128 }
129
130
131 - (void)controlTextDidChange:(NSNotification *)aNotification
132 {
133 if ( impl )
134 {
135 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
136 if ( wxpeer ) {
137 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
138 event.SetEventObject( wxpeer );
139 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
140 wxpeer->HandleWindowEvent( event );
141 }
142 }
143 }
144 @end
145
146 @implementation wxNSTextField
147
148 + (void)initialize
149 {
150 static BOOL initialized = NO;
151 if (!initialized)
152 {
153 initialized = YES;
154 wxOSXCocoaClassAddWXMethods( self );
155 }
156 }
157
158 - (wxWidgetCocoaImpl*) implementation
159 {
160 return impl;
161 }
162
163 - (void) setImplementation:(wxWidgetCocoaImpl*) item
164 {
165 impl = item;
166 }
167
168 - (void) setEnabled:(BOOL) flag
169 {
170 [super setEnabled: flag];
171
172 if (![self drawsBackground]) {
173 // Static text is drawn incorrectly when disabled.
174 // For an explanation, see
175 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
176 if (flag) {
177 [self setTextColor: [NSColor controlTextColor]];
178 } else {
179 [self setTextColor: [NSColor secondarySelectedControlColor]];
180 }
181 }
182 }
183
184 - (void)controlTextDidChange:(NSNotification *)aNotification
185 {
186 if ( impl )
187 {
188 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
189 if ( wxpeer ) {
190 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
191 event.SetEventObject( wxpeer );
192 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
193 wxpeer->HandleWindowEvent( event );
194 }
195 }
196 }
197
198 /*
199 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
200 {
201 if ( impl )
202 {
203 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
204 if ( wxpeer ) {
205 wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId());
206 event.SetEventObject( wxpeer );
207 event.SetWindow( wxpeer );
208 wxpeer->HandleWindowEvent( event );
209 }
210 }
211 }
212 */
213 @end
214
215 // wxNSTextViewControl
216
217 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
218 {
219 m_scrollView = (NSScrollView*) w;
220 [w setImplementation: this];
221
222 [m_scrollView setHasVerticalScroller:YES];
223 [m_scrollView setHasHorizontalScroller:NO];
224 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
225 NSSize contentSize = [m_scrollView contentSize];
226
227 m_textView = [[NSTextView alloc] initWithFrame: NSMakeRect(0, 0,
228 contentSize.width, contentSize.height)];
229 [m_textView setVerticallyResizable:YES];
230 [m_textView setHorizontallyResizable:NO];
231 [m_textView setAutoresizingMask:NSViewWidthSizable];
232
233 [m_scrollView setDocumentView: m_textView];
234
235 [m_textView setDelegate: w];
236 }
237
238 wxNSTextViewControl::~wxNSTextViewControl()
239 {
240 if (m_textView)
241 [m_textView setDelegate: nil];
242 }
243
244 wxString wxNSTextViewControl::GetStringValue() const
245 {
246 if (m_textView)
247 {
248 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
249 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
250 }
251 return wxEmptyString;
252 }
253 void wxNSTextViewControl::SetStringValue( const wxString &str)
254 {
255 if (m_textView)
256 [m_textView setString: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
257 }
258 void wxNSTextViewControl::Copy()
259 {
260 if (m_textView)
261 [m_textView copy:nil];
262
263 }
264
265 void wxNSTextViewControl::Cut()
266 {
267 if (m_textView)
268 [m_textView cut:nil];
269 }
270
271 void wxNSTextViewControl::Paste()
272 {
273 if (m_textView)
274 [m_textView paste:nil];
275 }
276
277 bool wxNSTextViewControl::CanPaste() const
278 {
279 return true;
280 }
281
282 void wxNSTextViewControl::SetEditable(bool editable)
283 {
284 if (m_textView)
285 [m_textView setEditable: editable];
286 }
287
288 void wxNSTextViewControl::GetSelection( long* from, long* to) const
289 {
290 if (m_textView)
291 {
292 NSRange range = [m_textView selectedRange];
293 *from = range.location;
294 *to = range.location + range.length;
295 }
296 }
297
298 void wxNSTextViewControl::SetSelection( long from , long to )
299 {
300 [m_textView setSelectedRange:NSMakeRange(from, to-from)];
301 }
302
303 void wxNSTextViewControl::WriteText(const wxString& str)
304 {
305 // temp hack to get logging working early
306 wxString former = GetStringValue();
307 SetStringValue( former + str );
308 }
309
310 // wxNSTextFieldControl
311
312 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
313 {
314 m_textField = (NSTextField*) w;
315 [m_textField setDelegate: w];
316 }
317
318 wxNSTextFieldControl::~wxNSTextFieldControl()
319 {
320 if (m_textField)
321 [m_textField setDelegate: nil];
322 }
323
324 wxString wxNSTextFieldControl::GetStringValue() const
325 {
326 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
327 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
328 }
329 void wxNSTextFieldControl::SetStringValue( const wxString &str)
330 {
331 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
332 }
333 void wxNSTextFieldControl::Copy()
334 {
335 NSText* editor = [m_textField currentEditor];
336 if ( editor )
337 {
338 [editor copy:nil];
339 }
340 }
341
342 void wxNSTextFieldControl::Cut()
343 {
344 NSText* editor = [m_textField currentEditor];
345 if ( editor )
346 {
347 [editor cut:nil];
348 }
349 }
350
351 void wxNSTextFieldControl::Paste()
352 {
353 NSText* editor = [m_textField currentEditor];
354 if ( editor )
355 {
356 [editor paste:nil];
357 }
358 }
359
360 bool wxNSTextFieldControl::CanPaste() const
361 {
362 return true;
363 }
364
365 void wxNSTextFieldControl::SetEditable(bool editable)
366 {
367 [m_textField setEditable:editable];
368 }
369
370 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
371 {
372 NSText* editor = [m_textField currentEditor];
373 if ( editor )
374 {
375 NSRange range = [editor selectedRange];
376 *from = range.location;
377 *to = range.location + range.length;
378 }
379 }
380
381 void wxNSTextFieldControl::SetSelection( long from , long to )
382 {
383 NSText* editor = [m_textField currentEditor];
384 if ( editor )
385 {
386 [editor setSelectedRange:NSMakeRange(from, to-from)];
387 }
388 }
389
390 void wxNSTextFieldControl::WriteText(const wxString& str)
391 {
392 // temp hack to get logging working early
393 wxString former = GetStringValue();
394 SetStringValue( former + str );
395 }
396
397 void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender)
398 {
399 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
400 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
401 {
402 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
403 event.SetEventObject( wxpeer );
404 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
405 wxpeer->HandleWindowEvent( event );
406 }
407 }
408
409 //
410 //
411 //
412
413 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
414 wxWindowMac* parent,
415 wxWindowID id,
416 const wxString& str,
417 const wxPoint& pos,
418 const wxSize& size,
419 long style,
420 long extraStyle)
421 {
422 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
423 NSTextField* v = nil;
424 wxWidgetCocoaImpl* c = NULL;
425
426 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
427 {
428 v = [[wxNSTextView alloc] initWithFrame:r];
429 c = new wxNSTextViewControl( wxpeer, v );
430 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
431 }
432 else
433 {
434 if ( style & wxTE_PASSWORD )
435 v = [[wxNSSecureTextField alloc] initWithFrame:r];
436 else
437 v = [[wxNSTextField alloc] initWithFrame:r];
438
439 if ( style & wxNO_BORDER )
440 {
441 // FIXME: How can we remove the native control's border?
442 // setBordered is separate from the text ctrl's border.
443 }
444
445 [v setBezeled:NO];
446 [v setBordered:NO];
447
448 c = new wxNSTextFieldControl( wxpeer, v );
449 [v setImplementation: c];
450 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
451 }
452
453 return c;
454 }
455
456
457 #endif // wxUSE_TEXTCTRL