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 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)
33 BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
35 WX_IMPLEMENT_COCOA_OWNER(wxTextCtrl,NSTextField,NSControl,NSView)
37 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID winid,
38 const wxString& value,
42 const wxValidator& validator,
45 wxAutoNSAutoreleasePool pool;
46 if(!CreateControl(parent,winid,pos,size,style,validator,name))
49 SetNSTextField([(style & wxTE_PASSWORD)?[NSSecureTextField alloc]:[NSTextField alloc] initWithFrame:MakeDefaultNSRect(size)]);
50 [m_cocoaNSView release];
51 [GetNSTextField() setStringValue:wxNSStringWithWxString(value)];
53 [GetNSControl() sizeToFit];
54 NSRect currentFrame = [m_cocoaNSView frame];
55 if(currentFrame.size.width < 70)
57 currentFrame.size.width = 70;
58 [m_cocoaNSView setFrame:currentFrame];
61 m_parent->CocoaAddChild(this);
62 SetInitialFrameRect(pos,size);
64 [(NSTextField*)m_cocoaNSView setTarget: sm_cocoaTarget];
65 [(NSTextField*)m_cocoaNSView setAction:@selector(wxNSControlAction:)];
67 // set the text alignment option
68 NSTextAlignment alignStyle;
69 if (style & wxTE_RIGHT)
70 alignStyle = NSRightTextAlignment;
71 else if (style & wxTE_CENTRE)
72 alignStyle = NSCenterTextAlignment;
73 else // default to wxTE_LEFT because it is 0 and can't be tested
74 alignStyle = NSLeftTextAlignment;
75 [GetNSControl() setAlignment:alignStyle];
77 // if Read-only then set as such, this flag is overwritable by wxTextCtrl::SetEditable(TRUE)
78 if (style & wxTE_READONLY)
86 wxTextCtrl::~wxTextCtrl()
88 DisassociateNSTextField(GetNSTextField());
91 void wxTextCtrl::Cocoa_didChangeText(void)
95 void wxTextCtrl::CocoaTarget_action(void)
97 SendTextUpdatedEvent();
100 void wxTextCtrl::AppendText(wxString const&)
104 void wxTextCtrl::SetEditable(bool editable)
106 // first ensure that the current value is stored (in case the user had not finished editing
107 // before SetEditable(FALSE) was called)
108 DoSetValue(GetValue(),1);
110 [GetNSTextField() setEditable: editable];
112 // forces the focus on the textctrl to be lost - while focus is still maintained
113 // after SetEditable(FALSE) the user may still edit the control
114 // (might not the best way to do this..)
115 [GetNSTextField() abortEditing];
118 void wxTextCtrl::MarkDirty()
122 void wxTextCtrl::DiscardEdits()
126 void wxTextCtrl::SetSelection(long, long)
130 void wxTextCtrl::ShowPosition(long)
134 void wxTextCtrl::SetInsertionPoint(long)
138 void wxTextCtrl::SetInsertionPointEnd()
142 void wxTextCtrl::Cut()
146 void wxTextCtrl::Copy()
150 void wxTextCtrl::Redo()
154 void wxTextCtrl::Undo()
158 void wxTextCtrl::Clear()
162 void wxTextCtrl::Paste()
166 void wxTextCtrl::Remove(long, long)
170 void wxTextCtrl::Replace(long, long, wxString const&)
174 void wxTextCtrl::DoSetValue(wxString const& value, int flags)
176 wxAutoNSAutoreleasePool pool;
177 [GetNSTextField() setStringValue: wxNSStringWithWxString(value)];
179 if ( flags & SetValue_SendEvent )
180 SendTextUpdatedEvent();
183 void wxTextCtrl::WriteText(wxString const&)
187 bool wxTextCtrl::IsEditable() const
189 return [GetNSTextField() isEditable];
192 bool wxTextCtrl::IsModified() const
197 wxString wxTextCtrl::GetLineText(long) const
199 return wxEmptyString;
202 void wxTextCtrl::GetSelection(long*, long*) const
206 bool wxTextCtrl::PositionToXY(long, long*, long*) const
211 long wxTextCtrl::XYToPosition(long, long) const
216 int wxTextCtrl::GetLineLength(long) const
221 wxTextPos wxTextCtrl::GetLastPosition() const
223 // working - returns the size of the wxString
224 return (long)(GetValue().Len());
227 int wxTextCtrl::GetNumberOfLines() const
232 long wxTextCtrl::GetInsertionPoint() const
237 bool wxTextCtrl::CanRedo() const
242 bool wxTextCtrl::CanUndo() const
247 wxString wxTextCtrl::GetValue() const
249 wxAutoNSAutoreleasePool pool;
250 return wxStringWithNSString([GetNSTextField() stringValue]);
253 wxSize wxTextCtrl::DoGetBestSize() const
255 wxAutoNSAutoreleasePool pool;
256 wxASSERT(GetNSControl());
257 NSCell *cell = [GetNSControl() cell];
259 NSSize cellSize = [cell cellSize];
260 wxSize size(100,(int)ceil(cellSize.height));
262 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y);