Fix crash when auto-sizing a wxDataViewCtrl column.
[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 // Copyright:   (c) Stefan Csomor
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_STATTEXT
14
15 #include "wx/stattext.h"
16
17 #ifndef WX_PRECOMP
18     #include "wx/app.h"
19     #include "wx/utils.h"
20     #include "wx/dc.h"
21     #include "wx/dcclient.h"
22     #include "wx/settings.h"
23 #endif // WX_PRECOMP
24
25 #include "wx/osx/private.h"
26
27 #if wxUSE_MARKUP
28     #include "wx/osx/cocoa/private/markuptoattr.h"
29 #endif // wxUSE_MARKUP
30
31 #include <stdio.h>
32
33 @interface wxNSStaticTextView : NSTextField
34 {
35 }
36 @end
37
38 @implementation wxNSStaticTextView
39
40 + (void)initialize
41 {
42     static BOOL initialized = NO;
43     if (!initialized)
44     {
45         initialized = YES;
46         wxOSXCocoaClassAddWXMethods( self );
47     }
48 }
49
50 - (void) setEnabled:(BOOL) flag 
51
52     [super setEnabled: flag]; 
53     
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 
58         if (flag)
59         { 
60             [self setTextColor: [NSColor controlTextColor]]; 
61         }
62         else 
63         { 
64             [self setTextColor: [NSColor secondarySelectedControlColor]]; 
65         } 
66     } 
67
68
69 @end
70
71 class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
72 {
73 public:
74     wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w)
75     {
76         m_lineBreak = lineBreak;
77     }
78
79     virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
80     {
81         wxCFStringRef text( title , encoding );
82
83         NSMutableAttributedString *
84             attrstring = [[NSMutableAttributedString alloc] initWithString:text.AsNSString()];
85         DoSetAttrString(attrstring);
86         [attrstring release];
87     }
88
89 #if wxUSE_MARKUP
90     virtual void SetLabelMarkup( const wxString& markup)
91     {
92         wxMarkupToAttrString toAttr(GetWXPeer(), markup);
93
94         DoSetAttrString(toAttr.GetNSAttributedString());
95     }
96 #endif // wxUSE_MARKUP
97
98 private:
99     void DoSetAttrString(NSMutableAttributedString *attrstring)
100     {
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];
108
109         [attrstring addAttribute:NSParagraphStyleAttributeName
110                     value:paragraphStyle
111                     range:NSMakeRange(0, [attrstring length])];
112         NSCell* cell = [(wxNSStaticTextView *)GetWXWidget() cell];
113         [cell setAttributedStringValue:attrstring];
114         [paragraphStyle release];
115     }
116
117     NSLineBreakMode m_lineBreak;
118 };
119
120 wxSize wxStaticText::DoGetBestSize() const
121 {
122     return wxWindowMac::DoGetBestSize() ;
123 }
124
125 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
126                                     wxWindowMac* WXUNUSED(parent),
127                                     wxWindowID WXUNUSED(id),
128                                     const wxString& WXUNUSED(label),
129                                     const wxPoint& pos,
130                                     const wxSize& size,
131                                     long style,
132                                     long WXUNUSED(extraStyle))
133 {
134     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
135     wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
136
137     [v setEditable:NO];
138     [v setDrawsBackground:NO];
139     [v setSelectable: NO];
140     [v setBezeled:NO];
141     [v setBordered:NO];
142
143     NSLineBreakMode linebreak = NSLineBreakByWordWrapping;
144     if ( ((wxStaticText*)wxpeer)->IsEllipsized() )
145     {
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;
152     }
153     else
154     {
155         [[v cell] setWraps:YES];
156     }
157
158     wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak );
159     return c;
160 }
161
162 #endif //if wxUSE_STATTEXT