]> git.saurik.com Git - wxWidgets.git/blob - src/common/statbar.cpp
SetBackgroundStyle(wxBG_STYLE_CUSTOM) can now be used to optimize
[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 nVarWidth;
247 if ( nVarCount )
248 {
249 int widthExtra = widthTotal - nTotalWidth;
250 nVarWidth = widthExtra > 0 ? widthExtra / nVarCount : 0;
251 }
252 else // no var width fields at all
253 {
254 nVarWidth = 0;
255 }
256
257 // do fill the array
258 for ( i = 0; i < m_nFields; i++ )
259 {
260 if ( m_statusWidths[i] >= 0 )
261 {
262 widths.Add(m_statusWidths[i]);
263 }
264 else
265 {
266 widths.Add(-m_statusWidths[i]*nVarWidth);
267 }
268 }
269 }
270
271 return widths;
272 }
273
274 // ----------------------------------------------------------------------------
275 // text stacks handling
276 // ----------------------------------------------------------------------------
277
278 void wxStatusBarBase::InitStacks()
279 {
280 m_statusTextStacks = NULL;
281 }
282
283 void wxStatusBarBase::FreeStacks()
284 {
285 if(!m_statusTextStacks) return;
286 size_t i;
287
288 for(i = 0; i < (size_t)m_nFields; ++i)
289 {
290 if(m_statusTextStacks[i])
291 {
292 wxListString& t = *m_statusTextStacks[i];
293 WX_CLEAR_LIST(wxListString, t);
294 delete m_statusTextStacks[i];
295 }
296 }
297
298 delete[] m_statusTextStacks;
299 }
300
301 // ----------------------------------------------------------------------------
302 // text stacks
303 // ----------------------------------------------------------------------------
304
305 void wxStatusBarBase::PushStatusText(const wxString& text, int number)
306 {
307 wxListString* st = GetOrCreateStatusStack(number);
308 // This long-winded way around avoids an internal compiler error
309 // in VC++ 6 with RTTI enabled
310 wxString tmp1(GetStatusText(number));
311 wxString* tmp = new wxString(tmp1);
312 st->Insert(tmp);
313 SetStatusText(text, number);
314 }
315
316 void wxStatusBarBase::PopStatusText(int number)
317 {
318 wxListString *st = GetStatusStack(number);
319 wxCHECK_RET( st, _T("Unbalanced PushStatusText/PopStatusText") );
320 wxListString::compatibility_iterator top = st->GetFirst();
321
322 SetStatusText(*top->GetData(), number);
323 delete top->GetData();
324 st->Erase(top);
325 if(st->GetCount() == 0)
326 {
327 delete st;
328 m_statusTextStacks[number] = 0;
329 }
330 }
331
332 wxListString *wxStatusBarBase::GetStatusStack(int i) const
333 {
334 if(!m_statusTextStacks)
335 return 0;
336 return m_statusTextStacks[i];
337 }
338
339 wxListString *wxStatusBarBase::GetOrCreateStatusStack(int i)
340 {
341 if(!m_statusTextStacks)
342 {
343 m_statusTextStacks = new wxListString*[m_nFields];
344
345 size_t j;
346 for(j = 0; j < (size_t)m_nFields; ++j) m_statusTextStacks[j] = 0;
347 }
348
349 if(!m_statusTextStacks[i])
350 {
351 m_statusTextStacks[i] = new wxListString();
352 }
353
354 return m_statusTextStacks[i];
355 }
356
357 #endif // wxUSE_STATUSBAR
358