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
246 int widthExtra
= widthTotal
- nTotalWidth
;
249 for ( i
= 0; i
< m_nFields
; i
++ )
251 if ( m_statusWidths
[i
] >= 0 )
253 widths
.Add(m_statusWidths
[i
]);
257 int nVarWidth
= widthExtra
> 0 ? (widthExtra
* -m_statusWidths
[i
]) / nVarCount
: 0;
258 nVarCount
+= m_statusWidths
[i
];
259 widthExtra
-= nVarWidth
;
260 widths
.Add(nVarWidth
);
268 // ----------------------------------------------------------------------------
269 // text stacks handling
270 // ----------------------------------------------------------------------------
272 void wxStatusBarBase::InitStacks()
274 m_statusTextStacks
= NULL
;
277 void wxStatusBarBase::FreeStacks()
279 if(!m_statusTextStacks
) return;
282 for(i
= 0; i
< (size_t)m_nFields
; ++i
)
284 if(m_statusTextStacks
[i
])
286 wxListString
& t
= *m_statusTextStacks
[i
];
287 WX_CLEAR_LIST(wxListString
, t
);
288 delete m_statusTextStacks
[i
];
292 delete[] m_statusTextStacks
;
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
299 void wxStatusBarBase::PushStatusText(const wxString
& text
, int number
)
301 wxListString
* st
= GetOrCreateStatusStack(number
);
302 // This long-winded way around avoids an internal compiler error
303 // in VC++ 6 with RTTI enabled
304 wxString
tmp1(GetStatusText(number
));
305 wxString
* tmp
= new wxString(tmp1
);
307 SetStatusText(text
, number
);
310 void wxStatusBarBase::PopStatusText(int number
)
312 wxListString
*st
= GetStatusStack(number
);
313 wxCHECK_RET( st
, _T("Unbalanced PushStatusText/PopStatusText") );
314 wxListString::compatibility_iterator top
= st
->GetFirst();
316 SetStatusText(*top
->GetData(), number
);
317 delete top
->GetData();
319 if(st
->GetCount() == 0)
322 m_statusTextStacks
[number
] = 0;
326 wxListString
*wxStatusBarBase::GetStatusStack(int i
) const
328 if(!m_statusTextStacks
)
330 return m_statusTextStacks
[i
];
333 wxListString
*wxStatusBarBase::GetOrCreateStatusStack(int i
)
335 if(!m_statusTextStacks
)
337 m_statusTextStacks
= new wxListString
*[m_nFields
];
340 for(j
= 0; j
< (size_t)m_nFields
; ++j
) m_statusTextStacks
[j
] = 0;
343 if(!m_statusTextStacks
[i
])
345 m_statusTextStacks
[i
] = new wxListString();
348 return m_statusTextStacks
[i
];
351 #endif // wxUSE_STATUSBAR