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"
27 #include "wx/control.h"
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
38 #if defined(__WXMAC__) || \
39 (defined(wxUSE_NATIVE_STATUSBAR) && wxUSE_NATIVE_STATUSBAR)
40 #include "wx/generic/statusbr.h"
42 IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric
, wxWindow
)
43 #endif // wxUSE_NATIVE_STATUSBAR
45 // Default status border dimensions
46 #define wxTHICK_LINE_BORDER 2
48 // Margin between the field text and the field rect
49 #define wxFIELD_TEXT_MARGIN 2
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 BEGIN_EVENT_TABLE(wxStatusBarGeneric
, wxWindow
)
57 EVT_PAINT(wxStatusBarGeneric
::OnPaint
)
58 EVT_SIZE(wxStatusBarGeneric
::OnSize
)
59 EVT_LEFT_DOWN(wxStatusBarGeneric
::OnLeftDown
)
60 EVT_RIGHT_DOWN(wxStatusBarGeneric
::OnRightDown
)
61 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric
::OnSysColourChanged
)
64 void wxStatusBarGeneric
::Init()
66 m_borderX
= wxTHICK_LINE_BORDER
;
67 m_borderY
= wxTHICK_LINE_BORDER
;
70 wxStatusBarGeneric
::~wxStatusBarGeneric()
74 bool wxStatusBarGeneric
::Create(wxWindow
*parent
,
79 style
|= wxTAB_TRAVERSAL
| wxFULL_REPAINT_ON_RESIZE
;
80 if ( !wxWindow
::Create(parent
, id
,
81 wxDefaultPosition
, wxDefaultSize
,
85 // The status bar should have a themed background
86 SetThemeEnabled( true );
91 SetFont(*wxSMALL_FONT
);
94 int height
= (int)((11*GetCharHeight())/10 + 2*GetBorderY());
95 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
);
102 wxSize wxStatusBarGeneric
::DoGetBestSize() const
106 // best width is the width of the parent
108 GetParent()->GetClientSize(&width
, NULL
);
110 width
= 80; // a dummy value
112 // best height is as calculated above in Create()
113 height
= (int)((11*GetCharHeight())/10 + 2*GetBorderY());
115 return wxSize(width
, height
);
118 void wxStatusBarGeneric
::SetFieldsCount(int number
, const int *widths
)
120 wxASSERT_MSG( number
>= 0, _T("negative number of fields in wxStatusBar?") );
122 // enlarge the m_statusStrings array if needed:
123 for (size_t i
= m_panes
.GetCount(); i
< (size_t)number
; ++i
)
124 m_statusStrings
.Add( wxEmptyString
);
126 // shrink the m_statusStrings array if needed:
127 for (int j
= (int)m_panes
.GetCount() - 1; j
>= number
; --j
)
128 m_statusStrings
.RemoveAt(j
);
130 // forget the old cached pixel widths
133 wxStatusBarBase
::SetFieldsCount(number
, widths
);
135 wxASSERT_MSG( m_panes
.GetCount() == m_statusStrings
.GetCount(),
136 _T("This really should never happen, can we do away with m_panes.GetCount() here?") );
139 void wxStatusBarGeneric
::SetStatusText(const wxString
& text
, int number
)
141 wxCHECK_RET( (number
>= 0) && ((size_t)number
< m_panes
.GetCount()),
142 _T("invalid status bar field index") );
144 wxString oldText
= m_statusStrings
[number
];
147 m_statusStrings
[number
] = text
;
150 GetFieldRect(number
, rect
);
152 Refresh(true, &rect
);
154 // it's common to show some text in the status bar before starting a
155 // relatively lengthy operation, ensure that the text is shown to the
156 // user immediately and not after the lengthy operation end
161 wxString wxStatusBarGeneric
::GetStatusText(int n
) const
163 wxCHECK_MSG( (n
>= 0) && ((size_t)n
< m_panes
.GetCount()), wxEmptyString
,
164 _T("invalid status bar field index") );
166 return m_statusStrings
[n
];
169 void wxStatusBarGeneric
::SetStatusWidths(int n
, const int widths_field
[])
171 // only set status widths when n == number of statuswindows
172 wxCHECK_RET( (size_t)n
== m_panes
.GetCount(), _T("status bar field count mismatch") );
174 // forget the old cached pixel widths
177 wxStatusBarBase
::SetStatusWidths(n
, widths_field
);
180 bool wxStatusBarGeneric
::ShowsSizeGrip() const
182 if ( !HasFlag(wxST_SIZEGRIP
) )
185 wxTopLevelWindow
* const
186 tlw
= wxDynamicCast(wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
187 return tlw
&& !tlw
->IsMaximized() && tlw
->HasFlag(wxRESIZE_BORDER
);
190 void wxStatusBarGeneric
::DrawFieldText(wxDC
& dc
, const wxRect
& rect
, int i
, int textHeight
)
192 wxString
text(GetStatusText(i
));
194 return; // optimization
196 int xpos
= rect
.x
+ wxFIELD_TEXT_MARGIN
,
197 maxWidth
= rect
.width
- 2*wxFIELD_TEXT_MARGIN
,
198 ypos
= (int) (((rect
.height
- textHeight
) / 2) + rect
.y
+ 0.5);
202 // don't write text over the size grip:
203 // NOTE: overloading DoGetClientSize() and GetClientAreaOrigin() wouldn't
204 // work because the adjustment needs to be done only when drawing
205 // the field text and not also when drawing the background, the
206 // size grip itself, etc
207 if ((GetLayoutDirection() == wxLayout_RightToLeft
&& i
== 0) ||
208 (GetLayoutDirection() != wxLayout_RightToLeft
&&
209 i
== (int)m_panes
.GetCount()-1))
211 const wxRect
& gripRc
= GetSizeGripRect();
213 // NOTE: we don't need any special treatment wrt to the layout direction
214 // since DrawText() will automatically adjust the origin of the
215 // text accordingly to the layout in use
217 maxWidth
-= gripRc
.width
;
221 // eventually ellipsize the text so that it fits the field width
222 text
= wxControl
::Ellipsize(
224 GetLayoutDirection() == wxLayout_RightToLeft ? wxELLIPSIZE_START
: wxELLIPSIZE_END
,
226 wxELLIPSIZE_EXPAND_TAB
);
227 // Ellipsize() will do something only if necessary
229 #if defined( __WXGTK__ ) || defined(__WXMAC__)
235 dc
.DrawText(text
, xpos
, ypos
);
238 void wxStatusBarGeneric
::DrawField(wxDC
& dc
, int i
, int textHeight
)
241 GetFieldRect(i
, rect
);
243 if (rect
.GetWidth() <= 0)
244 return; // happens when the status bar is shrinked in a very small area!
246 int style
= m_panes
[i
].nStyle
;
247 if (style
!= wxSB_FLAT
)
251 // Have grey background, plus 3-d border -
252 // One black rectangle.
253 // Inside this, left and top sides - dark grey. Bottom and right -
255 // Reverse it for wxSB_RAISED
257 dc
.SetPen((style
== wxSB_RAISED
) ? m_mediumShadowPen
: m_hilightPen
);
261 // Right and bottom lines
262 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
263 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
264 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
265 rect
.x
, rect
.y
+ rect
.height
);
267 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
269 // Left and top lines
270 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
272 dc
.DrawLine(rect
.x
, rect
.y
,
273 rect
.x
+ rect
.width
, rect
.y
);
276 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.height
+ 2,
277 rect
.x
, rect
.height
+ 2);
278 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
279 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
281 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
282 dc
.DrawLine(rect
.x
, rect
.y
,
283 rect
.x
+ rect
.width
, rect
.y
);
284 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
289 DrawFieldText(dc
, rect
, i
, textHeight
);
292 // Get the position and size of the field's internal bounding rectangle
293 bool wxStatusBarGeneric
::GetFieldRect(int n
, wxRect
& rect
) const
295 wxCHECK_MSG( (n
>= 0) && ((size_t)n
< m_panes
.GetCount()), false,
296 _T("invalid status bar field index") );
298 if (m_widthsAbs
.IsEmpty())
302 for ( int i
= 0; i
< n
; i
++ )
303 rect
.x
+= m_widthsAbs
[i
];
307 rect
.width
= m_widthsAbs
[n
] - 2*m_borderX
;
308 rect
.height
= m_lastClientHeight
- 2*m_borderY
;
313 // Initialize colours
314 void wxStatusBarGeneric
::InitColours()
316 #if defined(__WXPM__)
317 m_mediumShadowPen
= wxPen(wxColour(127, 127, 127));
318 m_hilightPen
= *wxWHITE_PEN
;
320 SetBackgroundColour(*wxLIGHT_GREY
);
321 SetForegroundColour(*wxBLACK
);
323 m_mediumShadowPen
= wxPen(wxSystemSettings
::GetColour(wxSYS_COLOUR_3DSHADOW
));
324 m_hilightPen
= wxPen(wxSystemSettings
::GetColour(wxSYS_COLOUR_3DHILIGHT
));
325 #endif // __WXPM__/!__WXPM__
328 void wxStatusBarGeneric
::SetMinHeight(int height
)
330 // check that this min height is not less than minimal height for the
331 // current font (min height is as calculated above in Create() except for border)
332 int minHeight
= (int)((11*GetCharHeight())/10);
334 if ( height
> minHeight
)
335 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
+ 2*m_borderY
);
338 wxRect wxStatusBarGeneric
::GetSizeGripRect() const
341 wxWindow
::DoGetClientSize(&width
, &height
);
343 if (GetLayoutDirection() == wxLayout_RightToLeft
)
344 return wxRect(2, 2, height
-2, height
-4);
346 return wxRect(width
-height
-2, 2, height
-2, height
-4);
349 // ----------------------------------------------------------------------------
350 // wxStatusBarGeneric - event handlers
351 // ----------------------------------------------------------------------------
353 void wxStatusBarGeneric
::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
359 if ( ShowsSizeGrip() )
361 const wxRect
& rc
= GetSizeGripRect();
363 GetLayoutDirection() == wxLayout_RightToLeft ? GDK_WINDOW_EDGE_SOUTH_WEST
:
364 GDK_WINDOW_EDGE_SOUTH_EAST
;
365 gtk_paint_resize_grip( m_widget
->style
,
366 GTKGetDrawingWindow(),
367 (GtkStateType
) GTK_WIDGET_STATE (m_widget
),
372 rc
.x
, rc
.y
, rc
.width
, rc
.height
);
374 #endif // __WXGTK20__
376 if (GetFont().IsOk())
377 dc
.SetFont(GetFont());
379 // compute char height only once for all panes:
380 int textHeight
= dc
.GetCharHeight();
382 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
383 for (size_t i
= 0; i
< m_panes
.GetCount(); i
++)
384 DrawField(dc
, i
, textHeight
);
387 // Responds to colour changes, and passes event on to children.
388 void wxStatusBarGeneric
::OnSysColourChanged(wxSysColourChangedEvent
& event
)
392 // Propagate the event to the non-top-level children
393 wxWindow
::OnSysColourChanged(event
);
396 void wxStatusBarGeneric
::OnLeftDown(wxMouseEvent
& event
)
400 GetClientSize(&width
, &height
);
402 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
404 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
406 if (!GTK_IS_WINDOW (ancestor
))
409 GdkWindow
*source
= GTKGetDrawingWindow();
413 gdk_window_get_origin( source
, &org_x
, &org_y
);
415 if (GetLayoutDirection() == wxLayout_RightToLeft
)
417 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
418 GDK_WINDOW_EDGE_SOUTH_WEST
,
420 org_x
- event
.GetX() + GetSize().x
,
421 org_y
+ event
.GetY(),
426 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
427 GDK_WINDOW_EDGE_SOUTH_EAST
,
429 org_x
+ event
.GetX(),
430 org_y
+ event
.GetY(),
443 void wxStatusBarGeneric
::OnRightDown(wxMouseEvent
& event
)
447 GetClientSize(&width
, &height
);
449 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
451 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
453 if (!GTK_IS_WINDOW (ancestor
))
456 GdkWindow
*source
= GTKGetDrawingWindow();
460 gdk_window_get_origin( source
, &org_x
, &org_y
);
462 gtk_window_begin_move_drag (GTK_WINDOW (ancestor
),
464 org_x
+ event
.GetX(),
465 org_y
+ event
.GetY(),
477 void wxStatusBarGeneric
::OnSize(wxSizeEvent
& WXUNUSED(event
))
479 // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
482 GetSize(&width
, &m_lastClientHeight
);
484 GetClientSize(&width
, &m_lastClientHeight
);
487 // recompute the cache of the field widths if the status bar width has changed
488 m_widthsAbs
= CalculateAbsWidths(width
);
491 #endif // wxUSE_STATUSBAR