]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/stattext.cpp
Menu label consistency changes
[wxWidgets.git] / src / mac / carbon / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/stattext.cpp
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/notebook.h"
27 #include "wx/tabctrl.h"
28
29 #include "wx/mac/uma.h"
30
31 #include <stdio.h>
32
33 IMPLEMENT_DYNAMIC_CLASS(wxStaticText, wxControl)
34
35
36 bool wxStaticText::Create( wxWindow *parent,
37 wxWindowID id,
38 const wxString& label,
39 const wxPoint& pos,
40 const wxSize& size,
41 long style,
42 const wxString& name )
43 {
44 m_macIsUserPane = false;
45
46 if ( !wxControl::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
47 return false;
48
49 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
50
51 m_peer = new wxMacControl( this );
52 OSStatus err = CreateStaticTextControl(
53 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
54 &bounds, NULL, NULL, m_peer->GetControlRefAddr() );
55 verify_noerr( err );
56
57 if ( ( style & wxST_ELLIPSIZE_END ) || ( style & wxST_ELLIPSIZE_MIDDLE ) )
58 {
59 TruncCode tCode = truncEnd;
60 if ( style & wxST_ELLIPSIZE_MIDDLE )
61 tCode = truncMiddle;
62
63 err = m_peer->SetData( kControlStaticTextTruncTag, tCode );
64 err = m_peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 );
65 }
66
67 MacPostControlCreate( pos, size );
68
69 SetLabel(label);
70
71 return true;
72 }
73
74 wxSize wxStaticText::DoGetBestSize() const
75 {
76 Rect bestsize = { 0 , 0 , 0 , 0 } ;
77 Point bounds;
78
79 // try the built-in best size if available
80 Boolean former = m_peer->GetData<Boolean>( kControlStaticTextIsMultilineTag);
81 m_peer->SetData( kControlStaticTextIsMultilineTag, (Boolean)0 );
82 m_peer->GetBestRect( &bestsize ) ;
83 m_peer->SetData( kControlStaticTextIsMultilineTag, former );
84 if ( !EmptyRect( &bestsize ) )
85 {
86 bounds.h = bestsize.right - bestsize.left ;
87 bounds.v = bestsize.bottom - bestsize.top ;
88 }
89 else
90 {
91 ControlFontStyleRec controlFont;
92 OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
93 verify_noerr( err );
94
95 SInt16 baseline;
96 wxMacCFStringHolder str( m_label, m_font.GetEncoding() );
97
98 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
99 {
100 err = GetThemeTextDimensions(
101 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
102 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
103 verify_noerr( err );
104 }
105 else
106 {
107 #if wxMAC_USE_CORE_GRAPHICS
108 wxClientDC dc(const_cast<wxStaticText*>(this));
109 wxCoord width, height ;
110 dc.GetTextExtent( m_label , &width, &height);
111 bounds.h = width;
112 bounds.v = height;
113 #else
114 wxMacWindowStateSaver sv( this );
115 ::TextFont( m_font.MacGetFontNum() );
116 ::TextSize( (short)(m_font.MacGetFontSize()) );
117 ::TextFace( m_font.MacGetFontStyle() );
118
119 err = GetThemeTextDimensions(
120 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
121 kThemeCurrentPortFont, kThemeStateActive, false, &bounds, &baseline );
122 verify_noerr( err );
123 #endif
124 }
125
126 if ( m_label.empty() )
127 bounds.h = 0;
128 }
129 bounds.h += MacGetLeftBorderSize() + MacGetRightBorderSize();
130 bounds.v += MacGetTopBorderSize() + MacGetBottomBorderSize();
131
132 return wxSize( bounds.h, bounds.v );
133 }
134
135 void wxStaticText::SetLabel(const wxString& label)
136 {
137 m_labelOrig = label;
138
139 // middle/end ellipsization is handled by the OS:
140 if ( HasFlag(wxST_ELLIPSIZE_END) || HasFlag(wxST_ELLIPSIZE_MIDDLE) )
141 {
142 // remove markup
143 wxString str(label);
144 if (HasFlag(wxST_MARKUP))
145 str = RemoveMarkup(label);
146
147 // and leave ellipsization to the OS
148 DoSetLabel(str);
149 }
150 else // not supported natively
151 {
152 DoSetLabel(GetEllipsizedLabelWithoutMarkup());
153 }
154
155 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) &&
156 !IsEllipsized() ) // don't resize if we adjust to current size
157 {
158 InvalidateBestSize();
159 SetSize( GetBestSize() );
160 }
161
162 Refresh();
163
164 // we shouldn't need forced updates
165 // Update();
166 }
167
168 bool wxStaticText::SetFont(const wxFont& font)
169 {
170 bool ret = wxControl::SetFont( font );
171
172 if ( ret )
173 {
174 if ( !(GetWindowStyle() & wxST_NO_AUTORESIZE) )
175 {
176 InvalidateBestSize();
177 SetSize( GetBestSize() );
178 }
179 }
180
181 return ret;
182 }
183
184
185 // for wxST_ELLIPSIZE_* support:
186
187 void wxStaticText::DoSetLabel(const wxString& label)
188 {
189 m_labelOrig = label;
190 m_label = RemoveMnemonics(label);
191
192 wxMacCFStringHolder str( m_label, m_font.GetEncoding() );
193 OSStatus err = m_peer->SetData<CFStringRef>(kControlEntireControl, kControlStaticTextCFStringTag, str);
194 verify_noerr( err );
195 }
196
197 wxString wxStaticText::DoGetLabel() const
198 {
199 return m_label;
200 }
201
202 /*
203 FIXME: UpdateLabel() should be called on size events when wxST_ELLIPSIZE_START is set
204 to allow correct dynamic ellipsizing of the label
205 */
206
207 #endif //if wxUSE_STATTEXT