1. added wxStatusBarUniv
[wxWidgets.git] / src / univ / statusbr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/statusbr.cpp
3 // Purpose: wxStatusBar implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 14.10.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "univstatusbr.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #if wxUSE_STATUSBAR
31
32 #ifndef WX_PRECOMP
33 #endif
34
35 #include "wx/statusbr.h"
36
37 #include "wx/univ/renderer.h"
38
39 // ============================================================================
40 // implementation
41 // ============================================================================
42
43 BEGIN_EVENT_TABLE(wxStatusBarUniv, wxStatusBarBase)
44 EVT_SIZE(wxStatusBarUniv::OnSize)
45
46 WX_EVENT_TABLE_INPUT_CONSUMER(wxStatusBarUniv)
47 END_EVENT_TABLE()
48
49 WX_FORWARD_TO_INPUT_CONSUMER(wxStatusBarUniv)
50
51 // ----------------------------------------------------------------------------
52 // creation
53 // ----------------------------------------------------------------------------
54
55 void wxStatusBarUniv::Init()
56 {
57 }
58
59 bool wxStatusBarUniv::Create(wxWindow *parent,
60 wxWindowID id,
61 long style,
62 const wxString& name)
63 {
64 if ( !wxWindow::Create(parent, id,
65 wxDefaultPosition, wxDefaultSize,
66 style, name) )
67 {
68 return FALSE;
69 }
70
71 SetFieldsCount(1);
72
73 CreateInputHandler(wxINP_HANDLER_STATUSBAR);
74
75 SetSize(DoGetBestSize());
76
77 return TRUE;
78 }
79
80 // ----------------------------------------------------------------------------
81 // drawing
82 // ----------------------------------------------------------------------------
83
84 wxRect wxStatusBarUniv::GetTotalFieldRect(wxCoord *borderBetweenFields)
85 {
86 // determine the space we have for the fields
87 wxSize sizeBorders = m_renderer->GetStatusBarBorders(borderBetweenFields);
88
89 wxRect rect = GetClientRect();
90
91 // no, don't do this - the borders are meant to be inside this rect
92 //rect.Deflate(sizeBorders.x, sizeBorders.y);
93
94 // recalc the field widths if needed
95 if ( m_widthsAbs.IsEmpty() )
96 {
97 // the total width for the fields doesn't include the borders between
98 // them
99 m_widthsAbs = CalculateAbsWidths(rect.width -
100 *borderBetweenFields*(m_nFields - 1));
101 }
102
103 return rect;
104 }
105
106 void wxStatusBarUniv::DoDraw(wxControlRenderer *renderer)
107 {
108 // get the fields rect
109 wxCoord borderBetweenFields;
110 wxRect rect = GetTotalFieldRect(&borderBetweenFields);
111
112 // prepare the DC
113 wxDC& dc = renderer->GetDC();
114 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
115
116 // do draw the fields
117 int flags = IsEnabled() ? 0 : wxCONTROL_DISABLED;
118 for ( int n = 0; n < m_nFields; n++ )
119 {
120 rect.width = m_widthsAbs[n];
121
122 if ( IsExposed(rect) )
123 {
124 // the size grip may be drawn only on the last field and only if we
125 // have the corresponding style and even then only if we really can
126 // resize this frame
127 if ( n == m_nFields - 1 &&
128 HasFlag(wxST_SIZEGRIP) &&
129 GetParent()->HasFlag(wxRESIZE_BORDER) )
130 {
131 // NB: we use wxCONTROL_ISDEFAULT for this because it doesn't
132 // have any meaning for the status bar otherwise anyhow
133 // (it's still ugly, of course, but there are too few flags
134 // to squander them for things like this)
135 flags |= wxCONTROL_ISDEFAULT;
136 }
137
138 m_renderer->DrawStatusField(dc, rect, m_statusText[n], flags);
139 }
140
141 rect.x += rect.width + borderBetweenFields;
142 }
143 }
144
145 void wxStatusBarUniv::RefreshField(int i)
146 {
147 wxRect rect;
148 if ( GetFieldRect(i, rect) )
149 {
150 RefreshRect(rect);
151 }
152 }
153
154 // ----------------------------------------------------------------------------
155 // fields text
156 // ----------------------------------------------------------------------------
157
158 void wxStatusBarUniv::SetStatusText(const wxString& text, int number)
159 {
160 wxCHECK_RET( number >= 0 && number < m_nFields,
161 _T("invalid status bar field index in SetStatusText()") );
162
163 if ( text == m_statusText[number] )
164 {
165 // nothing changed
166 return;
167 }
168
169 m_statusText[number] = text;
170
171 RefreshField(number);
172 }
173
174 wxString wxStatusBarUniv::GetStatusText(int number) const
175 {
176 wxCHECK_MSG( number >= 0 && number < m_nFields, _T(""),
177 _T("invalid status bar field index") );
178
179 return m_statusText[number];
180 }
181
182 // ----------------------------------------------------------------------------
183 // fields count/widths
184 // ----------------------------------------------------------------------------
185
186 void wxStatusBarUniv::SetFieldsCount(int number, const int *widths)
187 {
188 wxStatusBarBase::SetFieldsCount(number, widths);
189
190 m_statusText.SetCount(number);
191 m_widthsAbs.Empty();
192 }
193
194 void wxStatusBarUniv::SetStatusWidths(int n, const int widths[])
195 {
196 wxStatusBarBase::SetStatusWidths(n, widths);
197
198 m_widthsAbs.Empty();
199 }
200
201 // ----------------------------------------------------------------------------
202 // geometry
203 // ----------------------------------------------------------------------------
204
205 void wxStatusBarUniv::OnSize(wxSizeEvent& event)
206 {
207 // invalidate the widths, we'll have to recalc them
208 m_widthsAbs.Empty();
209
210 // refresh entirely, shouldn't matter much as the statusbar is quick to
211 // redraw and it would be difficult to avoid it as we'd need to find out
212 // which fields exactly were affected...
213 Refresh();
214
215 event.Skip();
216 }
217
218 bool wxStatusBarUniv::GetFieldRect(int n, wxRect& rect) const
219 {
220 wxCHECK_MSG( n >= 0 && n < m_nFields, FALSE,
221 _T("invalid field index in GetFieldRect()") );
222
223 // this is a fix for a bug exhibited by the statbar sample: if
224 // GetFieldRect() is called from the derived class OnSize() handler, then
225 // our geometry info is wrong as our OnSize() didn't invalidate m_widthsAbs
226 // yet - so recalc it just in case
227 wxStatusBarUniv *self = wxConstCast(this, wxStatusBarUniv);
228 self->m_widthsAbs.Empty();
229
230 wxCoord borderBetweenFields;
231 rect = self->GetTotalFieldRect(&borderBetweenFields);
232 for ( int i = 0; i <= n; i++ )
233 {
234 rect.width = m_widthsAbs[i];
235
236 if ( i < n )
237 rect.x += rect.width + borderBetweenFields;
238 }
239
240 return TRUE;
241 }
242
243 wxCoord wxStatusBarUniv::GetHeight() const
244 {
245 wxClientDC dc(wxConstCast(this, wxStatusBarUniv));
246 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
247
248 return dc.GetCharHeight() + 2*GetBorderY();
249 }
250
251 wxSize wxStatusBarUniv::DoGetBestSize() const
252 {
253 return wxSize(100, GetHeight());
254 }
255
256 void wxStatusBarUniv::DoSetSize(int x, int y,
257 int width, int WXUNUSED(height),
258 int sizeFlags)
259 {
260 wxStatusBarBase::DoSetSize(x, y, width, GetHeight(), sizeFlags);
261 }
262
263 // ----------------------------------------------------------------------------
264 // misc
265 // ----------------------------------------------------------------------------
266
267 void wxStatusBarUniv::SetMinHeight(int WXUNUSED(height))
268 {
269 // nothing to do here, we don't support it - and why would we?
270 }
271
272 int wxStatusBarUniv::GetBorderX() const
273 {
274 return m_renderer->GetStatusBarBorders(NULL).x;
275 }
276
277 int wxStatusBarUniv::GetBorderY() const
278 {
279 return m_renderer->GetStatusBarBorders(NULL).y;
280 }
281
282 #endif // wxUSE_STATUSBAR
283