]> git.saurik.com Git - wxWidgets.git/blame - src/common/statbar.cpp
small cleanup
[wxWidgets.git] / src / common / statbar.cpp
CommitLineData
71e03035 1///////////////////////////////////////////////////////////////////////////////
3304646d 2// Name: src/common/statbar.cpp
71e03035
VZ
3// Purpose: wxStatusBarBase implementation
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 14.10.01
7// RCS-ID: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// License: wxWindows licence
71e03035
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
71e03035
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
53b6d7a2
PC
27#if wxUSE_STATUSBAR
28
3304646d
WS
29#include "wx/statusbr.h"
30
71e03035 31#ifndef WX_PRECOMP
e9a05074 32 #include "wx/frame.h"
71e03035
VZ
33#endif //WX_PRECOMP
34
1f361cdd 35#include "wx/listimpl.cpp"
259c43f6 36WX_DEFINE_LIST(wxListString)
1f361cdd 37
53b6d7a2
PC
38const wxChar wxStatusBarNameStr[] = wxT("statusBar");
39
71e03035
VZ
40// ============================================================================
41// wxStatusBarBase implementation
42// ============================================================================
43
a93e536b 44IMPLEMENT_DYNAMIC_CLASS(wxStatusBar, wxWindow)
71e03035
VZ
45
46// ----------------------------------------------------------------------------
47// ctor/dtor
48// ----------------------------------------------------------------------------
49
50wxStatusBarBase::wxStatusBarBase()
51{
52 m_nFields = 0;
53
54 InitWidths();
1f361cdd 55 InitStacks();
c2919ab3 56 InitStyles();
71e03035
VZ
57}
58
59wxStatusBarBase::~wxStatusBarBase()
60{
61 FreeWidths();
1f361cdd 62 FreeStacks();
97152f78 63 FreeStyles();
2ab82214
VZ
64
65 // notify the frame that it doesn't have a status bar any longer to avoid
66 // dangling pointers
b2e3be1c 67 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
2ab82214
VZ
68 if ( frame && frame->GetStatusBar() == this )
69 {
70 frame->SetStatusBar(NULL);
71 }
71e03035
VZ
72}
73
74// ----------------------------------------------------------------------------
75// widths array handling
76// ----------------------------------------------------------------------------
77
78void wxStatusBarBase::InitWidths()
79{
80 m_statusWidths = NULL;
81}
82
83void wxStatusBarBase::FreeWidths()
84{
85 delete [] m_statusWidths;
86}
87
c2919ab3
VZ
88// ----------------------------------------------------------------------------
89// styles array handling
90// ----------------------------------------------------------------------------
91
92void wxStatusBarBase::InitStyles()
93{
94 m_statusStyles = NULL;
95}
96
97void wxStatusBarBase::FreeStyles()
98{
99 delete [] m_statusStyles;
100}
101
71e03035
VZ
102// ----------------------------------------------------------------------------
103// field widths
104// ----------------------------------------------------------------------------
105
106void wxStatusBarBase::SetFieldsCount(int number, const int *widths)
107{
108 wxCHECK_RET( number > 0, _T("invalid field number in SetFieldsCount") );
109
d775fa82 110 bool refresh = false;
71e03035
VZ
111
112 if ( number != m_nFields )
113 {
1f361cdd
MB
114 // copy stacks if present
115 if(m_statusTextStacks)
116 {
117 wxListString **newStacks = new wxListString*[number];
118 size_t i, j, max = wxMin(number, m_nFields);
119
120 // copy old stacks
121 for(i = 0; i < max; ++i)
122 newStacks[i] = m_statusTextStacks[i];
123 // free old stacks in excess
124 for(j = i; j < (size_t)m_nFields; ++j)
125 {
126 if(m_statusTextStacks[j])
127 {
128 m_statusTextStacks[j]->Clear();
129 delete m_statusTextStacks[j];
130 }
131 }
132 // initialize new stacks to NULL
133 for(j = i; j < (size_t)number; ++j)
134 newStacks[j] = 0;
135
136 m_statusTextStacks = newStacks;
137 }
138
c2919ab3
VZ
139 // Resize styles array
140 if (m_statusStyles)
141 {
142 int *oldStyles = m_statusStyles;
143 m_statusStyles = new int[number];
144 int i, max = wxMin(number, m_nFields);
145
146 // copy old styles
147 for (i = 0; i < max; ++i)
148 m_statusStyles[i] = oldStyles[i];
149
150 // initialize new styles to wxSB_NORMAL
151 for (i = max; i < number; ++i)
152 m_statusStyles[i] = wxSB_NORMAL;
153
154 // free old styles
155 delete [] oldStyles;
156 }
157
158
71e03035
VZ
159 m_nFields = number;
160
161 ReinitWidths();
162
d775fa82 163 refresh = true;
71e03035
VZ
164 }
165 //else: keep the old m_statusWidths if we had them
166
167 if ( widths )
168 {
169 SetStatusWidths(number, widths);
170
171 // already done from SetStatusWidths()
d775fa82 172 refresh = false;
71e03035
VZ
173 }
174
175 if ( refresh )
176 Refresh();
177}
178
179void wxStatusBarBase::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n),
180 const int widths[])
181{
182 wxCHECK_RET( widths, _T("NULL pointer in SetStatusWidths") );
183
184 wxASSERT_MSG( n == m_nFields, _T("field number mismatch") );
185
186 if ( !m_statusWidths )
187 m_statusWidths = new int[m_nFields];
188
189 for ( int i = 0; i < m_nFields; i++ )
190 {
191 m_statusWidths[i] = widths[i];
192 }
193
194 // update the display after the widths changed
195 Refresh();
196}
197
c2919ab3
VZ
198void wxStatusBarBase::SetStatusStyles(int WXUNUSED_UNLESS_DEBUG(n),
199 const int styles[])
200{
201 wxCHECK_RET( styles, _T("NULL pointer in SetStatusStyles") );
202
203 wxASSERT_MSG( n == m_nFields, _T("field number mismatch") );
204
205 if ( !m_statusStyles )
206 m_statusStyles = new int[m_nFields];
207
208 for ( int i = 0; i < m_nFields; i++ )
209 {
210 m_statusStyles[i] = styles[i];
211 }
212
213 // update the display after the widths changed
214 Refresh();
215}
216
71e03035
VZ
217wxArrayInt wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal) const
218{
219 wxArrayInt widths;
220
221 if ( m_statusWidths == NULL )
222 {
5057e659 223 if ( m_nFields )
71e03035 224 {
5057e659
VZ
225 // default: all fields have the same width
226 int nWidth = widthTotal / m_nFields;
227 for ( int i = 0; i < m_nFields; i++ )
228 {
229 widths.Add(nWidth);
230 }
71e03035 231 }
5057e659 232 //else: we're empty anyhow
71e03035
VZ
233 }
234 else // have explicit status widths
235 {
236 // calculate the total width of all the fixed width fields and the
237 // total number of var field widths counting with multiplicity
238 int nTotalWidth = 0,
239 nVarCount = 0,
240 i;
241 for ( i = 0; i < m_nFields; i++ )
242 {
243 if ( m_statusWidths[i] >= 0 )
244 {
245 nTotalWidth += m_statusWidths[i];
246 }
247 else
248 {
249 nVarCount += -m_statusWidths[i];
250 }
251 }
252
253 // the amount of extra width we have per each var width field
1a83b9bd 254 int widthExtra = widthTotal - nTotalWidth;
71e03035
VZ
255
256 // do fill the array
257 for ( i = 0; i < m_nFields; i++ )
258 {
259 if ( m_statusWidths[i] >= 0 )
260 {
261 widths.Add(m_statusWidths[i]);
262 }
263 else
264 {
1a83b9bd
VZ
265 int nVarWidth = widthExtra > 0 ? (widthExtra * -m_statusWidths[i]) / nVarCount : 0;
266 nVarCount += m_statusWidths[i];
267 widthExtra -= nVarWidth;
268 widths.Add(nVarWidth);
71e03035
VZ
269 }
270 }
271 }
272
273 return widths;
274}
275
1f361cdd
MB
276// ----------------------------------------------------------------------------
277// text stacks handling
278// ----------------------------------------------------------------------------
279
280void wxStatusBarBase::InitStacks()
281{
282 m_statusTextStacks = NULL;
283}
284
285void wxStatusBarBase::FreeStacks()
286{
97152f78
VZ
287 if ( !m_statusTextStacks )
288 return;
1f361cdd 289
97152f78 290 for ( size_t i = 0; i < (size_t)m_nFields; ++i )
1f361cdd 291 {
97152f78 292 if ( m_statusTextStacks[i] )
1f361cdd 293 {
222ed1d6
MB
294 wxListString& t = *m_statusTextStacks[i];
295 WX_CLEAR_LIST(wxListString, t);
1f361cdd
MB
296 delete m_statusTextStacks[i];
297 }
298 }
299
300 delete[] m_statusTextStacks;
301}
302
303// ----------------------------------------------------------------------------
304// text stacks
305// ----------------------------------------------------------------------------
306
307void wxStatusBarBase::PushStatusText(const wxString& text, int number)
308{
309 wxListString* st = GetOrCreateStatusStack(number);
7d2d5d81
JS
310 // This long-winded way around avoids an internal compiler error
311 // in VC++ 6 with RTTI enabled
312 wxString tmp1(GetStatusText(number));
313 wxString* tmp = new wxString(tmp1);
314 st->Insert(tmp);
1f361cdd
MB
315 SetStatusText(text, number);
316}
317
318void wxStatusBarBase::PopStatusText(int number)
319{
320 wxListString *st = GetStatusStack(number);
321 wxCHECK_RET( st, _T("Unbalanced PushStatusText/PopStatusText") );
222ed1d6 322 wxListString::compatibility_iterator top = st->GetFirst();
1f361cdd
MB
323
324 SetStatusText(*top->GetData(), number);
222ed1d6
MB
325 delete top->GetData();
326 st->Erase(top);
1f361cdd
MB
327 if(st->GetCount() == 0)
328 {
329 delete st;
330 m_statusTextStacks[number] = 0;
331 }
332}
333
334wxListString *wxStatusBarBase::GetStatusStack(int i) const
335{
336 if(!m_statusTextStacks)
337 return 0;
338 return m_statusTextStacks[i];
339}
340
341wxListString *wxStatusBarBase::GetOrCreateStatusStack(int i)
342{
343 if(!m_statusTextStacks)
344 {
345 m_statusTextStacks = new wxListString*[m_nFields];
346
347 size_t j;
348 for(j = 0; j < (size_t)m_nFields; ++j) m_statusTextStacks[j] = 0;
349 }
350
351 if(!m_statusTextStacks[i])
352 {
353 m_statusTextStacks[i] = new wxListString();
1f361cdd
MB
354 }
355
356 return m_statusTextStacks[i];
357}
358
71e03035 359#endif // wxUSE_STATUSBAR