]> git.saurik.com Git - wxWidgets.git/blob - src/common/statbar.cpp
added a note about the constraints being deprecated
[wxWidgets.git] / src / common / statbar.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/statbar.cpp
3 // Purpose: wxStatusBarBase implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 14.10.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "statbar.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/statusbr.h"
33 #endif //WX_PRECOMP
34
35 #if wxUSE_STATUSBAR
36
37 // ============================================================================
38 // wxStatusBarBase implementation
39 // ============================================================================
40
41 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar, wxWindow)
42
43 // ----------------------------------------------------------------------------
44 // ctor/dtor
45 // ----------------------------------------------------------------------------
46
47 wxStatusBarBase::wxStatusBarBase()
48 {
49 m_nFields = 0;
50
51 InitWidths();
52 }
53
54 wxStatusBarBase::~wxStatusBarBase()
55 {
56 FreeWidths();
57 }
58
59 // ----------------------------------------------------------------------------
60 // widths array handling
61 // ----------------------------------------------------------------------------
62
63 void wxStatusBarBase::InitWidths()
64 {
65 m_statusWidths = NULL;
66 }
67
68 void wxStatusBarBase::FreeWidths()
69 {
70 delete [] m_statusWidths;
71 }
72
73 // ----------------------------------------------------------------------------
74 // field widths
75 // ----------------------------------------------------------------------------
76
77 void wxStatusBarBase::SetFieldsCount(int number, const int *widths)
78 {
79 wxCHECK_RET( number > 0, _T("invalid field number in SetFieldsCount") );
80
81 bool refresh = FALSE;
82
83 if ( number != m_nFields )
84 {
85 m_nFields = number;
86
87 ReinitWidths();
88
89 refresh = TRUE;
90 }
91 //else: keep the old m_statusWidths if we had them
92
93 if ( widths )
94 {
95 SetStatusWidths(number, widths);
96
97 // already done from SetStatusWidths()
98 refresh = FALSE;
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
110 wxASSERT_MSG( n == m_nFields, _T("field number mismatch") );
111
112 if ( !m_statusWidths )
113 m_statusWidths = new int[m_nFields];
114
115 for ( int i = 0; i < m_nFields; i++ )
116 {
117 m_statusWidths[i] = widths[i];
118 }
119
120 // update the display after the widths changed
121 Refresh();
122 }
123
124 wxArrayInt wxStatusBarBase::CalculateAbsWidths(wxCoord widthTotal) const
125 {
126 wxArrayInt widths;
127
128 if ( m_statusWidths == NULL )
129 {
130 if ( m_nFields )
131 {
132 // default: all fields have the same width
133 int nWidth = widthTotal / m_nFields;
134 for ( int i = 0; i < m_nFields; i++ )
135 {
136 widths.Add(nWidth);
137 }
138 }
139 //else: we're empty anyhow
140 }
141 else // have explicit status widths
142 {
143 // calculate the total width of all the fixed width fields and the
144 // total number of var field widths counting with multiplicity
145 int nTotalWidth = 0,
146 nVarCount = 0,
147 i;
148 for ( i = 0; i < m_nFields; i++ )
149 {
150 if ( m_statusWidths[i] >= 0 )
151 {
152 nTotalWidth += m_statusWidths[i];
153 }
154 else
155 {
156 nVarCount += -m_statusWidths[i];
157 }
158 }
159
160 // the amount of extra width we have per each var width field
161 int nVarWidth;
162 if ( nVarCount )
163 {
164 int widthExtra = widthTotal - nTotalWidth;
165 nVarWidth = widthExtra > 0 ? widthExtra / nVarCount : 0;
166 }
167 else // no var width fields at all
168 {
169 nVarWidth = 0;
170 }
171
172 // do fill the array
173 for ( i = 0; i < m_nFields; i++ )
174 {
175 if ( m_statusWidths[i] >= 0 )
176 {
177 widths.Add(m_statusWidths[i]);
178 }
179 else
180 {
181 widths.Add(-m_statusWidths[i]*nVarWidth);
182 }
183 }
184 }
185
186 return widths;
187 }
188
189 #endif // wxUSE_STATUSBAR
190