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