1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/control.mm
3 // Purpose: wxControl class
4 // Author: David Elliiott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/control.h"
19 #include "wx/cocoa/autorelease.h"
21 #import <AppKit/NSControl.h>
23 @interface wxNonControlNSControl : NSControl
27 - (void)drawRect: (NSRect)rect;
28 @end // wxNonControlNSControl
30 @implementation wxNonControlNSControl : NSControl
31 - (void)drawRect: (NSRect)rect
33 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
34 if( !win || !win->Cocoa_drawRect(rect) )
35 [super drawRect:rect];
37 @end // wxNonControlNSControl
39 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
40 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
42 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
44 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
45 const wxPoint& pos, const wxSize& size, long style,
46 const wxValidator& validator, const wxString& name)
48 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
49 if(!CreateControl(parent,winid,pos,size,style,validator,name))
51 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
53 SetNSControl([[wxNonControlNSControl alloc] initWithFrame: MakeDefaultNSRect(size)]);
54 // NOTE: YES we want to release this (to match the alloc).
55 // DoAddChild(this) will retain us again since addSubView doesn't.
56 [m_cocoaNSView release];
58 [GetNSControl() sizeToFit];
61 m_parent->CocoaAddChild(this);
62 SetInitialFrameRect(pos,size);
67 wxControl::~wxControl()
69 DisassociateNSControl(GetNSControl());
72 wxSize wxControl::DoGetBestSize() const
74 wxAutoNSAutoreleasePool pool;
75 wxASSERT(m_cocoaNSView);
76 NSRect storedRect = [m_cocoaNSView frame];
77 [GetNSControl() sizeToFit];
78 NSRect cocoaRect = [m_cocoaNSView frame];
79 wxSize size((int)cocoaRect.size.width+10,(int)cocoaRect.size.height);
80 [m_cocoaNSView setFrame: storedRect];
81 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d)"),this,size.x,size.y);
85 bool wxControl::ProcessCommand(wxCommandEvent& event)
87 return GetEventHandler()->ProcessEvent(event);
90 void wxControl::CocoaSetEnabled(bool enable)
92 [GetNSControl() setEnabled: enable];