]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/textctrl.mm
Rename delegates to try and stop name clashes, probably with wxWebKitCtrl.
[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: wxWindows 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/NSSecureTextField.h>
28 #import <AppKit/NSCell.h>
29
30 #include <math.h>
31
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([(style & wxTE_PASSWORD)?[NSSecureTextField alloc]:[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 SendTextUpdatedEvent();
97 }
98
99 void wxTextCtrl::AppendText(wxString const&)
100 {
101 }
102
103 void wxTextCtrl::SetEditable(bool editable)
104 {
105 // first ensure that the current value is stored (in case the user had not finished editing
106 // before SetEditable(FALSE) was called)
107 DoSetValue(GetValue(),1);
108
109 [GetNSTextField() setEditable: editable];
110
111 // forces the focus on the textctrl to be lost - while focus is still maintained
112 // after SetEditable(FALSE) the user may still edit the control
113 // (might not the best way to do this..)
114 [GetNSTextField() abortEditing];
115 }
116
117 void wxTextCtrl::MarkDirty()
118 {
119 }
120
121 void wxTextCtrl::DiscardEdits()
122 {
123 }
124
125 void wxTextCtrl::SetSelection(long, long)
126 {
127 }
128
129 void wxTextCtrl::ShowPosition(long)
130 {
131 }
132
133 void wxTextCtrl::SetInsertionPoint(long)
134 {
135 }
136
137 void wxTextCtrl::SetInsertionPointEnd()
138 {
139 }
140
141 void wxTextCtrl::Cut()
142 {
143 }
144
145 void wxTextCtrl::Copy()
146 {
147 }
148
149 void wxTextCtrl::Redo()
150 {
151 }
152
153 void wxTextCtrl::Undo()
154 {
155 }
156
157 void wxTextCtrl::Clear()
158 {
159 }
160
161 void wxTextCtrl::Paste()
162 {
163 }
164
165 void wxTextCtrl::Remove(long, long)
166 {
167 }
168
169 void wxTextCtrl::Replace(long, long, wxString const&)
170 {
171 }
172
173 void wxTextCtrl::DoSetValue(wxString const& value, int flags)
174 {
175 wxAutoNSAutoreleasePool pool;
176 [GetNSTextField() setStringValue: wxNSStringWithWxString(value)];
177
178 if ( flags & SetValue_SendEvent )
179 SendTextUpdatedEvent();
180 }
181
182 void wxTextCtrl::WriteText(wxString const&)
183 {
184 }
185
186 bool wxTextCtrl::IsEditable() const
187 {
188 return [GetNSTextField() isEditable];
189 }
190
191 bool wxTextCtrl::IsModified() const
192 {
193 return false;
194 }
195
196 wxString wxTextCtrl::GetLineText(long) const
197 {
198 return wxEmptyString;
199 }
200
201 void wxTextCtrl::GetSelection(long*, long*) const
202 {
203 }
204
205 bool wxTextCtrl::PositionToXY(long, long*, long*) const
206 {
207 return false;
208 }
209
210 long wxTextCtrl::XYToPosition(long, long) const
211 {
212 return 0;
213 }
214
215 int wxTextCtrl::GetLineLength(long) const
216 {
217 return 0;
218 }
219
220 wxTextPos wxTextCtrl::GetLastPosition() const
221 {
222 // working - returns the size of the wxString
223 return (long)(GetValue().Len());
224 }
225
226 int wxTextCtrl::GetNumberOfLines() const
227 {
228 return 0;
229 }
230
231 long wxTextCtrl::GetInsertionPoint() const
232 {
233 return 0;
234 }
235
236 bool wxTextCtrl::CanRedo() const
237 {
238 return false;
239 }
240
241 bool wxTextCtrl::CanUndo() const
242 {
243 return false;
244 }
245
246 wxString wxTextCtrl::GetValue() const
247 {
248 wxAutoNSAutoreleasePool pool;
249 return wxStringWithNSString([GetNSTextField() stringValue]);
250 }
251
252 wxSize wxTextCtrl::DoGetBestSize() const
253 {
254 wxAutoNSAutoreleasePool pool;
255 wxASSERT(GetNSControl());
256 NSCell *cell = [GetNSControl() cell];
257 wxASSERT(cell);
258 NSSize cellSize = [cell cellSize];
259 wxSize size(100,(int)ceil(cellSize.height));
260
261 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y);
262 return size;
263 }