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
32 wxWidgetCocoaImpl* impl;
36 @implementation wxNSStaticTextView
40 static BOOL initialized = NO;
44 wxOSXCocoaClassAddWXMethods( self );
50 class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
53 wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w)
55 m_lineBreak = lineBreak;
58 virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
60 wxNSStaticTextView* v = (wxNSStaticTextView*)GetWXWidget();
61 wxWindow* wxpeer = GetWXPeer();
62 NSCell* cell = [v cell];
63 wxCFStringRef text( title , encoding );
65 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
66 [paragraphStyle setLineBreakMode:m_lineBreak];
67 int style = wxpeer->GetWindowStyleFlag();
68 if (style & wxALIGN_CENTER)
69 [paragraphStyle setAlignment: NSCenterTextAlignment];
70 else if (style & wxALIGN_RIGHT)
71 [paragraphStyle setAlignment: NSRightTextAlignment];
73 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:paragraphStyle, NSParagraphStyleAttributeName, nil];
74 NSAttributedString *attrstring = [[NSAttributedString alloc] initWithString:text.AsNSString() attributes:dict];
75 [cell setAttributedStringValue:attrstring];
77 [paragraphStyle release];
80 NSLineBreakMode m_lineBreak;
83 wxSize wxStaticText::DoGetBestSize() const
85 return wxWindowMac::DoGetBestSize() ;
88 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
89 wxWindowMac* WXUNUSED(parent),
90 wxWindowID WXUNUSED(id),
91 const wxString& WXUNUSED(label),
95 long WXUNUSED(extraStyle))
97 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
98 wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
101 [v setDrawsBackground:NO];
102 [v setSelectable: NO];
106 NSLineBreakMode linebreak = NSLineBreakByWordWrapping;
107 if ( ((wxStaticText*)wxpeer)->IsEllipsized() )
109 if ( style & wxST_ELLIPSIZE_MIDDLE )
110 linebreak = NSLineBreakByTruncatingMiddle;
111 else if (style & wxST_ELLIPSIZE_END )
112 linebreak = NSLineBreakByTruncatingTail;
113 else if (style & wxST_ELLIPSIZE_START )
114 linebreak = NSLineBreakByTruncatingHead;
118 [[v cell] setWraps:YES];
121 wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak );
125 #endif //if wxUSE_STATTEXT