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