]> git.saurik.com Git - wxWidgets.git/blame - src/common/statbar.cpp
wxDialogBase only has one ctor, so just do initialization in ctor instead of Init()
[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
7b6fefbe 5// Modified by: Francesco Montorsi
71e03035 6// Created: 14.10.01
71e03035 7// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
526954c5 8// Licence: wxWindows licence
71e03035
VZ
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
71e03035
VZ
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
53b6d7a2
PC
26#if wxUSE_STATUSBAR
27
3304646d
WS
28#include "wx/statusbr.h"
29
71e03035 30#ifndef WX_PRECOMP
e9a05074 31 #include "wx/frame.h"
71e03035
VZ
32#endif //WX_PRECOMP
33
23318a53 34const char wxStatusBarNameStr[] = "statusBar";
53b6d7a2 35
6cf68971
VZ
36// ============================================================================
37// wxStatusBarPane implementation
38// ============================================================================
39
40bool wxStatusBarPane::SetText(const wxString& text)
41{
42 if ( text == m_text )
43 return false;
44
45 /*
46 If we have a message to restore on the stack, we update it to
47 correspond to the current one so that a sequence of calls such as
48
49 1. SetStatusText("foo")
50 2. PushStatusText("bar")
51 3. SetStatusText("new foo")
52 4. PopStatusText()
53
54 doesn't overwrite the "new foo" which should be shown at the end with
55 the old value "foo". This would be unexpected and hard to avoid,
56 especially when PushStatusText() is used internally by wxWidgets
57 without knowledge of the user program, as it is for showing the menu
58 and toolbar help strings.
59
60 By updating the top of the stack we ensure that the next call to
61 PopStatusText() basically becomes a NOP without breaking the balance
62 between the calls to push and pop as we would have done if we really
63 called PopStatusText() here.
64 */
65 if ( !m_arrStack.empty() )
66 {
67 m_arrStack.back() = text;
68 }
69
70 m_text = text;
71
72 return true;
73}
74
75bool wxStatusBarPane::PushText(const wxString& text)
76{
77 // save the currently shown text
78 m_arrStack.push_back(m_text);
79
80 // and update the new one if necessary
81 if ( text == m_text )
82 return false;
83
84 m_text = text;
85
86 return true;
87}
88
89bool wxStatusBarPane::PopText()
90{
91 wxCHECK_MSG( !m_arrStack.empty(), false, "no status message to pop" );
92
93 const wxString text = m_arrStack.back();
94
95 m_arrStack.pop_back();
96
97 if ( text == m_text )
98 return false;
99
100 m_text = text;
101
102 return true;
103}
7b6fefbe 104
71e03035
VZ
105// ============================================================================
106// wxStatusBarBase implementation
107// ============================================================================
108
a93e536b 109IMPLEMENT_DYNAMIC_CLASS(wxStatusBar, wxWindow)
71e03035 110
0dc8fac2 111#include "wx/arrimpl.cpp" // This is a magic incantation which must be done!
b2d76621 112WX_DEFINE_EXPORTED_OBJARRAY(wxStatusBarPaneArray)
7b6fefbe
FM
113
114
71e03035
VZ
115// ----------------------------------------------------------------------------
116// ctor/dtor
117// ----------------------------------------------------------------------------
118
119wxStatusBarBase::wxStatusBarBase()
120{
7b6fefbe 121 m_bSameWidthForAllPanes = true;
71e03035
VZ
122}
123
124wxStatusBarBase::~wxStatusBarBase()
125{
2ab82214
VZ
126 // notify the frame that it doesn't have a status bar any longer to avoid
127 // dangling pointers
67f30682 128 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
2ab82214 129 if ( frame && frame->GetStatusBar() == this )
2ab82214 130 frame->SetStatusBar(NULL);
c2919ab3
VZ
131}
132
71e03035
VZ
133// ----------------------------------------------------------------------------
134// field widths
135// ----------------------------------------------------------------------------
136
137void wxStatusBarBase::SetFieldsCount(int number, const int *widths)
138{
9a83f860 139 wxCHECK_RET( number > 0, wxT("invalid field number in SetFieldsCount") );
71e03035 140
7b6fefbe 141 if ( (size_t)number > m_panes.GetCount() )
71e03035 142 {
7b6fefbe 143 wxStatusBarPane newPane;
c2919ab3 144
7b6fefbe
FM
145 // add more entries with the default style and zero width
146 // (this will be set later)
147 for (size_t i = m_panes.GetCount(); i < (size_t)number; ++i)
148 m_panes.Add(newPane);
71e03035 149 }
7b6fefbe
FM
150 else if ( (size_t)number < m_panes.GetCount() )
151 {
152 // remove entries in excess
153 m_panes.RemoveAt(number, m_panes.GetCount()-number);
154 }
155
fd3ece57
FM
156 // SetStatusWidths will automatically refresh
157 SetStatusWidths(number, widths);
71e03035
VZ
158}
159
160void wxStatusBarBase::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n),
fd3ece57 161 const int widths[])
71e03035 162{
9a83f860 163 wxASSERT_MSG( (size_t)n == m_panes.GetCount(), wxT("field number mismatch") );
71e03035 164
54e18afc 165 if (widths == NULL)
fd3ece57
FM
166 {
167 // special value meaning: override explicit pane widths and make them all
168 // of the same size
169 m_bSameWidthForAllPanes = true;
170 }
171 else
172 {
173 for ( size_t i = 0; i < m_panes.GetCount(); i++ )
6cf68971 174 m_panes[i].SetWidth(widths[i]);
fd3ece57
FM
175
176 m_bSameWidthForAllPanes = false;
177 }
71e03035
VZ
178
179 // update the display after the widths changed
180 Refresh();
181}
182
c2919ab3 183void wxStatusBarBase::SetStatusStyles(int WXUNUSED_UNLESS_DEBUG(n),
fd3ece57 184 const int styles[])
c2919ab3 185{
9a83f860 186 wxCHECK_RET( styles, wxT("NULL pointer in SetStatusStyles") );
c2919ab3 187
9a83f860 188 wxASSERT_MSG( (size_t)n == m_panes.GetCount(), wxT("field number mismatch") );
c2919ab3 189
7b6fefbe 190 for ( size_t i = 0; i < m_panes.GetCount(); i++ )
6cf68971 191 m_panes[i].SetStyle(styles[i]);
c2919ab3
VZ
192
193 // update the display after the widths changed
194 Refresh();
195}
196
71e03035
VZ
197wxArrayInt wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal) const
198{
199 wxArrayInt widths;
200
7b6fefbe 201 if ( m_bSameWidthForAllPanes )
71e03035 202 {
7b6fefbe
FM
203 // Default: all fields have the same width. This is not always
204 // possible to do exactly (if widthTotal is not divisible by
205 // m_panes.GetCount()) - if that happens, we distribute the extra
206 // pixels among all fields:
207 int widthToUse = widthTotal;
68ab959c 208
7b6fefbe
FM
209 for ( size_t i = m_panes.GetCount(); i > 0; i-- )
210 {
211 // divide the unassigned width evently between the
212 // not yet processed fields:
213 int w = widthToUse / i;
214 widths.Add(w);
215 widthToUse -= w;
71e03035
VZ
216 }
217 }
7b6fefbe 218 else // do not override explicit pane widths
71e03035
VZ
219 {
220 // calculate the total width of all the fixed width fields and the
221 // total number of var field widths counting with multiplicity
7b6fefbe 222 size_t nTotalWidth = 0,
fd3ece57
FM
223 nVarCount = 0,
224 i;
7b6fefbe
FM
225
226 for ( i = 0; i < m_panes.GetCount(); i++ )
71e03035 227 {
b31eaa5c
FM
228 if ( m_panes[i].GetWidth() >= 0 )
229 nTotalWidth += m_panes[i].GetWidth();
71e03035 230 else
b31eaa5c 231 nVarCount += -m_panes[i].GetWidth();
71e03035
VZ
232 }
233
234 // the amount of extra width we have per each var width field
1a83b9bd 235 int widthExtra = widthTotal - nTotalWidth;
71e03035
VZ
236
237 // do fill the array
7b6fefbe 238 for ( i = 0; i < m_panes.GetCount(); i++ )
71e03035 239 {
b31eaa5c
FM
240 if ( m_panes[i].GetWidth() >= 0 )
241 widths.Add(m_panes[i].GetWidth());
71e03035
VZ
242 else
243 {
b31eaa5c
FM
244 int nVarWidth = widthExtra > 0 ? (widthExtra * (-m_panes[i].GetWidth())) / nVarCount : 0;
245 nVarCount += m_panes[i].GetWidth();
1a83b9bd
VZ
246 widthExtra -= nVarWidth;
247 widths.Add(nVarWidth);
71e03035
VZ
248 }
249 }
250 }
251
252 return widths;
253}
254
1f361cdd 255// ----------------------------------------------------------------------------
6cf68971
VZ
256// setting/getting status text
257// ----------------------------------------------------------------------------
258
259void wxStatusBarBase::SetStatusText(const wxString& text, int number)
260{
261 wxCHECK_RET( (unsigned)number < m_panes.size(),
262 "invalid status bar field index" );
263
264 if ( m_panes[number].SetText(text) )
265 DoUpdateStatusText(number);
266}
267
268wxString wxStatusBarBase::GetStatusText(int number) const
269{
270 wxCHECK_MSG( (unsigned)number < m_panes.size(), wxString(),
271 "invalid status bar field index" );
272
273 return m_panes[number].GetText();
274}
275
276void wxStatusBarBase::SetEllipsizedFlag(int number, bool isEllipsized)
277{
278 wxCHECK_RET( (unsigned)number < m_panes.size(),
279 "invalid status bar field index" );
280
281 m_panes[number].SetIsEllipsized(isEllipsized);
282}
283
284// ----------------------------------------------------------------------------
285// pushing/popping status text
1f361cdd
MB
286// ----------------------------------------------------------------------------
287
288void wxStatusBarBase::PushStatusText(const wxString& text, int number)
289{
6cf68971
VZ
290 wxCHECK_RET( (unsigned)number < m_panes.size(),
291 "invalid status bar field index" );
292
293 if ( m_panes[number].PushText(text) )
294 DoUpdateStatusText(number);
1f361cdd
MB
295}
296
297void wxStatusBarBase::PopStatusText(int number)
298{
6cf68971
VZ
299 wxCHECK_RET( (unsigned)number < m_panes.size(),
300 "invalid status bar field index" );
301
302 if ( m_panes[number].PopText() )
303 DoUpdateStatusText(number);
1f361cdd
MB
304}
305
71e03035 306#endif // wxUSE_STATUSBAR