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