]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/stattext.mm
making sure peer->Move refreshes properly, bringing in synch with carbon, applying...
[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 class wxStaticTextCocoaImpl : public wxWidgetCocoaImpl
62 {
63 public:
64 wxStaticTextCocoaImpl( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
65 {
66 }
67
68 virtual void SetLabel(const wxString& title, wxFontEncoding encoding)
69 {
70 wxNSStaticTextView* v = (wxNSStaticTextView*)GetWXWidget();
71 wxWindow* wxpeer = GetWXPeer();
72 [v setString: wxCFStringRef( title , wxpeer->GetFont().GetEncoding() ).AsNSString()];
73
74 int style = wxpeer->GetWindowStyleFlag();
75 NSRange allText = NSMakeRange(0, title.length());
76 if (style & wxALIGN_CENTER)
77 [v setAlignment: NSCenterTextAlignment range: allText];
78 else if (style & wxALIGN_RIGHT)
79 [v setAlignment: NSRightTextAlignment range: allText];
80 }
81 };
82
83 wxSize wxStaticText::DoGetBestSize() const
84 {
85 Point bounds;
86
87 #if wxOSX_USE_ATSU_TEXT
88 OSStatus err = noErr;
89 wxCFStringRef str( m_label, GetFont().GetEncoding() );
90
91 SInt16 baseline;
92 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
93 {
94 err = GetThemeTextDimensions(
95 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
96 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
97 verify_noerr( err );
98 }
99 else
100 #endif
101 {
102 wxClientDC dc(const_cast<wxStaticText*>(this));
103 wxCoord width, height ;
104 dc.GetMultiLineTextExtent( m_label , &width, &height);
105 // FIXME: The calculations returned by this function are too small
106 // for some strings, so we adjust manually.
107 bounds.h = width+12;
108 bounds.v = height+4;
109 }
110
111 if ( m_label.empty() )
112 bounds.h = 0;
113
114 bounds.h += MacGetLeftBorderSize() + MacGetRightBorderSize();
115 bounds.v += MacGetTopBorderSize() + MacGetBottomBorderSize();
116
117 return wxSize( bounds.h, bounds.v );
118 }
119
120 // for wxST_ELLIPSIZE_* support:
121
122 /*
123 FIXME: UpdateLabel() should be called on size events when wxST_ELLIPSIZE_START is set
124 to allow correct dynamic ellipsizing of the label
125 */
126
127 wxWidgetImplType* wxWidgetImpl::CreateStaticText( wxWindowMac* wxpeer,
128 wxWindowMac* parent,
129 wxWindowID id,
130 const wxString& label,
131 const wxPoint& pos,
132 const wxSize& size,
133 long style,
134 long extraStyle)
135 {
136 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
137 wxNSStaticTextView* v = [[wxNSStaticTextView alloc] initWithFrame:r];
138
139 [v setEditable:NO];
140 [v setDrawsBackground:NO];
141 [v setSelectable: NO];
142
143 wxWidgetCocoaImpl* c = new wxStaticTextCocoaImpl( wxpeer, v );
144 return c;
145 /*
146 Rect bounds = wxMacGetBoundsForControl( wxpeer, pos, size );
147
148 wxMacControl* peer = new wxMacControl( wxpeer );
149 OSStatus err = CreateStaticTextControl(
150 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
151 &bounds, NULL, NULL, peer->GetControlRefAddr() );
152 verify_noerr( err );
153
154 if ( ( style & wxST_ELLIPSIZE_END ) || ( style & wxST_ELLIPSIZE_MIDDLE ) )
155 {
156 TruncCode tCode = truncEnd;
157 if ( style & wxST_ELLIPSIZE_MIDDLE )
158 tCode = truncMiddle;
159
160 err = peer->SetData( kControlStaticTextTruncTag, tCode );
161 err = peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 );
162 }
163 return peer;
164 */
165 }
166
167 #endif //if wxUSE_STATTEXT