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