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