]> git.saurik.com Git - wxWidgets.git/blob - src/common/statbar.cpp
don't crash in UnselectAll() if the tree has no root
[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 // default: all fields have the same width
131 int nWidth = widthTotal / m_nFields;
132 for ( int i = 0; i < m_nFields; i++ )
133 {
134 widths.Add(nWidth);
135 }
136 }
137 else // have explicit status widths
138 {
139 // calculate the total width of all the fixed width fields and the
140 // total number of var field widths counting with multiplicity
141 int nTotalWidth = 0,
142 nVarCount = 0,
143 i;
144 for ( i = 0; i < m_nFields; i++ )
145 {
146 if ( m_statusWidths[i] >= 0 )
147 {
148 nTotalWidth += m_statusWidths[i];
149 }
150 else
151 {
152 nVarCount += -m_statusWidths[i];
153 }
154 }
155
156 // the amount of extra width we have per each var width field
157 int nVarWidth;
158 if ( nVarCount )
159 {
160 int widthExtra = widthTotal - nTotalWidth;
161 nVarWidth = widthExtra > 0 ? widthExtra / nVarCount : 0;
162 }
163 else // no var width fields at all
164 {
165 nVarWidth = 0;
166 }
167
168 // do fill the array
169 for ( i = 0; i < m_nFields; i++ )
170 {
171 if ( m_statusWidths[i] >= 0 )
172 {
173 widths.Add(m_statusWidths[i]);
174 }
175 else
176 {
177 widths.Add(-m_statusWidths[i]*nVarWidth);
178 }
179 }
180 }
181
182 return widths;
183 }
184
185 #endif // wxUSE_STATUSBAR
186