]>
Commit | Line | Data |
---|---|---|
fb896a32 | 1 | ///////////////////////////////////////////////////////////////////////////// |
fec9cc08 | 2 | // Name: src/cocoa/textctrl.mm |
fb896a32 DE |
3 | // Purpose: wxTextCtrl |
4 | // Author: David Elliott | |
da19c58b | 5 | // Modified by: Mark Oxenham |
fb896a32 | 6 | // Created: 2003/03/16 |
fec9cc08 | 7 | // RCS-ID: $Id$ |
fb896a32 | 8 | // Copyright: (c) 2003 David Elliott |
7d8268a1 | 9 | // Licence: wxWidgets licence |
fb896a32 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
449c5673 | 12 | #include "wx/wxprec.h" |
fec9cc08 WS |
13 | |
14 | #include "wx/textctrl.h" | |
15 | ||
449c5673 DE |
16 | #ifndef WX_PRECOMP |
17 | #include "wx/app.h" | |
79e1f224 | 18 | #include "wx/log.h" |
449c5673 | 19 | #endif //WX_PRECOMP |
fb896a32 | 20 | |
7d27dcf9 DE |
21 | #include "wx/cocoa/string.h" |
22 | ||
7fc77f30 DE |
23 | #include "wx/cocoa/autorelease.h" |
24 | ||
fb896a32 DE |
25 | #import <Foundation/NSString.h> |
26 | #import <AppKit/NSTextField.h> | |
f513aa78 | 27 | #import <AppKit/NSSecureTextField.h> |
be134f05 | 28 | #import <AppKit/NSCell.h> |
fb896a32 | 29 | |
79e1f224 DE |
30 | #include <math.h> |
31 | ||
9d112688 JS |
32 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase) |
33 | BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase) | |
fb896a32 DE |
34 | END_EVENT_TABLE() |
35 | WX_IMPLEMENT_COCOA_OWNER(wxTextCtrl,NSTextField,NSControl,NSView) | |
36 | ||
37 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID winid, | |
38 | const wxString& value, | |
39 | const wxPoint& pos, | |
40 | const wxSize& size, | |
41 | long style, | |
42 | const wxValidator& validator, | |
43 | const wxString& name) | |
44 | { | |
7fc77f30 | 45 | wxAutoNSAutoreleasePool pool; |
fb896a32 DE |
46 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) |
47 | return false; | |
48 | m_cocoaNSView = NULL; | |
f513aa78 | 49 | SetNSTextField([(style & wxTE_PASSWORD)?[NSSecureTextField alloc]:[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]); |
fb896a32 | 50 | [m_cocoaNSView release]; |
b0c0a393 DE |
51 | [GetNSTextField() setStringValue:wxNSStringWithWxString(value)]; |
52 | ||
fb896a32 | 53 | [GetNSControl() sizeToFit]; |
9ba1d264 DE |
54 | NSRect currentFrame = [m_cocoaNSView frame]; |
55 | if(currentFrame.size.width < 70) | |
56 | { | |
57 | currentFrame.size.width = 70; | |
58 | [m_cocoaNSView setFrame:currentFrame]; | |
59 | } | |
fb896a32 DE |
60 | if(m_parent) |
61 | m_parent->CocoaAddChild(this); | |
9ba1d264 | 62 | SetInitialFrameRect(pos,size); |
715b3df0 DE |
63 | |
64 | [(NSTextField*)m_cocoaNSView setTarget: sm_cocoaTarget]; | |
65 | [(NSTextField*)m_cocoaNSView setAction:@selector(wxNSControlAction:)]; | |
da19c58b DE |
66 | |
67 | // set the text alignment option | |
68 | NSTextAlignment alignStyle; | |
69 | if (style & wxTE_RIGHT) | |
70 | alignStyle = NSRightTextAlignment; | |
71 | else if (style & wxTE_CENTRE) | |
72 | alignStyle = NSCenterTextAlignment; | |
73 | else // default to wxTE_LEFT because it is 0 and can't be tested | |
74 | alignStyle = NSLeftTextAlignment; | |
75 | [GetNSControl() setAlignment:alignStyle]; | |
76 | ||
77 | // if Read-only then set as such, this flag is overwritable by wxTextCtrl::SetEditable(TRUE) | |
78 | if (style & wxTE_READONLY) | |
79 | { | |
80 | SetEditable(FALSE); | |
81 | } | |
82 | ||
fb896a32 DE |
83 | return true; |
84 | } | |
85 | ||
86 | wxTextCtrl::~wxTextCtrl() | |
87 | { | |
911e17c6 | 88 | DisassociateNSTextField(GetNSTextField()); |
fb896a32 DE |
89 | } |
90 | ||
91 | void wxTextCtrl::Cocoa_didChangeText(void) | |
92 | { | |
93 | } | |
94 | ||
715b3df0 DE |
95 | void wxTextCtrl::CocoaTarget_action(void) |
96 | { | |
50135807 | 97 | SendTextUpdatedEvent(); |
715b3df0 DE |
98 | } |
99 | ||
fb896a32 DE |
100 | void wxTextCtrl::AppendText(wxString const&) |
101 | { | |
102 | } | |
103 | ||
da19c58b | 104 | void wxTextCtrl::SetEditable(bool editable) |
fb896a32 | 105 | { |
da19c58b DE |
106 | // first ensure that the current value is stored (in case the user had not finished editing |
107 | // before SetEditable(FALSE) was called) | |
108 | DoSetValue(GetValue(),1); | |
109 | ||
110 | [GetNSTextField() setEditable: editable]; | |
111 | ||
112 | // forces the focus on the textctrl to be lost - while focus is still maintained | |
113 | // after SetEditable(FALSE) the user may still edit the control | |
114 | // (might not the best way to do this..) | |
115 | [GetNSTextField() abortEditing]; | |
fb896a32 DE |
116 | } |
117 | ||
3a9fa0d6 VZ |
118 | void wxTextCtrl::MarkDirty() |
119 | { | |
120 | } | |
121 | ||
fb896a32 DE |
122 | void wxTextCtrl::DiscardEdits() |
123 | { | |
124 | } | |
125 | ||
126 | void wxTextCtrl::SetSelection(long, long) | |
127 | { | |
128 | } | |
129 | ||
130 | void wxTextCtrl::ShowPosition(long) | |
131 | { | |
132 | } | |
133 | ||
134 | void wxTextCtrl::SetInsertionPoint(long) | |
135 | { | |
136 | } | |
137 | ||
138 | void wxTextCtrl::SetInsertionPointEnd() | |
139 | { | |
140 | } | |
141 | ||
142 | void wxTextCtrl::Cut() | |
143 | { | |
144 | } | |
145 | ||
146 | void wxTextCtrl::Copy() | |
147 | { | |
148 | } | |
149 | ||
150 | void wxTextCtrl::Redo() | |
151 | { | |
152 | } | |
153 | ||
154 | void wxTextCtrl::Undo() | |
155 | { | |
156 | } | |
157 | ||
158 | void wxTextCtrl::Clear() | |
159 | { | |
160 | } | |
161 | ||
162 | void wxTextCtrl::Paste() | |
163 | { | |
164 | } | |
165 | ||
166 | void wxTextCtrl::Remove(long, long) | |
167 | { | |
168 | } | |
169 | ||
170 | void wxTextCtrl::Replace(long, long, wxString const&) | |
171 | { | |
172 | } | |
173 | ||
ee2ec18e | 174 | void wxTextCtrl::DoSetValue(wxString const& value, int flags) |
fb896a32 | 175 | { |
7fc77f30 | 176 | wxAutoNSAutoreleasePool pool; |
7d27dcf9 | 177 | [GetNSTextField() setStringValue: wxNSStringWithWxString(value)]; |
ee2ec18e VZ |
178 | |
179 | if ( flags & SetValue_SendEvent ) | |
180 | SendTextUpdatedEvent(); | |
fb896a32 DE |
181 | } |
182 | ||
183 | void wxTextCtrl::WriteText(wxString const&) | |
184 | { | |
185 | } | |
186 | ||
187 | bool wxTextCtrl::IsEditable() const | |
188 | { | |
da19c58b | 189 | return [GetNSTextField() isEditable]; |
fb896a32 DE |
190 | } |
191 | ||
192 | bool wxTextCtrl::IsModified() const | |
193 | { | |
194 | return false; | |
195 | } | |
196 | ||
197 | wxString wxTextCtrl::GetLineText(long) const | |
198 | { | |
199 | return wxEmptyString; | |
200 | } | |
201 | ||
202 | void wxTextCtrl::GetSelection(long*, long*) const | |
203 | { | |
204 | } | |
205 | ||
206 | bool wxTextCtrl::PositionToXY(long, long*, long*) const | |
207 | { | |
208 | return false; | |
209 | } | |
210 | ||
211 | long wxTextCtrl::XYToPosition(long, long) const | |
212 | { | |
213 | return 0; | |
214 | } | |
215 | ||
216 | int wxTextCtrl::GetLineLength(long) const | |
217 | { | |
218 | return 0; | |
219 | } | |
220 | ||
7d8268a1 | 221 | wxTextPos wxTextCtrl::GetLastPosition() const |
fb896a32 | 222 | { |
da19c58b DE |
223 | // working - returns the size of the wxString |
224 | return (long)(GetValue().Len()); | |
fb896a32 DE |
225 | } |
226 | ||
227 | int wxTextCtrl::GetNumberOfLines() const | |
228 | { | |
229 | return 0; | |
230 | } | |
231 | ||
232 | long wxTextCtrl::GetInsertionPoint() const | |
233 | { | |
234 | return 0; | |
235 | } | |
236 | ||
237 | bool wxTextCtrl::CanRedo() const | |
238 | { | |
239 | return false; | |
240 | } | |
241 | ||
242 | bool wxTextCtrl::CanUndo() const | |
243 | { | |
244 | return false; | |
245 | } | |
246 | ||
247 | wxString wxTextCtrl::GetValue() const | |
248 | { | |
7fc77f30 | 249 | wxAutoNSAutoreleasePool pool; |
b0c0a393 | 250 | return wxStringWithNSString([GetNSTextField() stringValue]); |
fb896a32 DE |
251 | } |
252 | ||
79e1f224 DE |
253 | wxSize wxTextCtrl::DoGetBestSize() const |
254 | { | |
255 | wxAutoNSAutoreleasePool pool; | |
256 | wxASSERT(GetNSControl()); | |
257 | NSCell *cell = [GetNSControl() cell]; | |
258 | wxASSERT(cell); | |
259 | NSSize cellSize = [cell cellSize]; | |
c05c7cb5 | 260 | wxSize size(100,(int)ceil(cellSize.height)); |
79e1f224 DE |
261 | |
262 | wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y); | |
263 | return size; | |
264 | } |