]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/stattext.mm
Make sure the user can't select the text.
[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 : NSTextView
31 {
32 wxWidgetCocoaImpl* impl;
33 }
34
35 - (void) setImplementation:(wxWidgetCocoaImpl*) item;
36 - (wxWidgetCocoaImpl*) implementation;
37 @end
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 - (wxWidgetCocoaImpl*) implementation
51 {
52 return impl;
53 }
54
55 - (void) setImplementation:(wxWidgetCocoaImpl*) item
56 {
57 impl = item;
58 }
59 @end
60
61
62 wxSize wxStaticText::DoGetBestSize() const
63 {
64 Point bounds;
65
66 #if wxOSX_USE_ATSU_TEXT
67 OSStatus err = noErr;
68 wxCFStringRef str( m_label, GetFont().GetEncoding() );
69
70 SInt16 baseline;
71 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
72 {
73 err = GetThemeTextDimensions(
74 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
75 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
76 verify_noerr( err );
77 }
78 else
79 #endif
80 {
81 wxClientDC dc(const_cast<wxStaticText*>(this));
82 wxCoord width, height ;
83 dc.GetMultiLineTextExtent( m_label , &width, &height);
84 // FIXME: Some labels seem to have their last characters
85 // stripped out. Adding 12 pixels seems to be enough to fix this.
86 // Perhaps m_label is not being synced properly...
87 bounds.h = width+12;
88 bounds.v = height;
89 }
90
91 if ( m_label.empty() )
92 bounds.h = 0;
93
94 bounds.h += MacGetLeftBorderSize() + MacGetRightBorderSize();
95 bounds.v += MacGetTopBorderSize() + MacGetBottomBorderSize();
96
97 return wxSize( bounds.h, bounds.v );
98 }
99
100 // for wxST_ELLIPSIZE_* support:
101
102 /*
103 FIXME: UpdateLabel() should be called on size events when wxST_ELLIPSIZE_START is set
104 to allow correct dynamic ellipsizing of the label
105 */
106
107 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
108 wxWindowMac* parent,
109 wxWindowID id,
110 const wxString& label,
111 const wxPoint& pos,
112 const wxSize& size,
113 long style,
114 long extraStyle)
115 {
116 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
117 wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
118
119 [v setEditable:NO];
120 [v setDrawsBackground:NO];
121 [v setSelectable: NO];
122 [v setString: wxCFStringRef( label , wxpeer->GetFont().GetEncoding() ).AsNSString()];
123
124 NSRange allText = NSMakeRange(0, label.length());
125 if (style & wxALIGN_CENTER)
126 [v setAlignment: NSCenterTextAlignment range: allText];
127 else if (style & wxALIGN_RIGHT)
128 [v setAlignment: NSRightTextAlignment range: allText];
129
130 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
131 return c;
132 /*
133 Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size );
134
135 wxMacControl* peer = new wxMacControl( wxpeer );
136 OSStatus err = CreateStaticTextControl(
137 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
138 &bounds, NULL, NULL, peer->GetControlRefAddr() );
139 verify_noerr( err );
140
141 if ( ( style & wxST_ELLIPSIZE_END ) || ( style & wxST_ELLIPSIZE_MIDDLE ) )
142 {
143 TruncCode tCode = truncEnd;
144 if ( style & wxST_ELLIPSIZE_MIDDLE )
145 tCode = truncMiddle;
146
147 err = peer->SetData( kControlStaticTextTruncTag, tCode );
148 err = peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 );
149 }
150 return peer;
151 */
152 }
153
154 #endif //if wxUSE_STATTEXT