don't use wxScopedPtr<> in wxDocTemplate::CreateDocument() as the document is implici...
[wxWidgets.git] / src / common / statbar.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/statbar.cpp
3 // Purpose: wxStatusBarBase implementation
4 // Author: Vadim Zeitlin
5 // Modified by: Francesco Montorsi
6 // Created: 14.10.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_STATUSBAR
28
29 #include "wx/statusbr.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/frame.h"
33 #endif //WX_PRECOMP
34
35 const char wxStatusBarNameStr[] = "statusBar";
36
37
38 // ============================================================================
39 // wxStatusBarBase implementation
40 // ============================================================================
41
42 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar, wxWindow)
43
44 #include "wx/arrimpl.cpp" // This is a magic incantation which must be done!
45 WX_DEFINE_OBJARRAY(wxStatusBarPaneArray)
46
47
48 // ----------------------------------------------------------------------------
49 // ctor/dtor
50 // ----------------------------------------------------------------------------
51
52 wxStatusBarBase::wxStatusBarBase()
53 {
54 m_bSameWidthForAllPanes = true;
55 }
56
57 wxStatusBarBase::~wxStatusBarBase()
58 {
59 // notify the frame that it doesn't have a status bar any longer to avoid
60 // dangling pointers
61 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
62 if ( frame && frame->GetStatusBar() == this )
63 frame->SetStatusBar(NULL);
64 }
65
66 // ----------------------------------------------------------------------------
67 // field widths
68 // ----------------------------------------------------------------------------
69
70 void wxStatusBarBase::SetFieldsCount(int number, const int *widths)
71 {
72 wxCHECK_RET( number > 0, _T("invalid field number in SetFieldsCount") );
73
74 if ( (size_t)number > m_panes.GetCount() )
75 {
76 wxStatusBarPane newPane;
77
78 // add more entries with the default style and zero width
79 // (this will be set later)
80 for (size_t i = m_panes.GetCount(); i < (size_t)number; ++i)
81 m_panes.Add(newPane);
82 }
83 else if ( (size_t)number < m_panes.GetCount() )
84 {
85 // remove entries in excess
86 m_panes.RemoveAt(number, m_panes.GetCount()-number);
87 }
88
89 // SetStatusWidths will automatically refresh
90 SetStatusWidths(number, widths);
91 }
92
93 void wxStatusBarBase::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n),
94 const int widths[])
95 {
96 wxASSERT_MSG( (size_t)n == m_panes.GetCount(), _T("field number mismatch") );
97
98 if (widths == NULL)
99 {
100 // special value meaning: override explicit pane widths and make them all
101 // of the same size
102 m_bSameWidthForAllPanes = true;
103 }
104 else
105 {
106 for ( size_t i = 0; i < m_panes.GetCount(); i++ )
107 m_panes[i].nWidth = widths[i];
108
109 m_bSameWidthForAllPanes = false;
110 }
111
112 // update the display after the widths changed
113 Refresh();
114 }
115
116 void wxStatusBarBase::SetStatusStyles(int WXUNUSED_UNLESS_DEBUG(n),
117 const int styles[])
118 {
119 wxCHECK_RET( styles, _T("NULL pointer in SetStatusStyles") );
120
121 wxASSERT_MSG( (size_t)n == m_panes.GetCount(), _T("field number mismatch") );
122
123 for ( size_t i = 0; i < m_panes.GetCount(); i++ )
124 m_panes[i].nStyle = styles[i];
125
126 // update the display after the widths changed
127 Refresh();
128 }
129
130 wxArrayInt wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal) const
131 {
132 wxArrayInt widths;
133
134 if ( m_bSameWidthForAllPanes )
135 {
136 // Default: all fields have the same width. This is not always
137 // possible to do exactly (if widthTotal is not divisible by
138 // m_panes.GetCount()) - if that happens, we distribute the extra
139 // pixels among all fields:
140 int widthToUse = widthTotal;
141
142 for ( size_t i = m_panes.GetCount(); i > 0; i-- )
143 {
144 // divide the unassigned width evently between the
145 // not yet processed fields:
146 int w = widthToUse / i;
147 widths.Add(w);
148 widthToUse -= w;
149 }
150 }
151 else // do not override explicit pane widths
152 {
153 // calculate the total width of all the fixed width fields and the
154 // total number of var field widths counting with multiplicity
155 size_t nTotalWidth = 0,
156 nVarCount = 0,
157 i;
158
159 for ( i = 0; i < m_panes.GetCount(); i++ )
160 {
161 if ( m_panes[i].nWidth >= 0 )
162 nTotalWidth += m_panes[i].nWidth;
163 else
164 nVarCount += -m_panes[i].nWidth;
165 }
166
167 // the amount of extra width we have per each var width field
168 int widthExtra = widthTotal - nTotalWidth;
169
170 // do fill the array
171 for ( i = 0; i < m_panes.GetCount(); i++ )
172 {
173 if ( m_panes[i].nWidth >= 0 )
174 widths.Add(m_panes[i].nWidth);
175 else
176 {
177 int nVarWidth = widthExtra > 0 ? (widthExtra * (-m_panes[i].nWidth)) / nVarCount : 0;
178 nVarCount += m_panes[i].nWidth;
179 widthExtra -= nVarWidth;
180 widths.Add(nVarWidth);
181 }
182 }
183 }
184
185 return widths;
186 }
187
188 // ----------------------------------------------------------------------------
189 // status text stacks
190 // ----------------------------------------------------------------------------
191
192 void wxStatusBarBase::PushStatusText(const wxString& text, int number)
193 {
194 // save current status text in the stack
195 m_panes[number].arrStack.push_back(GetStatusText(number));
196
197 SetStatusText(text, number);
198 // update current status text (eventually also in the native control)
199 }
200
201 void wxStatusBarBase::PopStatusText(int number)
202 {
203 wxASSERT_MSG(m_panes[number].arrStack.GetCount() == 1,
204 "can't pop any further string");
205
206 wxString text = m_panes[number].arrStack.back();
207 m_panes[number].arrStack.pop_back(); // also remove it from the stack
208
209 // restore the popped status text in the pane
210 SetStatusText(text, number);
211 }
212
213 #endif // wxUSE_STATUSBAR