]>
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
7 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/statusbr.h"
34 const char wxStatusBarNameStr
[] = "statusBar";
36 // ============================================================================
37 // wxStatusBarPane implementation
38 // ============================================================================
40 bool wxStatusBarPane::SetText(const wxString
& text
)
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
49 1. SetStatusText("foo")
50 2. PushStatusText("bar")
51 3. SetStatusText("new foo")
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.
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.
65 if ( !m_arrStack
.empty() )
67 m_arrStack
.back() = text
;
75 bool wxStatusBarPane::PushText(const wxString
& text
)
77 // save the currently shown text
78 m_arrStack
.push_back(m_text
);
80 // and update the new one if necessary
89 bool wxStatusBarPane::PopText()
91 wxCHECK_MSG( !m_arrStack
.empty(), false, "no status message to pop" );
93 const wxString text
= m_arrStack
.back();
95 m_arrStack
.pop_back();
105 // ============================================================================
106 // wxStatusBarBase implementation
107 // ============================================================================
109 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar
, wxWindow
)
111 #include "wx/arrimpl.cpp" // This is a magic incantation which must be done!
112 WX_DEFINE_EXPORTED_OBJARRAY(wxStatusBarPaneArray
)
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 wxStatusBarBase::wxStatusBarBase()
121 m_bSameWidthForAllPanes
= true;
124 wxStatusBarBase::~wxStatusBarBase()
126 // notify the frame that it doesn't have a status bar any longer to avoid
128 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
129 if ( frame
&& frame
->GetStatusBar() == this )
130 frame
->SetStatusBar(NULL
);
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 void wxStatusBarBase::SetFieldsCount(int number
, const int *widths
)
139 wxCHECK_RET( number
> 0, wxT("invalid field number in SetFieldsCount") );
141 if ( (size_t)number
> m_panes
.GetCount() )
143 wxStatusBarPane newPane
;
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
);
150 else if ( (size_t)number
< m_panes
.GetCount() )
152 // remove entries in excess
153 m_panes
.RemoveAt(number
, m_panes
.GetCount()-number
);
156 // SetStatusWidths will automatically refresh
157 SetStatusWidths(number
, widths
);
160 void wxStatusBarBase::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n
),
163 wxASSERT_MSG( (size_t)n
== m_panes
.GetCount(), wxT("field number mismatch") );
167 // special value meaning: override explicit pane widths and make them all
169 m_bSameWidthForAllPanes
= true;
173 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ )
174 m_panes
[i
].SetWidth(widths
[i
]);
176 m_bSameWidthForAllPanes
= false;
179 // update the display after the widths changed
183 void wxStatusBarBase::SetStatusStyles(int WXUNUSED_UNLESS_DEBUG(n
),
186 wxCHECK_RET( styles
, wxT("NULL pointer in SetStatusStyles") );
188 wxASSERT_MSG( (size_t)n
== m_panes
.GetCount(), wxT("field number mismatch") );
190 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ )
191 m_panes
[i
].SetStyle(styles
[i
]);
193 // update the display after the widths changed
197 wxArrayInt
wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal
) const
201 if ( m_bSameWidthForAllPanes
)
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
;
209 for ( size_t i
= m_panes
.GetCount(); i
> 0; i
-- )
211 // divide the unassigned width evently between the
212 // not yet processed fields:
213 int w
= widthToUse
/ i
;
218 else // do not override explicit pane widths
220 // calculate the total width of all the fixed width fields and the
221 // total number of var field widths counting with multiplicity
222 size_t nTotalWidth
= 0,
226 for ( i
= 0; i
< m_panes
.GetCount(); i
++ )
228 if ( m_panes
[i
].GetWidth() >= 0 )
229 nTotalWidth
+= m_panes
[i
].GetWidth();
231 nVarCount
+= -m_panes
[i
].GetWidth();
234 // the amount of extra width we have per each var width field
235 int widthExtra
= widthTotal
- nTotalWidth
;
238 for ( i
= 0; i
< m_panes
.GetCount(); i
++ )
240 if ( m_panes
[i
].GetWidth() >= 0 )
241 widths
.Add(m_panes
[i
].GetWidth());
244 int nVarWidth
= widthExtra
> 0 ? (widthExtra
* (-m_panes
[i
].GetWidth())) / nVarCount
: 0;
245 nVarCount
+= m_panes
[i
].GetWidth();
246 widthExtra
-= nVarWidth
;
247 widths
.Add(nVarWidth
);
255 // ----------------------------------------------------------------------------
256 // setting/getting status text
257 // ----------------------------------------------------------------------------
259 void wxStatusBarBase::SetStatusText(const wxString
& text
, int number
)
261 wxCHECK_RET( (unsigned)number
< m_panes
.size(),
262 "invalid status bar field index" );
264 if ( m_panes
[number
].SetText(text
) )
265 DoUpdateStatusText(number
);
268 wxString
wxStatusBarBase::GetStatusText(int number
) const
270 wxCHECK_MSG( (unsigned)number
< m_panes
.size(), wxString(),
271 "invalid status bar field index" );
273 return m_panes
[number
].GetText();
276 void wxStatusBarBase::SetEllipsizedFlag(int number
, bool isEllipsized
)
278 wxCHECK_RET( (unsigned)number
< m_panes
.size(),
279 "invalid status bar field index" );
281 m_panes
[number
].SetIsEllipsized(isEllipsized
);
284 // ----------------------------------------------------------------------------
285 // pushing/popping status text
286 // ----------------------------------------------------------------------------
288 void wxStatusBarBase::PushStatusText(const wxString
& text
, int number
)
290 wxCHECK_RET( (unsigned)number
< m_panes
.size(),
291 "invalid status bar field index" );
293 if ( m_panes
[number
].PushText(text
) )
294 DoUpdateStatusText(number
);
297 void wxStatusBarBase::PopStatusText(int number
)
299 wxCHECK_RET( (unsigned)number
< m_panes
.size(),
300 "invalid status bar field index" );
302 if ( m_panes
[number
].PopText() )
303 DoUpdateStatusText(number
);
306 #endif // wxUSE_STATUSBAR