1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/stattext.mm
3 // Purpose: wxStaticText
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: stattext.cpp 54845 2008-07-30 14:52:41Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/stattext.h"
22 #include "wx/dcclient.h"
23 #include "wx/settings.h"
26 #include "wx/osx/private.h"
30 @interface wxNSStaticTextView : NSTextView
32 wxWidgetCocoaImpl* impl;
35 - (void) setImplementation:(wxWidgetCocoaImpl*) item;
36 - (wxWidgetCocoaImpl*) implementation;
38 @implementation wxNSStaticTextView
42 static BOOL initialized = NO;
46 wxOSXCocoaClassAddWXMethods( self );
50 - (wxWidgetCocoaImpl*) implementation
55 - (void) setImplementation:(wxWidgetCocoaImpl*) item
62 wxSize wxStaticText::DoGetBestSize() const
66 #if wxOSX_USE_ATSU_TEXT
68 wxCFStringRef str( m_label, GetFont().GetEncoding() );
71 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
73 err = GetThemeTextDimensions(
74 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
75 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
81 wxClientDC dc(const_cast<wxStaticText*>(this));
82 wxCoord width, height ;
83 dc.GetMultiLineTextExtent( m_label , &width, &height);
84 // FIXME: Some labels seem to have their last characters
85 // stripped out. Adding 12 pixels seems to be enough to fix this.
86 // Perhaps m_label is not being synced properly...
91 if ( m_label.empty() )
94 bounds.h += MacGetLeftBorderSize() + MacGetRightBorderSize();
95 bounds.v += MacGetTopBorderSize() + MacGetBottomBorderSize();
97 return wxSize( bounds.h, bounds.v );
100 // for wxST_ELLIPSIZE_* support:
103 FIXME: UpdateLabel() should be called on size events when wxST_ELLIPSIZE_START is set
104 to allow correct dynamic ellipsizing of the label
107 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
110 const wxString& label,
116 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
117 wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
120 [v setDrawsBackground:NO];
121 [v setString: wxCFStringRef( label , wxpeer->GetFont().GetEncoding() ).AsNSString()];
123 NSRange allText = NSMakeRange(0, label.length());
124 if (style & wxALIGN_CENTER)
125 [v setAlignment: NSCenterTextAlignment range: allText];
126 else if (style & wxALIGN_RIGHT)
127 [v setAlignment: NSRightTextAlignment range: allText];
129 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
132 Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size );
134 wxMacControl* peer = new wxMacControl( wxpeer );
135 OSStatus err = CreateStaticTextControl(
136 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
137 &bounds, NULL, NULL, peer->GetControlRefAddr() );
140 if ( ( style & wxST_ELLIPSIZE_END ) || ( style & wxST_ELLIPSIZE_MIDDLE ) )
142 TruncCode tCode = truncEnd;
143 if ( style & wxST_ELLIPSIZE_MIDDLE )
146 err = peer->SetData( kControlStaticTextTruncTag, tCode );
147 err = peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 );
153 #endif //if wxUSE_STATTEXT