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