]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/textctrl.mm
X property data is long for format 32
[wxWidgets.git] / src / cocoa / textctrl.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/textctrl.mm
3 // Purpose: wxTextCtrl
4 // Author: David Elliott
5 // Modified by: Mark Oxenham
6 // Created: 2003/03/16
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/textctrl.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/app.h"
18 #include "wx/log.h"
19 #endif //WX_PRECOMP
20
21 #include "wx/cocoa/string.h"
22
23 #include "wx/cocoa/autorelease.h"
24
25 #import <Foundation/NSString.h>
26 #import <AppKit/NSTextField.h>
27 #import <AppKit/NSCell.h>
28
29 #include <math.h>
30
31 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)
32 BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
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 {
44 wxAutoNSAutoreleasePool pool;
45 if(!CreateControl(parent,winid,pos,size,style,validator,name))
46 return false;
47 m_cocoaNSView = NULL;
48 SetNSTextField([[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
49 [m_cocoaNSView release];
50 [GetNSTextField() setStringValue:wxNSStringWithWxString(value)];
51
52 [GetNSControl() sizeToFit];
53 NSRect currentFrame = [m_cocoaNSView frame];
54 if(currentFrame.size.width < 70)
55 {
56 currentFrame.size.width = 70;
57 [m_cocoaNSView setFrame:currentFrame];
58 }
59 if(m_parent)
60 m_parent->CocoaAddChild(this);
61 SetInitialFrameRect(pos,size);
62
63 [(NSTextField*)m_cocoaNSView setTarget: sm_cocoaTarget];
64 [(NSTextField*)m_cocoaNSView setAction:@selector(wxNSControlAction:)];
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
82 return true;
83 }
84
85 wxTextCtrl::~wxTextCtrl()
86 {
87 DisassociateNSTextField(GetNSTextField());
88 }
89
90 void wxTextCtrl::Cocoa_didChangeText(void)
91 {
92 }
93
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
107 void wxTextCtrl::AppendText(wxString const&)
108 {
109 }
110
111 void wxTextCtrl::SetEditable(bool editable)
112 {
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];
123 }
124
125 void wxTextCtrl::MarkDirty()
126 {
127 }
128
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
181 void wxTextCtrl::DoSetValue(wxString const& value, int flags)
182 {
183 wxAutoNSAutoreleasePool pool;
184 [GetNSTextField() setStringValue: wxNSStringWithWxString(value)];
185
186 if ( flags & SetValue_SendEvent )
187 SendTextUpdatedEvent();
188 }
189
190 void wxTextCtrl::WriteText(wxString const&)
191 {
192 }
193
194 bool wxTextCtrl::IsEditable() const
195 {
196 return [GetNSTextField() isEditable];
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
228 wxTextPos wxTextCtrl::GetLastPosition() const
229 {
230 // working - returns the size of the wxString
231 return (long)(GetValue().Len());
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 {
256 wxAutoNSAutoreleasePool pool;
257 return wxStringWithNSString([GetNSTextField() stringValue]);
258 }
259
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];
267 wxSize size(100,(int)ceil(cellSize.height));
268
269 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y);
270 return size;
271 }