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