]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/stattext.mm
Workaround for #15404: wxRichTextCtrl: caret does not disappear when focus is lost...
[wxWidgets.git] / src / osx / cocoa / stattext.mm
CommitLineData
f033830e
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/stattext.mm
3// Purpose: wxStaticText
4// Author: Stefan Csomor
5// Modified by:
6// Created: 04/01/98
f033830e
SC
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
f033830e
SC
25#include "wx/osx/private.h"
26
f672c969
VZ
27#if wxUSE_MARKUP
28 #include "wx/osx/cocoa/private/markuptoattr.h"
29#endif // wxUSE_MARKUP
30
f033830e
SC
31#include <stdio.h>
32
f1c40652 33@interface wxNSStaticTextView : NSTextField
c2a4d428 34{
c2a4d428 35}
c2a4d428 36@end
f1c40652 37
c2a4d428
KO
38@implementation wxNSStaticTextView
39
40+ (void)initialize
41{
42 static BOOL initialized = NO;
03647350
VZ
43 if (!initialized)
44 {
c2a4d428
KO
45 initialized = YES;
46 wxOSXCocoaClassAddWXMethods( self );
47 }
48}
49
8f5bce64
SC
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
c2a4d428
KO
69@end
70
a4e32492
KO
71class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
72{
73public:
f1c40652 74 wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w , NSLineBreakMode lineBreak) : wxWidgetCocoaImpl(peer, w)
a4e32492 75 {
f1c40652 76 m_lineBreak = lineBreak;
a4e32492 77 }
03647350
VZ
78
79 virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
80 {
d8207702 81 wxCFStringRef text( title , encoding );
f1c40652 82
f672c969
VZ
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
98private:
99 void DoSetAttrString(NSMutableAttributedString *attrstring)
100 {
f1c40652
SC
101 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
102 [paragraphStyle setLineBreakMode:m_lineBreak];
f672c969 103 int style = GetWXPeer()->GetWindowStyleFlag();
af098dd3 104 if (style & wxALIGN_CENTER_HORIZONTAL)
f1c40652 105 [paragraphStyle setAlignment: NSCenterTextAlignment];
a4e32492 106 else if (style & wxALIGN_RIGHT)
f1c40652 107 [paragraphStyle setAlignment: NSRightTextAlignment];
03647350 108
f672c969
VZ
109 [attrstring addAttribute:NSParagraphStyleAttributeName
110 value:paragraphStyle
111 range:NSMakeRange(0, [attrstring length])];
112 NSCell* cell = [(wxNSStaticTextView *)GetWXWidget() cell];
f1c40652 113 [cell setAttributedStringValue:attrstring];
f1c40652 114 [paragraphStyle release];
a4e32492 115 }
f672c969 116
f1c40652 117 NSLineBreakMode m_lineBreak;
a4e32492 118};
c2a4d428 119
f033830e
SC
120wxSize wxStaticText::DoGetBestSize() const
121{
f1c40652 122 return wxWindowMac::DoGetBestSize() ;
f033830e
SC
123}
124
6e7a89af 125wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
d8207702
SC
126 wxWindowMac* WXUNUSED(parent),
127 wxWindowID WXUNUSED(id),
128 const wxString& WXUNUSED(label),
6e7a89af 129 const wxPoint& pos,
f033830e 130 const wxSize& size,
6e7a89af 131 long style,
d8207702 132 long WXUNUSED(extraStyle))
f033830e 133{
dbeddfb9 134 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
c2a4d428 135 wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
f033830e 136
f033830e
SC
137 [v setEditable:NO];
138 [v setDrawsBackground:NO];
44ca06dd 139 [v setSelectable: NO];
f1c40652
SC
140 [v setBezeled:NO];
141 [v setBordered:NO];
03647350 142
f1c40652
SC
143 NSLineBreakMode linebreak = NSLineBreakByWordWrapping;
144 if ( ((wxStaticText*)wxpeer)->IsEllipsized() )
f033830e 145 {
f033830e 146 if ( style & wxST_ELLIPSIZE_MIDDLE )
f1c40652
SC
147 linebreak = NSLineBreakByTruncatingMiddle;
148 else if (style & wxST_ELLIPSIZE_END )
149 linebreak = NSLineBreakByTruncatingTail;
150 else if (style & wxST_ELLIPSIZE_START )
151 linebreak = NSLineBreakByTruncatingHead;
f033830e 152 }
03647350 153 else
f1c40652
SC
154 {
155 [[v cell] setWraps:YES];
156 }
03647350 157
f1c40652
SC
158 wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v, linebreak );
159 return c;
f033830e
SC
160}
161
162#endif //if wxUSE_STATTEXT