]> git.saurik.com Git - wxWidgets.git/blame - src/generic/statusbr.cpp
Guard against comparing invalid iterators, which produces an assertion
[wxWidgets.git] / src / generic / statusbr.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
a71d815b 2// Name: src/generic/statusbr.cpp
ed791986 3// Purpose: wxStatusBarGeneric class implementation
c801d85f
KB
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
c801d85f
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
76b49cf4 16 #pragma hdrstop
c801d85f
KB
17#endif
18
1e6feb95 19#if wxUSE_STATUSBAR
ed791986 20
c801d85f 21#ifndef WX_PRECOMP
76b49cf4
WS
22 #include "wx/frame.h"
23 #include "wx/settings.h"
24 #include "wx/dcclient.h"
c801d85f
KB
25#endif
26
2b5f62a0
VZ
27#ifdef __WXGTK20__
28#include "wx/gtk/private.h"
29#include "wx/gtk/win_gtk.h"
30#endif
31
412e4edf 32#include "wx/statusbr.h"
c801d85f 33
84f68c21
VZ
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)
b64a8473 40 #include "wx/generic/statusbr.h"
c801d85f 41
b64a8473
VZ
42 IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric, wxWindow)
43#endif // wxUSE_NATIVE_STATUSBAR
ed791986 44
ed791986
VZ
45BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow)
46 EVT_PAINT(wxStatusBarGeneric::OnPaint)
2b5f62a0
VZ
47 EVT_LEFT_DOWN(wxStatusBarGeneric::OnLeftDown)
48 EVT_RIGHT_DOWN(wxStatusBarGeneric::OnRightDown)
ed791986 49 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged)
c801d85f 50END_EVENT_TABLE()
c801d85f
KB
51
52// Default status border dimensions
53#define wxTHICK_LINE_BORDER 2
c801d85f 54
390015c0 55void wxStatusBarGeneric::Init()
c801d85f 56{
48f7ffbe
WS
57 m_borderX = wxTHICK_LINE_BORDER;
58 m_borderY = wxTHICK_LINE_BORDER;
c801d85f
KB
59}
60
ed791986 61wxStatusBarGeneric::~wxStatusBarGeneric()
c801d85f 62{
c801d85f
KB
63}
64
ed791986 65bool wxStatusBarGeneric::Create(wxWindow *parent,
76880b87
RL
66 wxWindowID id,
67 long style,
68 const wxString& name)
c801d85f 69{
48f7ffbe
WS
70 if ( !wxWindow::Create(parent, id,
71 wxDefaultPosition, wxDefaultSize,
72 style | wxTAB_TRAVERSAL, name) )
73 return false;
c801d85f 74
48f7ffbe
WS
75 // The status bar should have a themed background
76 SetThemeEnabled( true );
77
78 InitColours();
f90566f5 79
1ca21594 80#ifdef __WXPM__
48f7ffbe 81 SetFont(*wxSMALL_FONT);
1ca21594 82#endif
c801d85f 83
48f7ffbe
WS
84 // Set the height according to the font and the border size
85 wxClientDC dc(this);
86 dc.SetFont(GetFont());
71e03035 87
48f7ffbe
WS
88 wxCoord y;
89 dc.GetTextExtent(_T("X"), NULL, &y );
71e03035 90
48f7ffbe 91 int height = (int)( (11*y)/10 + 2*GetBorderY());
71e03035 92
48f7ffbe 93 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height);
71e03035 94
48f7ffbe 95 SetFieldsCount(1);
8aa2cea1 96
48f7ffbe 97 return true;
c801d85f
KB
98}
99
595a9493
RD
100
101wxSize wxStatusBarGeneric::DoGetBestSize() const
102{
103 int width, height;
104
105 // best width is the width of the parent
106 GetParent()->GetClientSize(&width, NULL);
107
108 // best height is as calculated above in Create
109 wxClientDC dc((wxWindow*)this);
110 dc.SetFont(GetFont());
111 wxCoord y;
112 dc.GetTextExtent(_T("X"), NULL, &y );
113 height = (int)( (11*y)/10 + 2*GetBorderY());
114
115 return wxSize(width, height);
ca65c044 116}
595a9493 117
cc56206f 118void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths)
c801d85f 119{
390015c0 120 wxASSERT_MSG( number >= 0, _T("negative number of fields in wxStatusBar?") );
76880b87 121
893f25f2
JS
122 int i;
123 for(i = m_nFields; i < number; ++i)
76880b87
RL
124 m_statusStrings.Add( wxEmptyString );
125
893f25f2 126 for (i = m_nFields - 1; i >= number; --i)
390015c0 127 m_statusStrings.RemoveAt(i);
76880b87 128
f9a961e1
DS
129 // forget the old cached pixel widths
130 m_widthsAbs.Empty();
131
d64b1c76
DS
132 wxStatusBarBase::SetFieldsCount(number, widths);
133
76880b87
RL
134 wxASSERT_MSG( m_nFields == (int)m_statusStrings.GetCount(),
135 _T("This really should never happen, can we do away with m_nFields here?") );
c801d85f
KB
136}
137
ed791986 138void wxStatusBarGeneric::SetStatusText(const wxString& text, int number)
c801d85f 139{
633d67bb
VZ
140 wxCHECK_RET( (number >= 0) && (number < m_nFields),
141 _T("invalid status bar field index") );
c801d85f 142
87ff599d
JS
143 wxString oldText = m_statusStrings[number];
144 if (oldText != text)
145 {
146 m_statusStrings[number] = text;
c801d85f 147
87ff599d
JS
148 wxRect rect;
149 GetFieldRect(number, rect);
e715f4e7 150
ca65c044 151 Refresh( true, &rect );
87ff599d 152 }
c801d85f
KB
153}
154
ed791986 155wxString wxStatusBarGeneric::GetStatusText(int n) const
c801d85f 156{
ed791986
VZ
157 wxCHECK_MSG( (n >= 0) && (n < m_nFields), wxEmptyString,
158 _T("invalid status bar field index") );
159
c801d85f
KB
160 return m_statusStrings[n];
161}
162
ed791986 163void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[])
c801d85f 164{
633d67bb
VZ
165 // only set status widths, when n == number of statuswindows
166 wxCHECK_RET( n == m_nFields, _T("status bar field count mismatch") );
167
168 // delete the old widths in any case - this function may be used to reset
169 // the widths to the default (all equal)
1f361cdd
MB
170 // MBN: this is incompatible with at least wxMSW and wxMAC and not
171 // documented, but let's keep it for now
172 ReinitWidths();
633d67bb 173
390015c0
VZ
174 // forget the old cached pixel widths
175 m_widthsAbs.Empty();
176
633d67bb
VZ
177 if ( !widths_field )
178 {
179 // not an error, see the comment above
f8a4fa4c 180 Refresh();
633d67bb
VZ
181 return;
182 }
183
1f361cdd 184 wxStatusBarBase::SetStatusWidths(n, widths_field);
c801d85f
KB
185}
186
ed791986 187void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
c801d85f 188{
2b5f62a0 189 wxPaintDC dc(this);
c801d85f 190
2b5f62a0
VZ
191#ifdef __WXGTK20__
192 // Draw grip first
193 if (HasFlag( wxST_SIZEGRIP ))
194 {
195 int width, height;
196 GetClientSize(&width, &height);
8aa2cea1 197
2b5f62a0
VZ
198 gtk_paint_resize_grip( m_widget->style,
199 GTK_PIZZA(m_wxwindow)->bin_window,
200 (GtkStateType) GTK_WIDGET_STATE (m_widget),
201 NULL,
202 m_widget,
203 "statusbar",
204 GDK_WINDOW_EDGE_SOUTH_EAST,
205 width-height-2, 1, height-2, height-3 );
8aa2cea1 206
2b5f62a0
VZ
207 }
208#endif
5b3ed311 209
48f7ffbe 210 if (GetFont().Ok())
2b5f62a0 211 dc.SetFont(GetFont());
8aa2cea1 212
48f7ffbe 213 dc.SetBackgroundMode(wxTRANSPARENT);
c801d85f 214
28be2e8a 215#ifdef __WXPM__
48f7ffbe 216 wxColour vColor;
b34590eb 217
48f7ffbe
WS
218 vColor = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR);
219 ::WinFillRect(dc.m_hPS, &dc.m_vRclPaint, vColor.GetPixel());
28be2e8a
DW
220#endif
221
48f7ffbe 222 for (int i = 0; i < m_nFields; i ++)
2b5f62a0 223 DrawField(dc, i);
c801d85f
KB
224}
225
ed791986 226void wxStatusBarGeneric::DrawFieldText(wxDC& dc, int i)
c801d85f 227{
48f7ffbe 228 int leftMargin = 2;
c801d85f 229
48f7ffbe
WS
230 wxRect rect;
231 GetFieldRect(i, rect);
c801d85f 232
48f7ffbe 233 wxString text(GetStatusText(i));
c801d85f 234
8d7eaf91 235 long x = 0, y = 0;
c801d85f 236
48f7ffbe 237 dc.GetTextExtent(text, &x, &y);
c801d85f 238
48f7ffbe
WS
239 int xpos = rect.x + leftMargin;
240 int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ;
ed791986 241
7c74e7fe 242#if defined( __WXGTK__ ) || defined(__WXMAC__)
48f7ffbe
WS
243 xpos++;
244 ypos++;
92976ab6 245#endif
c801d85f 246
48f7ffbe 247 dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height);
c801d85f 248
48f7ffbe 249 dc.DrawText(text, xpos, ypos);
c801d85f 250
48f7ffbe 251 dc.DestroyClippingRegion();
c801d85f
KB
252}
253
ed791986 254void wxStatusBarGeneric::DrawField(wxDC& dc, int i)
c801d85f 255{
2b5f62a0
VZ
256 wxRect rect;
257 GetFieldRect(i, rect);
c801d85f 258
c2919ab3
VZ
259 int style = wxSB_NORMAL;
260 if (m_statusStyles)
261 style = m_statusStyles[i];
c801d85f 262
c2919ab3
VZ
263 if (style != wxSB_FLAT)
264 {
265 // Draw border
266 // For wxSB_NORMAL:
267 // Have grey background, plus 3-d border -
268 // One black rectangle.
269 // Inside this, left and top sides - dark grey. Bottom and right -
270 // white.
271 // Reverse it for wxSB_RAISED
272
273 dc.SetPen((style == wxSB_RAISED) ? m_mediumShadowPen : m_hilightPen);
274
275 #ifndef __WXPM__
276
277 // Right and bottom lines
278 dc.DrawLine(rect.x + rect.width, rect.y,
279 rect.x + rect.width, rect.y + rect.height);
280 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
281 rect.x, rect.y + rect.height);
282
283 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
284
285 // Left and top lines
286 dc.DrawLine(rect.x, rect.y + rect.height,
287 rect.x, rect.y);
288 dc.DrawLine(rect.x, rect.y,
289 rect.x + rect.width, rect.y);
290 #else
291
292 dc.DrawLine(rect.x + rect.width, rect.height + 2,
293 rect.x, rect.height + 2);
294 dc.DrawLine(rect.x + rect.width, rect.y,
295 rect.x + rect.width, rect.y + rect.height);
296
297 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
298 dc.DrawLine(rect.x, rect.y,
299 rect.x + rect.width, rect.y);
300 dc.DrawLine(rect.x, rect.y + rect.height,
301 rect.x, rect.y);
b34590eb
DW
302
303#endif
c2919ab3 304 }
c801d85f 305
f51f94ea 306 DrawFieldText(dc, i);
c801d85f
KB
307}
308
309 // Get the position and size of the field's internal bounding rectangle
ed791986 310bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
c801d85f 311{
ca65c044 312 wxCHECK_MSG( (n >= 0) && (n < m_nFields), false,
390015c0 313 _T("invalid status bar field index") );
c801d85f 314
390015c0
VZ
315 // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
316 int width, height;
28be2e8a 317#ifdef __WXPM__
390015c0 318 GetSize(&width, &height);
28be2e8a 319#else
390015c0 320 GetClientSize(&width, &height);
28be2e8a 321#endif
c801d85f 322
2b5f62a0
VZ
323 // we cache m_widthsAbs between calls and recompute it if client
324 // width has changed (or when it is initially empty)
325 if ( m_widthsAbs.IsEmpty() || (m_lastClientWidth != width) )
c801d85f 326 {
390015c0
VZ
327 wxConstCast(this, wxStatusBarGeneric)->
328 m_widthsAbs = CalculateAbsWidths(width);
2b5f62a0
VZ
329 // remember last width for which we have recomputed the widths in pixels
330 wxConstCast(this, wxStatusBarGeneric)->
331 m_lastClientWidth = width;
c801d85f 332 }
c801d85f 333
390015c0
VZ
334 rect.x = 0;
335 for ( int i = 0; i < n; i++ )
336 {
337 rect.x += m_widthsAbs[i];
c801d85f 338 }
c801d85f 339
390015c0
VZ
340 rect.x += m_borderX;
341 rect.y = m_borderY;
c801d85f 342
390015c0
VZ
343 rect.width = m_widthsAbs[n] - 2*m_borderX;
344 rect.height = height - 2*m_borderY;
c801d85f 345
ca65c044 346 return true;
c801d85f
KB
347}
348
349// Initialize colours
ed791986 350void wxStatusBarGeneric::InitColours()
c801d85f
KB
351{
352 // Shadow colours
a71d815b 353#if defined(__WXMSW__) || defined(__WXMAC__)
a756f210 354 wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
c801d85f
KB
355 m_mediumShadowPen = wxPen(mediumShadowColour, 1, wxSOLID);
356
a756f210 357 wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
c801d85f 358 m_hilightPen = wxPen(hilightColour, 1, wxSOLID);
b34590eb 359#elif defined(__WXPM__)
e715f4e7 360 m_mediumShadowPen = wxPen(wxColour(127, 127, 127), 1, wxSOLID);
b0fa2187 361 m_hilightPen = *wxWHITE_PEN;
dd7d1435 362
b0fa2187
PC
363 SetBackgroundColour(*wxLIGHT_GREY);
364 SetForegroundColour(*wxBLACK);
c801d85f 365#else
b0fa2187
PC
366 m_mediumShadowPen = *wxGREY_PEN;
367 m_hilightPen = *wxWHITE_PEN;
c801d85f 368#endif
c801d85f
KB
369}
370
371// Responds to colour changes, and passes event on to children.
ed791986 372void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event)
c801d85f
KB
373{
374 InitColours();
c801d85f
KB
375
376 // Propagate the event to the non-top-level children
377 wxWindow::OnSysColourChanged(event);
378}
379
ed791986
VZ
380void wxStatusBarGeneric::SetMinHeight(int height)
381{
382 // check that this min height is not less than minimal height for the
383 // current font
384 wxClientDC dc(this);
385 wxCoord y;
2b5f62a0 386 dc.GetTextExtent( wxT("X"), NULL, &y );
ed791986
VZ
387
388 if ( height > (11*y)/10 )
389 {
422d0ff0 390 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height + 2*m_borderY);
ed791986
VZ
391 }
392}
393
2b5f62a0 394void wxStatusBarGeneric::OnLeftDown(wxMouseEvent& event)
390015c0 395{
2b5f62a0
VZ
396#ifdef __WXGTK20__
397 int width, height;
398 GetClientSize(&width, &height);
8aa2cea1 399
2b5f62a0
VZ
400 if (HasFlag( wxST_SIZEGRIP ) && (event.GetX() > width-height))
401 {
402 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
403
404 if (!GTK_IS_WINDOW (ancestor))
405 return;
390015c0 406
2b5f62a0
VZ
407 GdkWindow *source = GTK_PIZZA(m_wxwindow)->bin_window;
408
409 int org_x = 0;
410 int org_y = 0;
411 gdk_window_get_origin( source, &org_x, &org_y );
412
413 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
414 GDK_WINDOW_EDGE_SOUTH_EAST,
415 1,
8aa2cea1
RD
416 org_x + event.GetX(),
417 org_y + event.GetY(),
2b5f62a0
VZ
418 0);
419 }
420 else
421 {
ca65c044 422 event.Skip( true );
2b5f62a0
VZ
423 }
424#else
ca65c044 425 event.Skip( true );
2b5f62a0
VZ
426#endif
427}
428
429void wxStatusBarGeneric::OnRightDown(wxMouseEvent& event)
430{
431#ifdef __WXGTK20__
432 int width, height;
433 GetClientSize(&width, &height);
8aa2cea1 434
2b5f62a0
VZ
435 if (HasFlag( wxST_SIZEGRIP ) && (event.GetX() > width-height))
436 {
437 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
438
439 if (!GTK_IS_WINDOW (ancestor))
440 return;
441
442 GdkWindow *source = GTK_PIZZA(m_wxwindow)->bin_window;
443
444 int org_x = 0;
445 int org_y = 0;
446 gdk_window_get_origin( source, &org_x, &org_y );
8aa2cea1 447
2b5f62a0
VZ
448 gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
449 2,
8aa2cea1
RD
450 org_x + event.GetX(),
451 org_y + event.GetY(),
2b5f62a0
VZ
452 0);
453 }
454 else
455 {
ca65c044 456 event.Skip( true );
2b5f62a0
VZ
457 }
458#else
ca65c044 459 event.Skip( true );
2b5f62a0 460#endif
390015c0
VZ
461}
462
1e6feb95 463#endif // wxUSE_STATUSBAR