- wxASSERT(m_cocoaNSView);
- NSRect storedRect = [m_cocoaNSView frame];
- [GetNSControl() sizeToFit];
- NSRect cocoaRect = [m_cocoaNSView frame];
- wxSize size((int)cocoaRect.size.width+10,(int)cocoaRect.size.height);
- [m_cocoaNSView setFrame: storedRect];
- wxLogDebug("wxControl=%p::DoGetBestSize()==(%d,%d)",this,size.x,size.y);
- return size;
+ wxASSERT(GetNSControl());
+ /* We can ask single-celled controls for their cell and get its size */
+ NSCell *cell = nil;
+ if([GetNSControl() respondsToSelector:@selector(cell)])
+ cell = [GetNSControl() cell];
+ if(cell)
+ {
+ NSSize cellSize = [cell cellSize];
+ wxSize size((int)ceil(cellSize.width),(int)ceil(cellSize.height));
+ wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from NSCell"),this,size.x,size.y);
+ return size;
+ }
+
+ /* multi-celled control? size to fit, get the size, then set it back */
+ if([GetNSControl() respondsToSelector:@selector(sizeToFit)])
+ {
+ NSRect storedRect = [m_cocoaNSView frame];
+ [GetNSControl() sizeToFit];
+ NSRect cocoaRect = [m_cocoaNSView frame];
+ wxSize size((int)ceil(cocoaRect.size.width),(int)ceil(cocoaRect.size.height));
+ [m_cocoaNSView setFrame: storedRect];
+ wxLogTrace(wxTRACE_COCOA_Window_Size,wxT("wxControl=%p::DoGetBestSize()==(%d,%d) from sizeToFit"),this,size.x,size.y);
+ return size;
+ }
+ // Cocoa can't tell us the size, probably not an NSControl.
+ wxLogDebug(wxT("Class %s (or superclass still below wxControl) should implement DoGetBestSize()"),GetClassInfo()->GetClassName());
+ return wxControlBase::DoGetBestSize();