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
*widget
,
63 gboolean 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 gtk_tooltip_set_text(tooltip
, wxGTK_CONV_SYS(statbar
->GetStatusText(n
)));
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 BEGIN_EVENT_TABLE(wxStatusBarGeneric
, wxWindow
)
87 EVT_PAINT(wxStatusBarGeneric::OnPaint
)
88 EVT_SIZE(wxStatusBarGeneric::OnSize
)
89 EVT_LEFT_DOWN(wxStatusBarGeneric::OnLeftDown
)
90 EVT_RIGHT_DOWN(wxStatusBarGeneric::OnRightDown
)
91 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged
)
94 void wxStatusBarGeneric::Init()
96 m_borderX
= wxTHICK_LINE_BORDER
;
97 m_borderY
= wxTHICK_LINE_BORDER
;
100 wxStatusBarGeneric::~wxStatusBarGeneric()
104 bool wxStatusBarGeneric::Create(wxWindow
*parent
,
107 const wxString
& name
)
109 style
|= wxTAB_TRAVERSAL
| wxFULL_REPAINT_ON_RESIZE
;
110 if ( !wxWindow::Create(parent
, id
,
111 wxDefaultPosition
, wxDefaultSize
,
115 // The status bar should have a themed background
116 SetThemeEnabled( true );
121 SetFont(*wxSMALL_FONT
);
124 int height
= (int)((11*GetCharHeight())/10 + 2*GetBorderY());
125 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
);
129 #if defined( __WXGTK20__ )
130 #if GTK_CHECK_VERSION(2,12,0)
131 if (HasFlag(wxST_SHOW_TIPS
) && !gtk_check_version(2,12,0))
133 g_object_set(m_widget
, "has-tooltip", TRUE
, NULL
);
134 g_signal_connect(m_widget
, "query-tooltip",
135 G_CALLBACK(statusbar_query_tooltip
), this);
143 wxSize
wxStatusBarGeneric::DoGetBestSize() const
147 // best width is the width of the parent
149 GetParent()->GetClientSize(&width
, NULL
);
151 width
= 80; // a dummy value
153 // best height is as calculated above in Create()
154 height
= (int)((11*GetCharHeight())/10 + 2*GetBorderY());
156 return wxSize(width
, height
);
159 void wxStatusBarGeneric::SetFieldsCount(int number
, const int *widths
)
161 wxASSERT_MSG( number
>= 0, _T("negative number of fields in wxStatusBar?") );
163 // this will result in a call to SetStatusWidths() and thus an update to our
165 wxStatusBarBase::SetFieldsCount(number
, widths
);
168 void wxStatusBarGeneric::SetStatusText(const wxString
& text
, int number
)
170 wxCHECK_RET( (number
>= 0) && ((size_t)number
< m_panes
.GetCount()),
171 _T("invalid status bar field index") );
173 wxString oldText
= GetStatusText(number
);
176 wxStatusBarBase::SetStatusText(text
, number
);
179 GetFieldRect(number
, rect
);
181 Refresh(true, &rect
);
183 // it's common to show some text in the status bar before starting a
184 // relatively lengthy operation, ensure that the text is shown to the
185 // user immediately and not after the lengthy operation end
190 void wxStatusBarGeneric::SetStatusWidths(int n
, const int widths_field
[])
192 // only set status widths when n == number of statuswindows
193 wxCHECK_RET( (size_t)n
== m_panes
.GetCount(), _T("status bar field count mismatch") );
195 wxStatusBarBase::SetStatusWidths(n
, widths_field
);
199 GetClientSize(&width
, &m_lastClientHeight
);
200 m_widthsAbs
= CalculateAbsWidths(width
);
203 bool wxStatusBarGeneric::ShowsSizeGrip() const
205 if ( !HasFlag(wxST_SIZEGRIP
) )
208 wxTopLevelWindow
* const
209 tlw
= wxDynamicCast(wxGetTopLevelParent(GetParent()), wxTopLevelWindow
);
210 return tlw
&& !tlw
->IsMaximized() && tlw
->HasFlag(wxRESIZE_BORDER
);
213 void wxStatusBarGeneric::DrawFieldText(wxDC
& dc
, const wxRect
& rect
, int i
, int textHeight
)
215 wxString
text(GetStatusText(i
));
217 return; // optimization
219 int xpos
= rect
.x
+ wxFIELD_TEXT_MARGIN
,
220 maxWidth
= rect
.width
- 2*wxFIELD_TEXT_MARGIN
,
221 ypos
= (int) (((rect
.height
- textHeight
) / 2) + rect
.y
+ 0.5);
225 // don't write text over the size grip:
226 // NOTE: overloading DoGetClientSize() and GetClientAreaOrigin() wouldn't
227 // work because the adjustment needs to be done only when drawing
228 // the field text and not also when drawing the background, the
229 // size grip itself, etc
230 if ((GetLayoutDirection() == wxLayout_RightToLeft
&& i
== 0) ||
231 (GetLayoutDirection() != wxLayout_RightToLeft
&&
232 i
== (int)m_panes
.GetCount()-1))
234 const wxRect
& gripRc
= GetSizeGripRect();
236 // NOTE: we don't need any special treatment wrt to the layout direction
237 // since DrawText() will automatically adjust the origin of the
238 // text accordingly to the layout in use
240 maxWidth
-= gripRc
.width
;
244 // eventually ellipsize the text so that it fits the field width
245 text
= wxControl::Ellipsize(
247 GetLayoutDirection() == wxLayout_RightToLeft
? wxELLIPSIZE_START
: wxELLIPSIZE_END
,
249 wxELLIPSIZE_EXPAND_TAB
);
250 // Ellipsize() will do something only if necessary
252 // update the ellipsization status for this pane; this is used to decide
253 // whether a tooltip should be shown or not for this pane
254 SetEllipsizedFlag(i
, text
!= GetStatusText(i
));
256 #if defined( __WXGTK__ ) || defined(__WXMAC__)
262 dc
.DrawText(text
, xpos
, ypos
);
265 void wxStatusBarGeneric::DrawField(wxDC
& dc
, int i
, int textHeight
)
268 GetFieldRect(i
, rect
);
270 if (rect
.GetWidth() <= 0)
271 return; // happens when the status bar is shrinked in a very small area!
273 int style
= m_panes
[i
].GetStyle();
274 if (style
!= wxSB_FLAT
)
277 // For wxSB_NORMAL: paint a grey background, plus 3-d border (one black rectangle)
278 // Inside this, left and top sides (dark grey). Bottom and right (white).
279 // Reverse it for wxSB_RAISED
281 dc
.SetPen((style
== wxSB_RAISED
) ? m_mediumShadowPen
: m_hilightPen
);
285 // Right and bottom lines
286 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
287 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
288 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
+ rect
.height
,
289 rect
.x
, rect
.y
+ rect
.height
);
291 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
293 // Left and top lines
294 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
296 dc
.DrawLine(rect
.x
, rect
.y
,
297 rect
.x
+ rect
.width
, rect
.y
);
300 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.height
+ 2,
301 rect
.x
, rect
.height
+ 2);
302 dc
.DrawLine(rect
.x
+ rect
.width
, rect
.y
,
303 rect
.x
+ rect
.width
, rect
.y
+ rect
.height
);
305 dc
.SetPen((style
== wxSB_RAISED
) ? m_hilightPen
: m_mediumShadowPen
);
306 dc
.DrawLine(rect
.x
, rect
.y
,
307 rect
.x
+ rect
.width
, rect
.y
);
308 dc
.DrawLine(rect
.x
, rect
.y
+ rect
.height
,
313 DrawFieldText(dc
, rect
, i
, textHeight
);
316 bool wxStatusBarGeneric::GetFieldRect(int n
, wxRect
& rect
) const
318 wxCHECK_MSG( (n
>= 0) && ((size_t)n
< m_panes
.GetCount()), false,
319 _T("invalid status bar field index") );
321 if (m_widthsAbs
.IsEmpty())
325 for ( int i
= 0; i
< n
; i
++ )
326 rect
.x
+= m_widthsAbs
[i
];
330 rect
.width
= m_widthsAbs
[n
] - 2*m_borderX
;
331 rect
.height
= m_lastClientHeight
- 2*m_borderY
;
336 int wxStatusBarGeneric::GetFieldFromPoint(const wxPoint
& pt
) const
338 if (m_widthsAbs
.IsEmpty())
341 // NOTE: we explicitely don't take in count the borders since they are only
342 // useful when rendering the status text, not for hit-test computations
344 if (pt
.y
<= 0 || pt
.y
>= m_lastClientHeight
)
348 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ )
350 if (pt
.x
> x
&& pt
.x
< x
+m_widthsAbs
[i
])
359 void wxStatusBarGeneric::InitColours()
361 #if defined(__WXPM__)
362 m_mediumShadowPen
= wxPen(wxColour(127, 127, 127));
363 m_hilightPen
= *wxWHITE_PEN
;
365 SetBackgroundColour(*wxLIGHT_GREY
);
366 SetForegroundColour(*wxBLACK
);
368 m_mediumShadowPen
= wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW
));
369 m_hilightPen
= wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT
));
370 #endif // __WXPM__/!__WXPM__
373 void wxStatusBarGeneric::SetMinHeight(int height
)
375 // check that this min height is not less than minimal height for the
376 // current font (min height is as calculated above in Create() except for border)
377 int minHeight
= (int)((11*GetCharHeight())/10);
379 if ( height
> minHeight
)
380 SetSize(wxDefaultCoord
, wxDefaultCoord
, wxDefaultCoord
, height
+ 2*m_borderY
);
383 wxRect
wxStatusBarGeneric::GetSizeGripRect() const
386 wxWindow::DoGetClientSize(&width
, &height
);
388 if (GetLayoutDirection() == wxLayout_RightToLeft
)
389 return wxRect(2, 2, height
-2, height
-4);
391 return wxRect(width
-height
-2, 2, height
-2, height
-4);
394 // ----------------------------------------------------------------------------
395 // wxStatusBarGeneric - event handlers
396 // ----------------------------------------------------------------------------
398 void wxStatusBarGeneric::OnPaint(wxPaintEvent
& WXUNUSED(event
) )
404 if ( ShowsSizeGrip() )
406 const wxRect
& rc
= GetSizeGripRect();
408 GetLayoutDirection() == wxLayout_RightToLeft
? GDK_WINDOW_EDGE_SOUTH_WEST
:
409 GDK_WINDOW_EDGE_SOUTH_EAST
;
410 gtk_paint_resize_grip( m_widget
->style
,
411 GTKGetDrawingWindow(),
412 (GtkStateType
) GTK_WIDGET_STATE (m_widget
),
417 rc
.x
, rc
.y
, rc
.width
, rc
.height
);
419 #endif // __WXGTK20__
421 if (GetFont().IsOk())
422 dc
.SetFont(GetFont());
424 // compute char height only once for all panes:
425 int textHeight
= dc
.GetCharHeight();
427 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
428 for (size_t i
= 0; i
< m_panes
.GetCount(); i
++)
429 DrawField(dc
, i
, textHeight
);
432 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent
& event
)
436 // Propagate the event to the non-top-level children
437 wxWindow::OnSysColourChanged(event
);
440 void wxStatusBarGeneric::OnLeftDown(wxMouseEvent
& event
)
444 GetClientSize(&width
, &height
);
446 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
448 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
450 if (!GTK_IS_WINDOW (ancestor
))
453 GdkWindow
*source
= GTKGetDrawingWindow();
457 gdk_window_get_origin( source
, &org_x
, &org_y
);
459 if (GetLayoutDirection() == wxLayout_RightToLeft
)
461 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
462 GDK_WINDOW_EDGE_SOUTH_WEST
,
464 org_x
- event
.GetX() + GetSize().x
,
465 org_y
+ event
.GetY(),
470 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor
),
471 GDK_WINDOW_EDGE_SOUTH_EAST
,
473 org_x
+ event
.GetX(),
474 org_y
+ event
.GetY(),
487 void wxStatusBarGeneric::OnRightDown(wxMouseEvent
& event
)
491 GetClientSize(&width
, &height
);
493 if ( ShowsSizeGrip() && (event
.GetX() > width
-height
) )
495 GtkWidget
*ancestor
= gtk_widget_get_toplevel( m_widget
);
497 if (!GTK_IS_WINDOW (ancestor
))
500 GdkWindow
*source
= GTKGetDrawingWindow();
504 gdk_window_get_origin( source
, &org_x
, &org_y
);
506 gtk_window_begin_move_drag (GTK_WINDOW (ancestor
),
508 org_x
+ event
.GetX(),
509 org_y
+ event
.GetY(),
521 void wxStatusBarGeneric::OnSize(wxSizeEvent
& WXUNUSED(event
))
523 // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
526 GetSize(&width
, &m_lastClientHeight
);
528 GetClientSize(&width
, &m_lastClientHeight
);
531 // recompute the cache of the field widths if the status bar width has changed
532 m_widthsAbs
= CalculateAbsWidths(width
);
535 #endif // wxUSE_STATUSBAR