]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/stattext.mm
patch applied with thanks, fixes #10524
[wxWidgets.git] / src / osx / cocoa / stattext.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/stattext.mm
3 // Purpose: wxStaticText
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id: stattext.cpp 54845 2008-07-30 14:52:41Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_STATTEXT
15
16 #include "wx/stattext.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #include "wx/utils.h"
21 #include "wx/dc.h"
22 #include "wx/dcclient.h"
23 #include "wx/settings.h"
24 #endif // WX_PRECOMP
25
26 #include "wx/osx/private.h"
27
28 #include <stdio.h>
29
30 @interface wxNSStaticTextView : NSTextField
31 {
32 }
33 @end
34
35 @implementation wxNSStaticTextView
36
37 + (void)initialize
38 {
39 static BOOL initialized = NO;
40 if (!initialized)
41 {
42 initialized = YES;
43 wxOSXCocoaClassAddWXMethods( self );
44 }
45 }
46
47 - (void) setEnabled:(BOOL) flag
48 {
49 [super setEnabled: flag];
50
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
55 if (flag)
56 {
57 [self setTextColor: [NSColor controlTextColor]];
58 }
59 else
60 {
61 [self setTextColor: [NSColor secondarySelectedControlColor]];
62 }
63 }
64 }
65
66 @end
67
68 class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
69 {
70 public:
71 wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w)
72 {
73 m_lineBreak = lineBreak;
74 }
75
76 virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
77 {
78 wxNSStaticTextView* v = (wxNSStaticTextView*)GetWXWidget();
79 wxWindow* wxpeer = GetWXPeer();
80 NSCell* cell = [v cell];
81 wxCFStringRef text( title , encoding );
82
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];
90
91 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:paragraphStyle, NSParagraphStyleAttributeName, nil];
92 NSAttributedString *attrstring = [[NSAttributedString alloc] initWithString:text.AsNSString() attributes:dict];
93 [cell setAttributedStringValue:attrstring];
94 [attrstring release];
95 [paragraphStyle release];
96 }
97 private :
98 NSLineBreakMode m_lineBreak;
99 };
100
101 wxSize wxStaticText::DoGetBestSize() const
102 {
103 return wxWindowMac::DoGetBestSize() ;
104 }
105
106 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
107 wxWindowMac* WXUNUSED(parent),
108 wxWindowID WXUNUSED(id),
109 const wxString& WXUNUSED(label),
110 const wxPoint& pos,
111 const wxSize& size,
112 long style,
113 long WXUNUSED(extraStyle))
114 {
115 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
116 wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
117
118 [v setEditable:NO];
119 [v setDrawsBackground:NO];
120 [v setSelectable: NO];
121 [v setBezeled:NO];
122 [v setBordered:NO];
123
124 NSLineBreakMode linebreak = NSLineBreakByWordWrapping;
125 if ( ((wxStaticText*)wxpeer)->IsEllipsized() )
126 {
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;
133 }
134 else
135 {
136 [[v cell] setWraps:YES];
137 }
138
139 wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak );
140 return c;
141 }
142
143 #endif //if wxUSE_STATTEXT