]>
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 | |
e32090ba SC |
52 | @interface wxNSSecureTextField : NSSecureTextField |
53 | ||
54 | @end | |
55 | ||
56 | @implementation wxNSSecureTextField | |
dbeddfb9 | 57 | |
4dd9fdf8 SC |
58 | + (void)initialize |
59 | { | |
60 | static BOOL initialized = NO; | |
61 | if (!initialized) | |
62 | { | |
63 | initialized = YES; | |
64 | wxOSXCocoaClassAddWXMethods( self ); | |
65 | } | |
66 | } | |
dbeddfb9 | 67 | |
e32090ba SC |
68 | @end |
69 | ||
70 | @implementation wxNSTextField | |
ffad7b0d | 71 | |
e32090ba | 72 | + (void)initialize |
dbeddfb9 | 73 | { |
e32090ba SC |
74 | static BOOL initialized = NO; |
75 | if (!initialized) | |
76 | { | |
77 | initialized = YES; | |
78 | wxOSXCocoaClassAddWXMethods( self ); | |
79 | } | |
dbeddfb9 | 80 | } |
e32090ba | 81 | |
ffad7b0d SC |
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 | } | |
dbeddfb9 | 96 | |
ffad7b0d SC |
97 | - (void)controlTextDidEndEditing:(NSNotification *)aNotification |
98 | { | |
99 | if ( impl ) | |
100 | { | |
101 | wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); | |
102 | if ( wxpeer ) { | |
103 | wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId()); | |
104 | event.SetEventObject( wxpeer ); | |
105 | event.SetWindow( wxpeer ); | |
106 | wxpeer->HandleWindowEvent( event ); | |
107 | } | |
108 | } | |
109 | } | |
ffad7b0d | 110 | */ |
dbeddfb9 SC |
111 | @end |
112 | ||
1e181c7a SC |
113 | wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w) |
114 | { | |
e32090ba SC |
115 | m_textField = (NSTextField*) w; |
116 | [m_textField setDelegate: w]; | |
1e181c7a SC |
117 | } |
118 | ||
119 | wxNSTextFieldControl::~wxNSTextFieldControl() | |
120 | { | |
121 | } | |
122 | ||
123 | wxString wxNSTextFieldControl::GetStringValue() const | |
124 | { | |
e32090ba | 125 | wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] ); |
1e181c7a SC |
126 | return cf.AsString(m_wxPeer->GetFont().GetEncoding()); |
127 | } | |
128 | void wxNSTextFieldControl::SetStringValue( const wxString &str) | |
129 | { | |
e32090ba | 130 | [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
1e181c7a SC |
131 | } |
132 | void wxNSTextFieldControl::Copy() | |
133 | { | |
e32090ba SC |
134 | NSText* editor = [m_textField currentEditor]; |
135 | if ( editor ) | |
136 | { | |
137 | [editor copy:nil]; | |
138 | } | |
1e181c7a SC |
139 | } |
140 | ||
141 | void wxNSTextFieldControl::Cut() | |
142 | { | |
e32090ba SC |
143 | NSText* editor = [m_textField currentEditor]; |
144 | if ( editor ) | |
145 | { | |
146 | [editor cut:nil]; | |
147 | } | |
1e181c7a SC |
148 | } |
149 | ||
150 | void wxNSTextFieldControl::Paste() | |
151 | { | |
e32090ba SC |
152 | NSText* editor = [m_textField currentEditor]; |
153 | if ( editor ) | |
154 | { | |
155 | [editor paste:nil]; | |
156 | } | |
1e181c7a SC |
157 | } |
158 | ||
159 | bool wxNSTextFieldControl::CanPaste() const | |
160 | { | |
e32090ba | 161 | return true; |
1e181c7a SC |
162 | } |
163 | ||
164 | void wxNSTextFieldControl::SetEditable(bool editable) | |
165 | { | |
e32090ba | 166 | [m_textField setEditable:editable]; |
1e181c7a SC |
167 | } |
168 | ||
169 | void wxNSTextFieldControl::GetSelection( long* from, long* to) const | |
170 | { | |
e32090ba SC |
171 | NSText* editor = [m_textField currentEditor]; |
172 | if ( editor ) | |
173 | { | |
174 | NSRange range = [editor selectedRange]; | |
175 | *from = range.location; | |
176 | *to = range.location + range.length; | |
177 | } | |
1e181c7a SC |
178 | } |
179 | ||
180 | void wxNSTextFieldControl::SetSelection( long from , long to ) | |
181 | { | |
e32090ba SC |
182 | NSText* editor = [m_textField currentEditor]; |
183 | if ( editor ) | |
184 | { | |
185 | [editor setSelectedRange:NSMakeRange(from, to-from)]; | |
186 | } | |
1e181c7a SC |
187 | } |
188 | ||
189 | void wxNSTextFieldControl::WriteText(const wxString& str) | |
190 | { | |
191 | // temp hack to get logging working early | |
192 | wxString former = GetStringValue(); | |
193 | SetStringValue( former + str ); | |
194 | } | |
dbeddfb9 | 195 | |
e32090ba SC |
196 | void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender) |
197 | { | |
198 | wxWindow* wxpeer = (wxWindow*) GetWXPeer(); | |
199 | if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) ) | |
200 | { | |
201 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId()); | |
202 | event.SetEventObject( wxpeer ); | |
203 | event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() ); | |
204 | wxpeer->HandleWindowEvent( event ); | |
205 | } | |
206 | } | |
207 | ||
208 | // | |
209 | // | |
210 | // | |
211 | ||
dbeddfb9 SC |
212 | wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer, |
213 | wxWindowMac* parent, | |
214 | wxWindowID id, | |
215 | const wxString& str, | |
216 | const wxPoint& pos, | |
217 | const wxSize& size, | |
218 | long style, | |
219 | long extraStyle) | |
220 | { | |
dbeddfb9 | 221 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
e32090ba SC |
222 | NSTextField* v = nil; |
223 | ||
224 | if ( style & wxTE_PASSWORD ) | |
225 | v =[[wxNSSecureTextField alloc] initWithFrame:r]; | |
226 | else | |
227 | v= [[wxNSTextField alloc] initWithFrame:r]; | |
b15f9375 SC |
228 | |
229 | if ( style & wxNO_BORDER ) | |
230 | { | |
231 | [v setBezeled:NO]; | |
232 | [v setBordered:NO]; | |
233 | } | |
dbeddfb9 | 234 | |
e32090ba SC |
235 | [v setBezeled:NO]; |
236 | [v setBordered:NO]; | |
dbeddfb9 SC |
237 | //[v setBezeled:NO]; |
238 | //[v setEditable:NO]; | |
239 | //[v setDrawsBackground:NO]; | |
240 | ||
241 | wxWidgetCocoaImpl* c = new wxNSTextFieldControl( wxpeer, v ); | |
dbeddfb9 SC |
242 | return c; |
243 | } | |
244 | ||
245 | ||
246 | #endif // wxUSE_TEXTCTRL |