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"
21 #include "wx/cocoa/string.h"
22 #include "wx/cocoa/trackingrectmanager.h"
23 #include "wx/cocoa/objc/objc_uniquifying.h"
24 #include "wx/cocoa/objc/NSView.h"
26 #import <AppKit/NSControl.h>
27 #import <AppKit/NSCell.h>
28 #import <Foundation/NSException.h>
32 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
33 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
35 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
37 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
38 const wxPoint& pos, const wxSize& size, long style,
39 const wxValidator& validator, const wxString& name)
41 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
42 if(!CreateControl(parent,winid,pos,size,style,validator,name))
44 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
46 SetNSControl([[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: MakeDefaultNSRect(size)]);
47 // NOTE: YES we want to release this (to match the alloc).
48 // DoAddChild(this) will retain us again since addSubView doesn't.
49 [m_cocoaNSView release];
52 m_parent->CocoaAddChild(this);
53 SetInitialFrameRect(pos,size);
55 // Controls should have a viewable-area tracking rect by default
56 m_visibleTrackingRectManager = new wxCocoaTrackingRectManager(this);
61 wxControl::~wxControl()
63 DisassociateNSControl(GetNSControl());
66 wxSize wxControl::DoGetBestSize() const
68 wxAutoNSAutoreleasePool pool;
69 wxASSERT(GetNSControl());
70 /* We can ask single-celled controls for their cell and get its size */
72 if([GetNSControl() respondsToSelector:@selector(cell)])
73 cell = [GetNSControl() cell];
76 NSSize cellSize = [cell cellSize];
77 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
78 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
82 /* multi-celled control? size to fit, get the size, then set it back */
83 if([GetNSControl() respondsToSelector:@selector(sizeToFit)])
85 NSRect storedRect = [m_cocoaNSView frame];
86 [GetNSControl() sizeToFit];
87 NSRect cocoaRect = [m_cocoaNSView frame];
88 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
89 [m_cocoaNSView setFrame: storedRect];
90 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
93 // Cocoa can't tell us the size, probably not an NSControl.
94 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
95 return wxControlBase::DoGetBestSize();
98 bool wxControl::ProcessCommand(wxCommandEvent& event)
100 return GetEventHandler()->ProcessEvent(event);
103 void wxControl::CocoaSetEnabled(bool enable)
105 if([GetNSControl() respondsToSelector:@selector(setEnabled:)])
106 [GetNSControl() setEnabled: enable];
109 /*static*/ void wxControl::CocoaSetLabelForObject(const wxString& label, struct objc_object *aView)
111 [aView setTitle:wxNSStringWithWxString(GetLabelText(label))];