]> git.saurik.com Git - wxWidgets.git/blob - src/univ/statusbr.cpp
Include wx/dcmemory.h according to precompiled headers of wx/wx.h (with other minor...
[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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_STATUSBAR
27
28 #ifndef WX_PRECOMP
29 #include "wx/settings.h"
30 #include "wx/dcclient.h"
31 #endif
32
33 #include "wx/statusbr.h"
34 #include "wx/toplevel.h"
35
36 #include "wx/univ/renderer.h"
37
38 // ============================================================================
39 // implementation
40 // ============================================================================
41
42 BEGIN_EVENT_TABLE(wxStatusBarUniv, wxStatusBarBase)
43 EVT_SIZE(wxStatusBarUniv::OnSize)
44
45 WX_EVENT_TABLE_INPUT_CONSUMER(wxStatusBarUniv)
46 END_EVENT_TABLE()
47
48 WX_FORWARD_TO_INPUT_CONSUMER(wxStatusBarUniv)
49
50 // ----------------------------------------------------------------------------
51 // creation
52 // ----------------------------------------------------------------------------
53
54 void wxStatusBarUniv::Init()
55 {
56 }
57
58 bool wxStatusBarUniv::Create(wxWindow *parent,
59 wxWindowID id,
60 long style,
61 const wxString& name)
62 {
63 if ( !wxWindow::Create(parent, id,
64 wxDefaultPosition, wxDefaultSize,
65 style, name) )
66 {
67 return false;
68 }
69
70 SetFieldsCount(1);
71
72 CreateInputHandler(wxINP_HANDLER_STATUSBAR);
73
74 SetSize(DoGetBestSize());
75
76 return true;
77 }
78
79 // ----------------------------------------------------------------------------
80 // drawing
81 // ----------------------------------------------------------------------------
82
83 wxRect wxStatusBarUniv::GetTotalFieldRect(wxCoord *borderBetweenFields)
84 {
85 wxRect rect = GetClientRect();
86
87 // no, don't do this - the borders are meant to be inside this rect
88 // wxSize sizeBorders =
89 m_renderer->GetStatusBarBorders(borderBetweenFields);
90 //rect.Deflate(sizeBorders.x, sizeBorders.y);
91
92 // recalc the field widths if needed
93 if ( m_widthsAbs.IsEmpty() )
94 {
95 // the total width for the fields doesn't include the borders between
96 // them
97 m_widthsAbs = CalculateAbsWidths(rect.width -
98 *borderBetweenFields*(m_nFields - 1));
99 }
100
101 return rect;
102 }
103
104 void wxStatusBarUniv::DoDraw(wxControlRenderer *renderer)
105 {
106 // get the fields rect
107 wxCoord borderBetweenFields;
108 wxRect rect = GetTotalFieldRect(&borderBetweenFields);
109
110 // prepare the DC
111 wxDC& dc = renderer->GetDC();
112 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
113
114 // do draw the fields
115 int flags = IsEnabled() ? 0 : (int)wxCONTROL_DISABLED;
116 for ( int n = 0; n < m_nFields; n++ )
117 {
118 rect.width = m_widthsAbs[n];
119
120 if ( IsExposed(rect) )
121 {
122 wxTopLevelWindow *parentTLW = wxDynamicCast(GetParent(), wxTopLevelWindow);
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 parentTLW && !parentTLW->IsMaximized() )
131 {
132 // NB: we use wxCONTROL_ISDEFAULT for this because it doesn't
133 // have any meaning for the status bar otherwise anyhow
134 // (it's still ugly, of course, but there are too few flags
135 // to squander them for things like this)
136 flags |= wxCONTROL_ISDEFAULT;
137 }
138
139 int style;
140 if (m_statusStyles)
141 style = m_statusStyles[n];
142 else
143 style = wxSB_NORMAL;
144 m_renderer->DrawStatusField(dc, rect, m_statusText[n], flags, style);
145 }
146
147 rect.x += rect.width + borderBetweenFields;
148 }
149 }
150
151 void wxStatusBarUniv::RefreshField(int i)
152 {
153 wxRect rect;
154 if ( GetFieldRect(i, rect) )
155 {
156 RefreshRect(rect);
157 }
158 }
159
160 // ----------------------------------------------------------------------------
161 // fields text
162 // ----------------------------------------------------------------------------
163
164 void wxStatusBarUniv::SetStatusText(const wxString& text, int number)
165 {
166 wxCHECK_RET( number >= 0 && number < m_nFields,
167 _T("invalid status bar field index in SetStatusText()") );
168
169 if ( text == m_statusText[number] )
170 {
171 // nothing changed
172 return;
173 }
174
175 m_statusText[number] = text;
176
177 RefreshField(number);
178 }
179
180 wxString wxStatusBarUniv::GetStatusText(int number) const
181 {
182 wxCHECK_MSG( number >= 0 && number < m_nFields, wxEmptyString,
183 _T("invalid status bar field index") );
184
185 return m_statusText[number];
186 }
187
188 // ----------------------------------------------------------------------------
189 // fields count/widths
190 // ----------------------------------------------------------------------------
191
192 void wxStatusBarUniv::SetFieldsCount(int number, const int *widths)
193 {
194 m_statusText.SetCount(number);
195 wxStatusBarBase::SetFieldsCount(number, widths);
196 m_widthsAbs.Empty();
197 }
198
199 void wxStatusBarUniv::SetStatusWidths(int n, const int widths[])
200 {
201 wxStatusBarBase::SetStatusWidths(n, widths);
202
203 m_widthsAbs.Empty();
204 }
205
206 // ----------------------------------------------------------------------------
207 // geometry
208 // ----------------------------------------------------------------------------
209
210 void wxStatusBarUniv::OnSize(wxSizeEvent& event)
211 {
212 // we don't need to refresh the fields whose width didn't change, so find
213 // the first field whose width did change and refresh starting from it
214 int field;
215 if ( m_statusWidths )
216 {
217 for ( field = 0; field < m_nFields; field++ )
218 {
219 if ( m_statusWidths[field] < 0 )
220 {
221 // var width field
222 break;
223 }
224 }
225 }
226 else // all fields have the same width
227 {
228 // hence all fields widths have changed
229 field = 0;
230 }
231
232 if ( field < m_nFields )
233 {
234 // call this before invalidating the old widths as we want to use them,
235 // not the new ones
236 wxRect rect = DoGetFieldRect(field);
237
238 // invalidate the widths, we'll have to recalc them
239 m_widthsAbs.Empty();
240
241 // refresh everything after the first invalid field
242 rect.y = 0;
243 rect.SetRight(event.GetSize().x);
244 rect.height = event.GetSize().y;
245 RefreshRect(rect);
246 }
247
248 event.Skip();
249 }
250
251 bool wxStatusBarUniv::GetFieldRect(int n, wxRect& rect) const
252 {
253 wxCHECK_MSG( n >= 0 && n < m_nFields, false,
254 _T("invalid field index in GetFieldRect()") );
255
256 // this is a fix for a bug exhibited by the statbar sample: if
257 // GetFieldRect() is called from the derived class OnSize() handler, then
258 // our geometry info is wrong as our OnSize() didn't invalidate m_widthsAbs
259 // yet - so recalc it just in case
260 wxConstCast(this, wxStatusBarUniv)->m_widthsAbs.Empty();
261
262 rect = DoGetFieldRect(n);
263
264 return true;
265 }
266
267 wxRect wxStatusBarUniv::DoGetFieldRect(int n) const
268 {
269 wxStatusBarUniv *self = wxConstCast(this, wxStatusBarUniv);
270
271 wxCoord borderBetweenFields;
272 wxRect rect = self->GetTotalFieldRect(&borderBetweenFields);
273
274 // it's the caller responsability to check this, if unsure - call
275 // GetFieldRect() instead
276 wxCHECK_MSG( !m_widthsAbs.IsEmpty(), rect,
277 _T("can't be called if we don't have the widths") );
278
279 for ( int i = 0; i <= n; i++ )
280 {
281 rect.width = m_widthsAbs[i];
282
283 if ( i < n )
284 rect.x += rect.width + borderBetweenFields;
285 }
286
287 return rect;
288 }
289
290 wxCoord wxStatusBarUniv::GetHeight() const
291 {
292 wxClientDC dc(wxConstCast(this, wxStatusBarUniv));
293 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
294
295 return dc.GetCharHeight() + 2*GetBorderY();
296 }
297
298 wxSize wxStatusBarUniv::DoGetBestSize() const
299 {
300 return wxSize(100, GetHeight());
301 }
302
303 void wxStatusBarUniv::DoSetSize(int x, int y,
304 int width, int WXUNUSED(height),
305 int sizeFlags)
306 {
307 wxStatusBarBase::DoSetSize(x, y, width, GetHeight(), sizeFlags);
308 }
309
310 // ----------------------------------------------------------------------------
311 // misc
312 // ----------------------------------------------------------------------------
313
314 void wxStatusBarUniv::SetMinHeight(int WXUNUSED(height))
315 {
316 // nothing to do here, we don't support it - and why would we?
317 }
318
319 int wxStatusBarUniv::GetBorderX() const
320 {
321 return m_renderer->GetStatusBarBorders(NULL).x;
322 }
323
324 int wxStatusBarUniv::GetBorderY() const
325 {
326 return m_renderer->GetStatusBarBorders(NULL).y;
327 }
328
329 #endif // wxUSE_STATUSBAR
330