1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/statusbr.cpp
3 // Purpose: wxStatusBarGeneric class implementation
4 // Author: Julian Smart
5 // Modified by: Francesco Montorsi
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"
32 #include "wx/gtk/private.h"
35 // we only have to do it here when we use wxStatusBarGeneric in addition to the
36 // standard wxStatusBar class, if wxStatusBarGeneric is the same as
37 // wxStatusBar, then the corresponding IMPLEMENT_DYNAMIC_CLASS is already in
39 #if defined(__WXMAC__) || \
40 (defined(wxUSE_NATIVE_STATUSBAR) && wxUSE_NATIVE_STATUSBAR)
41 #include "wx/generic/statusbr.h"
43 IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric
, wxWindow
)
44 #endif // wxUSE_NATIVE_STATUSBAR
46 // Default status border dimensions
47 #define wxTHICK_LINE_BORDER 2
49 // Margin between the field text and the field rect
50 #define wxFIELD_TEXT_MARGIN 2
52 // ----------------------------------------------------------------------------
53 // GTK+ signal handler
54 // ----------------------------------------------------------------------------
56 #if defined( __WXGTK20__ )
57 #if GTK_CHECK_VERSION(2,12,0)
60 gboolean
statusbar_query_tooltip(GtkWidget
* WXUNUSED(widget
),
63 gboolean
WXUNUSED(keyboard_mode
),
67 int n
= statbar
->GetFieldFromPoint(wxPoint(x
,y
));
71 // should we show the tooltip for the n-th pane of the statusbar?
72 if (!statbar
->GetField(n
).IsEllipsized())
73 return FALSE
; // no, it's not useful
75 const wxString
& str
= statbar
->GetStatusText(n
);
79 gtk_tooltip_set_text(tooltip
, wxGTK_CONV_SYS(str
));
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 BEGIN_EVENT_TABLE(wxStatusBarGeneric
, wxWindow
)
91 EVT_PAINT(wxStatusBarGeneric::OnPaint
)
92 EVT_SIZE(wxStatusBarGeneric::OnSize
)
93 EVT_LEFT_DOWN(wxStatusBarGeneric::OnLeftDown
)
94 EVT_RIGHT_DOWN(wxStatusBarGeneric::OnRightDown
)
95 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged
)
98 void wxStatusBarGeneric::Init()
100 m_borderX
= wxTHICK_LINE_BORDER
;
101 m_borderY
= wxTHICK_LINE_BORDER
;
104 wxStatusBarGeneric::~wxStatusBarGeneric()
108 bool wxStatusBarGeneric::Create(wxWindow
*parent
,
111 const wxString
& name
)
113 style
|= wxTAB_TRAVERSAL
| wxFULL_REPAINT_ON_RESIZE
;
114 if ( !wxWindow::Create(parent
, id
,
115 wxDefaultPosition
, wxDefaultSize
,
119 // The status bar should have a themed background
120 SetThemeEnabled( true );
125 SetFont(*wxSMALL_FONT
);
128 int height
= (int)((11*GetCharHeight())/10 + 2*GetBorderY());
129 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
);
133 #if defined( __WXGTK20__ )
134 #if GTK_CHECK_VERSION(2,12,0)
135 if (HasFlag(wxSTB_SHOW_TIPS
) && !gtk_check_version(2,12,0))
137 g_object_set(m_widget
, "has-tooltip", TRUE
, NULL
);
138 g_signal_connect(m_widget
, "query-tooltip",
139 G_CALLBACK(statusbar_query_tooltip
), this);
147 wxSize
wxStatusBarGeneric::DoGetBestSize() const
151 // best width is the width of the parent
153 GetParent()->GetClientSize(&width
, NULL
);
155 width
= 80; // a dummy value
157 // best height is as calculated above in Create()
158 height
= (int)((11*GetCharHeight())/10 + 2*GetBorderY());
160 return wxSize(width
, height
);
163 void wxStatusBarGeneric::SetFieldsCount(int number
, const int *widths
)
165 wxASSERT_MSG( number
>= 0, _T("negative number of fields in wxStatusBar?") );
167 // this will result in a call to SetStatusWidths() and thus an update to our
169 wxStatusBarBase::SetFieldsCount(number
, widths
);
172 void wxStatusBarGeneric::SetStatusText(const wxString
& text
, int number
)
174 wxCHECK_RET( (number
>= 0) && ((size_t)number
< m_panes
.GetCount()),
175 _T("invalid status bar field index") );
177 wxString oldText
= GetStatusText(number
);
180 wxStatusBarBase::SetStatusText(text
, number
);
183 GetFieldRect(number
, rect
);
185 Refresh(true, &rect
);
187 // it's common to show some text in the status bar before starting a
188 // relatively lengthy operation, ensure that the text is shown to the
189 // user immediately and not after the lengthy operation end
194 void wxStatusBarGeneric::SetStatusWidths(int n
, const int widths_field
[])
196 // only set status widths when n == number of statuswindows
197 wxCHECK_RET( (size_t)n
== m_panes
.GetCount(), _T("status bar field count mismatch") );
199 wxStatusBarBase::SetStatusWidths(n
, widths_field
);
203 GetClientSize(&width
, &m_lastClientHeight
);
204 m_widthsAbs
= CalculateAbsWidths(width
);
207 bool wxStatusBarGeneric::ShowsSizeGrip() const
209 if ( !HasFlag(wxSTB_SIZEGRIP
) )
212 wxTopLevelWindow
* const
213 tlw
= wxDynamicCast(wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
214 return tlw
&& !tlw
->IsMaximized() && tlw
->HasFlag(wxRESIZE_BORDER
);
217 void wxStatusBarGeneric::DrawFieldText(wxDC
& dc
, const wxRect
& rect
, int i
, int textHeight
)
219 wxString
text(GetStatusText(i
));
221 return; // optimization
223 int xpos
= rect
.x
+ wxFIELD_TEXT_MARGIN
,
224 maxWidth
= rect
.width
- 2*wxFIELD_TEXT_MARGIN
,
225 ypos
= (int) (((rect
.height
- textHeight
) / 2) + rect
.y
+ 0.5);
229 // don't write text over the size grip:
230 // NOTE: overloading DoGetClientSize() and GetClientAreaOrigin() wouldn't
231 // work because the adjustment needs to be done only when drawing
232 // the field text and not also when drawing the background, the
233 // size grip itself, etc
234 if ((GetLayoutDirection() == wxLayout_RightToLeft
&& i
== 0) ||
235 (GetLayoutDirection() != wxLayout_RightToLeft
&&
236 i
== (int)m_panes
.GetCount()-1))
238 const wxRect
& gripRc
= GetSizeGripRect();
240 // NOTE: we don't need any special treatment wrt to the layout direction
241 // since DrawText() will automatically adjust the origin of the
242 // text accordingly to the layout in use
244 maxWidth
-= gripRc
.width
;
248 // eventually ellipsize the text so that it fits the field width
250 wxEllipsizeMode ellmode
= (wxEllipsizeMode
)-1;
251 if (HasFlag(wxSTB_ELLIPSIZE_START
)) ellmode
= wxELLIPSIZE_START
;
252 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE
)) ellmode
= wxELLIPSIZE_MIDDLE
;
253 else if (HasFlag(wxSTB_ELLIPSIZE_END
)) ellmode
= wxELLIPSIZE_END
;
255 if (ellmode
== (wxEllipsizeMode
)-1)
257 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
258 // we don't ellipsize the text but just truncate it
259 if (HasFlag(wxSTB_SHOW_TIPS
))
260 SetEllipsizedFlag(i
, dc
.GetTextExtent(text
).GetWidth() > maxWidth
);
262 dc
.SetClippingRegion(rect
);
266 text
= wxControl::Ellipsize(text
, dc
,
269 wxELLIPSIZE_EXPAND_TAB
);
270 // Ellipsize() will do something only if necessary
272 // update the ellipsization status for this pane; this is used later to
273 // decide whether a tooltip should be shown or not for this pane
274 // (if we have wxSTB_SHOW_TIPS)
275 SetEllipsizedFlag(i
, text
!= GetStatusText(i
));
278 #if defined( __WXGTK__ ) || defined(__WXMAC__)
284 dc
.DrawText(text
, xpos
, ypos
);
286 if (ellmode
== (wxEllipsizeMode
)-1)
287 dc
.DestroyClippingRegion();
290 void wxStatusBarGeneric::DrawField(wxDC
& dc
, int i
, int textHeight
)
293 GetFieldRect(i
, rect
);
295 if (rect
.GetWidth() <= 0)
296 return; // happens when the status bar is shrinked in a very small area!
298 int style
= m_panes
[i
].GetStyle();
299 if (style
!= wxSB_FLAT
)
302 // For wxSB_NORMAL: paint a grey background, plus 3-d border (one black rectangle)
303 // Inside this, left and top sides (dark grey). Bottom and right (white).
304 // Reverse it for wxSB_RAISED
306 dc
.SetPen((style
== wxSB_RAISED
) ? m_mediumShadowPen
: m_hilightPen
);
310 // Right and bottom lines
311 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
312 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
313 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
314 rect
.x
, rect
.y
+ rect
.height
);
316 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
318 // Left and top lines
319 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
321 dc
.DrawLine(rect
.x
, rect
.y
,
322 rect
.x
+ rect
.width
, rect
.y
);
325 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.height
+ 2,
326 rect
.x
, rect
.height
+ 2);
327 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
328 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
330 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
331 dc
.DrawLine(rect
.x
, rect
.y
,
332 rect
.x
+ rect
.width
, rect
.y
);
333 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
338 DrawFieldText(dc
, rect
, i
, textHeight
);
341 bool wxStatusBarGeneric::GetFieldRect(int n
, wxRect
& rect
) const
343 wxCHECK_MSG( (n
>= 0) && ((size_t)n
< m_panes
.GetCount()), false,
344 _T("invalid status bar field index") );
346 if (m_widthsAbs
.IsEmpty())
350 for ( int i
= 0; i
< n
; i
++ )
351 rect
.x
+= m_widthsAbs
[i
];
355 rect
.width
= m_widthsAbs
[n
] - 2*m_borderX
;
356 rect
.height
= m_lastClientHeight
- 2*m_borderY
;
361 int wxStatusBarGeneric::GetFieldFromPoint(const wxPoint
& pt
) const
363 if (m_widthsAbs
.IsEmpty())
366 // NOTE: we explicitely don't take in count the borders since they are only
367 // useful when rendering the status text, not for hit-test computations
369 if (pt
.y
<= 0 || pt
.y
>= m_lastClientHeight
)
373 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ )
375 if (pt
.x
> x
&& pt
.x
< x
+m_widthsAbs
[i
])
384 void wxStatusBarGeneric::InitColours()
386 #if defined(__WXPM__)
387 m_mediumShadowPen
= wxPen(wxColour(127, 127, 127));
388 m_hilightPen
= *wxWHITE_PEN
;
390 SetBackgroundColour(*wxLIGHT_GREY
);
391 SetForegroundColour(*wxBLACK
);
393 m_mediumShadowPen
= wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
));
394 m_hilightPen
= wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT
));
395 #endif // __WXPM__/!__WXPM__
398 void wxStatusBarGeneric::SetMinHeight(int height
)
400 // check that this min height is not less than minimal height for the
401 // current font (min height is as calculated above in Create() except for border)
402 int minHeight
= (int)((11*GetCharHeight())/10);
404 if ( height
> minHeight
)
405 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
+ 2*m_borderY
);
408 wxRect
wxStatusBarGeneric::GetSizeGripRect() const
411 wxWindow::DoGetClientSize(&width
, &height
);
413 if (GetLayoutDirection() == wxLayout_RightToLeft
)
414 return wxRect(2, 2, height
-2, height
-4);
416 return wxRect(width
-height
-2, 2, height
-2, height
-4);
419 // ----------------------------------------------------------------------------
420 // wxStatusBarGeneric - event handlers
421 // ----------------------------------------------------------------------------
423 void wxStatusBarGeneric::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
429 if ( ShowsSizeGrip() )
431 const wxRect
& rc
= GetSizeGripRect();
433 GetLayoutDirection() == wxLayout_RightToLeft
? GDK_WINDOW_EDGE_SOUTH_WEST
:
434 GDK_WINDOW_EDGE_SOUTH_EAST
;
435 gtk_paint_resize_grip( m_widget
->style
,
436 GTKGetDrawingWindow(),
437 (GtkStateType
) GTK_WIDGET_STATE (m_widget
),
442 rc
.x
, rc
.y
, rc
.width
, rc
.height
);
444 #endif // __WXGTK20__
446 if (GetFont().IsOk())
447 dc
.SetFont(GetFont());
449 // compute char height only once for all panes:
450 int textHeight
= dc
.GetCharHeight();
452 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
453 for (size_t i
= 0; i
< m_panes
.GetCount(); i
++)
454 DrawField(dc
, i
, textHeight
);
457 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent
& event
)
461 // Propagate the event to the non-top-level children
462 wxWindow::OnSysColourChanged(event
);
465 void wxStatusBarGeneric::OnLeftDown(wxMouseEvent
& event
)
469 GetClientSize(&width
, &height
);
471 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
473 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
475 if (!GTK_IS_WINDOW (ancestor
))
478 GdkWindow
*source
= GTKGetDrawingWindow();
482 gdk_window_get_origin( source
, &org_x
, &org_y
);
484 if (GetLayoutDirection() == wxLayout_RightToLeft
)
486 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
487 GDK_WINDOW_EDGE_SOUTH_WEST
,
489 org_x
- event
.GetX() + GetSize().x
,
490 org_y
+ event
.GetY(),
495 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
496 GDK_WINDOW_EDGE_SOUTH_EAST
,
498 org_x
+ event
.GetX(),
499 org_y
+ event
.GetY(),
512 void wxStatusBarGeneric::OnRightDown(wxMouseEvent
& event
)
516 GetClientSize(&width
, &height
);
518 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
520 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
522 if (!GTK_IS_WINDOW (ancestor
))
525 GdkWindow
*source
= GTKGetDrawingWindow();
529 gdk_window_get_origin( source
, &org_x
, &org_y
);
531 gtk_window_begin_move_drag (GTK_WINDOW (ancestor
),
533 org_x
+ event
.GetX(),
534 org_y
+ event
.GetY(),
546 void wxStatusBarGeneric::OnSize(wxSizeEvent
& WXUNUSED(event
))
548 // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
551 GetSize(&width
, &m_lastClientHeight
);
553 GetClientSize(&width
, &m_lastClientHeight
);
556 // recompute the cache of the field widths if the status bar width has changed
557 m_widthsAbs
= CalculateAbsWidths(width
);
560 #endif // wxUSE_STATUSBAR