1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/control.mm
3 // Purpose: wxControl class
4 // Author: David Elliiott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #include "wx/control.h"
19 #include "wx/cocoa/autorelease.h"
20 #include "wx/cocoa/string.h"
21 #include "wx/cocoa/trackingrectmanager.h"
22 #include "wx/cocoa/objc/objc_uniquifying.h"
23 #include "wx/cocoa/objc/NSView.h"
25 #import <AppKit/NSControl.h>
26 #import <AppKit/NSCell.h>
27 #import <Foundation/NSException.h>
31 IMPLEMENT_ABSTRACT_CLASS(wxControl, wxWindow)
32 BEGIN_EVENT_TABLE(wxControl, wxControlBase)
34 WX_IMPLEMENT_COCOA_OWNER(wxControl,NSControl,NSView,NSView)
36 bool wxControl::Create(wxWindow *parent, wxWindowID winid,
37 const wxPoint& pos, const wxSize& size, long style,
38 const wxValidator& validator, const wxString& name)
40 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
41 if(!CreateControl(parent,winid,pos,size,style,validator,name))
43 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
45 SetNSControl([[WX_GET_OBJC_CLASS(WXNSView) alloc] initWithFrame: MakeDefaultNSRect(size)]);
46 // NOTE: YES we want to release this (to match the alloc).
47 // DoAddChild(this) will retain us again since addSubView doesn't.
48 [m_cocoaNSView release];
51 m_parent->CocoaAddChild(this);
52 SetInitialFrameRect(pos,size);
54 // Controls should have a viewable-area tracking rect by default
55 m_visibleTrackingRectManager = new wxCocoaTrackingRectManager(this);
60 wxControl::~wxControl()
62 DisassociateNSControl(GetNSControl());
65 wxSize wxControl::DoGetBestSize() const
67 wxAutoNSAutoreleasePool pool;
68 wxASSERT(GetNSControl());
69 /* We can ask single-celled controls for their cell and get its size */
71 if([GetNSControl() respondsToSelector:@selector(cell)])
72 cell = [GetNSControl() cell];
75 NSSize cellSize = [cell cellSize];
76 wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
77 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
81 /* multi-celled control? size to fit, get the size, then set it back */
82 if([GetNSControl() respondsToSelector:@selector(sizeToFit)])
84 NSRect storedRect = [m_cocoaNSView frame];
85 [GetNSControl() sizeToFit];
86 NSRect cocoaRect = [m_cocoaNSView frame];
87 wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
88 [m_cocoaNSView setFrame: storedRect];
89 wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
92 // Cocoa can't tell us the size, probably not an NSControl.
93 wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
94 return wxControlBase::DoGetBestSize();
97 bool wxControl::ProcessCommand(wxCommandEvent& event)
99 return HandleWindowEvent(event);
102 void wxControl::CocoaSetEnabled(bool enable)
104 if([GetNSControl() respondsToSelector:@selector(setEnabled:)])
105 [GetNSControl() setEnabled: enable];
108 /*static*/ void wxControl::CocoaSetLabelForObject(const wxString& label, struct objc_object *aView)
110 [aView setTitle:wxNSStringWithWxString(GetLabelText(label))];