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"
29 #include "wx/statusbr.h"
35 #include "wx/listimpl.cpp"
36 WX_DEFINE_LIST(wxListString
)
38 const wxChar wxStatusBarNameStr
[] = wxT("statusBar");
40 // ============================================================================
41 // wxStatusBarBase implementation
42 // ============================================================================
44 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar
, wxWindow
)
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 wxStatusBarBase::wxStatusBarBase()
59 wxStatusBarBase::~wxStatusBarBase()
65 // notify the frame that it doesn't have a status bar any longer to avoid
67 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
68 if ( frame
&& frame
->GetStatusBar() == this )
70 frame
->SetStatusBar(NULL
);
74 // ----------------------------------------------------------------------------
75 // widths array handling
76 // ----------------------------------------------------------------------------
78 void wxStatusBarBase::InitWidths()
80 m_statusWidths
= NULL
;
83 void wxStatusBarBase::FreeWidths()
85 delete [] m_statusWidths
;
88 // ----------------------------------------------------------------------------
89 // styles array handling
90 // ----------------------------------------------------------------------------
92 void wxStatusBarBase::InitStyles()
94 m_statusStyles
= NULL
;
97 void wxStatusBarBase::FreeStyles()
99 delete [] m_statusStyles
;
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 void wxStatusBarBase::SetFieldsCount(int number
, const int *widths
)
108 wxCHECK_RET( number
> 0, _T("invalid field number in SetFieldsCount") );
110 bool refresh
= false;
112 if ( number
!= m_nFields
)
114 // copy stacks if present
115 if(m_statusTextStacks
)
117 wxListString
**newStacks
= new wxListString
*[number
];
118 size_t i
, j
, max
= wxMin(number
, m_nFields
);
121 for(i
= 0; i
< max
; ++i
)
122 newStacks
[i
] = m_statusTextStacks
[i
];
123 // free old stacks in excess
124 for(j
= i
; j
< (size_t)m_nFields
; ++j
)
126 if(m_statusTextStacks
[j
])
128 m_statusTextStacks
[j
]->Clear();
129 delete m_statusTextStacks
[j
];
132 // initialize new stacks to NULL
133 for(j
= i
; j
< (size_t)number
; ++j
)
136 m_statusTextStacks
= newStacks
;
139 // Resize styles array
142 int *oldStyles
= m_statusStyles
;
143 m_statusStyles
= new int[number
];
144 int i
, max
= wxMin(number
, m_nFields
);
147 for (i
= 0; i
< max
; ++i
)
148 m_statusStyles
[i
] = oldStyles
[i
];
150 // initialize new styles to wxSB_NORMAL
151 for (i
= max
; i
< number
; ++i
)
152 m_statusStyles
[i
] = wxSB_NORMAL
;
165 //else: keep the old m_statusWidths if we had them
169 SetStatusWidths(number
, widths
);
171 // already done from SetStatusWidths()
179 void wxStatusBarBase::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n
),
182 wxCHECK_RET( widths
, _T("NULL pointer in SetStatusWidths") );
184 wxASSERT_MSG( n
== m_nFields
, _T("field number mismatch") );
186 if ( !m_statusWidths
)
187 m_statusWidths
= new int[m_nFields
];
189 for ( int i
= 0; i
< m_nFields
; i
++ )
191 m_statusWidths
[i
] = widths
[i
];
194 // update the display after the widths changed
198 void wxStatusBarBase::SetStatusStyles(int WXUNUSED_UNLESS_DEBUG(n
),
201 wxCHECK_RET( styles
, _T("NULL pointer in SetStatusStyles") );
203 wxASSERT_MSG( n
== m_nFields
, _T("field number mismatch") );
205 if ( !m_statusStyles
)
206 m_statusStyles
= new int[m_nFields
];
208 for ( int i
= 0; i
< m_nFields
; i
++ )
210 m_statusStyles
[i
] = styles
[i
];
213 // update the display after the widths changed
217 wxArrayInt
wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal
) const
221 if ( m_statusWidths
== NULL
)
225 // default: all fields have the same width
226 int nWidth
= widthTotal
/ m_nFields
;
227 for ( int i
= 0; i
< m_nFields
; i
++ )
232 //else: we're empty anyhow
234 else // have explicit status widths
236 // calculate the total width of all the fixed width fields and the
237 // total number of var field widths counting with multiplicity
241 for ( i
= 0; i
< m_nFields
; i
++ )
243 if ( m_statusWidths
[i
] >= 0 )
245 nTotalWidth
+= m_statusWidths
[i
];
249 nVarCount
+= -m_statusWidths
[i
];
253 // the amount of extra width we have per each var width field
254 int widthExtra
= widthTotal
- nTotalWidth
;
257 for ( i
= 0; i
< m_nFields
; i
++ )
259 if ( m_statusWidths
[i
] >= 0 )
261 widths
.Add(m_statusWidths
[i
]);
265 int nVarWidth
= widthExtra
> 0 ? (widthExtra
* -m_statusWidths
[i
]) / nVarCount
: 0;
266 nVarCount
+= m_statusWidths
[i
];
267 widthExtra
-= nVarWidth
;
268 widths
.Add(nVarWidth
);
276 // ----------------------------------------------------------------------------
277 // text stacks handling
278 // ----------------------------------------------------------------------------
280 void wxStatusBarBase::InitStacks()
282 m_statusTextStacks
= NULL
;
285 void wxStatusBarBase::FreeStacks()
287 if ( !m_statusTextStacks
)
290 for ( size_t i
= 0; i
< (size_t)m_nFields
; ++i
)
292 if ( m_statusTextStacks
[i
] )
294 wxListString
& t
= *m_statusTextStacks
[i
];
295 WX_CLEAR_LIST(wxListString
, t
);
296 delete m_statusTextStacks
[i
];
300 delete[] m_statusTextStacks
;
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
307 void wxStatusBarBase::PushStatusText(const wxString
& text
, int number
)
309 wxListString
* st
= GetOrCreateStatusStack(number
);
310 // This long-winded way around avoids an internal compiler error
311 // in VC++ 6 with RTTI enabled
312 wxString
tmp1(GetStatusText(number
));
313 wxString
* tmp
= new wxString(tmp1
);
315 SetStatusText(text
, number
);
318 void wxStatusBarBase::PopStatusText(int number
)
320 wxListString
*st
= GetStatusStack(number
);
321 wxCHECK_RET( st
, _T("Unbalanced PushStatusText/PopStatusText") );
322 wxListString::compatibility_iterator top
= st
->GetFirst();
324 SetStatusText(*top
->GetData(), number
);
325 delete top
->GetData();
327 if(st
->GetCount() == 0)
330 m_statusTextStacks
[number
] = 0;
334 wxListString
*wxStatusBarBase::GetStatusStack(int i
) const
336 if(!m_statusTextStacks
)
338 return m_statusTextStacks
[i
];
341 wxListString
*wxStatusBarBase::GetOrCreateStatusStack(int i
)
343 if(!m_statusTextStacks
)
345 m_statusTextStacks
= new wxListString
*[m_nFields
];
348 for(j
= 0; j
< (size_t)m_nFields
; ++j
) m_statusTextStacks
[j
] = 0;
351 if(!m_statusTextStacks
[i
])
353 m_statusTextStacks
[i
] = new wxListString();
356 return m_statusTextStacks
[i
];
359 #endif // wxUSE_STATUSBAR