1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/statbar.cpp
3 // Purpose: wxStatusBarBase implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/statusbr.h"
35 #include "wx/listimpl.cpp"
36 WX_DEFINE_LIST(wxListString
)
38 // ============================================================================
39 // wxStatusBarBase implementation
40 // ============================================================================
42 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar
, wxWindow
)
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 wxStatusBarBase::wxStatusBarBase()
57 wxStatusBarBase::~wxStatusBarBase()
63 // notify the frame that it doesn't have a status bar any longer to avoid
65 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
66 if ( frame
&& frame
->GetStatusBar() == this )
68 frame
->SetStatusBar(NULL
);
72 // ----------------------------------------------------------------------------
73 // widths array handling
74 // ----------------------------------------------------------------------------
76 void wxStatusBarBase::InitWidths()
78 m_statusWidths
= NULL
;
81 void wxStatusBarBase::FreeWidths()
83 delete [] m_statusWidths
;
86 // ----------------------------------------------------------------------------
87 // styles array handling
88 // ----------------------------------------------------------------------------
90 void wxStatusBarBase::InitStyles()
92 m_statusStyles
= NULL
;
95 void wxStatusBarBase::FreeStyles()
97 delete [] m_statusStyles
;
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 void wxStatusBarBase::SetFieldsCount(int number
, const int *widths
)
106 wxCHECK_RET( number
> 0, _T("invalid field number in SetFieldsCount") );
108 bool refresh
= false;
110 if ( number
!= m_nFields
)
112 // copy stacks if present
113 if(m_statusTextStacks
)
115 wxListString
**newStacks
= new wxListString
*[number
];
116 size_t i
, j
, max
= wxMin(number
, m_nFields
);
119 for(i
= 0; i
< max
; ++i
)
120 newStacks
[i
] = m_statusTextStacks
[i
];
121 // free old stacks in excess
122 for(j
= i
; j
< (size_t)m_nFields
; ++j
)
124 if(m_statusTextStacks
[j
])
126 m_statusTextStacks
[j
]->Clear();
127 delete m_statusTextStacks
[j
];
130 // initialize new stacks to NULL
131 for(j
= i
; j
< (size_t)number
; ++j
)
134 m_statusTextStacks
= newStacks
;
137 // Resize styles array
140 int *oldStyles
= m_statusStyles
;
141 m_statusStyles
= new int[number
];
142 int i
, max
= wxMin(number
, m_nFields
);
145 for (i
= 0; i
< max
; ++i
)
146 m_statusStyles
[i
] = oldStyles
[i
];
148 // initialize new styles to wxSB_NORMAL
149 for (i
= max
; i
< number
; ++i
)
150 m_statusStyles
[i
] = wxSB_NORMAL
;
163 //else: keep the old m_statusWidths if we had them
167 SetStatusWidths(number
, widths
);
169 // already done from SetStatusWidths()
177 void wxStatusBarBase::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n
),
180 wxCHECK_RET( widths
, _T("NULL pointer in SetStatusWidths") );
182 wxASSERT_MSG( n
== m_nFields
, _T("field number mismatch") );
184 if ( !m_statusWidths
)
185 m_statusWidths
= new int[m_nFields
];
187 for ( int i
= 0; i
< m_nFields
; i
++ )
189 m_statusWidths
[i
] = widths
[i
];
192 // update the display after the widths changed
196 void wxStatusBarBase::SetStatusStyles(int WXUNUSED_UNLESS_DEBUG(n
),
199 wxCHECK_RET( styles
, _T("NULL pointer in SetStatusStyles") );
201 wxASSERT_MSG( n
== m_nFields
, _T("field number mismatch") );
203 if ( !m_statusStyles
)
204 m_statusStyles
= new int[m_nFields
];
206 for ( int i
= 0; i
< m_nFields
; i
++ )
208 m_statusStyles
[i
] = styles
[i
];
211 // update the display after the widths changed
215 wxArrayInt
wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal
) const
219 if ( m_statusWidths
== NULL
)
223 // default: all fields have the same width
224 int nWidth
= widthTotal
/ m_nFields
;
225 for ( int i
= 0; i
< m_nFields
; i
++ )
230 //else: we're empty anyhow
232 else // have explicit status widths
234 // calculate the total width of all the fixed width fields and the
235 // total number of var field widths counting with multiplicity
239 for ( i
= 0; i
< m_nFields
; i
++ )
241 if ( m_statusWidths
[i
] >= 0 )
243 nTotalWidth
+= m_statusWidths
[i
];
247 nVarCount
+= -m_statusWidths
[i
];
251 // the amount of extra width we have per each var width field
252 int widthExtra
= widthTotal
- nTotalWidth
;
255 for ( i
= 0; i
< m_nFields
; i
++ )
257 if ( m_statusWidths
[i
] >= 0 )
259 widths
.Add(m_statusWidths
[i
]);
263 int nVarWidth
= widthExtra
> 0 ? (widthExtra
* -m_statusWidths
[i
]) / nVarCount
: 0;
264 nVarCount
+= m_statusWidths
[i
];
265 widthExtra
-= nVarWidth
;
266 widths
.Add(nVarWidth
);
274 // ----------------------------------------------------------------------------
275 // text stacks handling
276 // ----------------------------------------------------------------------------
278 void wxStatusBarBase::InitStacks()
280 m_statusTextStacks
= NULL
;
283 void wxStatusBarBase::FreeStacks()
285 if ( !m_statusTextStacks
)
288 for ( size_t i
= 0; i
< (size_t)m_nFields
; ++i
)
290 if ( m_statusTextStacks
[i
] )
292 wxListString
& t
= *m_statusTextStacks
[i
];
293 WX_CLEAR_LIST(wxListString
, t
);
294 delete m_statusTextStacks
[i
];
298 delete[] m_statusTextStacks
;
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 void wxStatusBarBase::PushStatusText(const wxString
& text
, int number
)
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
);
313 SetStatusText(text
, number
);
316 void wxStatusBarBase::PopStatusText(int number
)
318 wxListString
*st
= GetStatusStack(number
);
319 wxCHECK_RET( st
, _T("Unbalanced PushStatusText/PopStatusText") );
320 wxListString::compatibility_iterator top
= st
->GetFirst();
322 SetStatusText(*top
->GetData(), number
);
323 delete top
->GetData();
325 if(st
->GetCount() == 0)
328 m_statusTextStacks
[number
] = 0;
332 wxListString
*wxStatusBarBase::GetStatusStack(int i
) const
334 if(!m_statusTextStacks
)
336 return m_statusTextStacks
[i
];
339 wxListString
*wxStatusBarBase::GetOrCreateStatusStack(int i
)
341 if(!m_statusTextStacks
)
343 m_statusTextStacks
= new wxListString
*[m_nFields
];
346 for(j
= 0; j
< (size_t)m_nFields
; ++j
) m_statusTextStacks
[j
] = 0;
349 if(!m_statusTextStacks
[i
])
351 m_statusTextStacks
[i
] = new wxListString();
354 return m_statusTextStacks
[i
];
357 #endif // wxUSE_STATUSBAR