1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/textctrl.mm
4 // Author: David Elliott
5 // Modified by: Mark Oxenham
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/textctrl.h"
21 #include "wx/cocoa/string.h"
23 #include "wx/cocoa/autorelease.h"
25 #import <Foundation/NSString.h>
26 #import <AppKit/NSTextField.h>
27 #import <AppKit/NSSecureTextField.h>
28 #import <AppKit/NSCell.h>
32 BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
34 WX_IMPLEMENT_COCOA_OWNER(wxTextCtrl,NSTextField,NSControl,NSView)
36 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID winid,
37 const wxString& value,
41 const wxValidator& validator,
44 wxAutoNSAutoreleasePool pool;
45 if(!CreateControl(parent,winid,pos,size,style,validator,name))
48 SetNSTextField([(style & wxTE_PASSWORD)?[NSSecureTextField alloc]:[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
49 [m_cocoaNSView release];
50 [GetNSTextField() setStringValue:wxNSStringWithWxString(value)];
52 [GetNSControl() sizeToFit];
53 NSRect currentFrame = [m_cocoaNSView frame];
54 if(currentFrame.size.width < 70)
56 currentFrame.size.width = 70;
57 [m_cocoaNSView setFrame:currentFrame];
60 m_parent->CocoaAddChild(this);
61 SetInitialFrameRect(pos,size);
63 [(NSTextField*)m_cocoaNSView setTarget: sm_cocoaTarget];
64 [(NSTextField*)m_cocoaNSView setAction:@selector(wxNSControlAction:)];
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];
76 // if Read-only then set as such, this flag is overwritable by wxTextCtrl::SetEditable(TRUE)
77 if (style & wxTE_READONLY)
85 wxTextCtrl::~wxTextCtrl()
87 DisassociateNSTextField(GetNSTextField());
90 void wxTextCtrl::Cocoa_didChangeText(void)
94 void wxTextCtrl::CocoaTarget_action(void)
96 SendTextUpdatedEvent();
99 void wxTextCtrl::AppendText(wxString const&)
103 void wxTextCtrl::SetEditable(bool editable)
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);
109 [GetNSTextField() setEditable: editable];
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];
117 void wxTextCtrl::MarkDirty()
121 void wxTextCtrl::DiscardEdits()
125 void wxTextCtrl::SetSelection(long, long)
129 void wxTextCtrl::ShowPosition(long)
133 void wxTextCtrl::SetInsertionPoint(long)
137 void wxTextCtrl::SetInsertionPointEnd()
141 void wxTextCtrl::Cut()
145 void wxTextCtrl::Copy()
149 void wxTextCtrl::Redo()
153 void wxTextCtrl::Undo()
157 void wxTextCtrl::Clear()
161 void wxTextCtrl::Paste()
165 void wxTextCtrl::Remove(long, long)
169 void wxTextCtrl::Replace(long, long, wxString const&)
173 void wxTextCtrl::DoSetValue(wxString const& value, int flags)
175 wxAutoNSAutoreleasePool pool;
176 [GetNSTextField() setStringValue: wxNSStringWithWxString(value)];
178 if ( flags & SetValue_SendEvent )
179 SendTextUpdatedEvent();
182 void wxTextCtrl::WriteText(wxString const&)
186 bool wxTextCtrl::IsEditable() const
188 return [GetNSTextField() isEditable];
191 bool wxTextCtrl::IsModified() const
196 wxString wxTextCtrl::GetLineText(long) const
198 return wxEmptyString;
201 void wxTextCtrl::GetSelection(long*, long*) const
205 bool wxTextCtrl::PositionToXY(long, long*, long*) const
210 long wxTextCtrl::XYToPosition(long, long) const
215 int wxTextCtrl::GetLineLength(long) const
220 wxTextPos wxTextCtrl::GetLastPosition() const
222 // working - returns the size of the wxString
223 return (long)(GetValue().Len());
226 int wxTextCtrl::GetNumberOfLines() const
231 long wxTextCtrl::GetInsertionPoint() const
236 bool wxTextCtrl::CanRedo() const
241 bool wxTextCtrl::CanUndo() const
246 wxString wxTextCtrl::GetValue() const
248 wxAutoNSAutoreleasePool pool;
249 return wxStringWithNSString([GetNSTextField() stringValue]);
252 wxSize wxTextCtrl::DoGetBestSize() const
254 wxAutoNSAutoreleasePool pool;
255 wxASSERT(GetNSControl());
256 NSCell *cell = [GetNSControl() cell];
258 NSSize cellSize = [cell cellSize];
259 wxSize size(100,(int)ceil(cellSize.height));
261 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y);