X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f033830e257227dc73225809de2326db0093e5fe..01639b0809cfceac03392aeefd5a5d8af62b2014:/src/osx/cocoa/stattext.mm diff --git a/src/osx/cocoa/stattext.mm b/src/osx/cocoa/stattext.mm index f35aebba81..b9f812e308 100644 --- a/src/osx/cocoa/stattext.mm +++ b/src/osx/cocoa/stattext.mm @@ -23,155 +23,103 @@ #include "wx/settings.h" #endif // WX_PRECOMP -#include "wx/notebook.h" -#include "wx/tabctrl.h" - #include "wx/osx/private.h" #include -wxSize wxStaticText::DoGetBestSize() const +@interface wxNSStaticTextView : NSTextField { - Point bounds; -#if wxOSX_USE_CARBON - Rect bestsize = { 0 , 0 , 0 , 0 } ; - - // try the built-in best size if available - Boolean former = m_peer->GetData( kControlStaticTextIsMultilineTag); - m_peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 ); - m_peer->GetBestRect( &bestsize ) ; - m_peer->SetData( kControlStaticTextIsMultilineTag, former ); - if ( !EmptyRect( &bestsize ) ) - { - bounds.h = bestsize.right - bestsize.left ; - bounds.v = bestsize.bottom - bestsize.top ; - } - else -#endif - { -#if wxOSX_USE_CARBON - ControlFontStyleRec controlFont; - OSStatus err = m_peer->GetData( kControlEntireControl, kControlFontStyleTag, &controlFont ); - verify_noerr( err ); - - wxCFStringRef str( m_label, GetFont().GetEncoding() ); - -#if wxOSX_USE_ATSU_TEXT - SInt16 baseline; - if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont ) - { - err = GetThemeTextDimensions( - (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")), - m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline ); - verify_noerr( err ); - } - else -#endif -#endif - { - wxClientDC dc(const_cast(this)); - wxCoord width, height ; - dc.GetTextExtent( m_label , &width, &height); - bounds.h = width; - bounds.v = height; - } - - if ( m_label.empty() ) - bounds.h = 0; - } - bounds.h += MacGetLeftBorderSize() + MacGetRightBorderSize(); - bounds.v += MacGetTopBorderSize() + MacGetBottomBorderSize(); - - return wxSize( bounds.h, bounds.v ); + wxWidgetCocoaImpl* impl; } +@end -// for wxST_ELLIPSIZE_* support: - -/* - FIXME: UpdateLabel() should be called on size events when wxST_ELLIPSIZE_START is set - to allow correct dynamic ellipsizing of the label -*/ +@implementation wxNSStaticTextView -@interface wxNSTextField : NSTextField ++ (void)initialize { - wxWidgetImpl* m_impl; + static BOOL initialized = NO; + if (!initialized) + { + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); + } } -- (void)setImplementation: (wxWidgetImpl *) theImplementation; -- (wxWidgetImpl*) implementation; -- (BOOL) isFlipped; - @end -@implementation wxNSTextField - -- (void)setImplementation: (wxWidgetImpl *) theImplementation +class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl { - m_impl = theImplementation; -} - -- (wxWidgetImpl*) implementation -{ - return m_impl; -} +public: + wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w) + { + m_lineBreak = lineBreak; + } -- (BOOL) isFlipped -{ - return YES; -} + virtual void SetLabel(const wxString& title, wxFontEncoding encoding) + { + wxNSStaticTextView* v = (wxNSStaticTextView*)GetWXWidget(); + wxWindow* wxpeer = GetWXPeer(); + NSCell* cell = [v cell]; + wxCFStringRef text( title , encoding ); + + NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; + [paragraphStyle setLineBreakMode:m_lineBreak]; + int style = wxpeer->GetWindowStyleFlag(); + if (style & wxALIGN_CENTER) + [paragraphStyle setAlignment: NSCenterTextAlignment]; + else if (style & wxALIGN_RIGHT) + [paragraphStyle setAlignment: NSRightTextAlignment]; + + NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:paragraphStyle, NSParagraphStyleAttributeName, nil]; + NSAttributedString *attrstring = [[NSAttributedString alloc] initWithString:text.AsNSString() attributes:dict]; + [cell setAttributedStringValue:attrstring]; + [attrstring release]; + [paragraphStyle release]; + } +private : + NSLineBreakMode m_lineBreak; +}; -// use our common calls -- (void) setTitle:(NSString *) title +wxSize wxStaticText::DoGetBestSize() const { - [self setStringValue: title]; + return wxWindowMac::DoGetBestSize() ; } -@end - - -wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer, - wxWindowMac* parent, - wxWindowID id, - const wxString& label, - const wxPoint& pos, +wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer, + wxWindowMac* WXUNUSED(parent), + wxWindowID WXUNUSED(id), + const wxString& WXUNUSED(label), + const wxPoint& pos, const wxSize& size, - long style, - long extraStyle) + long style, + long WXUNUSED(extraStyle)) { - NSView* sv = (wxpeer->GetParent()->GetHandle() ); - - NSRect r = wxToNSRect( sv, wxRect( pos, size) ); - // Rect bounds = wxMacGetBoundsForControl( wxpeer, pos , size ) ; - wxNSTextField* v = [[wxNSTextField alloc] initWithFrame:r]; - [sv addSubview:v]; + NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; + wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r]; - [v setBezeled:NO]; [v setEditable:NO]; [v setDrawsBackground:NO]; - - wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - [v setImplementation:c]; - return c; -/* - Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size ); - - wxMacControl* peer = new wxMacControl( wxpeer ); - OSStatus err = CreateStaticTextControl( - MAC_WXHWND(parent->MacGetTopLevelWindowRef()), - &bounds, NULL, NULL, peer->GetControlRefAddr() ); - verify_noerr( err ); + [v setSelectable: NO]; + [v setBezeled:NO]; + [v setBordered:NO]; - if ( ( style & wxST_ELLIPSIZE_END ) || ( style & wxST_ELLIPSIZE_MIDDLE ) ) + NSLineBreakMode linebreak = NSLineBreakByWordWrapping; + if ( ((wxStaticText*)wxpeer)->IsEllipsized() ) { - TruncCode tCode = truncEnd; if ( style & wxST_ELLIPSIZE_MIDDLE ) - tCode = truncMiddle; - - err = peer->SetData( kControlStaticTextTruncTag, tCode ); - err = peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 ); + linebreak = NSLineBreakByTruncatingMiddle; + else if (style & wxST_ELLIPSIZE_END ) + linebreak = NSLineBreakByTruncatingTail; + else if (style & wxST_ELLIPSIZE_START ) + linebreak = NSLineBreakByTruncatingHead; } - return peer; - */ + else + { + [[v cell] setWraps:YES]; + } + + wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak ); + return c; } #endif //if wxUSE_STATTEXT