]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/textctrl.mm
move wxGridCellRenderer::Draw before other derived classes implementations (no real...
[wxWidgets.git] / src / osx / cocoa / textctrl.mm
CommitLineData
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
e32090ba 52@interface wxNSSecureTextField : NSSecureTextField
4d23a0d3
KO
53{
54 wxWidgetCocoaImpl* impl;
55}
e32090ba 56
4d23a0d3
KO
57- (void) setImplementation:(wxWidgetCocoaImpl*) item;
58- (wxWidgetCocoaImpl*) implementation;
e32090ba
SC
59@end
60
61@implementation wxNSSecureTextField
dbeddfb9 62
4dd9fdf8
SC
63+ (void)initialize
64{
65 static BOOL initialized = NO;
66 if (!initialized)
67 {
68 initialized = YES;
69 wxOSXCocoaClassAddWXMethods( self );
70 }
71}
dbeddfb9 72
4d23a0d3
KO
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
e32090ba
SC
97@end
98
c824c165 99@interface wxNSTextView : NSScrollView
4d23a0d3
KO
100{
101 wxWidgetCocoaImpl* impl;
102}
c824c165 103
4d23a0d3
KO
104- (void) setImplementation:(wxWidgetCocoaImpl*) item;
105- (wxWidgetCocoaImpl*) implementation;
c824c165
KO
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
4d23a0d3
KO
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}
c824c165
KO
144@end
145
e32090ba 146@implementation wxNSTextField
ffad7b0d 147
e32090ba 148+ (void)initialize
dbeddfb9 149{
e32090ba
SC
150 static BOOL initialized = NO;
151 if (!initialized)
152 {
153 initialized = YES;
154 wxOSXCocoaClassAddWXMethods( self );
155 }
dbeddfb9 156}
e32090ba 157
4d23a0d3
KO
158- (wxWidgetCocoaImpl*) implementation
159{
160 return impl;
161}
162
163- (void) setImplementation:(wxWidgetCocoaImpl*) item
164{
165 impl = item;
166}
167
c3e433b1
SC
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}
4d23a0d3 183
ffad7b0d
SC
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}
dbeddfb9 197
4d23a0d3 198/*
ffad7b0d
SC
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}
ffad7b0d 212*/
dbeddfb9
SC
213@end
214
c824c165
KO
215// wxNSTextViewControl
216
217wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
218{
219 m_scrollView = (NSScrollView*) w;
4d23a0d3 220 [w setImplementation: this];
c824c165
KO
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
238wxNSTextViewControl::~wxNSTextViewControl()
239{
240 if (m_textView)
241 [m_textView setDelegate: nil];
242}
243
244wxString 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}
253void wxNSTextViewControl::SetStringValue( const wxString &str)
254{
255 if (m_textView)
256 [m_textView setString: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
257}
258void wxNSTextViewControl::Copy()
259{
260 if (m_textView)
261 [m_textView copy:nil];
262
263}
264
265void wxNSTextViewControl::Cut()
266{
267 if (m_textView)
268 [m_textView cut:nil];
269}
270
271void wxNSTextViewControl::Paste()
272{
273 if (m_textView)
274 [m_textView paste:nil];
275}
276
277bool wxNSTextViewControl::CanPaste() const
278{
279 return true;
280}
281
282void wxNSTextViewControl::SetEditable(bool editable)
283{
284 if (m_textView)
285 [m_textView setEditable: editable];
286}
287
288void 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
298void wxNSTextViewControl::SetSelection( long from , long to )
299{
300 [m_textView setSelectedRange:NSMakeRange(from, to-from)];
301}
302
303void 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
1e181c7a
SC
312wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
313{
e32090ba
SC
314 m_textField = (NSTextField*) w;
315 [m_textField setDelegate: w];
1e181c7a
SC
316}
317
318wxNSTextFieldControl::~wxNSTextFieldControl()
319{
c824c165
KO
320 if (m_textField)
321 [m_textField setDelegate: nil];
1e181c7a
SC
322}
323
324wxString wxNSTextFieldControl::GetStringValue() const
325{
e32090ba 326 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
1e181c7a
SC
327 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
328}
329void wxNSTextFieldControl::SetStringValue( const wxString &str)
330{
e32090ba 331 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
1e181c7a
SC
332}
333void wxNSTextFieldControl::Copy()
334{
e32090ba
SC
335 NSText* editor = [m_textField currentEditor];
336 if ( editor )
337 {
338 [editor copy:nil];
339 }
1e181c7a
SC
340}
341
342void wxNSTextFieldControl::Cut()
343{
e32090ba
SC
344 NSText* editor = [m_textField currentEditor];
345 if ( editor )
346 {
347 [editor cut:nil];
348 }
1e181c7a
SC
349}
350
351void wxNSTextFieldControl::Paste()
352{
e32090ba
SC
353 NSText* editor = [m_textField currentEditor];
354 if ( editor )
355 {
356 [editor paste:nil];
357 }
1e181c7a
SC
358}
359
360bool wxNSTextFieldControl::CanPaste() const
361{
e32090ba 362 return true;
1e181c7a
SC
363}
364
365void wxNSTextFieldControl::SetEditable(bool editable)
366{
e32090ba 367 [m_textField setEditable:editable];
1e181c7a
SC
368}
369
370void wxNSTextFieldControl::GetSelection( long* from, long* to) const
371{
e32090ba
SC
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 }
1e181c7a
SC
379}
380
381void wxNSTextFieldControl::SetSelection( long from , long to )
382{
e32090ba
SC
383 NSText* editor = [m_textField currentEditor];
384 if ( editor )
385 {
386 [editor setSelectedRange:NSMakeRange(from, to-from)];
387 }
1e181c7a
SC
388}
389
390void wxNSTextFieldControl::WriteText(const wxString& str)
391{
392 // temp hack to get logging working early
393 wxString former = GetStringValue();
394 SetStringValue( former + str );
395}
dbeddfb9 396
e32090ba
SC
397void 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
dbeddfb9
SC
413wxWidgetImplType* 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{
dbeddfb9 422 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
e32090ba 423 NSTextField* v = nil;
c824c165 424 wxWidgetCocoaImpl* c = NULL;
e32090ba 425
c824c165 426 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
b15f9375 427 {
c824c165
KO
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
b15f9375
SC
445 [v setBezeled:NO];
446 [v setBordered:NO];
c824c165
KO
447
448 c = new wxNSTextFieldControl( wxpeer, v );
4d23a0d3 449 [v setImplementation: c];
c824c165 450 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
b15f9375 451 }
dbeddfb9 452
dbeddfb9
SC
453 return c;
454}
455
456
457#endif // wxUSE_TEXTCTRL