1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/textctrl.mm
4 // Author: David Elliott
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:)];
69 wxTextCtrl::~wxTextCtrl()
71 DisassociateNSTextField(GetNSTextField());
74 void wxTextCtrl::Cocoa_didChangeText(void)
78 void wxTextCtrl::CocoaTarget_action(void)
80 // NSTextField only sends the action message on enter key press and thus
81 // we send the appropriate event type.
82 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId());
84 // See wxTextCtrlBase::SendTextUpdatedEvent for why we don't set the string.
85 //event.SetString(GetValue());
87 event.SetEventObject(this);
88 GetEventHandler()->ProcessEvent(event);
91 void wxTextCtrl::AppendText(wxString const&)
95 void wxTextCtrl::SetEditable(bool)
99 void wxTextCtrl::MarkDirty()
103 void wxTextCtrl::DiscardEdits()
107 void wxTextCtrl::SetSelection(long, long)
111 void wxTextCtrl::ShowPosition(long)
115 void wxTextCtrl::SetInsertionPoint(long)
119 void wxTextCtrl::SetInsertionPointEnd()
123 void wxTextCtrl::Cut()
127 void wxTextCtrl::Copy()
131 void wxTextCtrl::Redo()
135 void wxTextCtrl::Undo()
139 void wxTextCtrl::Clear()
143 void wxTextCtrl::Paste()
147 void wxTextCtrl::Remove(long, long)
151 void wxTextCtrl::Replace(long, long, wxString const&)
155 void wxTextCtrl::DoSetValue(wxString const& value, int flags)
157 wxAutoNSAutoreleasePool pool;
158 [GetNSTextField() setStringValue: wxNSStringWithWxString(value)];
160 if ( flags & SetValue_SendEvent )
161 SendTextUpdatedEvent();
164 void wxTextCtrl::WriteText(wxString const&)
168 bool wxTextCtrl::IsEditable() const
173 bool wxTextCtrl::IsModified() const
178 wxString wxTextCtrl::GetLineText(long) const
180 return wxEmptyString;
183 void wxTextCtrl::GetSelection(long*, long*) const
187 bool wxTextCtrl::PositionToXY(long, long*, long*) const
192 long wxTextCtrl::XYToPosition(long, long) const
197 int wxTextCtrl::GetLineLength(long) const
202 wxTextPos wxTextCtrl::GetLastPosition() const
207 int wxTextCtrl::GetNumberOfLines() const
212 long wxTextCtrl::GetInsertionPoint() const
217 bool wxTextCtrl::CanRedo() const
222 bool wxTextCtrl::CanUndo() const
227 wxString wxTextCtrl::GetValue() const
229 wxAutoNSAutoreleasePool pool;
230 return wxStringWithNSString([GetNSTextField() stringValue]);
233 wxSize wxTextCtrl::DoGetBestSize() const
235 wxAutoNSAutoreleasePool pool;
236 wxASSERT(GetNSControl());
237 NSCell *cell = [GetNSControl() cell];
239 NSSize cellSize = [cell cellSize];
240 wxSize size(100,(int)ceil(cellSize.height));
242 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y);