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/NSCell.h>
31 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase)
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([[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 // NSTextField only sends the action message on enter key press and thus
97 // we send the appropriate event type.
98 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId());
100 // See wxTextCtrlBase::SendTextUpdatedEvent for why we don't set the string.
101 //event.SetString(GetValue());
103 event.SetEventObject(this);
104 GetEventHandler()->ProcessEvent(event);
107 void wxTextCtrl::AppendText(wxString const&)
111 void wxTextCtrl::SetEditable(bool editable)
113 // first ensure that the current value is stored (in case the user had not finished editing
114 // before SetEditable(FALSE) was called)
115 DoSetValue(GetValue(),1);
117 [GetNSTextField() setEditable: editable];
119 // forces the focus on the textctrl to be lost - while focus is still maintained
120 // after SetEditable(FALSE) the user may still edit the control
121 // (might not the best way to do this..)
122 [GetNSTextField() abortEditing];
125 void wxTextCtrl::MarkDirty()
129 void wxTextCtrl::DiscardEdits()
133 void wxTextCtrl::SetSelection(long, long)
137 void wxTextCtrl::ShowPosition(long)
141 void wxTextCtrl::SetInsertionPoint(long)
145 void wxTextCtrl::SetInsertionPointEnd()
149 void wxTextCtrl::Cut()
153 void wxTextCtrl::Copy()
157 void wxTextCtrl::Redo()
161 void wxTextCtrl::Undo()
165 void wxTextCtrl::Clear()
169 void wxTextCtrl::Paste()
173 void wxTextCtrl::Remove(long, long)
177 void wxTextCtrl::Replace(long, long, wxString const&)
181 void wxTextCtrl::DoSetValue(wxString const& value, int flags)
183 wxAutoNSAutoreleasePool pool;
184 [GetNSTextField() setStringValue: wxNSStringWithWxString(value)];
186 if ( flags & SetValue_SendEvent )
187 SendTextUpdatedEvent();
190 void wxTextCtrl::WriteText(wxString const&)
194 bool wxTextCtrl::IsEditable() const
196 return [GetNSTextField() isEditable];
199 bool wxTextCtrl::IsModified() const
204 wxString wxTextCtrl::GetLineText(long) const
206 return wxEmptyString;
209 void wxTextCtrl::GetSelection(long*, long*) const
213 bool wxTextCtrl::PositionToXY(long, long*, long*) const
218 long wxTextCtrl::XYToPosition(long, long) const
223 int wxTextCtrl::GetLineLength(long) const
228 wxTextPos wxTextCtrl::GetLastPosition() const
230 // working - returns the size of the wxString
231 return (long)(GetValue().Len());
234 int wxTextCtrl::GetNumberOfLines() const
239 long wxTextCtrl::GetInsertionPoint() const
244 bool wxTextCtrl::CanRedo() const
249 bool wxTextCtrl::CanUndo() const
254 wxString wxTextCtrl::GetValue() const
256 wxAutoNSAutoreleasePool pool;
257 return wxStringWithNSString([GetNSTextField() stringValue]);
260 wxSize wxTextCtrl::DoGetBestSize() const
262 wxAutoNSAutoreleasePool pool;
263 wxASSERT(GetNSControl());
264 NSCell *cell = [GetNSControl() cell];
266 NSSize cellSize = [cell cellSize];
267 wxSize size(100,(int)ceil(cellSize.height));
269 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y);