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
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 BEGIN_EVENT_TABLE(wxStatusBarGeneric
, wxWindow
)
53 EVT_PAINT(wxStatusBarGeneric::OnPaint
)
54 EVT_LEFT_DOWN(wxStatusBarGeneric::OnLeftDown
)
55 EVT_RIGHT_DOWN(wxStatusBarGeneric::OnRightDown
)
56 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged
)
59 void wxStatusBarGeneric::Init()
61 m_borderX
= wxTHICK_LINE_BORDER
;
62 m_borderY
= wxTHICK_LINE_BORDER
;
65 wxStatusBarGeneric::~wxStatusBarGeneric()
69 bool wxStatusBarGeneric::Create(wxWindow
*parent
,
74 style
|= wxTAB_TRAVERSAL
| wxFULL_REPAINT_ON_RESIZE
;
75 if ( !wxWindow::Create(parent
, id
,
76 wxDefaultPosition
, wxDefaultSize
,
80 // The status bar should have a themed background
81 SetThemeEnabled( true );
86 SetFont(*wxSMALL_FONT
);
91 // Set the height according to the font and the border size
93 dc
.SetFont(GetFont());
95 dc
.GetTextExtent(_T("X"), NULL
, &y
);
97 int height
= (int)( (11*y
)/10 + 2*GetBorderY());
99 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
);
106 wxSize
wxStatusBarGeneric::DoGetBestSize() const
111 // best width is the width of the parent
112 GetParent()->GetClientSize(&width
, NULL
);
114 // best height is as calculated above in Create()
115 wxClientDC
dc((wxWindow
*)this);
116 dc
.SetFont(GetFont());
117 dc
.GetTextExtent(_T("X"), NULL
, &y
);
118 height
= (int)((11*y
)/10 + 2*GetBorderY());
120 return wxSize(width
, height
);
123 void wxStatusBarGeneric::SetFieldsCount(int number
, const int *widths
)
125 wxASSERT_MSG( number
>= 0, _T("negative number of fields in wxStatusBar?") );
127 // enlarge the m_statusStrings array if needed:
128 for (size_t i
= m_panes
.GetCount(); i
< (size_t)number
; ++i
)
129 m_statusStrings
.Add( wxEmptyString
);
131 // shrink the m_statusStrings array if needed:
132 for (int j
= (int)m_panes
.GetCount() - 1; j
>= number
; --j
)
133 m_statusStrings
.RemoveAt(j
);
135 // forget the old cached pixel widths
138 wxStatusBarBase::SetFieldsCount(number
, widths
);
140 wxASSERT_MSG( m_panes
.GetCount() == m_statusStrings
.GetCount(),
141 _T("This really should never happen, can we do away with m_panes.GetCount() here?") );
144 void wxStatusBarGeneric::SetStatusText(const wxString
& text
, int number
)
146 wxCHECK_RET( (number
>= 0) && ((size_t)number
< m_panes
.GetCount()),
147 _T("invalid status bar field index") );
149 wxString oldText
= m_statusStrings
[number
];
152 m_statusStrings
[number
] = text
;
155 GetFieldRect(number
, rect
);
157 Refresh(true, &rect
);
159 // it's common to show some text in the status bar before starting a
160 // relatively lengthy operation, ensure that the text is shown to the
161 // user immediately and not after the lengthy operation end
166 wxString
wxStatusBarGeneric::GetStatusText(int n
) const
168 wxCHECK_MSG( (n
>= 0) && ((size_t)n
< m_panes
.GetCount()), wxEmptyString
,
169 _T("invalid status bar field index") );
171 return m_statusStrings
[n
];
174 void wxStatusBarGeneric::SetStatusWidths(int n
, const int widths_field
[])
176 // only set status widths when n == number of statuswindows
177 wxCHECK_RET( (size_t)n
== m_panes
.GetCount(), _T("status bar field count mismatch") );
179 // forget the old cached pixel widths
182 wxStatusBarBase::SetStatusWidths(n
, widths_field
);
185 bool wxStatusBarGeneric::ShowsSizeGrip() const
187 if ( !HasFlag(wxST_SIZEGRIP
) )
190 wxTopLevelWindow
* const
191 tlw
= wxDynamicCast(wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
192 return tlw
&& !tlw
->IsMaximized() && tlw
->HasFlag(wxRESIZE_BORDER
);
195 void wxStatusBarGeneric::DrawFieldText(wxDC
& dc
, int i
)
200 GetFieldRect(i
, rect
);
202 wxString
text(GetStatusText(i
));
204 wxCoord x
= 0, y
= 0;
205 dc
.GetTextExtent(text
, &x
, &y
);
207 int xpos
= rect
.x
+ leftMargin
;
208 int ypos
= (int) (((rect
.height
- y
) / 2 ) + rect
.y
+ 0.5);
210 #if defined( __WXGTK__ ) || defined(__WXMAC__)
215 dc
.SetClippingRegion(rect
.x
, rect
.y
, rect
.width
, rect
.height
);
217 dc
.DrawText(text
, xpos
, ypos
);
219 dc
.DestroyClippingRegion();
222 void wxStatusBarGeneric::DrawField(wxDC
& dc
, int i
)
225 GetFieldRect(i
, rect
);
227 int style
= m_panes
[i
].nStyle
;
228 if (style
!= wxSB_FLAT
)
232 // Have grey background, plus 3-d border -
233 // One black rectangle.
234 // Inside this, left and top sides - dark grey. Bottom and right -
236 // Reverse it for wxSB_RAISED
238 dc
.SetPen((style
== wxSB_RAISED
) ? m_mediumShadowPen
: m_hilightPen
);
242 // Right and bottom lines
243 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
244 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
245 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
246 rect
.x
, rect
.y
+ rect
.height
);
248 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
250 // Left and top lines
251 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
253 dc
.DrawLine(rect
.x
, rect
.y
,
254 rect
.x
+ rect
.width
, rect
.y
);
257 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.height
+ 2,
258 rect
.x
, rect
.height
+ 2);
259 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
260 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
262 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
263 dc
.DrawLine(rect
.x
, rect
.y
,
264 rect
.x
+ rect
.width
, rect
.y
);
265 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
271 DrawFieldText(dc
, i
);
274 // Get the position and size of the field's internal bounding rectangle
275 bool wxStatusBarGeneric::GetFieldRect(int n
, wxRect
& rect
) const
277 wxCHECK_MSG( (n
>= 0) && ((size_t)n
< m_panes
.GetCount()), false,
278 _T("invalid status bar field index") );
280 // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
283 GetSize(&width
, &height
);
285 GetClientSize(&width
, &height
);
288 // we cache m_widthsAbs between calls and recompute it if client
289 // width has changed (or when it is initially empty)
290 if ( m_widthsAbs
.IsEmpty() || m_lastClientWidth
!= width
)
292 wxConstCast(this, wxStatusBarGeneric
)->m_widthsAbs
= CalculateAbsWidths(width
);
294 // remember last width for which we have recomputed the widths in pixels
295 wxConstCast(this, wxStatusBarGeneric
)->m_lastClientWidth
= width
;
299 for ( int i
= 0; i
< n
; i
++ )
300 rect
.x
+= m_widthsAbs
[i
];
305 rect
.width
= m_widthsAbs
[n
] - 2*m_borderX
;
306 rect
.height
= height
- 2*m_borderY
;
311 // Initialize colours
312 void wxStatusBarGeneric::InitColours()
314 #if defined(__WXPM__)
315 m_mediumShadowPen
= wxPen(wxColour(127, 127, 127), 1, wxSOLID
);
316 m_hilightPen
= *wxWHITE_PEN
;
318 SetBackgroundColour(*wxLIGHT_GREY
);
319 SetForegroundColour(*wxBLACK
);
321 m_mediumShadowPen
= wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
));
322 m_hilightPen
= wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT
));
323 #endif // __WXPM__/!__WXPM__
326 void wxStatusBarGeneric::SetMinHeight(int height
)
328 // check that this min height is not less than minimal height for the
332 dc
.GetTextExtent( wxT("X"), NULL
, &y
);
334 if ( height
> (11*y
)/10 )
336 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
+ 2*m_borderY
);
341 // ----------------------------------------------------------------------------
342 // wxStatusBarGeneric - event handlers
343 // ----------------------------------------------------------------------------
345 void wxStatusBarGeneric::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
351 if ( ShowsSizeGrip() )
354 GetClientSize(&width
, &height
);
356 if (GetLayoutDirection() == wxLayout_RightToLeft
)
358 gtk_paint_resize_grip( m_widget
->style
,
359 GTKGetDrawingWindow(),
360 (GtkStateType
) GTK_WIDGET_STATE (m_widget
),
364 GDK_WINDOW_EDGE_SOUTH_WEST
,
365 2, 2, height
-2, height
-4 );
369 gtk_paint_resize_grip( m_widget
->style
,
370 GTKGetDrawingWindow(),
371 (GtkStateType
) GTK_WIDGET_STATE (m_widget
),
375 GDK_WINDOW_EDGE_SOUTH_EAST
,
376 width
-height
-2, 2, height
-2, height
-4 );
379 #endif // __WXGTK20__
381 if (GetFont().IsOk())
382 dc
.SetFont(GetFont());
384 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
386 for (size_t i
= 0; i
< m_panes
.GetCount(); i
++)
390 // Responds to colour changes, and passes event on to children.
391 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent
& event
)
395 // Propagate the event to the non-top-level children
396 wxWindow::OnSysColourChanged(event
);
399 void wxStatusBarGeneric::OnLeftDown(wxMouseEvent
& event
)
403 GetClientSize(&width
, &height
);
405 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
407 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
409 if (!GTK_IS_WINDOW (ancestor
))
412 GdkWindow
*source
= GTKGetDrawingWindow();
416 gdk_window_get_origin( source
, &org_x
, &org_y
);
418 if (GetLayoutDirection() == wxLayout_RightToLeft
)
420 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
421 GDK_WINDOW_EDGE_SOUTH_WEST
,
423 org_x
- event
.GetX() + GetSize().x
,
424 org_y
+ event
.GetY(),
429 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
430 GDK_WINDOW_EDGE_SOUTH_EAST
,
432 org_x
+ event
.GetX(),
433 org_y
+ event
.GetY(),
446 void wxStatusBarGeneric::OnRightDown(wxMouseEvent
& event
)
450 GetClientSize(&width
, &height
);
452 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
454 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
456 if (!GTK_IS_WINDOW (ancestor
))
459 GdkWindow
*source
= GTKGetDrawingWindow();
463 gdk_window_get_origin( source
, &org_x
, &org_y
);
465 gtk_window_begin_move_drag (GTK_WINDOW (ancestor
),
467 org_x
+ event
.GetX(),
468 org_y
+ event
.GetY(),
480 #endif // wxUSE_STATUSBAR