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
61 class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
64 wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
68 virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
70 wxNSStaticTextView* v = (wxNSStaticTextView*)GetWXWidget();
71 wxWindow* wxpeer = GetWXPeer();
72 [v setString: wxCFStringRef( title , wxpeer->GetFont().GetEncoding() ).AsNSString()];
74 int style = wxpeer->GetWindowStyleFlag();
75 NSRange allText = NSMakeRange(0, title.length());
76 if (style & wxALIGN_CENTER)
77 [v setAlignment: NSCenterTextAlignment range: allText];
78 else if (style & wxALIGN_RIGHT)
79 [v setAlignment: NSRightTextAlignment range: allText];
83 wxSize wxStaticText::DoGetBestSize() const
87 #if wxOSX_USE_ATSU_TEXT
89 wxCFStringRef str( m_label, GetFont().GetEncoding() );
92 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
94 err = GetThemeTextDimensions(
95 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
96 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
102 wxClientDC dc(const_cast<wxStaticText*>(this));
103 wxCoord width, height ;
104 dc.GetMultiLineTextExtent( m_label , &width, &height);
105 // FIXME: The calculations returned by this function are too small
106 // for some strings, so we adjust manually.
111 if ( m_label.empty() )
114 bounds.h += MacGetLeftBorderSize() + MacGetRightBorderSize();
115 bounds.v += MacGetTopBorderSize() + MacGetBottomBorderSize();
117 return wxSize( bounds.h, bounds.v );
120 // for wxST_ELLIPSIZE_* support:
123 FIXME: UpdateLabel() should be called on size events when wxST_ELLIPSIZE_START is set
124 to allow correct dynamic ellipsizing of the label
127 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
130 const wxString& label,
136 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
137 wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
140 [v setDrawsBackground:NO];
141 [v setSelectable: NO];
143 wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v );
146 Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size );
148 wxMacControl* peer = new wxMacControl( wxpeer );
149 OSStatus err = CreateStaticTextControl(
150 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
151 &bounds, NULL, NULL, peer->GetControlRefAddr() );
154 if ( ( style & wxST_ELLIPSIZE_END ) || ( style & wxST_ELLIPSIZE_MIDDLE ) )
156 TruncCode tCode = truncEnd;
157 if ( style & wxST_ELLIPSIZE_MIDDLE )
160 err = peer->SetData( kControlStaticTextTruncTag, tCode );
161 err = peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 );
167 #endif //if wxUSE_STATTEXT