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"
33 #include "wx/statusbr.h"
38 #include "wx/listimpl.cpp"
39 WX_DEFINE_LIST(wxListString
);
41 // ============================================================================
42 // wxStatusBarBase implementation
43 // ============================================================================
45 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar
, wxWindow
)
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 wxStatusBarBase::wxStatusBarBase()
60 wxStatusBarBase::~wxStatusBarBase()
66 // notify the frame that it doesn't have a status bar any longer to avoid
68 wxFrameBase
*frame
= wxDynamicCast(GetParent(), wxFrameBase
);
69 if ( frame
&& frame
->GetStatusBar() == this )
71 frame
->SetStatusBar(NULL
);
75 // ----------------------------------------------------------------------------
76 // widths array handling
77 // ----------------------------------------------------------------------------
79 void wxStatusBarBase::InitWidths()
81 m_statusWidths
= NULL
;
84 void wxStatusBarBase::FreeWidths()
86 delete [] m_statusWidths
;
89 // ----------------------------------------------------------------------------
90 // styles array handling
91 // ----------------------------------------------------------------------------
93 void wxStatusBarBase::InitStyles()
95 m_statusStyles
= NULL
;
98 void wxStatusBarBase::FreeStyles()
100 delete [] m_statusStyles
;
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 void wxStatusBarBase::SetFieldsCount(int number
, const int *widths
)
109 wxCHECK_RET( number
> 0, _T("invalid field number in SetFieldsCount") );
111 bool refresh
= false;
113 if ( number
!= m_nFields
)
115 // copy stacks if present
116 if(m_statusTextStacks
)
118 wxListString
**newStacks
= new wxListString
*[number
];
119 size_t i
, j
, max
= wxMin(number
, m_nFields
);
122 for(i
= 0; i
< max
; ++i
)
123 newStacks
[i
] = m_statusTextStacks
[i
];
124 // free old stacks in excess
125 for(j
= i
; j
< (size_t)m_nFields
; ++j
)
127 if(m_statusTextStacks
[j
])
129 m_statusTextStacks
[j
]->Clear();
130 delete m_statusTextStacks
[j
];
133 // initialize new stacks to NULL
134 for(j
= i
; j
< (size_t)number
; ++j
)
137 m_statusTextStacks
= newStacks
;
140 // Resize styles array
143 int *oldStyles
= m_statusStyles
;
144 m_statusStyles
= new int[number
];
145 int i
, max
= wxMin(number
, m_nFields
);
148 for (i
= 0; i
< max
; ++i
)
149 m_statusStyles
[i
] = oldStyles
[i
];
151 // initialize new styles to wxSB_NORMAL
152 for (i
= max
; i
< number
; ++i
)
153 m_statusStyles
[i
] = wxSB_NORMAL
;
166 //else: keep the old m_statusWidths if we had them
170 SetStatusWidths(number
, widths
);
172 // already done from SetStatusWidths()
180 void wxStatusBarBase::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n
),
183 wxCHECK_RET( widths
, _T("NULL pointer in SetStatusWidths") );
185 wxASSERT_MSG( n
== m_nFields
, _T("field number mismatch") );
187 if ( !m_statusWidths
)
188 m_statusWidths
= new int[m_nFields
];
190 for ( int i
= 0; i
< m_nFields
; i
++ )
192 m_statusWidths
[i
] = widths
[i
];
195 // update the display after the widths changed
199 void wxStatusBarBase::SetStatusStyles(int WXUNUSED_UNLESS_DEBUG(n
),
202 wxCHECK_RET( styles
, _T("NULL pointer in SetStatusStyles") );
204 wxASSERT_MSG( n
== m_nFields
, _T("field number mismatch") );
206 if ( !m_statusStyles
)
207 m_statusStyles
= new int[m_nFields
];
209 for ( int i
= 0; i
< m_nFields
; i
++ )
211 m_statusStyles
[i
] = styles
[i
];
214 // update the display after the widths changed
218 wxArrayInt
wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal
) const
222 if ( m_statusWidths
== NULL
)
226 // default: all fields have the same width
227 int nWidth
= widthTotal
/ m_nFields
;
228 for ( int i
= 0; i
< m_nFields
; i
++ )
233 //else: we're empty anyhow
235 else // have explicit status widths
237 // calculate the total width of all the fixed width fields and the
238 // total number of var field widths counting with multiplicity
242 for ( i
= 0; i
< m_nFields
; i
++ )
244 if ( m_statusWidths
[i
] >= 0 )
246 nTotalWidth
+= m_statusWidths
[i
];
250 nVarCount
+= -m_statusWidths
[i
];
254 // the amount of extra width we have per each var width field
255 int widthExtra
= widthTotal
- nTotalWidth
;
258 for ( i
= 0; i
< m_nFields
; i
++ )
260 if ( m_statusWidths
[i
] >= 0 )
262 widths
.Add(m_statusWidths
[i
]);
266 int nVarWidth
= widthExtra
> 0 ? (widthExtra
* -m_statusWidths
[i
]) / nVarCount
: 0;
267 nVarCount
+= m_statusWidths
[i
];
268 widthExtra
-= nVarWidth
;
269 widths
.Add(nVarWidth
);
277 // ----------------------------------------------------------------------------
278 // text stacks handling
279 // ----------------------------------------------------------------------------
281 void wxStatusBarBase::InitStacks()
283 m_statusTextStacks
= NULL
;
286 void wxStatusBarBase::FreeStacks()
288 if(!m_statusTextStacks
) return;
291 for(i
= 0; i
< (size_t)m_nFields
; ++i
)
293 if(m_statusTextStacks
[i
])
295 wxListString
& t
= *m_statusTextStacks
[i
];
296 WX_CLEAR_LIST(wxListString
, t
);
297 delete m_statusTextStacks
[i
];
301 delete[] m_statusTextStacks
;
304 // ----------------------------------------------------------------------------
306 // ----------------------------------------------------------------------------
308 void wxStatusBarBase::PushStatusText(const wxString
& text
, int number
)
310 wxListString
* st
= GetOrCreateStatusStack(number
);
311 // This long-winded way around avoids an internal compiler error
312 // in VC++ 6 with RTTI enabled
313 wxString
tmp1(GetStatusText(number
));
314 wxString
* tmp
= new wxString(tmp1
);
316 SetStatusText(text
, number
);
319 void wxStatusBarBase::PopStatusText(int number
)
321 wxListString
*st
= GetStatusStack(number
);
322 wxCHECK_RET( st
, _T("Unbalanced PushStatusText/PopStatusText") );
323 wxListString::compatibility_iterator top
= st
->GetFirst();
325 SetStatusText(*top
->GetData(), number
);
326 delete top
->GetData();
328 if(st
->GetCount() == 0)
331 m_statusTextStacks
[number
] = 0;
335 wxListString
*wxStatusBarBase::GetStatusStack(int i
) const
337 if(!m_statusTextStacks
)
339 return m_statusTextStacks
[i
];
342 wxListString
*wxStatusBarBase::GetOrCreateStatusStack(int i
)
344 if(!m_statusTextStacks
)
346 m_statusTextStacks
= new wxListString
*[m_nFields
];
349 for(j
= 0; j
< (size_t)m_nFields
; ++j
) m_statusTextStacks
[j
] = 0;
352 if(!m_statusTextStacks
[i
])
354 m_statusTextStacks
[i
] = new wxListString();
357 return m_statusTextStacks
[i
];
360 #endif // wxUSE_STATUSBAR