1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "statbar.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/statusbr.h"
37 #include "wx/listimpl.cpp"
38 WX_DEFINE_LIST(wxListString
);
40 // ============================================================================
41 // wxStatusBarBase implementation
42 // ============================================================================
44 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar
, wxWindow
)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 wxStatusBarBase::wxStatusBarBase()
59 wxStatusBarBase::~wxStatusBarBase()
66 // ----------------------------------------------------------------------------
67 // widths array handling
68 // ----------------------------------------------------------------------------
70 void wxStatusBarBase::InitWidths()
72 m_statusWidths
= NULL
;
75 void wxStatusBarBase::FreeWidths()
77 delete [] m_statusWidths
;
80 // ----------------------------------------------------------------------------
81 // styles array handling
82 // ----------------------------------------------------------------------------
84 void wxStatusBarBase::InitStyles()
86 m_statusStyles
= NULL
;
89 void wxStatusBarBase::FreeStyles()
91 delete [] m_statusStyles
;
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 void wxStatusBarBase::SetFieldsCount(int number
, const int *widths
)
100 wxCHECK_RET( number
> 0, _T("invalid field number in SetFieldsCount") );
102 bool refresh
= FALSE
;
104 if ( number
!= m_nFields
)
106 // copy stacks if present
107 if(m_statusTextStacks
)
109 wxListString
**newStacks
= new wxListString
*[number
];
110 size_t i
, j
, max
= wxMin(number
, m_nFields
);
113 for(i
= 0; i
< max
; ++i
)
114 newStacks
[i
] = m_statusTextStacks
[i
];
115 // free old stacks in excess
116 for(j
= i
; j
< (size_t)m_nFields
; ++j
)
118 if(m_statusTextStacks
[j
])
120 m_statusTextStacks
[j
]->Clear();
121 delete m_statusTextStacks
[j
];
124 // initialize new stacks to NULL
125 for(j
= i
; j
< (size_t)number
; ++j
)
128 m_statusTextStacks
= newStacks
;
131 // Resize styles array
134 int *oldStyles
= m_statusStyles
;
135 m_statusStyles
= new int[number
];
136 int i
, max
= wxMin(number
, m_nFields
);
139 for (i
= 0; i
< max
; ++i
)
140 m_statusStyles
[i
] = oldStyles
[i
];
142 // initialize new styles to wxSB_NORMAL
143 for (i
= max
; i
< number
; ++i
)
144 m_statusStyles
[i
] = wxSB_NORMAL
;
157 //else: keep the old m_statusWidths if we had them
161 SetStatusWidths(number
, widths
);
163 // already done from SetStatusWidths()
171 void wxStatusBarBase::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n
),
174 wxCHECK_RET( widths
, _T("NULL pointer in SetStatusWidths") );
176 wxASSERT_MSG( n
== m_nFields
, _T("field number mismatch") );
178 if ( !m_statusWidths
)
179 m_statusWidths
= new int[m_nFields
];
181 for ( int i
= 0; i
< m_nFields
; i
++ )
183 m_statusWidths
[i
] = widths
[i
];
186 // update the display after the widths changed
190 void wxStatusBarBase::SetStatusStyles(int WXUNUSED_UNLESS_DEBUG(n
),
193 wxCHECK_RET( styles
, _T("NULL pointer in SetStatusStyles") );
195 wxASSERT_MSG( n
== m_nFields
, _T("field number mismatch") );
197 if ( !m_statusStyles
)
198 m_statusStyles
= new int[m_nFields
];
200 for ( int i
= 0; i
< m_nFields
; i
++ )
202 m_statusStyles
[i
] = styles
[i
];
205 // update the display after the widths changed
209 wxArrayInt
wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal
) const
213 if ( m_statusWidths
== NULL
)
217 // default: all fields have the same width
218 int nWidth
= widthTotal
/ m_nFields
;
219 for ( int i
= 0; i
< m_nFields
; i
++ )
224 //else: we're empty anyhow
226 else // have explicit status widths
228 // calculate the total width of all the fixed width fields and the
229 // total number of var field widths counting with multiplicity
233 for ( i
= 0; i
< m_nFields
; i
++ )
235 if ( m_statusWidths
[i
] >= 0 )
237 nTotalWidth
+= m_statusWidths
[i
];
241 nVarCount
+= -m_statusWidths
[i
];
245 // the amount of extra width we have per each var width field
249 int widthExtra
= widthTotal
- nTotalWidth
;
250 nVarWidth
= widthExtra
> 0 ? widthExtra
/ nVarCount
: 0;
252 else // no var width fields at all
258 for ( i
= 0; i
< m_nFields
; i
++ )
260 if ( m_statusWidths
[i
] >= 0 )
262 widths
.Add(m_statusWidths
[i
]);
266 widths
.Add(-m_statusWidths
[i
]*nVarWidth
);
274 // ----------------------------------------------------------------------------
275 // text stacks handling
276 // ----------------------------------------------------------------------------
278 void wxStatusBarBase::InitStacks()
280 m_statusTextStacks
= NULL
;
283 void wxStatusBarBase::FreeStacks()
285 if(!m_statusTextStacks
) return;
288 for(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