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_SIZE(wxStatusBarGeneric::OnSize
)
58 EVT_LEFT_DOWN(wxStatusBarGeneric::OnLeftDown
)
59 EVT_RIGHT_DOWN(wxStatusBarGeneric::OnRightDown
)
60 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged
)
63 void wxStatusBarGeneric::Init()
65 m_borderX
= wxTHICK_LINE_BORDER
;
66 m_borderY
= wxTHICK_LINE_BORDER
;
69 wxStatusBarGeneric::~wxStatusBarGeneric()
73 bool wxStatusBarGeneric::Create(wxWindow
*parent
,
78 style
|= wxTAB_TRAVERSAL
| wxFULL_REPAINT_ON_RESIZE
;
79 if ( !wxWindow::Create(parent
, id
,
80 wxDefaultPosition
, wxDefaultSize
,
84 // The status bar should have a themed background
85 SetThemeEnabled( true );
90 SetFont(*wxSMALL_FONT
);
93 int height
= (int)((11*GetCharHeight())/10 + 2*GetBorderY());
94 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
);
101 wxSize
wxStatusBarGeneric::DoGetBestSize() const
105 // best width is the width of the parent
107 GetParent()->GetClientSize(&width
, NULL
);
109 width
= 80; // a dummy value
111 // best height is as calculated above in Create()
112 height
= (int)((11*GetCharHeight())/10 + 2*GetBorderY());
114 return wxSize(width
, height
);
117 void wxStatusBarGeneric::SetFieldsCount(int number
, const int *widths
)
119 wxASSERT_MSG( number
>= 0, _T("negative number of fields in wxStatusBar?") );
121 // enlarge the m_statusStrings array if needed:
122 for (size_t i
= m_panes
.GetCount(); i
< (size_t)number
; ++i
)
123 m_statusStrings
.Add( wxEmptyString
);
125 // shrink the m_statusStrings array if needed:
126 for (int j
= (int)m_panes
.GetCount() - 1; j
>= number
; --j
)
127 m_statusStrings
.RemoveAt(j
);
129 // forget the old cached pixel widths
132 wxStatusBarBase::SetFieldsCount(number
, widths
);
134 wxASSERT_MSG( m_panes
.GetCount() == m_statusStrings
.GetCount(),
135 _T("This really should never happen, can we do away with m_panes.GetCount() here?") );
138 void wxStatusBarGeneric::SetStatusText(const wxString
& text
, int number
)
140 wxCHECK_RET( (number
>= 0) && ((size_t)number
< m_panes
.GetCount()),
141 _T("invalid status bar field index") );
143 wxString oldText
= m_statusStrings
[number
];
146 m_statusStrings
[number
] = text
;
149 GetFieldRect(number
, rect
);
151 Refresh(true, &rect
);
153 // it's common to show some text in the status bar before starting a
154 // relatively lengthy operation, ensure that the text is shown to the
155 // user immediately and not after the lengthy operation end
160 wxString
wxStatusBarGeneric::GetStatusText(int n
) const
162 wxCHECK_MSG( (n
>= 0) && ((size_t)n
< m_panes
.GetCount()), wxEmptyString
,
163 _T("invalid status bar field index") );
165 return m_statusStrings
[n
];
168 void wxStatusBarGeneric::SetStatusWidths(int n
, const int widths_field
[])
170 // only set status widths when n == number of statuswindows
171 wxCHECK_RET( (size_t)n
== m_panes
.GetCount(), _T("status bar field count mismatch") );
173 // forget the old cached pixel widths
176 wxStatusBarBase::SetStatusWidths(n
, widths_field
);
179 bool wxStatusBarGeneric::ShowsSizeGrip() const
181 if ( !HasFlag(wxST_SIZEGRIP
) )
184 wxTopLevelWindow
* const
185 tlw
= wxDynamicCast(wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
186 return tlw
&& !tlw
->IsMaximized() && tlw
->HasFlag(wxRESIZE_BORDER
);
189 void wxStatusBarGeneric::DrawFieldText(wxDC
& dc
, const wxRect
& rect
, int i
, int textHeight
)
191 wxString
text(GetStatusText(i
));
193 return; // optimization
195 int xpos
= rect
.x
+ wxFIELD_TEXT_MARGIN
,
196 maxWidth
= rect
.width
- 2*wxFIELD_TEXT_MARGIN
,
197 ypos
= (int) (((rect
.height
- textHeight
) / 2) + rect
.y
+ 0.5);
201 // don't write text over the size grip:
202 // NOTE: overloading DoGetClientSize() and GetClientAreaOrigin() wouldn't
203 // work because the adjustment needs to be done only when drawing
204 // the field text and not also when drawing the background, the
205 // size grip itself, etc
206 if ((GetLayoutDirection() == wxLayout_RightToLeft
&& i
== 0) ||
207 (GetLayoutDirection() != wxLayout_RightToLeft
&&
208 i
== (int)m_panes
.GetCount()-1))
210 const wxRect
& gripRc
= GetSizeGripRect();
212 // NOTE: we don't need any special treatment wrt to the layout direction
213 // since DrawText() will automatically adjust the origin of the
214 // text accordingly to the layout in use
216 maxWidth
-= gripRc
.width
;
220 // eventually ellipsize the text so that it fits the field width
221 text
= wxControl::Ellipsize(
223 GetLayoutDirection() == wxLayout_RightToLeft
? wxELLIPSIZE_START
: wxELLIPSIZE_END
,
225 wxELLIPSIZE_EXPAND_TAB
);
226 // Ellipsize() will do something only if necessary
228 #if defined( __WXGTK__ ) || defined(__WXMAC__)
234 dc
.DrawText(text
, xpos
, ypos
);
237 void wxStatusBarGeneric::DrawField(wxDC
& dc
, int i
, int textHeight
)
240 GetFieldRect(i
, rect
);
242 if (rect
.GetWidth() <= 0)
243 return; // happens when the status bar is shrinked in a very small area!
245 int style
= m_panes
[i
].nStyle
;
246 if (style
!= wxSB_FLAT
)
250 // Have grey background, plus 3-d border -
251 // One black rectangle.
252 // Inside this, left and top sides - dark grey. Bottom and right -
254 // Reverse it for wxSB_RAISED
256 dc
.SetPen((style
== wxSB_RAISED
) ? m_mediumShadowPen
: m_hilightPen
);
260 // Right and bottom lines
261 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
262 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
263 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
264 rect
.x
, rect
.y
+ rect
.height
);
266 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
268 // Left and top lines
269 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
271 dc
.DrawLine(rect
.x
, rect
.y
,
272 rect
.x
+ rect
.width
, rect
.y
);
275 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.height
+ 2,
276 rect
.x
, rect
.height
+ 2);
277 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
278 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
280 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
281 dc
.DrawLine(rect
.x
, rect
.y
,
282 rect
.x
+ rect
.width
, rect
.y
);
283 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
288 DrawFieldText(dc
, rect
, i
, textHeight
);
291 // Get the position and size of the field's internal bounding rectangle
292 bool wxStatusBarGeneric::GetFieldRect(int n
, wxRect
& rect
) const
294 wxCHECK_MSG( (n
>= 0) && ((size_t)n
< m_panes
.GetCount()), false,
295 _T("invalid status bar field index") );
297 if (m_widthsAbs
.IsEmpty())
301 for ( int i
= 0; i
< n
; i
++ )
302 rect
.x
+= m_widthsAbs
[i
];
306 rect
.width
= m_widthsAbs
[n
] - 2*m_borderX
;
307 rect
.height
= m_lastClientHeight
- 2*m_borderY
;
312 // Initialize colours
313 void wxStatusBarGeneric::InitColours()
315 #if defined(__WXPM__)
316 m_mediumShadowPen
= wxPen(wxColour(127, 127, 127), 1, wxSOLID
);
317 m_hilightPen
= *wxWHITE_PEN
;
319 SetBackgroundColour(*wxLIGHT_GREY
);
320 SetForegroundColour(*wxBLACK
);
322 m_mediumShadowPen
= wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
));
323 m_hilightPen
= wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT
));
324 #endif // __WXPM__/!__WXPM__
327 void wxStatusBarGeneric::SetMinHeight(int height
)
329 // check that this min height is not less than minimal height for the
330 // current font (min height is as calculated above in Create() except for border)
331 int minHeight
= (int)((11*GetCharHeight())/10);
333 if ( height
> minHeight
)
334 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
+ 2*m_borderY
);
337 wxRect
wxStatusBarGeneric::GetSizeGripRect() const
340 wxWindow::DoGetClientSize(&width
, &height
);
342 if (GetLayoutDirection() == wxLayout_RightToLeft
)
343 return wxRect(2, 2, height
-2, height
-4);
345 return wxRect(width
-height
-2, 2, height
-2, height
-4);
348 // ----------------------------------------------------------------------------
349 // wxStatusBarGeneric - event handlers
350 // ----------------------------------------------------------------------------
352 void wxStatusBarGeneric::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
358 if ( ShowsSizeGrip() )
360 const wxRect
& rc
= GetSizeGripRect();
362 GetLayoutDirection() == wxLayout_RightToLeft
? GDK_WINDOW_EDGE_SOUTH_WEST
:
363 GDK_WINDOW_EDGE_SOUTH_EAST
;
364 gtk_paint_resize_grip( m_widget
->style
,
365 GTKGetDrawingWindow(),
366 (GtkStateType
) GTK_WIDGET_STATE (m_widget
),
371 rc
.x
, rc
.y
, rc
.width
, rc
.height
);
373 #endif // __WXGTK20__
375 if (GetFont().IsOk())
376 dc
.SetFont(GetFont());
378 // compute char height only once for all panes:
379 int textHeight
= dc
.GetCharHeight();
381 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
382 for (size_t i
= 0; i
< m_panes
.GetCount(); i
++)
383 DrawField(dc
, i
, textHeight
);
386 // Responds to colour changes, and passes event on to children.
387 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent
& event
)
391 // Propagate the event to the non-top-level children
392 wxWindow::OnSysColourChanged(event
);
395 void wxStatusBarGeneric::OnLeftDown(wxMouseEvent
& event
)
399 GetClientSize(&width
, &height
);
401 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
403 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
405 if (!GTK_IS_WINDOW (ancestor
))
408 GdkWindow
*source
= GTKGetDrawingWindow();
412 gdk_window_get_origin( source
, &org_x
, &org_y
);
414 if (GetLayoutDirection() == wxLayout_RightToLeft
)
416 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
417 GDK_WINDOW_EDGE_SOUTH_WEST
,
419 org_x
- event
.GetX() + GetSize().x
,
420 org_y
+ event
.GetY(),
425 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
426 GDK_WINDOW_EDGE_SOUTH_EAST
,
428 org_x
+ event
.GetX(),
429 org_y
+ event
.GetY(),
442 void wxStatusBarGeneric::OnRightDown(wxMouseEvent
& event
)
446 GetClientSize(&width
, &height
);
448 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
450 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
452 if (!GTK_IS_WINDOW (ancestor
))
455 GdkWindow
*source
= GTKGetDrawingWindow();
459 gdk_window_get_origin( source
, &org_x
, &org_y
);
461 gtk_window_begin_move_drag (GTK_WINDOW (ancestor
),
463 org_x
+ event
.GetX(),
464 org_y
+ event
.GetY(),
476 void wxStatusBarGeneric::OnSize(wxSizeEvent
& WXUNUSED(event
))
478 // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
481 GetSize(&width
, &m_lastClientHeight
);
483 GetClientSize(&width
, &m_lastClientHeight
);
486 // recompute the cache of the field widths if the status bar width has changed
487 m_widthsAbs
= CalculateAbsWidths(width
);
490 #endif // wxUSE_STATUSBAR