1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/textctrl.mm
4 // Author: David Elliott
5 // Modified by: Mark Oxenham
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets 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 // NSTextField only sends the action message on enter key press and thus
98 // we send the appropriate event type.
99 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId());
101 // See wxTextCtrlBase::SendTextUpdatedEvent for why we don't set the string.
102 //event.SetString(GetValue());
104 event.SetEventObject(this);
105 GetEventHandler()->ProcessEvent(event);
108 void wxTextCtrl::AppendText(wxString const&)
112 void wxTextCtrl::SetEditable(bool editable)
114 // first ensure that the current value is stored (in case the user had not finished editing
115 // before SetEditable(FALSE) was called)
116 DoSetValue(GetValue(),1);
118 [GetNSTextField() setEditable: editable];
120 // forces the focus on the textctrl to be lost - while focus is still maintained
121 // after SetEditable(FALSE) the user may still edit the control
122 // (might not the best way to do this..)
123 [GetNSTextField() abortEditing];
126 void wxTextCtrl::MarkDirty()
130 void wxTextCtrl::DiscardEdits()
134 void wxTextCtrl::SetSelection(long, long)
138 void wxTextCtrl::ShowPosition(long)
142 void wxTextCtrl::SetInsertionPoint(long)
146 void wxTextCtrl::SetInsertionPointEnd()
150 void wxTextCtrl::Cut()
154 void wxTextCtrl::Copy()
158 void wxTextCtrl::Redo()
162 void wxTextCtrl::Undo()
166 void wxTextCtrl::Clear()
170 void wxTextCtrl::Paste()
174 void wxTextCtrl::Remove(long, long)
178 void wxTextCtrl::Replace(long, long, wxString const&)
182 void wxTextCtrl::DoSetValue(wxString const& value, int flags)
184 wxAutoNSAutoreleasePool pool;
185 [GetNSTextField() setStringValue: wxNSStringWithWxString(value)];
187 if ( flags & SetValue_SendEvent )
188 SendTextUpdatedEvent();
191 void wxTextCtrl::WriteText(wxString const&)
195 bool wxTextCtrl::IsEditable() const
197 return [GetNSTextField() isEditable];
200 bool wxTextCtrl::IsModified() const
205 wxString wxTextCtrl::GetLineText(long) const
207 return wxEmptyString;
210 void wxTextCtrl::GetSelection(long*, long*) const
214 bool wxTextCtrl::PositionToXY(long, long*, long*) const
219 long wxTextCtrl::XYToPosition(long, long) const
224 int wxTextCtrl::GetLineLength(long) const
229 wxTextPos wxTextCtrl::GetLastPosition() const
231 // working - returns the size of the wxString
232 return (long)(GetValue().Len());
235 int wxTextCtrl::GetNumberOfLines() const
240 long wxTextCtrl::GetInsertionPoint() const
245 bool wxTextCtrl::CanRedo() const
250 bool wxTextCtrl::CanUndo() const
255 wxString wxTextCtrl::GetValue() const
257 wxAutoNSAutoreleasePool pool;
258 return wxStringWithNSString([GetNSTextField() stringValue]);
261 wxSize wxTextCtrl::DoGetBestSize() const
263 wxAutoNSAutoreleasePool pool;
264 wxASSERT(GetNSControl());
265 NSCell *cell = [GetNSControl() cell];
267 NSSize cellSize = [cell cellSize];
268 wxSize size(100,(int)ceil(cellSize.height));
270 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y);