Implemented wxStatusBar::Push/PopStatusText.
[wxWidgets.git] / src / generic / statusbr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/statusbr.cpp
3 // Purpose: wxStatusBarGeneric class implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "statusbr.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_STATUSBAR
24
25 #ifndef WX_PRECOMP
26 #include "wx/setup.h"
27 #include "wx/frame.h"
28 #include "wx/settings.h"
29 #include "wx/dcclient.h"
30 #endif
31
32 #include "wx/statusbr.h"
33
34 // we only have to do it here when we use wxStatusBarGeneric in addition to the
35 // standard wxStatusBar class, if wxStatusBarGeneric is the same as
36 // wxStatusBar, then the corresponding IMPLEMENT_DYNAMIC_CLASS is already in
37 // common/statbar.cpp
38 #if defined(__WXMAC__) || \
39 (defined(wxUSE_NATIVE_STATUSBAR) && wxUSE_NATIVE_STATUSBAR)
40 #include "wx/generic/statusbr.h"
41
42 IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric, wxWindow)
43 #endif // wxUSE_NATIVE_STATUSBAR
44
45 BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow)
46 EVT_PAINT(wxStatusBarGeneric::OnPaint)
47 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged)
48 END_EVENT_TABLE()
49
50 // Default status border dimensions
51 #define wxTHICK_LINE_BORDER 2
52 #define wxTHICK_LINE_WIDTH 1
53
54 wxStatusBarGeneric::wxStatusBarGeneric()
55 {
56 m_statusStrings = (wxString *) NULL;
57 m_borderX = wxTHICK_LINE_BORDER;
58 m_borderY = wxTHICK_LINE_BORDER;
59 }
60
61 wxStatusBarGeneric::~wxStatusBarGeneric()
62 {
63 # ifdef __WXMSW__
64 SetFont(wxNullFont);
65 # endif // MSW
66
67 if ( m_statusStrings )
68 delete[] m_statusStrings;
69 }
70
71 bool wxStatusBarGeneric::Create(wxWindow *parent,
72 wxWindowID id,
73 long style,
74 const wxString& name)
75 {
76 m_statusStrings = (wxString *) NULL;
77 m_borderX = wxTHICK_LINE_BORDER;
78 m_borderY = wxTHICK_LINE_BORDER;
79
80 bool success = wxWindow::Create(parent, id,
81 wxDefaultPosition, wxDefaultSize,
82 style | wxTAB_TRAVERSAL, name);
83
84 // The status bar should have a themed background
85 SetThemeEnabled( TRUE );
86
87 // Don't wish this to be found as a child
88 #ifndef __WXMAC__
89 parent->GetChildren().DeleteObject(this);
90 #endif
91 InitColours();
92
93 SetFont(m_defaultStatusBarFont);
94
95 // Set the height according to the font and the border size
96 wxClientDC dc(this);
97 dc.SetFont(GetFont());
98
99 wxCoord y;
100 dc.GetTextExtent(_T("X"), NULL, &y );
101
102 int height = (int)( (11*y)/10 + 2*GetBorderY());
103
104 SetSize(-1, -1, -1, height);
105
106 return success;
107 }
108
109 void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths)
110 {
111 if ( number != m_nFields )
112 {
113 m_nFields = number;
114
115 delete[] m_statusStrings;
116 m_statusStrings = new wxString[number];
117 }
118
119 SetStatusWidths(number, widths);
120 }
121
122 void wxStatusBarGeneric::SetStatusText(const wxString& text, int number)
123 {
124 wxCHECK_RET( (number >= 0) && (number < m_nFields),
125 _T("invalid status bar field index") );
126
127 m_statusStrings[number] = text;
128
129 wxRect rect;
130 GetFieldRect(number, rect);
131
132 Refresh( TRUE, &rect );
133 }
134
135 wxString wxStatusBarGeneric::GetStatusText(int n) const
136 {
137 wxCHECK_MSG( (n >= 0) && (n < m_nFields), wxEmptyString,
138 _T("invalid status bar field index") );
139
140 return m_statusStrings[n];
141 }
142
143 void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[])
144 {
145 // only set status widths, when n == number of statuswindows
146 wxCHECK_RET( n == m_nFields, _T("status bar field count mismatch") );
147
148 // delete the old widths in any case - this function may be used to reset
149 // the widths to the default (all equal)
150 // MBN: this is incompatible with at least wxMSW and wxMAC and not
151 // documented, but let's keep it for now
152 ReinitWidths();
153
154 if ( !widths_field )
155 {
156 // not an error, see the comment above
157 Refresh();
158 return;
159 }
160
161 wxStatusBarBase::SetStatusWidths(n, widths_field);
162 }
163
164 void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
165 {
166 wxPaintDC dc(this);
167
168
169 int i;
170 if ( GetFont().Ok() )
171 dc.SetFont(GetFont());
172 dc.SetBackgroundMode(wxTRANSPARENT);
173
174 #ifdef __WXPM__
175 wxColour vColor;
176
177 vColor.InitFromName("LIGHT GREY");
178 ::WinFillRect(dc.m_hPS, &dc.m_vRclPaint, vColor.GetPixel());
179 #endif
180
181 for ( i = 0; i < m_nFields; i ++ )
182 DrawField(dc, i);
183
184 #ifdef __WXMSW__
185 dc.SetFont(wxNullFont);
186 #endif // MSW
187 }
188
189 void wxStatusBarGeneric::DrawFieldText(wxDC& dc, int i)
190 {
191 int leftMargin = 2;
192
193 wxRect rect;
194 GetFieldRect(i, rect);
195
196 wxString text(GetStatusText(i));
197
198 long x, y;
199
200 dc.GetTextExtent(text, &x, &y);
201
202 int xpos = rect.x + leftMargin;
203 int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ;
204
205 #if defined( __WXGTK__ ) || defined(__WXMAC__)
206 xpos++;
207 ypos++;
208 #endif
209
210 dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height);
211
212 dc.DrawText(text, xpos, ypos);
213
214 dc.DestroyClippingRegion();
215 }
216
217 void wxStatusBarGeneric::DrawField(wxDC& dc, int i)
218 {
219 wxRect rect;
220 GetFieldRect(i, rect);
221
222 // Draw border
223 // Have grey background, plus 3-d border -
224 // One black rectangle.
225 // Inside this, left and top sides - dark grey. Bottom and right -
226 // white.
227
228 dc.SetPen(m_hilightPen);
229
230 #ifndef __WXPM__
231
232 // Right and bottom white lines
233 dc.DrawLine(rect.x + rect.width, rect.y,
234 rect.x + rect.width, rect.y + rect.height);
235 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
236 rect.x, rect.y + rect.height);
237
238 dc.SetPen(m_mediumShadowPen);
239
240 // Left and top grey lines
241 dc.DrawLine(rect.x, rect.y + rect.height,
242 rect.x, rect.y);
243 dc.DrawLine(rect.x, rect.y,
244 rect.x + rect.width, rect.y);
245 #else
246 // Right
247 dc.DrawLine(rect.x + rect.width, rect.y,
248 rect.x + rect.width, rect.y + rect.height + 2);
249 dc.SetPen(m_mediumShadowPen);
250 dc.DrawLine(rect.x + rect.width + 1, rect.y,
251 rect.x + rect.width + 1, rect.y + rect.height + 2);
252 dc.DrawLine(rect.x + rect.width + 2, rect.y,
253 rect.x + rect.width + 2, rect.y + rect.height + 2);
254 // Top
255 dc.DrawLine(rect.x + rect.width + 2, rect.y,
256 rect.x - 2, rect.y);
257 dc.DrawLine(rect.x + rect.width + 1, rect.y - 1,
258 rect.x - 2, rect.y - 1);
259 dc.SetPen(m_hilightPen);
260 dc.DrawLine(rect.x + rect.width, rect.y - 2,
261 rect.x - 2, rect.y - 2);
262
263 #endif
264
265 DrawFieldText(dc, i);
266 }
267
268 // Get the position and size of the field's internal bounding rectangle
269 bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
270 {
271 wxCHECK_MSG( (n >= 0) && (n < m_nFields), FALSE,
272 _T("invalid status bar field index") );
273
274 int width, height;
275 #ifdef __WXPM__
276 GetSize(&width, &height);
277 #else
278 GetClientSize(&width, &height);
279 #endif
280
281 int i;
282 int sum_of_nonvar = 0;
283 int num_of_var = 0;
284 bool do_same_width = FALSE;
285
286 int fieldWidth = 0;
287 int fieldPosition = 0;
288
289 if (m_statusWidths)
290 {
291 // if sum(not variable Windows) > c_width - (20 points per variable_window)
292 // then do_same_width = TRUE;
293 for (i = 0; i < m_nFields; i++)
294 {
295 if (m_statusWidths[i] > 0) sum_of_nonvar += m_statusWidths[i];
296 else num_of_var++;
297 }
298 if (sum_of_nonvar > (width - 20*num_of_var)) do_same_width = TRUE;
299 }
300 else do_same_width = TRUE;
301 if (do_same_width)
302 {
303 for (i = 0; i < m_nFields; i++)
304 {
305 fieldWidth = (int)(width/m_nFields);
306 fieldPosition = i*fieldWidth;
307 if ( i == n )
308 break;
309 }
310 }
311 else // no_same_width
312 {
313 int *tempwidth = new int[m_nFields];
314 int temppos = 0;
315 for (i = 0; i < m_nFields; i++)
316 {
317 if (m_statusWidths[i] > 0) tempwidth[i] = m_statusWidths[i];
318 else tempwidth[i] = (width - sum_of_nonvar) / num_of_var;
319 }
320 for (i = 0; i < m_nFields; i++)
321 {
322 fieldWidth = tempwidth[i];
323 fieldPosition = temppos;
324
325 temppos += tempwidth[i];
326
327 if ( i == n )
328 break;
329 }
330 delete [] tempwidth;
331 }
332
333 rect.x = fieldPosition + wxTHICK_LINE_BORDER;
334 rect.y = wxTHICK_LINE_BORDER;
335
336 rect.width = fieldWidth - 2 * wxTHICK_LINE_BORDER ;
337 rect.height = height - 2 * wxTHICK_LINE_BORDER ;
338
339 return TRUE;
340 }
341
342 // Initialize colours
343 void wxStatusBarGeneric::InitColours()
344 {
345 // Shadow colours
346 #if defined(__WIN95__)
347 wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
348 m_mediumShadowPen = wxPen(mediumShadowColour, 1, wxSOLID);
349
350 wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
351 m_hilightPen = wxPen(hilightColour, 1, wxSOLID);
352 #elif defined(__WXPM__)
353 m_mediumShadowPen = wxPen("DARK GREY", 1, wxSOLID);
354 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
355
356 wxColour vColour;
357
358 vColour.Set(wxString("LIGHT GREY"));
359 SetBackgroundColour(vColour);
360 vColour.Set(wxString("BLACK"));
361 SetForegroundColour(vColour);
362 m_defaultStatusBarFont = *wxSMALL_FONT;
363 #else
364 m_mediumShadowPen = wxPen("GREY", 1, wxSOLID);
365 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
366 #endif
367
368 #ifndef __WXPM__
369 m_defaultStatusBarFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
370 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
371 #endif
372 }
373
374 // Responds to colour changes, and passes event on to children.
375 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event)
376 {
377 InitColours();
378 Refresh();
379
380 // Propagate the event to the non-top-level children
381 wxWindow::OnSysColourChanged(event);
382 }
383
384 void wxStatusBarGeneric::SetMinHeight(int height)
385 {
386 // check that this min height is not less than minimal height for the
387 // current font
388 wxClientDC dc(this);
389 wxCoord y;
390 dc.GetTextExtent( _T("X"), NULL, &y );
391
392 if ( height > (11*y)/10 )
393 {
394 SetSize(-1, -1, -1, height + 2*m_borderY);
395 }
396 }
397
398 #endif // wxUSE_STATUSBAR