diff --git a/include/wx/cocoa/textctrl.h b/include/wx/cocoa/textctrl.h index 290ac2a485..050b5e9074 100644 --- a/include/wx/cocoa/textctrl.h +++ b/include/wx/cocoa/textctrl.h @@ -51,6 +51,11 @@ public: protected: virtual void Cocoa_didChangeText(void); virtual void CocoaTarget_action(void); +public: // We can't make an Objective-C class or method a friend + // These are the new (non-virtual for ABI compatibility) messages: + void CocoaNotification_controlTextDidChange(WX_NSNotification notification); + void CocoaNotification_controlTextDidEndEditing(WX_NSNotification notification); + void CocoaNotification_controlTextDidBeginEditing(WX_NSNotification notification); // ------------------------------------------------------------------------ // Implementation // ------------------------------------------------------------------------ diff --git a/src/cocoa/textctrl.mm b/src/cocoa/textctrl.mm index 67da0acd68..09f25f9b18 100644 --- a/src/cocoa/textctrl.mm +++ b/src/cocoa/textctrl.mm @@ -21,6 +21,7 @@ #include "wx/cocoa/string.h" #include "wx/cocoa/autorelease.h" +#include "wx/cocoa/ObjcRef.h" #import #import @@ -32,8 +33,9 @@ IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase) BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase) END_EVENT_TABLE() -WX_IMPLEMENT_COCOA_OWNER(wxTextCtrl,NSTextField,NSControl,NSView) +// Replaced by special 2.8 code: +//WX_IMPLEMENT_COCOA_OWNER(wxTextCtrl,NSTextField,NSControl,NSView) bool wxTextCtrl::Create(wxWindow *parent, wxWindowID winid, const wxString& value, const wxPoint& pos, @@ -270,3 +272,67 @@ wxSize wxTextCtrl::DoGetBestSize() const wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxTextCtrl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y); return size; } + +/////////////////////////////////////////////////////////////////////////// +// Added within the 2.8 release cycle, just after 2.8.7: + +@interface WXNSTextFieldDelegate : NSObject +{ +} + +- (void)controlTextDidChange:(NSNotification*)notification; +@end // @interface WXNSTextFieldDelegate : NSObject + +WX_DECLARE_HASH_MAP(WX_NSTextField, wxTextCtrl*, wxPointerHash, wxPointerEqual, wxCocoaNSTextField_wxTextCtrl_Hash); + +static wxCocoaNSTextField_wxTextCtrl_Hash sg_textField_textCtrl_hash; +static wxObjcAutoRefFromAlloc sg_cocoaDelegate([[WXNSTextFieldDelegate alloc] init]); + +static inline wxTextCtrl* GetTextCtrlFromCocoaTextField(WX_NSTextField cocoaObjcClass) +{ + wxCocoaNSTextField_wxTextCtrl_Hash::iterator iter = sg_textField_textCtrl_hash.find(cocoaObjcClass); + if(iter != sg_textField_textCtrl_hash.end()) + return iter->second; + return NULL; +} + +void wxTextCtrl::SetNSTextField(WX_NSTextField cocoaObjcClass) +{ + DisassociateNSTextField((WX_NSTextField)m_cocoaNSView); + if(m_cocoaNSView) + { + sg_textField_textCtrl_hash.erase((WX_NSTextField)m_cocoaNSView); + [(WX_NSTextField)m_cocoaNSView setDelegate:nil]; + } + SetNSControl(cocoaObjcClass); + AssociateNSTextField((WX_NSTextField)m_cocoaNSView); + if(m_cocoaNSView) + { + sg_textField_textCtrl_hash.insert(wxCocoaNSTextField_wxTextCtrl_Hash::value_type((WX_NSTextField)m_cocoaNSView, this)); + [(WX_NSTextField)m_cocoaNSView setDelegate:sg_cocoaDelegate]; + } +} + +inline void wxTextCtrl::CocoaNotification_controlTextDidChange(WX_NSNotification notification) +{ + wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); + + // See wxTextCtrlBase::SendTextUpdatedEvent for why we don't set the string. + //event.SetString(GetValue()); + + event.SetEventObject(this); + GetEventHandler()->ProcessEvent(event); +} + +@implementation WXNSTextFieldDelegate : NSObject + +- (void)controlTextDidChange:(NSNotification*)notification +{ + wxTextCtrl *tc = GetTextCtrlFromCocoaTextField([notification object]); + if(tc != NULL) + tc->CocoaNotification_controlTextDidChange(notification); +} + + +@end // @implementation WXNSTextFieldDelegate : NSObject +