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() 
  58 wxStatusBarBase::~wxStatusBarBase() 
  64 // ---------------------------------------------------------------------------- 
  65 // widths array handling 
  66 // ---------------------------------------------------------------------------- 
  68 void wxStatusBarBase::InitWidths() 
  70     m_statusWidths 
= NULL
; 
  73 void wxStatusBarBase::FreeWidths() 
  75     delete [] m_statusWidths
; 
  78 // ---------------------------------------------------------------------------- 
  80 // ---------------------------------------------------------------------------- 
  82 void wxStatusBarBase::SetFieldsCount(int number
, const int *widths
) 
  84     wxCHECK_RET( number 
> 0, _T("invalid field number in SetFieldsCount") ); 
  88     if ( number 
!= m_nFields 
) 
  90         // copy stacks if present 
  91         if(m_statusTextStacks
) 
  93             wxListString 
**newStacks 
= new wxListString
*[number
]; 
  94             size_t i
, j
, max 
= wxMin(number
, m_nFields
); 
  97             for(i 
= 0; i 
< max
; ++i
) 
  98                 newStacks
[i
] = m_statusTextStacks
[i
]; 
  99             // free old stacks in excess 
 100             for(j 
= i
; j 
< (size_t)m_nFields
; ++j
) 
 102                 if(m_statusTextStacks
[j
]) 
 104                     m_statusTextStacks
[j
]->Clear(); 
 105                     delete m_statusTextStacks
[j
]; 
 108             // initialize new stacks to NULL 
 109             for(j 
= i
; j 
< (size_t)number
; ++j
) 
 112             m_statusTextStacks 
= newStacks
; 
 121     //else: keep the old m_statusWidths if we had them 
 125         SetStatusWidths(number
, widths
); 
 127         // already done from SetStatusWidths() 
 135 void wxStatusBarBase::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n
), 
 138     wxCHECK_RET( widths
, _T("NULL pointer in SetStatusWidths") ); 
 140     wxASSERT_MSG( n 
== m_nFields
, _T("field number mismatch") ); 
 142     if ( !m_statusWidths 
) 
 143         m_statusWidths 
= new int[m_nFields
]; 
 145     for ( int i 
= 0; i 
< m_nFields
; i
++ ) 
 147         m_statusWidths
[i
] = widths
[i
]; 
 150     // update the display after the widths changed 
 154 wxArrayInt 
wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal
) const 
 158     if ( m_statusWidths 
== NULL 
) 
 162             // default: all fields have the same width 
 163             int nWidth 
= widthTotal 
/ m_nFields
; 
 164             for ( int i 
= 0; i 
< m_nFields
; i
++ ) 
 169         //else: we're empty anyhow 
 171     else // have explicit status widths 
 173         // calculate the total width of all the fixed width fields and the 
 174         // total number of var field widths counting with multiplicity 
 178         for ( i 
= 0; i 
< m_nFields
; i
++ ) 
 180             if ( m_statusWidths
[i
] >= 0 ) 
 182                 nTotalWidth 
+= m_statusWidths
[i
]; 
 186                 nVarCount 
+= -m_statusWidths
[i
]; 
 190         // the amount of extra width we have per each var width field 
 194             int widthExtra 
= widthTotal 
- nTotalWidth
; 
 195             nVarWidth 
= widthExtra 
> 0 ? widthExtra 
/ nVarCount 
: 0; 
 197         else // no var width fields at all 
 203         for ( i 
= 0; i 
< m_nFields
; i
++ ) 
 205             if ( m_statusWidths
[i
] >= 0 ) 
 207                 widths
.Add(m_statusWidths
[i
]); 
 211                 widths
.Add(-m_statusWidths
[i
]*nVarWidth
); 
 219 // ---------------------------------------------------------------------------- 
 220 // text stacks handling 
 221 // ---------------------------------------------------------------------------- 
 223 void wxStatusBarBase::InitStacks() 
 225     m_statusTextStacks 
= NULL
; 
 228 void wxStatusBarBase::FreeStacks() 
 230     if(!m_statusTextStacks
) return; 
 233     for(i 
= 0; i 
< (size_t)m_nFields
; ++i
) 
 235         if(m_statusTextStacks
[i
]) 
 237             wxListString
& t 
= *m_statusTextStacks
[i
]; 
 238             WX_CLEAR_LIST(wxListString
, t
); 
 239             delete m_statusTextStacks
[i
]; 
 243     delete[] m_statusTextStacks
; 
 246 // ---------------------------------------------------------------------------- 
 248 // ---------------------------------------------------------------------------- 
 250 void wxStatusBarBase::PushStatusText(const wxString
& text
, int number
) 
 252     wxListString
* st 
= GetOrCreateStatusStack(number
); 
 253     // This long-winded way around avoids an internal compiler error 
 254     // in VC++ 6 with RTTI enabled 
 255     wxString 
tmp1(GetStatusText(number
)); 
 256     wxString
* tmp 
= new wxString(tmp1
); 
 258     SetStatusText(text
, number
); 
 261 void wxStatusBarBase::PopStatusText(int number
) 
 263     wxListString 
*st 
= GetStatusStack(number
); 
 264     wxCHECK_RET( st
, _T("Unbalanced PushStatusText/PopStatusText") ); 
 265     wxListString::compatibility_iterator top 
= st
->GetFirst(); 
 267     SetStatusText(*top
->GetData(), number
); 
 268     delete top
->GetData(); 
 270     if(st
->GetCount() == 0) 
 273         m_statusTextStacks
[number
] = 0; 
 277 wxListString 
*wxStatusBarBase::GetStatusStack(int i
) const 
 279     if(!m_statusTextStacks
) 
 281     return m_statusTextStacks
[i
]; 
 284 wxListString 
*wxStatusBarBase::GetOrCreateStatusStack(int i
) 
 286     if(!m_statusTextStacks
) 
 288         m_statusTextStacks 
= new wxListString
*[m_nFields
]; 
 291         for(j 
= 0; j 
< (size_t)m_nFields
; ++j
) m_statusTextStacks
[j
] = 0; 
 294     if(!m_statusTextStacks
[i
]) 
 296         m_statusTextStacks
[i
] = new wxListString(); 
 299     return m_statusTextStacks
[i
]; 
 302 #endif // wxUSE_STATUSBAR