1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/statusbr.cpp
3 // Purpose: wxStatusBarGeneric class implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/statusbr.h"
24 #include "wx/settings.h"
25 #include "wx/dcclient.h"
26 #include "wx/toplevel.h"
33 // we only have to do it here when we use wxStatusBarGeneric in addition to the
34 // standard wxStatusBar class, if wxStatusBarGeneric is the same as
35 // wxStatusBar, then the corresponding IMPLEMENT_DYNAMIC_CLASS is already in
37 #if defined(__WXMAC__) || \
38 (defined(wxUSE_NATIVE_STATUSBAR) && wxUSE_NATIVE_STATUSBAR)
39 #include "wx/generic/statusbr.h"
41 IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric
, wxWindow
)
42 #endif // wxUSE_NATIVE_STATUSBAR
44 // Default status border dimensions
45 #define wxTHICK_LINE_BORDER 2
47 // Margin between the field text and the field rect
48 #define wxFIELD_TEXT_MARGIN 2
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 BEGIN_EVENT_TABLE(wxStatusBarGeneric
, wxWindow
)
56 EVT_PAINT(wxStatusBarGeneric
::OnPaint
)
57 EVT_LEFT_DOWN(wxStatusBarGeneric
::OnLeftDown
)
58 EVT_RIGHT_DOWN(wxStatusBarGeneric
::OnRightDown
)
59 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric
::OnSysColourChanged
)
62 void wxStatusBarGeneric
::Init()
64 m_borderX
= wxTHICK_LINE_BORDER
;
65 m_borderY
= wxTHICK_LINE_BORDER
;
68 wxStatusBarGeneric
::~wxStatusBarGeneric()
72 bool wxStatusBarGeneric
::Create(wxWindow
*parent
,
77 style
|= wxTAB_TRAVERSAL
| wxFULL_REPAINT_ON_RESIZE
;
78 if ( !wxWindow
::Create(parent
, id
,
79 wxDefaultPosition
, wxDefaultSize
,
83 // The status bar should have a themed background
84 SetThemeEnabled( true );
89 SetFont(*wxSMALL_FONT
);
94 // Set the height according to the font and the border size
96 dc
.SetFont(GetFont());
98 dc
.GetTextExtent(_T("X"), NULL
, &y
);
100 int height
= (int)( (11*y
)/10 + 2*GetBorderY());
102 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
);
109 wxSize wxStatusBarGeneric
::DoGetBestSize() const
114 // best width is the width of the parent
115 GetParent()->GetClientSize(&width
, NULL
);
117 // best height is as calculated above in Create()
118 wxClientDC
dc((wxWindow
*)this);
119 dc
.SetFont(GetFont());
120 dc
.GetTextExtent(_T("X"), NULL
, &y
);
121 height
= (int)((11*y
)/10 + 2*GetBorderY());
123 return wxSize(width
, height
);
126 void wxStatusBarGeneric
::SetFieldsCount(int number
, const int *widths
)
128 wxASSERT_MSG( number
>= 0, _T("negative number of fields in wxStatusBar?") );
130 // enlarge the m_statusStrings array if needed:
131 for (size_t i
= m_panes
.GetCount(); i
< (size_t)number
; ++i
)
132 m_statusStrings
.Add( wxEmptyString
);
134 // shrink the m_statusStrings array if needed:
135 for (int j
= (int)m_panes
.GetCount() - 1; j
>= number
; --j
)
136 m_statusStrings
.RemoveAt(j
);
138 // forget the old cached pixel widths
141 wxStatusBarBase
::SetFieldsCount(number
, widths
);
143 wxASSERT_MSG( m_panes
.GetCount() == m_statusStrings
.GetCount(),
144 _T("This really should never happen, can we do away with m_panes.GetCount() here?") );
147 void wxStatusBarGeneric
::SetStatusText(const wxString
& text
, int number
)
149 wxCHECK_RET( (number
>= 0) && ((size_t)number
< m_panes
.GetCount()),
150 _T("invalid status bar field index") );
152 wxString oldText
= m_statusStrings
[number
];
155 m_statusStrings
[number
] = text
;
158 GetFieldRect(number
, rect
);
160 Refresh(true, &rect
);
162 // it's common to show some text in the status bar before starting a
163 // relatively lengthy operation, ensure that the text is shown to the
164 // user immediately and not after the lengthy operation end
169 wxString wxStatusBarGeneric
::GetStatusText(int n
) const
171 wxCHECK_MSG( (n
>= 0) && ((size_t)n
< m_panes
.GetCount()), wxEmptyString
,
172 _T("invalid status bar field index") );
174 return m_statusStrings
[n
];
177 void wxStatusBarGeneric
::SetStatusWidths(int n
, const int widths_field
[])
179 // only set status widths when n == number of statuswindows
180 wxCHECK_RET( (size_t)n
== m_panes
.GetCount(), _T("status bar field count mismatch") );
182 // forget the old cached pixel widths
185 wxStatusBarBase
::SetStatusWidths(n
, widths_field
);
188 bool wxStatusBarGeneric
::ShowsSizeGrip() const
190 if ( !HasFlag(wxST_SIZEGRIP
) )
193 wxTopLevelWindow
* const
194 tlw
= wxDynamicCast(wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
195 return tlw
&& !tlw
->IsMaximized() && tlw
->HasFlag(wxRESIZE_BORDER
);
198 void wxStatusBarGeneric
::DrawFieldText(wxDC
& dc
, int i
)
201 GetFieldRect(i
, rect
);
203 if (rect
.GetWidth() <= 0)
204 return; // happens when the status bar is shrinked in a very small area!
208 // don't write text over the size grip:
209 // NOTE: overloading DoGetClientSize() and GetClientAreaOrigin() wouldn't
210 // work because the adjustment needs to be done only when drawing
211 // the field text and not also when drawing the background, the
212 // size grip itself, etc
213 if (GetLayoutDirection() == wxLayout_RightToLeft
&& i
== 0)
215 const wxRect
& rc
= GetSizeGripRect();
217 rect
.x
+= rc
.GetRight();
218 rect
.width
-= rc
.GetRight();
220 else if (GetLayoutDirection() != wxLayout_RightToLeft
&& i
== m_panes
.GetCount()-1)
222 const wxRect
& rc
= GetSizeGripRect();
224 rect
.width
= rc
.x
- rect
.x
;
228 // eventually ellipsize the text so that it fits the field width
229 wxString
text(GetStatusText(i
));
231 text
= wxControl
::Ellipsize(
233 GetLayoutDirection() == wxLayout_RightToLeft ? wxELLIPSIZE_START
: wxELLIPSIZE_END
,
234 rect
.GetWidth() - 2*wxFIELD_TEXT_MARGIN
,
235 wxELLIPSIZE_EXPAND_TAB
);
236 // Ellipsize() will do something only if necessary
238 // center the text in its field
239 wxSize sz
= dc
.GetTextExtent(text
);
240 int xpos
= rect
.x
+ wxFIELD_TEXT_MARGIN
;
241 int ypos
= (int) (((rect
.height
- sz
.GetHeight()) / 2) + rect
.y
+ 0.5);
243 #if defined( __WXGTK__ ) || defined(__WXMAC__)
249 dc
.DrawText(text
, xpos
, ypos
);
252 void wxStatusBarGeneric
::DrawField(wxDC
& dc
, int i
)
255 GetFieldRect(i
, rect
);
257 int style
= m_panes
[i
].nStyle
;
258 if (style
!= wxSB_FLAT
)
262 // Have grey background, plus 3-d border -
263 // One black rectangle.
264 // Inside this, left and top sides - dark grey. Bottom and right -
266 // Reverse it for wxSB_RAISED
268 dc
.SetPen((style
== wxSB_RAISED
) ? m_mediumShadowPen
: m_hilightPen
);
272 // Right and bottom lines
273 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
274 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
275 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
276 rect
.x
, rect
.y
+ rect
.height
);
278 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
280 // Left and top lines
281 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
283 dc
.DrawLine(rect
.x
, rect
.y
,
284 rect
.x
+ rect
.width
, rect
.y
);
287 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.height
+ 2,
288 rect
.x
, rect
.height
+ 2);
289 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
290 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
292 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
293 dc
.DrawLine(rect
.x
, rect
.y
,
294 rect
.x
+ rect
.width
, rect
.y
);
295 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
301 DrawFieldText(dc
, i
);
304 // Get the position and size of the field's internal bounding rectangle
305 bool wxStatusBarGeneric
::GetFieldRect(int n
, wxRect
& rect
) const
307 wxCHECK_MSG( (n
>= 0) && ((size_t)n
< m_panes
.GetCount()), false,
308 _T("invalid status bar field index") );
310 // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
313 GetSize(&width
, &height
);
315 GetClientSize(&width
, &height
);
318 // we cache m_widthsAbs between calls and recompute it if client
319 // width has changed (or when it is initially empty)
320 if ( m_widthsAbs
.IsEmpty() || m_lastClientWidth
!= width
)
322 // FIXME: why don't we use an OnSize(wxSizeEvent&) event handler to
323 // update the cache? (FM)
325 wxConstCast(this, wxStatusBarGeneric
)->m_widthsAbs
= CalculateAbsWidths(width
);
327 // remember last width for which we have recomputed the widths in pixels
328 wxConstCast(this, wxStatusBarGeneric
)->m_lastClientWidth
= width
;
332 for ( int i
= 0; i
< n
; i
++ )
333 rect
.x
+= m_widthsAbs
[i
];
338 rect
.width
= m_widthsAbs
[n
] - 2*m_borderX
;
339 rect
.height
= height
- 2*m_borderY
;
344 // Initialize colours
345 void wxStatusBarGeneric
::InitColours()
347 #if defined(__WXPM__)
348 m_mediumShadowPen
= wxPen(wxColour(127, 127, 127), 1, wxSOLID
);
349 m_hilightPen
= *wxWHITE_PEN
;
351 SetBackgroundColour(*wxLIGHT_GREY
);
352 SetForegroundColour(*wxBLACK
);
354 m_mediumShadowPen
= wxPen(wxSystemSettings
::GetColour(wxSYS_COLOUR_3DSHADOW
));
355 m_hilightPen
= wxPen(wxSystemSettings
::GetColour(wxSYS_COLOUR_3DHILIGHT
));
356 #endif // __WXPM__/!__WXPM__
359 void wxStatusBarGeneric
::SetMinHeight(int height
)
361 // check that this min height is not less than minimal height for the
365 dc
.GetTextExtent( wxT("X"), NULL
, &y
);
367 if ( height
> (11*y
)/10 )
369 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
+ 2*m_borderY
);
373 wxRect wxStatusBarGeneric
::GetSizeGripRect() const
376 wxWindow
::DoGetClientSize(&width
, &height
);
378 if (GetLayoutDirection() == wxLayout_RightToLeft
)
379 return wxRect(2, 2, height
-2, height
-4);
381 return wxRect(width
-height
-2, 2, height
-2, height
-4);
384 // ----------------------------------------------------------------------------
385 // wxStatusBarGeneric - event handlers
386 // ----------------------------------------------------------------------------
388 void wxStatusBarGeneric
::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
394 if ( ShowsSizeGrip() )
396 const wxRect
& rc
= GetSizeGripRect();
398 GetLayoutDirection() == wxLayout_RightToLeft ? GDK_WINDOW_EDGE_SOUTH_WEST
:
399 GDK_WINDOW_EDGE_SOUTH_EAST
;
400 gtk_paint_resize_grip( m_widget
->style
,
401 GTKGetDrawingWindow(),
402 (GtkStateType
) GTK_WIDGET_STATE (m_widget
),
407 rc
.x
, rc
.y
, rc
.width
, rc
.height
);
409 #endif // __WXGTK20__
411 if (GetFont().IsOk())
412 dc
.SetFont(GetFont());
414 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
416 for (size_t i
= 0; i
< m_panes
.GetCount(); i
++)
420 // Responds to colour changes, and passes event on to children.
421 void wxStatusBarGeneric
::OnSysColourChanged(wxSysColourChangedEvent
& event
)
425 // Propagate the event to the non-top-level children
426 wxWindow
::OnSysColourChanged(event
);
429 void wxStatusBarGeneric
::OnLeftDown(wxMouseEvent
& event
)
433 GetClientSize(&width
, &height
);
435 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
437 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
439 if (!GTK_IS_WINDOW (ancestor
))
442 GdkWindow
*source
= GTKGetDrawingWindow();
446 gdk_window_get_origin( source
, &org_x
, &org_y
);
448 if (GetLayoutDirection() == wxLayout_RightToLeft
)
450 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
451 GDK_WINDOW_EDGE_SOUTH_WEST
,
453 org_x
- event
.GetX() + GetSize().x
,
454 org_y
+ event
.GetY(),
459 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
460 GDK_WINDOW_EDGE_SOUTH_EAST
,
462 org_x
+ event
.GetX(),
463 org_y
+ event
.GetY(),
476 void wxStatusBarGeneric
::OnRightDown(wxMouseEvent
& event
)
480 GetClientSize(&width
, &height
);
482 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
484 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
486 if (!GTK_IS_WINDOW (ancestor
))
489 GdkWindow
*source
= GTKGetDrawingWindow();
493 gdk_window_get_origin( source
, &org_x
, &org_y
);
495 gtk_window_begin_move_drag (GTK_WINDOW (ancestor
),
497 org_x
+ event
.GetX(),
498 org_y
+ event
.GetY(),
510 #endif // wxUSE_STATUSBAR