1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/stattext.mm
3 // Purpose: wxStaticText
4 // Author: Stefan Csomor
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"
29 #include "wx/osx/cocoa/private/markuptoattr.h"
30 #endif // wxUSE_MARKUP
34 @interface wxNSStaticTextView : NSTextField
39 @implementation wxNSStaticTextView
43 static BOOL initialized = NO;
47 wxOSXCocoaClassAddWXMethods( self );
51 - (void) setEnabled:(BOOL) flag
53 [super setEnabled: flag];
55 if (![self drawsBackground]) {
56 // Static text is drawn incorrectly when disabled.
57 // For an explanation, see
58 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
61 [self setTextColor: [NSColor controlTextColor]];
65 [self setTextColor: [NSColor secondarySelectedControlColor]];
72 class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
75 wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w)
77 m_lineBreak = lineBreak;
80 virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
82 wxCFStringRef text( title , encoding );
84 NSMutableAttributedString *
85 attrstring = [[NSMutableAttributedString alloc] initWithString:text.AsNSString()];
86 DoSetAttrString(attrstring);
91 virtual void SetLabelMarkup( const wxString& markup)
93 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
95 DoSetAttrString(toAttr.GetNSAttributedString());
97 #endif // wxUSE_MARKUP
100 void DoSetAttrString(NSMutableAttributedString *attrstring)
102 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
103 [paragraphStyle setLineBreakMode:m_lineBreak];
104 int style = GetWXPeer()->GetWindowStyleFlag();
105 if (style & wxALIGN_CENTER)
106 [paragraphStyle setAlignment: NSCenterTextAlignment];
107 else if (style & wxALIGN_RIGHT)
108 [paragraphStyle setAlignment: NSRightTextAlignment];
110 [attrstring addAttribute:NSParagraphStyleAttributeName
112 range:NSMakeRange(0, [attrstring length])];
113 NSCell* cell = [(wxNSStaticTextView *)GetWXWidget() cell];
114 [cell setAttributedStringValue:attrstring];
115 [paragraphStyle release];
118 NSLineBreakMode m_lineBreak;
121 wxSize wxStaticText::DoGetBestSize() const
123 return wxWindowMac::DoGetBestSize() ;
126 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
127 wxWindowMac* WXUNUSED(parent),
128 wxWindowID WXUNUSED(id),
129 const wxString& WXUNUSED(label),
133 long WXUNUSED(extraStyle))
135 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
136 wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
139 [v setDrawsBackground:NO];
140 [v setSelectable: NO];
144 NSLineBreakMode linebreak = NSLineBreakByWordWrapping;
145 if ( ((wxStaticText*)wxpeer)->IsEllipsized() )
147 if ( style & wxST_ELLIPSIZE_MIDDLE )
148 linebreak = NSLineBreakByTruncatingMiddle;
149 else if (style & wxST_ELLIPSIZE_END )
150 linebreak = NSLineBreakByTruncatingTail;
151 else if (style & wxST_ELLIPSIZE_START )
152 linebreak = NSLineBreakByTruncatingHead;
156 [[v cell] setWraps:YES];
159 wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak );
163 #endif //if wxUSE_STATTEXT