1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/stattext.mm
3 // Purpose: wxStaticText
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/stattext.h"
21 #include "wx/dcclient.h"
22 #include "wx/settings.h"
25 #include "wx/osx/private.h"
28 #include "wx/osx/cocoa/private/markuptoattr.h"
29 #endif // wxUSE_MARKUP
33 @interface wxNSStaticTextView : NSTextField
38 @implementation wxNSStaticTextView
42 static BOOL initialized = NO;
46 wxOSXCocoaClassAddWXMethods( self );
50 - (void) setEnabled:(BOOL) flag
52 [super setEnabled: flag];
54 if (![self drawsBackground]) {
55 // Static text is drawn incorrectly when disabled.
56 // For an explanation, see
57 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
60 [self setTextColor: [NSColor controlTextColor]];
64 [self setTextColor: [NSColor secondarySelectedControlColor]];
71 class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
74 wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w)
76 m_lineBreak = lineBreak;
79 virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
81 wxCFStringRef text( title , encoding );
83 NSMutableAttributedString *
84 attrstring = [[NSMutableAttributedString alloc] initWithString:text.AsNSString()];
85 DoSetAttrString(attrstring);
90 virtual void SetLabelMarkup( const wxString& markup)
92 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
94 DoSetAttrString(toAttr.GetNSAttributedString());
96 #endif // wxUSE_MARKUP
99 void DoSetAttrString(NSMutableAttributedString *attrstring)
101 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
102 [paragraphStyle setLineBreakMode:m_lineBreak];
103 int style = GetWXPeer()->GetWindowStyleFlag();
104 if (style & wxALIGN_CENTER_HORIZONTAL)
105 [paragraphStyle setAlignment: NSCenterTextAlignment];
106 else if (style & wxALIGN_RIGHT)
107 [paragraphStyle setAlignment: NSRightTextAlignment];
109 [attrstring addAttribute:NSParagraphStyleAttributeName
111 range:NSMakeRange(0, [attrstring length])];
112 NSCell* cell = [(wxNSStaticTextView *)GetWXWidget() cell];
113 [cell setAttributedStringValue:attrstring];
114 [paragraphStyle release];
117 NSLineBreakMode m_lineBreak;
120 wxSize wxStaticText::DoGetBestSize() const
122 return wxWindowMac::DoGetBestSize() ;
125 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
126 wxWindowMac* WXUNUSED(parent),
127 wxWindowID WXUNUSED(id),
128 const wxString& WXUNUSED(label),
132 long WXUNUSED(extraStyle))
134 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
135 wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
138 [v setDrawsBackground:NO];
139 [v setSelectable: NO];
143 NSLineBreakMode linebreak = NSLineBreakByWordWrapping;
144 if ( ((wxStaticText*)wxpeer)->IsEllipsized() )
146 if ( style & wxST_ELLIPSIZE_MIDDLE )
147 linebreak = NSLineBreakByTruncatingMiddle;
148 else if (style & wxST_ELLIPSIZE_END )
149 linebreak = NSLineBreakByTruncatingTail;
150 else if (style & wxST_ELLIPSIZE_START )
151 linebreak = NSLineBreakByTruncatingHead;
155 [[v cell] setWraps:YES];
158 wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak );
162 #endif //if wxUSE_STATTEXT