fixed wxStatusBar size calculations so that the text is not clipped
[wxWidgets.git] / src / univ / statusbr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/statusbr.cpp
3 // Purpose: wxStatusBar implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 14.10.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_STATUSBAR
27
28 #include "wx/statusbr.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/settings.h"
32 #include "wx/dcclient.h"
33 #include "wx/toplevel.h"
34 #endif
35
36 #include "wx/univ/renderer.h"
37
38 // ============================================================================
39 // implementation
40 // ============================================================================
41
42 BEGIN_EVENT_TABLE(wxStatusBarUniv, wxStatusBarBase)
43 EVT_SIZE(wxStatusBarUniv::OnSize)
44
45 WX_EVENT_TABLE_INPUT_CONSUMER(wxStatusBarUniv)
46 END_EVENT_TABLE()
47
48 WX_FORWARD_TO_INPUT_CONSUMER(wxStatusBarUniv)
49
50 // ----------------------------------------------------------------------------
51 // creation
52 // ----------------------------------------------------------------------------
53
54 void wxStatusBarUniv::Init()
55 {
56 }
57
58 bool wxStatusBarUniv::Create(wxWindow *parent,
59 wxWindowID id,
60 long style,
61 const wxString& name)
62 {
63 if ( !wxWindow::Create(parent, id,
64 wxDefaultPosition, wxDefaultSize,
65 style, name) )
66 {
67 return false;
68 }
69
70 SetFieldsCount(1);
71
72 CreateInputHandler(wxINP_HANDLER_STATUSBAR);
73
74 SetSize(DoGetBestSize());
75
76 return true;
77 }
78
79 // ----------------------------------------------------------------------------
80 // drawing
81 // ----------------------------------------------------------------------------
82
83 wxRect wxStatusBarUniv::GetTotalFieldRect(wxCoord *borderBetweenFields)
84 {
85 wxRect rect = GetClientRect();
86
87 // no, don't do this - the borders are meant to be inside this rect
88 // wxSize sizeBorders =
89 if ( borderBetweenFields )
90 *borderBetweenFields = m_renderer->GetStatusBarBorderBetweenFields();
91 //rect.Deflate(sizeBorders.x, sizeBorders.y);
92
93 // recalc the field widths if needed
94 if ( m_widthsAbs.IsEmpty() )
95 {
96 // the total width for the fields doesn't include the borders between
97 // them
98 m_widthsAbs = CalculateAbsWidths(rect.width -
99 *borderBetweenFields*(m_nFields - 1));
100 }
101
102 return rect;
103 }
104
105 void wxStatusBarUniv::DoDraw(wxControlRenderer *renderer)
106 {
107 // get the fields rect
108 wxCoord borderBetweenFields;
109 wxRect rect = GetTotalFieldRect(&borderBetweenFields);
110
111 // prepare the DC
112 wxDC& dc = renderer->GetDC();
113 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
114
115 // do draw the fields
116 int flags = IsEnabled() ? 0 : (int)wxCONTROL_DISABLED;
117 for ( int n = 0; n < m_nFields; n++ )
118 {
119 rect.width = m_widthsAbs[n];
120
121 if ( IsExposed(rect) )
122 {
123 wxTopLevelWindow *parentTLW = wxDynamicCast(GetParent(), wxTopLevelWindow);
124
125 // the size grip may be drawn only on the last field and only if we
126 // have the corresponding style and even then only if we really can
127 // resize this frame
128 if ( n == m_nFields - 1 &&
129 HasFlag(wxST_SIZEGRIP) &&
130 GetParent()->HasFlag(wxRESIZE_BORDER) &&
131 parentTLW && !parentTLW->IsMaximized() )
132 {
133 flags |= wxCONTROL_SIZEGRIP;
134 }
135
136 int style;
137 if (m_statusStyles)
138 style = m_statusStyles[n];
139 else
140 style = wxSB_NORMAL;
141 m_renderer->DrawStatusField(dc, rect, m_statusText[n], flags, style);
142 }
143
144 rect.x += rect.width + borderBetweenFields;
145 }
146 }
147
148 void wxStatusBarUniv::RefreshField(int i)
149 {
150 wxRect rect;
151 if ( GetFieldRect(i, rect) )
152 {
153 RefreshRect(rect);
154 }
155 }
156
157 // ----------------------------------------------------------------------------
158 // fields text
159 // ----------------------------------------------------------------------------
160
161 void wxStatusBarUniv::SetStatusText(const wxString& text, int number)
162 {
163 wxCHECK_RET( number >= 0 && number < m_nFields,
164 _T("invalid status bar field index in SetStatusText()") );
165
166 if ( text == m_statusText[number] )
167 {
168 // nothing changed
169 return;
170 }
171
172 m_statusText[number] = text;
173
174 RefreshField(number);
175 }
176
177 wxString wxStatusBarUniv::GetStatusText(int number) const
178 {
179 wxCHECK_MSG( number >= 0 && number < m_nFields, wxEmptyString,
180 _T("invalid status bar field index") );
181
182 return m_statusText[number];
183 }
184
185 // ----------------------------------------------------------------------------
186 // fields count/widths
187 // ----------------------------------------------------------------------------
188
189 void wxStatusBarUniv::SetFieldsCount(int number, const int *widths)
190 {
191 m_statusText.SetCount(number);
192 wxStatusBarBase::SetFieldsCount(number, widths);
193 m_widthsAbs.Empty();
194 }
195
196 void wxStatusBarUniv::SetStatusWidths(int n, const int widths[])
197 {
198 wxStatusBarBase::SetStatusWidths(n, widths);
199
200 m_widthsAbs.Empty();
201 }
202
203 // ----------------------------------------------------------------------------
204 // geometry
205 // ----------------------------------------------------------------------------
206
207 void wxStatusBarUniv::OnSize(wxSizeEvent& event)
208 {
209 // we don't need to refresh the fields whose width didn't change, so find
210 // the first field whose width did change and refresh starting from it
211 int field;
212 if ( m_statusWidths )
213 {
214 for ( field = 0; field < m_nFields; field++ )
215 {
216 if ( m_statusWidths[field] < 0 )
217 {
218 // var width field
219 break;
220 }
221 }
222 }
223 else // all fields have the same width
224 {
225 // hence all fields widths have changed
226 field = 0;
227 }
228
229 if ( field < m_nFields )
230 {
231 // call this before invalidating the old widths as we want to use them,
232 // not the new ones
233 wxRect rect = DoGetFieldRect(field);
234
235 // invalidate the widths, we'll have to recalc them
236 m_widthsAbs.Empty();
237
238 // refresh everything after the first invalid field
239 rect.y = 0;
240 rect.SetRight(event.GetSize().x);
241 rect.height = event.GetSize().y;
242 RefreshRect(rect);
243 }
244
245 event.Skip();
246 }
247
248 bool wxStatusBarUniv::GetFieldRect(int n, wxRect& rect) const
249 {
250 wxCHECK_MSG( n >= 0 && n < m_nFields, false,
251 _T("invalid field index in GetFieldRect()") );
252
253 // this is a fix for a bug exhibited by the statbar sample: if
254 // GetFieldRect() is called from the derived class OnSize() handler, then
255 // our geometry info is wrong as our OnSize() didn't invalidate m_widthsAbs
256 // yet - so recalc it just in case
257 wxConstCast(this, wxStatusBarUniv)->m_widthsAbs.Empty();
258
259 rect = DoGetFieldRect(n);
260
261 return true;
262 }
263
264 wxRect wxStatusBarUniv::DoGetFieldRect(int n) const
265 {
266 wxStatusBarUniv *self = wxConstCast(this, wxStatusBarUniv);
267
268 wxCoord borderBetweenFields;
269 wxRect rect = self->GetTotalFieldRect(&borderBetweenFields);
270
271 // it's the caller responsability to check this, if unsure - call
272 // GetFieldRect() instead
273 wxCHECK_MSG( !m_widthsAbs.IsEmpty(), rect,
274 _T("can't be called if we don't have the widths") );
275
276 for ( int i = 0; i <= n; i++ )
277 {
278 rect.width = m_widthsAbs[i];
279
280 if ( i < n )
281 rect.x += rect.width + borderBetweenFields;
282 }
283
284 return rect;
285 }
286
287 wxCoord wxStatusBarUniv::GetHeight() const
288 {
289 return GetCharHeight() + 2*GetBorderY();
290 }
291
292 wxSize wxStatusBarUniv::DoGetBestSize() const
293 {
294 return wxSize(100, GetHeight());
295 }
296
297 void wxStatusBarUniv::DoSetSize(int x, int y,
298 int width, int WXUNUSED(height),
299 int sizeFlags)
300 {
301 wxStatusBarBase::DoSetSize(x, y, width, GetHeight(), sizeFlags);
302 }
303
304 // ----------------------------------------------------------------------------
305 // misc
306 // ----------------------------------------------------------------------------
307
308 void wxStatusBarUniv::SetMinHeight(int WXUNUSED(height))
309 {
310 // nothing to do here, we don't support it - and why would we?
311 }
312
313 int wxStatusBarUniv::GetBorderX() const
314 {
315 return m_renderer->GetStatusBarBorders().x +
316 m_renderer->GetStatusBarFieldMargins().x;
317 }
318
319 int wxStatusBarUniv::GetBorderY() const
320 {
321 return m_renderer->GetStatusBarBorders().y +
322 m_renderer->GetStatusBarFieldMargins().y;
323 }
324
325 #endif // wxUSE_STATUSBAR