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>
22 #import <AppKit/NSCell.h>
23 #import <Foundation/NSException.h>
27 @interface wxNonControlNSControl : NSControl
31 - (void)drawRect: (NSRect)rect;
32 @end // wxNonControlNSControl
34 @implementation wxNonControlNSControl : NSControl
35 - (void)drawRect: (NSRect)rect
37 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
38 if( !win || !win->Cocoa_drawRect(rect) )
39 [super drawRect:rect];
41 @end // wxNonControlNSControl
43 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
44 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
46 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
48 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
49 const wxPoint& pos, const wxSize& size, long style,
50 const wxValidator& validator, const wxString& name)
52 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
53 if(!CreateControl(parent,winid,pos,size,style,validator,name))
55 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
57 SetNSControl([[wxNonControlNSControl alloc] initWithFrame: MakeDefaultNSRect(size)]);
58 // NOTE: YES we want to release this (to match the alloc).
59 // DoAddChild(this) will retain us again since addSubView doesn't.
60 [m_cocoaNSView release];
62 [GetNSControl() sizeToFit];
65 m_parent->CocoaAddChild(this);
66 SetInitialFrameRect(pos,size);
71 wxControl::~wxControl()
73 DisassociateNSControl(GetNSControl());
76 wxSize wxControl::DoGetBestSize() const
78 wxAutoNSAutoreleasePool pool;
79 wxASSERT(GetNSControl());
80 /* We can ask single-celled controls for their cell and get its size */
83 cell = [GetNSControl() cell];
85 // TODO: if anything other than method not implemented, re-raise
89 NSSize cellSize = [cell cellSize];
90 wxSize size((int)ceilf(cellSize.width),(int)ceilf(cellSize.height));
91 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
95 /* multi-celled control? size to fit, get the size, then set it back */
96 NSRect storedRect = [m_cocoaNSView frame];
99 [GetNSControl() sizeToFit];
102 // TODO: if anything other than method not implemented, re-raise
106 NSRect cocoaRect = [m_cocoaNSView frame];
107 wxSize size((int)ceilf(cocoaRect.size.width),(int)ceilf(cocoaRect.size.height));
108 [m_cocoaNSView setFrame: storedRect];
109 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
112 // Cocoa can't tell us the size, probably not an NSControl.
113 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
114 return wxControlBase::DoGetBestSize();
117 bool wxControl::ProcessCommand(wxCommandEvent& event)
119 return GetEventHandler()->ProcessEvent(event);
122 void wxControl::CocoaSetEnabled(bool enable)
124 [GetNSControl() setEnabled: enable];