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::DoUpdateStatusText(int number
)
166 GetFieldRect(number
, rect
);
168 Refresh(true, &rect
);
170 // it's common to show some text in the status bar before starting a
171 // relatively lengthy operation, ensure that the text is shown to the
172 // user immediately and not after the lengthy operation end
176 void wxStatusBarGeneric::SetStatusWidths(int n
, const int widths_field
[])
178 // only set status widths when n == number of statuswindows
179 wxCHECK_RET( (size_t)n
== m_panes
.GetCount(), wxT("status bar field count mismatch") );
181 wxStatusBarBase::SetStatusWidths(n
, widths_field
);
184 DoUpdateFieldWidths();
187 void wxStatusBarGeneric::DoUpdateFieldWidths()
189 m_lastClientSize
= GetClientSize();
191 // recompute the cache of the field widths if the status bar width has changed
192 m_widthsAbs
= CalculateAbsWidths(m_lastClientSize
.x
);
195 bool wxStatusBarGeneric::ShowsSizeGrip() const
197 if ( !HasFlag(wxSTB_SIZEGRIP
) )
200 wxTopLevelWindow
* const
201 tlw
= wxDynamicCast(wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
202 return tlw
&& !tlw
->IsMaximized() && tlw
->HasFlag(wxRESIZE_BORDER
);
205 void wxStatusBarGeneric::DrawFieldText(wxDC
& dc
, const wxRect
& rect
, int i
, int textHeight
)
207 wxString
text(GetStatusText(i
));
209 return; // optimization
211 int xpos
= rect
.x
+ wxFIELD_TEXT_MARGIN
,
212 maxWidth
= rect
.width
- 2*wxFIELD_TEXT_MARGIN
,
213 ypos
= (int) (((rect
.height
- textHeight
) / 2) + rect
.y
+ 0.5);
217 // don't write text over the size grip:
218 // NOTE: overloading DoGetClientSize() and GetClientAreaOrigin() wouldn't
219 // work because the adjustment needs to be done only when drawing
220 // the field text and not also when drawing the background, the
221 // size grip itself, etc
222 if ((GetLayoutDirection() == wxLayout_RightToLeft
&& i
== 0) ||
223 (GetLayoutDirection() != wxLayout_RightToLeft
&&
224 i
== (int)m_panes
.GetCount()-1))
226 const wxRect
& gripRc
= GetSizeGripRect();
228 // NOTE: we don't need any special treatment wrt to the layout direction
229 // since DrawText() will automatically adjust the origin of the
230 // text accordingly to the layout in use
232 maxWidth
-= gripRc
.width
;
236 // eventually ellipsize the text so that it fits the field width
238 wxEllipsizeMode ellmode
= (wxEllipsizeMode
)-1;
239 if (HasFlag(wxSTB_ELLIPSIZE_START
)) ellmode
= wxELLIPSIZE_START
;
240 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE
)) ellmode
= wxELLIPSIZE_MIDDLE
;
241 else if (HasFlag(wxSTB_ELLIPSIZE_END
)) ellmode
= wxELLIPSIZE_END
;
243 if (ellmode
== (wxEllipsizeMode
)-1)
245 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
246 // we don't ellipsize the text but just truncate it
247 if (HasFlag(wxSTB_SHOW_TIPS
))
248 SetEllipsizedFlag(i
, dc
.GetTextExtent(text
).GetWidth() > maxWidth
);
250 dc
.SetClippingRegion(rect
);
254 text
= wxControl::Ellipsize(text
, dc
,
257 wxELLIPSIZE_FLAGS_EXPAND_TABS
);
258 // Ellipsize() will do something only if necessary
260 // update the ellipsization status for this pane; this is used later to
261 // decide whether a tooltip should be shown or not for this pane
262 // (if we have wxSTB_SHOW_TIPS)
263 SetEllipsizedFlag(i
, text
!= GetStatusText(i
));
266 #if defined( __WXGTK__ ) || defined(__WXMAC__)
272 dc
.DrawText(text
, xpos
, ypos
);
274 if (ellmode
== (wxEllipsizeMode
)-1)
275 dc
.DestroyClippingRegion();
278 void wxStatusBarGeneric::DrawField(wxDC
& dc
, int i
, int textHeight
)
281 GetFieldRect(i
, rect
);
283 if (rect
.GetWidth() <= 0)
284 return; // happens when the status bar is shrunk in a very small area!
286 int style
= m_panes
[i
].GetStyle();
287 if (style
!= wxSB_FLAT
)
290 // For wxSB_NORMAL: paint a grey background, plus 3-d border (one black rectangle)
291 // Inside this, left and top sides (dark grey). Bottom and right (white).
292 // Reverse it for wxSB_RAISED
294 dc
.SetPen((style
== wxSB_RAISED
) ? m_mediumShadowPen
: m_hilightPen
);
298 // Right and bottom lines
299 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
300 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
301 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
302 rect
.x
, rect
.y
+ rect
.height
);
304 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
306 // Left and top lines
307 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
309 dc
.DrawLine(rect
.x
, rect
.y
,
310 rect
.x
+ rect
.width
, rect
.y
);
313 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.height
+ 2,
314 rect
.x
, rect
.height
+ 2);
315 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
316 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
318 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
319 dc
.DrawLine(rect
.x
, rect
.y
,
320 rect
.x
+ rect
.width
, rect
.y
);
321 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
326 DrawFieldText(dc
, rect
, i
, textHeight
);
329 bool wxStatusBarGeneric::GetFieldRect(int n
, wxRect
& rect
) const
331 wxCHECK_MSG( (n
>= 0) && ((size_t)n
< m_panes
.GetCount()), false,
332 wxT("invalid status bar field index") );
334 // We can be called from the user-defined EVT_SIZE handler in which case
335 // the widths haven't been updated yet and we need to do it now. This is
336 // not very efficient as we keep testing the size but there is no other way
337 // to make the code needing the up-to-date fields sizes in its EVT_SIZE to
339 if ( GetClientSize().x
!= m_lastClientSize
.x
)
341 const_cast<wxStatusBarGeneric
*>(this)->DoUpdateFieldWidths();
344 if (m_widthsAbs
.IsEmpty())
348 for ( int i
= 0; i
< n
; i
++ )
349 rect
.x
+= m_widthsAbs
[i
];
353 rect
.width
= m_widthsAbs
[n
] - 2*m_borderX
;
354 rect
.height
= m_lastClientSize
.y
- 2*m_borderY
;
359 int wxStatusBarGeneric::GetFieldFromPoint(const wxPoint
& pt
) const
361 if (m_widthsAbs
.IsEmpty())
364 // NOTE: we explicitly don't take in count the borders since they are only
365 // useful when rendering the status text, not for hit-test computations
367 if (pt
.y
<= 0 || pt
.y
>= m_lastClientSize
.y
)
371 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ )
373 if (pt
.x
> x
&& pt
.x
< x
+m_widthsAbs
[i
])
382 void wxStatusBarGeneric::InitColours()
384 #if defined(__WXPM__)
385 m_mediumShadowPen
= wxPen(wxColour(127, 127, 127));
386 m_hilightPen
= *wxWHITE_PEN
;
388 SetBackgroundColour(*wxLIGHT_GREY
);
389 SetForegroundColour(*wxBLACK
);
391 m_mediumShadowPen
= wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
));
392 m_hilightPen
= wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT
));
393 #endif // __WXPM__/!__WXPM__
396 void wxStatusBarGeneric::SetMinHeight(int height
)
398 // check that this min height is not less than minimal height for the
399 // current font (min height is as calculated above in Create() except for border)
400 int minHeight
= (int)((11*GetCharHeight())/10);
402 if ( height
> minHeight
)
403 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
+ 2*m_borderY
);
406 wxRect
wxStatusBarGeneric::GetSizeGripRect() const
409 wxWindow::DoGetClientSize(&width
, &height
);
411 if (GetLayoutDirection() == wxLayout_RightToLeft
)
412 return wxRect(2, 2, height
-2, height
-4);
414 return wxRect(width
-height
-2, 2, height
-2, height
-4);
417 // ----------------------------------------------------------------------------
418 // wxStatusBarGeneric - event handlers
419 // ----------------------------------------------------------------------------
421 void wxStatusBarGeneric::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
427 if ( ShowsSizeGrip() )
429 const wxRect
& rc
= GetSizeGripRect();
431 GetLayoutDirection() == wxLayout_RightToLeft
? GDK_WINDOW_EDGE_SOUTH_WEST
:
432 GDK_WINDOW_EDGE_SOUTH_EAST
;
433 gtk_paint_resize_grip(gtk_widget_get_style(m_widget
),
434 GTKGetDrawingWindow(),
435 gtk_widget_get_state(m_widget
),
440 rc
.x
, rc
.y
, rc
.width
, rc
.height
);
442 #endif // __WXGTK20__
444 if (GetFont().IsOk())
445 dc
.SetFont(GetFont());
447 // compute char height only once for all panes:
448 int textHeight
= dc
.GetCharHeight();
450 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
451 for (size_t i
= 0; i
< m_panes
.GetCount(); i
++)
452 DrawField(dc
, i
, textHeight
);
455 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent
& event
)
459 // Propagate the event to the non-top-level children
460 wxWindow::OnSysColourChanged(event
);
463 void wxStatusBarGeneric::OnLeftDown(wxMouseEvent
& event
)
467 GetClientSize(&width
, &height
);
469 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
471 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
473 if (!GTK_IS_WINDOW (ancestor
))
476 GdkWindow
*source
= GTKGetDrawingWindow();
480 gdk_window_get_origin( source
, &org_x
, &org_y
);
482 if (GetLayoutDirection() == wxLayout_RightToLeft
)
484 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
485 GDK_WINDOW_EDGE_SOUTH_WEST
,
487 org_x
- event
.GetX() + GetSize().x
,
488 org_y
+ event
.GetY(),
493 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
494 GDK_WINDOW_EDGE_SOUTH_EAST
,
496 org_x
+ event
.GetX(),
497 org_y
+ event
.GetY(),
510 void wxStatusBarGeneric::OnRightDown(wxMouseEvent
& event
)
514 GetClientSize(&width
, &height
);
516 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
518 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
520 if (!GTK_IS_WINDOW (ancestor
))
523 GdkWindow
*source
= GTKGetDrawingWindow();
527 gdk_window_get_origin( source
, &org_x
, &org_y
);
529 gtk_window_begin_move_drag (GTK_WINDOW (ancestor
),
531 org_x
+ event
.GetX(),
532 org_y
+ event
.GetY(),
544 void wxStatusBarGeneric::OnSize(wxSizeEvent
& event
)
546 DoUpdateFieldWidths();
551 #endif // wxUSE_STATUSBAR