]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/stattext.mm
proper braces avoiding potential dangling else resulting from expansion
[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$
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 #if wxUSE_MARKUP
29 #include "wx/osx/cocoa/private/markuptoattr.h"
30 #endif // wxUSE_MARKUP
31
32 #include <stdio.h>
33
34 @interface wxNSStaticTextView : NSTextField
35 {
36 }
37 @end
38
39 @implementation wxNSStaticTextView
40
41 + (void)initialize
42 {
43 static BOOL initialized = NO;
44 if (!initialized)
45 {
46 initialized = YES;
47 wxOSXCocoaClassAddWXMethods( self );
48 }
49 }
50
51 - (void) setEnabled:(BOOL) flag
52 {
53 [super setEnabled: flag];
54
55 if (![self drawsBackground]) {
56 // Static text is drawn incorrectly when disabled.
57 // For an explanation, see
58 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
59 if (flag)
60 {
61 [self setTextColor: [NSColor controlTextColor]];
62 }
63 else
64 {
65 [self setTextColor: [NSColor secondarySelectedControlColor]];
66 }
67 }
68 }
69
70 @end
71
72 class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
73 {
74 public:
75 wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w)
76 {
77 m_lineBreak = lineBreak;
78 }
79
80 virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
81 {
82 wxCFStringRef text( title , encoding );
83
84 NSMutableAttributedString *
85 attrstring = [[NSMutableAttributedString alloc] initWithString:text.AsNSString()];
86 DoSetAttrString(attrstring);
87 [attrstring release];
88 }
89
90 #if wxUSE_MARKUP
91 virtual void SetLabelMarkup( const wxString& markup)
92 {
93 wxMarkupToAttrString toAttr(GetWXPeer(), markup);
94
95 DoSetAttrString(toAttr.GetNSAttributedString());
96 }
97 #endif // wxUSE_MARKUP
98
99 private:
100 void DoSetAttrString(NSMutableAttributedString *attrstring)
101 {
102 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
103 [paragraphStyle setLineBreakMode:m_lineBreak];
104 int style = GetWXPeer()->GetWindowStyleFlag();
105 if (style & wxALIGN_CENTER)
106 [paragraphStyle setAlignment: NSCenterTextAlignment];
107 else if (style & wxALIGN_RIGHT)
108 [paragraphStyle setAlignment: NSRightTextAlignment];
109
110 [attrstring addAttribute:NSParagraphStyleAttributeName
111 value:paragraphStyle
112 range:NSMakeRange(0, [attrstring length])];
113 NSCell* cell = [(wxNSStaticTextView *)GetWXWidget() cell];
114 [cell setAttributedStringValue:attrstring];
115 [paragraphStyle release];
116 }
117
118 NSLineBreakMode m_lineBreak;
119 };
120
121 wxSize wxStaticText::DoGetBestSize() const
122 {
123 return wxWindowMac::DoGetBestSize() ;
124 }
125
126 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
127 wxWindowMac* WXUNUSED(parent),
128 wxWindowID WXUNUSED(id),
129 const wxString& WXUNUSED(label),
130 const wxPoint& pos,
131 const wxSize& size,
132 long style,
133 long WXUNUSED(extraStyle))
134 {
135 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
136 wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
137
138 [v setEditable:NO];
139 [v setDrawsBackground:NO];
140 [v setSelectable: NO];
141 [v setBezeled:NO];
142 [v setBordered:NO];
143
144 NSLineBreakMode linebreak = NSLineBreakByWordWrapping;
145 if ( ((wxStaticText*)wxpeer)->IsEllipsized() )
146 {
147 if ( style & wxST_ELLIPSIZE_MIDDLE )
148 linebreak = NSLineBreakByTruncatingMiddle;
149 else if (style & wxST_ELLIPSIZE_END )
150 linebreak = NSLineBreakByTruncatingTail;
151 else if (style & wxST_ELLIPSIZE_START )
152 linebreak = NSLineBreakByTruncatingHead;
153 }
154 else
155 {
156 [[v cell] setWraps:YES];
157 }
158
159 wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak );
160 return c;
161 }
162
163 #endif //if wxUSE_STATTEXT