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 : NSTextField
35 @implementation wxNSStaticTextView
39 static BOOL initialized = NO;
43 wxOSXCocoaClassAddWXMethods( self );
47 - (void) setEnabled:(BOOL) flag
49 [super setEnabled: flag];
51 if (![self drawsBackground]) {
52 // Static text is drawn incorrectly when disabled.
53 // For an explanation, see
54 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
57 [self setTextColor: [NSColor controlTextColor]];
61 [self setTextColor: [NSColor secondarySelectedControlColor]];
68 class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
71 wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w)
73 m_lineBreak = lineBreak;
76 virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
78 wxNSStaticTextView* v = (wxNSStaticTextView*)GetWXWidget();
79 wxWindow* wxpeer = GetWXPeer();
80 NSCell* cell = [v cell];
81 wxCFStringRef text( title , encoding );
83 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
84 [paragraphStyle setLineBreakMode:m_lineBreak];
85 int style = wxpeer->GetWindowStyleFlag();
86 if (style & wxALIGN_CENTER)
87 [paragraphStyle setAlignment: NSCenterTextAlignment];
88 else if (style & wxALIGN_RIGHT)
89 [paragraphStyle setAlignment: NSRightTextAlignment];
91 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:paragraphStyle, NSParagraphStyleAttributeName, nil];
92 NSAttributedString *attrstring = [[NSAttributedString alloc] initWithString:text.AsNSString() attributes:dict];
93 [cell setAttributedStringValue:attrstring];
95 [paragraphStyle release];
98 NSLineBreakMode m_lineBreak;
101 wxSize wxStaticText::DoGetBestSize() const
103 return wxWindowMac::DoGetBestSize() ;
106 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
107 wxWindowMac* WXUNUSED(parent),
108 wxWindowID WXUNUSED(id),
109 const wxString& WXUNUSED(label),
113 long WXUNUSED(extraStyle))
115 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
116 wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
119 [v setDrawsBackground:NO];
120 [v setSelectable: NO];
124 NSLineBreakMode linebreak = NSLineBreakByWordWrapping;
125 if ( ((wxStaticText*)wxpeer)->IsEllipsized() )
127 if ( style & wxST_ELLIPSIZE_MIDDLE )
128 linebreak = NSLineBreakByTruncatingMiddle;
129 else if (style & wxST_ELLIPSIZE_END )
130 linebreak = NSLineBreakByTruncatingTail;
131 else if (style & wxST_ELLIPSIZE_START )
132 linebreak = NSLineBreakByTruncatingHead;
136 [[v cell] setWraps:YES];
139 wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak );
143 #endif //if wxUSE_STATTEXT