]>
git.saurik.com Git - wxWidgets.git/blob - src/common/statbar.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/statbar.cpp
3 // Purpose: wxStatusBarBase implementation
4 // Author: Vadim Zeitlin
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/statusbr.h"
35 const char wxStatusBarNameStr
[] = "statusBar";
37 // ============================================================================
38 // wxStatusBarPane implementation
39 // ============================================================================
41 bool wxStatusBarPane::SetText(const wxString
& text
)
47 If we have a message to restore on the stack, we update it to
48 correspond to the current one so that a sequence of calls such as
50 1. SetStatusText("foo")
51 2. PushStatusText("bar")
52 3. SetStatusText("new foo")
55 doesn't overwrite the "new foo" which should be shown at the end with
56 the old value "foo". This would be unexpected and hard to avoid,
57 especially when PushStatusText() is used internally by wxWidgets
58 without knowledge of the user program, as it is for showing the menu
59 and toolbar help strings.
61 By updating the top of the stack we ensure that the next call to
62 PopStatusText() basically becomes a NOP without breaking the balance
63 between the calls to push and pop as we would have done if we really
64 called PopStatusText() here.
66 if ( !m_arrStack
.empty() )
68 m_arrStack
.back() = text
;
76 bool wxStatusBarPane::PushText(const wxString
& text
)
78 // save the currently shown text
79 m_arrStack
.push_back(m_text
);
81 // and update the new one if necessary
90 bool wxStatusBarPane::PopText()
92 wxCHECK_MSG( !m_arrStack
.empty(), false, "no status message to pop" );
94 const wxString text
= m_arrStack
.back();
96 m_arrStack
.pop_back();
106 // ============================================================================
107 // wxStatusBarBase implementation
108 // ============================================================================
110 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar
, wxWindow
)
112 #include "wx/arrimpl.cpp" // This is a magic incantation which must be done!
113 WX_DEFINE_EXPORTED_OBJARRAY(wxStatusBarPaneArray
)
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 wxStatusBarBase::wxStatusBarBase()
122 m_bSameWidthForAllPanes
= true;
125 wxStatusBarBase::~wxStatusBarBase()
127 // notify the frame that it doesn't have a status bar any longer to avoid
129 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
130 if ( frame
&& frame
->GetStatusBar() == this )
131 frame
->SetStatusBar(NULL
);
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 void wxStatusBarBase::SetFieldsCount(int number
, const int *widths
)
140 wxCHECK_RET( number
> 0, wxT("invalid field number in SetFieldsCount") );
142 if ( (size_t)number
> m_panes
.GetCount() )
144 wxStatusBarPane newPane
;
146 // add more entries with the default style and zero width
147 // (this will be set later)
148 for (size_t i
= m_panes
.GetCount(); i
< (size_t)number
; ++i
)
149 m_panes
.Add(newPane
);
151 else if ( (size_t)number
< m_panes
.GetCount() )
153 // remove entries in excess
154 m_panes
.RemoveAt(number
, m_panes
.GetCount()-number
);
157 // SetStatusWidths will automatically refresh
158 SetStatusWidths(number
, widths
);
161 void wxStatusBarBase::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n
),
164 wxASSERT_MSG( (size_t)n
== m_panes
.GetCount(), wxT("field number mismatch") );
168 // special value meaning: override explicit pane widths and make them all
170 m_bSameWidthForAllPanes
= true;
174 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ )
175 m_panes
[i
].SetWidth(widths
[i
]);
177 m_bSameWidthForAllPanes
= false;
180 // update the display after the widths changed
184 void wxStatusBarBase::SetStatusStyles(int WXUNUSED_UNLESS_DEBUG(n
),
187 wxCHECK_RET( styles
, wxT("NULL pointer in SetStatusStyles") );
189 wxASSERT_MSG( (size_t)n
== m_panes
.GetCount(), wxT("field number mismatch") );
191 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ )
192 m_panes
[i
].SetStyle(styles
[i
]);
194 // update the display after the widths changed
198 wxArrayInt
wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal
) const
202 if ( m_bSameWidthForAllPanes
)
204 // Default: all fields have the same width. This is not always
205 // possible to do exactly (if widthTotal is not divisible by
206 // m_panes.GetCount()) - if that happens, we distribute the extra
207 // pixels among all fields:
208 int widthToUse
= widthTotal
;
210 for ( size_t i
= m_panes
.GetCount(); i
> 0; i
-- )
212 // divide the unassigned width evently between the
213 // not yet processed fields:
214 int w
= widthToUse
/ i
;
219 else // do not override explicit pane widths
221 // calculate the total width of all the fixed width fields and the
222 // total number of var field widths counting with multiplicity
223 size_t nTotalWidth
= 0,
227 for ( i
= 0; i
< m_panes
.GetCount(); i
++ )
229 if ( m_panes
[i
].GetWidth() >= 0 )
230 nTotalWidth
+= m_panes
[i
].GetWidth();
232 nVarCount
+= -m_panes
[i
].GetWidth();
235 // the amount of extra width we have per each var width field
236 int widthExtra
= widthTotal
- nTotalWidth
;
239 for ( i
= 0; i
< m_panes
.GetCount(); i
++ )
241 if ( m_panes
[i
].GetWidth() >= 0 )
242 widths
.Add(m_panes
[i
].GetWidth());
245 int nVarWidth
= widthExtra
> 0 ? (widthExtra
* (-m_panes
[i
].GetWidth())) / nVarCount
: 0;
246 nVarCount
+= m_panes
[i
].GetWidth();
247 widthExtra
-= nVarWidth
;
248 widths
.Add(nVarWidth
);
256 // ----------------------------------------------------------------------------
257 // setting/getting status text
258 // ----------------------------------------------------------------------------
260 void wxStatusBarBase::SetStatusText(const wxString
& text
, int number
)
262 wxCHECK_RET( (unsigned)number
< m_panes
.size(),
263 "invalid status bar field index" );
265 if ( m_panes
[number
].SetText(text
) )
266 DoUpdateStatusText(number
);
269 wxString
wxStatusBarBase::GetStatusText(int number
) const
271 wxCHECK_MSG( (unsigned)number
< m_panes
.size(), wxString(),
272 "invalid status bar field index" );
274 return m_panes
[number
].GetText();
277 void wxStatusBarBase::SetEllipsizedFlag(int number
, bool isEllipsized
)
279 wxCHECK_RET( (unsigned)number
< m_panes
.size(),
280 "invalid status bar field index" );
282 m_panes
[number
].SetIsEllipsized(isEllipsized
);
285 // ----------------------------------------------------------------------------
286 // pushing/popping status text
287 // ----------------------------------------------------------------------------
289 void wxStatusBarBase::PushStatusText(const wxString
& text
, int number
)
291 wxCHECK_RET( (unsigned)number
< m_panes
.size(),
292 "invalid status bar field index" );
294 if ( m_panes
[number
].PushText(text
) )
295 DoUpdateStatusText(number
);
298 void wxStatusBarBase::PopStatusText(int number
)
300 wxCHECK_RET( (unsigned)number
< m_panes
.size(),
301 "invalid status bar field index" );
303 if ( m_panes
[number
].PopText() )
304 DoUpdateStatusText(number
);
307 #endif // wxUSE_STATUSBAR