1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/control.mm
3 // Purpose: wxControl class
4 // Author: David Elliiott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/control.h"
20 #include "wx/cocoa/autorelease.h"
22 #import <AppKit/NSControl.h>
23 #import <AppKit/NSCell.h>
24 #import <Foundation/NSException.h>
28 @interface wxNonControlNSControl : NSControl
32 - (void)drawRect: (NSRect)rect;
33 @end // wxNonControlNSControl
35 @implementation wxNonControlNSControl : NSControl
36 - (void)drawRect: (NSRect)rect
38 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
39 if( !win || !win->Cocoa_drawRect(rect) )
40 [super drawRect:rect];
42 @end // wxNonControlNSControl
44 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
45 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
47 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
49 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
50 const wxPoint& pos, const wxSize& size, long style,
51 const wxValidator& validator, const wxString& name)
53 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
54 if(!CreateControl(parent,winid,pos,size,style,validator,name))
56 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
58 SetNSControl([[wxNonControlNSControl alloc] initWithFrame: MakeDefaultNSRect(size)]);
59 // NOTE: YES we want to release this (to match the alloc).
60 // DoAddChild(this) will retain us again since addSubView doesn't.
61 [m_cocoaNSView release];
63 [GetNSControl() sizeToFit];
66 m_parent->CocoaAddChild(this);
67 SetInitialFrameRect(pos,size);
72 wxControl::~wxControl()
74 DisassociateNSControl(GetNSControl());
77 wxSize wxControl::DoGetBestSize() const
79 wxAutoNSAutoreleasePool pool;
80 wxASSERT(GetNSControl());
81 /* We can ask single-celled controls for their cell and get its size */
84 cell = [GetNSControl() cell];
86 // TODO: if anything other than method not implemented, re-raise
90 NSSize cellSize = [cell cellSize];
91 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
92 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
96 /* multi-celled control? size to fit, get the size, then set it back */
97 NSRect storedRect = [m_cocoaNSView frame];
100 [GetNSControl() sizeToFit];
103 // TODO: if anything other than method not implemented, re-raise
107 NSRect cocoaRect = [m_cocoaNSView frame];
108 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
109 [m_cocoaNSView setFrame: storedRect];
110 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
113 // Cocoa can't tell us the size, probably not an NSControl.
114 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
115 return wxControlBase::DoGetBestSize();
118 bool wxControl::ProcessCommand(wxCommandEvent& event)
120 return GetEventHandler()->ProcessEvent(event);
123 void wxControl::CocoaSetEnabled(bool enable)
125 [GetNSControl() setEnabled: enable];