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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/statusbr.h"
34 #include "wx/listimpl.cpp"
35 WX_DEFINE_LIST(wxListString
)
37 // ============================================================================
38 // wxStatusBarBase implementation
39 // ============================================================================
41 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar
, wxWindow
)
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 wxStatusBarBase::wxStatusBarBase()
56 wxStatusBarBase::~wxStatusBarBase()
62 // notify the frame that it doesn't have a status bar any longer to avoid
64 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
65 if ( frame
&& frame
->GetStatusBar() == this )
67 frame
->SetStatusBar(NULL
);
71 // ----------------------------------------------------------------------------
72 // widths array handling
73 // ----------------------------------------------------------------------------
75 void wxStatusBarBase::InitWidths()
77 m_statusWidths
= NULL
;
80 void wxStatusBarBase::FreeWidths()
82 delete [] m_statusWidths
;
85 // ----------------------------------------------------------------------------
86 // styles array handling
87 // ----------------------------------------------------------------------------
89 void wxStatusBarBase::InitStyles()
91 m_statusStyles
= NULL
;
94 void wxStatusBarBase::FreeStyles()
96 delete [] m_statusStyles
;
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 void wxStatusBarBase::SetFieldsCount(int number
, const int *widths
)
105 wxCHECK_RET( number
> 0, _T("invalid field number in SetFieldsCount") );
107 bool refresh
= false;
109 if ( number
!= m_nFields
)
111 // copy stacks if present
112 if(m_statusTextStacks
)
114 wxListString
**newStacks
= new wxListString
*[number
];
115 size_t i
, j
, max
= wxMin(number
, m_nFields
);
118 for(i
= 0; i
< max
; ++i
)
119 newStacks
[i
] = m_statusTextStacks
[i
];
120 // free old stacks in excess
121 for(j
= i
; j
< (size_t)m_nFields
; ++j
)
123 if(m_statusTextStacks
[j
])
125 m_statusTextStacks
[j
]->Clear();
126 delete m_statusTextStacks
[j
];
129 // initialize new stacks to NULL
130 for(j
= i
; j
< (size_t)number
; ++j
)
133 m_statusTextStacks
= newStacks
;
136 // Resize styles array
139 int *oldStyles
= m_statusStyles
;
140 m_statusStyles
= new int[number
];
141 int i
, max
= wxMin(number
, m_nFields
);
144 for (i
= 0; i
< max
; ++i
)
145 m_statusStyles
[i
] = oldStyles
[i
];
147 // initialize new styles to wxSB_NORMAL
148 for (i
= max
; i
< number
; ++i
)
149 m_statusStyles
[i
] = wxSB_NORMAL
;
162 //else: keep the old m_statusWidths if we had them
166 SetStatusWidths(number
, widths
);
168 // already done from SetStatusWidths()
176 void wxStatusBarBase::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n
),
179 wxCHECK_RET( widths
, _T("NULL pointer in SetStatusWidths") );
181 wxASSERT_MSG( n
== m_nFields
, _T("field number mismatch") );
183 if ( !m_statusWidths
)
184 m_statusWidths
= new int[m_nFields
];
186 for ( int i
= 0; i
< m_nFields
; i
++ )
188 m_statusWidths
[i
] = widths
[i
];
191 // update the display after the widths changed
195 void wxStatusBarBase::SetStatusStyles(int WXUNUSED_UNLESS_DEBUG(n
),
198 wxCHECK_RET( styles
, _T("NULL pointer in SetStatusStyles") );
200 wxASSERT_MSG( n
== m_nFields
, _T("field number mismatch") );
202 if ( !m_statusStyles
)
203 m_statusStyles
= new int[m_nFields
];
205 for ( int i
= 0; i
< m_nFields
; i
++ )
207 m_statusStyles
[i
] = styles
[i
];
210 // update the display after the widths changed
214 wxArrayInt
wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal
) const
218 if ( m_statusWidths
== NULL
)
222 // default: all fields have the same width
223 int nWidth
= widthTotal
/ m_nFields
;
224 for ( int i
= 0; i
< m_nFields
; i
++ )
229 //else: we're empty anyhow
231 else // have explicit status widths
233 // calculate the total width of all the fixed width fields and the
234 // total number of var field widths counting with multiplicity
238 for ( i
= 0; i
< m_nFields
; i
++ )
240 if ( m_statusWidths
[i
] >= 0 )
242 nTotalWidth
+= m_statusWidths
[i
];
246 nVarCount
+= -m_statusWidths
[i
];
250 // the amount of extra width we have per each var width field
251 int widthExtra
= widthTotal
- nTotalWidth
;
254 for ( i
= 0; i
< m_nFields
; i
++ )
256 if ( m_statusWidths
[i
] >= 0 )
258 widths
.Add(m_statusWidths
[i
]);
262 int nVarWidth
= widthExtra
> 0 ? (widthExtra
* -m_statusWidths
[i
]) / nVarCount
: 0;
263 nVarCount
+= m_statusWidths
[i
];
264 widthExtra
-= nVarWidth
;
265 widths
.Add(nVarWidth
);
273 // ----------------------------------------------------------------------------
274 // text stacks handling
275 // ----------------------------------------------------------------------------
277 void wxStatusBarBase::InitStacks()
279 m_statusTextStacks
= NULL
;
282 void wxStatusBarBase::FreeStacks()
284 if ( !m_statusTextStacks
)
287 for ( size_t i
= 0; i
< (size_t)m_nFields
; ++i
)
289 if ( m_statusTextStacks
[i
] )
291 wxListString
& t
= *m_statusTextStacks
[i
];
292 WX_CLEAR_LIST(wxListString
, t
);
293 delete m_statusTextStacks
[i
];
297 delete[] m_statusTextStacks
;
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 void wxStatusBarBase::PushStatusText(const wxString
& text
, int number
)
306 wxListString
* st
= GetOrCreateStatusStack(number
);
307 // This long-winded way around avoids an internal compiler error
308 // in VC++ 6 with RTTI enabled
309 wxString
tmp1(GetStatusText(number
));
310 wxString
* tmp
= new wxString(tmp1
);
312 SetStatusText(text
, number
);
315 void wxStatusBarBase::PopStatusText(int number
)
317 wxListString
*st
= GetStatusStack(number
);
318 wxCHECK_RET( st
, _T("Unbalanced PushStatusText/PopStatusText") );
319 wxListString::compatibility_iterator top
= st
->GetFirst();
321 SetStatusText(*top
->GetData(), number
);
322 delete top
->GetData();
324 if(st
->GetCount() == 0)
327 m_statusTextStacks
[number
] = 0;
331 wxListString
*wxStatusBarBase::GetStatusStack(int i
) const
333 if(!m_statusTextStacks
)
335 return m_statusTextStacks
[i
];
338 wxListString
*wxStatusBarBase::GetOrCreateStatusStack(int i
)
340 if(!m_statusTextStacks
)
342 m_statusTextStacks
= new wxListString
*[m_nFields
];
345 for(j
= 0; j
< (size_t)m_nFields
; ++j
) m_statusTextStacks
[j
] = 0;
348 if(!m_statusTextStacks
[i
])
350 m_statusTextStacks
[i
] = new wxListString();
353 return m_statusTextStacks
[i
];
356 #endif // wxUSE_STATUSBAR