1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/textctrl.mm
4 // Author: David Elliott
5 // Modified by: Mark Oxenham
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #include "wx/textctrl.h"
20 #include "wx/cocoa/string.h"
22 #include "wx/cocoa/autorelease.h"
24 #import <Foundation/NSString.h>
25 #import <AppKit/NSTextField.h>
26 #import <AppKit/NSSecureTextField.h>
27 #import <AppKit/NSCell.h>
31 BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
33 WX_IMPLEMENT_COCOA_OWNER(wxTextCtrl,NSTextField,NSControl,NSView)
35 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID winid,
36 const wxString& value,
40 const wxValidator& validator,
43 wxAutoNSAutoreleasePool pool;
44 if(!CreateControl(parent,winid,pos,size,style,validator,name))
47 SetNSTextField([(style & wxTE_PASSWORD)?[NSSecureTextField alloc]:[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
48 [m_cocoaNSView release];
49 [GetNSTextField() setStringValue:wxNSStringWithWxString(value)];
51 [GetNSControl() sizeToFit];
52 NSRect currentFrame = [m_cocoaNSView frame];
53 if(currentFrame.size.width < 70)
55 currentFrame.size.width = 70;
56 [m_cocoaNSView setFrame:currentFrame];
59 m_parent->CocoaAddChild(this);
60 SetInitialFrameRect(pos,size);
62 [(NSTextField*)m_cocoaNSView setTarget: sm_cocoaTarget];
63 [(NSTextField*)m_cocoaNSView setAction:@selector(wxNSControlAction:)];
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];
75 // if Read-only then set as such, this flag is overwritable by wxTextCtrl::SetEditable(TRUE)
76 if (style & wxTE_READONLY)
84 wxTextCtrl::~wxTextCtrl()
86 DisassociateNSTextField(GetNSTextField());
89 void wxTextCtrl::Cocoa_didChangeText(void)
93 void wxTextCtrl::CocoaTarget_action(void)
95 SendTextUpdatedEvent();
98 void wxTextCtrl::AppendText(wxString const&)
102 void wxTextCtrl::SetEditable(bool editable)
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);
108 [GetNSTextField() setEditable: editable];
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];
116 void wxTextCtrl::MarkDirty()
120 void wxTextCtrl::DiscardEdits()
124 void wxTextCtrl::SetSelection(long, long)
128 void wxTextCtrl::ShowPosition(long)
132 void wxTextCtrl::SetInsertionPoint(long)
136 void wxTextCtrl::SetInsertionPointEnd()
140 void wxTextCtrl::Cut()
144 void wxTextCtrl::Copy()
148 void wxTextCtrl::Redo()
152 void wxTextCtrl::Undo()
156 void wxTextCtrl::Clear()
160 void wxTextCtrl::Paste()
164 void wxTextCtrl::Remove(long, long)
168 void wxTextCtrl::Replace(long, long, wxString const&)
172 void wxTextCtrl::DoSetValue(wxString const& value, int flags)
174 wxAutoNSAutoreleasePool pool;
175 [GetNSTextField() setStringValue: wxNSStringWithWxString(value)];
177 if ( flags & SetValue_SendEvent )
178 SendTextUpdatedEvent();
181 void wxTextCtrl::WriteText(wxString const&)
185 bool wxTextCtrl::IsEditable() const
187 return [GetNSTextField() isEditable];
190 bool wxTextCtrl::IsModified() const
195 wxString wxTextCtrl::GetLineText(long) const
197 return wxEmptyString;
200 void wxTextCtrl::GetSelection(long*, long*) const
204 bool wxTextCtrl::PositionToXY(long, long*, long*) const
209 long wxTextCtrl::XYToPosition(long, long) const
214 int wxTextCtrl::GetLineLength(long) const
219 wxTextPos wxTextCtrl::GetLastPosition() const
221 // working - returns the size of the wxString
222 return (long)(GetValue().Len());
225 int wxTextCtrl::GetNumberOfLines() const
230 long wxTextCtrl::GetInsertionPoint() const
235 bool wxTextCtrl::CanRedo() const
240 bool wxTextCtrl::CanUndo() const
245 wxString wxTextCtrl::GetValue() const
247 wxAutoNSAutoreleasePool pool;
248 return wxStringWithNSString([GetNSTextField() stringValue]);
251 wxSize wxTextCtrl::DoGetBestSize() const
253 wxAutoNSAutoreleasePool pool;
254 wxASSERT(GetNSControl());
255 NSCell *cell = [GetNSControl() cell];
257 NSSize cellSize = [cell cellSize];
258 wxSize size(100,(int)ceil(cellSize.height));
260 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y);