X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4dd9fdf82749bb5f9e2bf9c2ad1bbd59e0eeb00e..f675b4f521c6696648674d8901e2d6162ab5e2fb:/src/osx/cocoa/stattext.mm diff --git a/src/osx/cocoa/stattext.mm b/src/osx/cocoa/stattext.mm index efb6e56343..42e5c85254 100644 --- a/src/osx/cocoa/stattext.mm +++ b/src/osx/cocoa/stattext.mm @@ -27,88 +27,98 @@ #include -wxSize wxStaticText::DoGetBestSize() const +@interface wxNSStaticTextView : NSTextField { -Point bounds; +} +@end -#if wxOSX_USE_ATSU_TEXT - OSStatus err = noErr; - wxCFStringRef str( m_label, GetFont().GetEncoding() ); +@implementation wxNSStaticTextView - SInt16 baseline; - if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont ) ++ (void)initialize +{ + static BOOL initialized = NO; + if (!initialized) { - err = GetThemeTextDimensions( - (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")), - m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline ); - verify_noerr( err ); + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); } - else -#endif +} + +@end + +class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl +{ +public: + wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w) { - wxClientDC dc(const_cast(this)); - wxCoord width, height ; - dc.GetTextExtent( m_label , &width, &height); - // Some labels seem to have their last characters - // stripped out. Adding 4 pixels seems to be enough to fix this. - bounds.h = width+4; - bounds.v = height; + m_lineBreak = lineBreak; } - if ( m_label.empty() ) - bounds.h = 0; - - bounds.h += MacGetLeftBorderSize() + MacGetRightBorderSize(); - bounds.v += MacGetTopBorderSize() + MacGetBottomBorderSize(); + 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; +}; - return wxSize( bounds.h, bounds.v ); +wxSize wxStaticText::DoGetBestSize() const +{ + return wxWindowMac::DoGetBestSize() ; } -// 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 -*/ - wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer, - wxWindowMac* parent, - wxWindowID id, - const wxString& label, + wxWindowMac* WXUNUSED(parent), + wxWindowID WXUNUSED(id), + const wxString& WXUNUSED(label), const wxPoint& pos, const wxSize& size, long style, - long extraStyle) + long WXUNUSED(extraStyle)) { NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; - wxNSTextField* v = [[wxNSTextField alloc] initWithFrame:r]; + wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r]; - [v setBezeled:NO]; [v setEditable:NO]; [v setDrawsBackground:NO]; + [v setSelectable: NO]; + [v setBezeled:NO]; + [v setBordered:NO]; - wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v ); - 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 ); - - 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; + } + else + { + [[v cell] setWraps:YES]; } - return peer; - */ + + wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak ); + return c; } #endif //if wxUSE_STATTEXT