1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/statusbar.cpp
3 // Purpose: native implementation of wxStatusBar
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
29 #include "wx/statusbr.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
34 #include "wx/settings.h"
35 #include "wx/dcclient.h"
38 #include "wx/control.h"
41 #include "wx/msw/private.h"
42 #include "wx/tooltip.h"
46 #include "wx/msw/uxtheme.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 // no idea for a default width, just choose something
57 static const int DEFAULT_FIELD_WIDTH
= 25;
59 } // anonymous namespace
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // windowsx.h and commctrl.h don't define those, so we do it here
66 #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w)
67 #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
68 #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
69 #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
71 // ============================================================================
73 // ============================================================================
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 wxStatusBar::wxStatusBar()
87 WXDWORD
wxStatusBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
89 WXDWORD msStyle
= wxStatusBarBase::MSWGetStyle(style
, exstyle
);
91 // wxSTB_SIZEGRIP is part of our default style but it doesn't make sense to
92 // show size grip if this is the status bar of a non-resizable TLW so turn
93 // it off in such case
94 wxWindow
* const parent
= GetParent();
95 wxCHECK_MSG( parent
, msStyle
, wxS("Status bar must have a parent") );
96 if ( parent
->IsTopLevel() && !parent
->HasFlag(wxRESIZE_BORDER
) )
97 style
&= ~wxSTB_SIZEGRIP
;
99 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
100 // (at least in the version of comctl32.dll I'm using), and the only way to
101 // turn it off is to use CCS_TOP style - as we position the status bar
102 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxSTB_SIZEGRIP
104 if ( !(style
& wxSTB_SIZEGRIP
) )
111 // may be some versions of comctl32.dll do need it - anyhow, it won't
113 msStyle
|= SBARS_SIZEGRIP
;
120 bool wxStatusBar::Create(wxWindow
*parent
,
123 const wxString
& name
)
125 if ( !CreateControl(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
126 style
, wxDefaultValidator
, name
) )
129 if ( !MSWCreateControl(STATUSCLASSNAME
, wxString(),
130 wxDefaultPosition
, wxDefaultSize
) )
135 // cache the DC instance used by DoUpdateStatusText:
136 m_pDC
= new wxClientDC(this);
138 // we must refresh the frame size when the statusbar is created, because
139 // its client area might change
141 // notice that we must post the event, not send it, as the frame doesn't
142 // know that we're its status bar yet so laying it out right now wouldn't
143 // work correctly, we need to wait until we return to the main loop
144 PostSizeEventToParent();
149 wxStatusBar::~wxStatusBar()
151 // we must refresh the frame size when the statusbar is deleted but the
152 // frame is not - otherwise statusbar leaves a hole in the place it used to
154 PostSizeEventToParent();
156 // delete existing tooltips
157 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
159 wxDELETE(m_tooltips
[i
]);
165 bool wxStatusBar::SetFont(const wxFont
& font
)
167 if (!wxWindow::SetFont(font
))
170 if (m_pDC
) m_pDC
->SetFont(font
);
174 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
176 // this is a Windows limitation
177 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), "too many fields" );
179 // keep in synch also our m_tooltips array
181 // reset all current tooltips
182 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
184 wxDELETE(m_tooltips
[i
]);
187 // shrink/expand the array:
188 m_tooltips
.resize(nFields
, NULL
);
190 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
192 MSWUpdateFieldsWidths();
195 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
197 wxStatusBarBase::SetStatusWidths(n
, widths
);
199 MSWUpdateFieldsWidths();
202 void wxStatusBar::MSWUpdateFieldsWidths()
204 if ( m_panes
.IsEmpty() )
207 const int count
= m_panes
.GetCount();
209 const int extraWidth
= MSWGetBorderWidth() + MSWGetMetrics().textMargin
;
211 // compute the effectively available amount of space:
212 int widthAvailable
= GetClientSize().x
; // start with the entire width
213 widthAvailable
-= extraWidth
*(count
- 1); // extra space between fields
214 widthAvailable
-= MSWGetMetrics().textMargin
; // and for the last field
216 // Deal with the grip: we shouldn't overflow onto the space occupied by it
217 // so the effectively available space is smaller.
218 const int gripWidth
= HasFlag(wxSTB_SIZEGRIP
) ? MSWGetMetrics().gripWidth
220 widthAvailable
-= gripWidth
;
222 // distribute the available space (client width) among the various fields:
224 wxArrayInt widthsAbs
= CalculateAbsWidths(widthAvailable
);
227 // update the field widths in the native control:
229 int *pWidths
= new int[count
];
233 for ( i
= 0; i
< count
; i
++ )
235 nCurPos
+= widthsAbs
[i
] + extraWidth
;
236 pWidths
[i
] = nCurPos
;
239 // The total width of the panes passed to Windows must be equal to the
240 // total width available, including the grip. Otherwise we get an extra
241 // separator line just before it.
242 pWidths
[count
- 1] += gripWidth
;
244 if ( !StatusBar_SetParts(GetHwnd(), count
, pWidths
) )
246 wxLogLastError("StatusBar_SetParts");
249 // Now that all parts have been created, set their text.
250 for ( i
= 0; i
< count
; i
++ )
252 DoUpdateStatusText(i
);
258 void wxStatusBar::DoUpdateStatusText(int nField
)
263 // Get field style, if any
265 switch(m_panes
[nField
].GetStyle())
271 style
= SBT_NOBORDERS
;
281 GetFieldRect(nField
, rc
);
283 const int maxWidth
= rc
.GetWidth() - MSWGetMetrics().textMargin
;
285 wxString text
= GetStatusText(nField
);
287 // do we need to ellipsize this string?
288 wxEllipsizeMode ellmode
= (wxEllipsizeMode
)-1;
289 if (HasFlag(wxSTB_ELLIPSIZE_START
)) ellmode
= wxELLIPSIZE_START
;
290 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE
)) ellmode
= wxELLIPSIZE_MIDDLE
;
291 else if (HasFlag(wxSTB_ELLIPSIZE_END
)) ellmode
= wxELLIPSIZE_END
;
293 if (ellmode
== (wxEllipsizeMode
)-1)
295 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
296 // we don't ellipsize the text but just truncate it
297 if (HasFlag(wxSTB_SHOW_TIPS
))
298 SetEllipsizedFlag(nField
, m_pDC
->GetTextExtent(text
).GetWidth() > maxWidth
);
302 text
= wxControl::Ellipsize(text
,
306 wxELLIPSIZE_FLAGS_EXPAND_TABS
);
308 // update the ellipsization status for this pane; this is used later to
309 // decide whether a tooltip should be shown or not for this pane
310 // (if we have wxSTB_SHOW_TIPS)
311 SetEllipsizedFlag(nField
, text
!= GetStatusText(nField
));
314 // Set the status text in the native control passing both field number and style.
315 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
316 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, text
.wx_str()) )
318 wxLogLastError("StatusBar_SetText");
321 if (HasFlag(wxSTB_SHOW_TIPS
))
323 wxASSERT(m_tooltips
.size() == m_panes
.GetCount());
325 if (m_tooltips
[nField
])
327 if (GetField(nField
).IsEllipsized())
329 // update the rect of this tooltip:
330 m_tooltips
[nField
]->SetRect(rc
);
332 // update also the text:
333 m_tooltips
[nField
]->SetTip(GetStatusText(nField
));
337 // delete the tooltip associated with this pane; it's not needed anymore
338 wxDELETE(m_tooltips
[nField
]);
343 // create a new tooltip for this pane if needed
344 if (GetField(nField
).IsEllipsized())
345 m_tooltips
[nField
] = new wxToolTip(this, nField
, GetStatusText(nField
), rc
);
346 //else: leave m_tooltips[nField]==NULL
351 wxStatusBar::MSWBorders
wxStatusBar::MSWGetBorders() const
354 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
357 borders
.horz
= aBorders
[0];
358 borders
.vert
= aBorders
[1];
359 borders
.between
= aBorders
[2];
363 int wxStatusBar::GetBorderX() const
365 return MSWGetBorders().horz
;
368 int wxStatusBar::GetBorderY() const
370 return MSWGetBorders().vert
;
373 int wxStatusBar::MSWGetBorderWidth() const
375 return MSWGetBorders().between
;
379 const wxStatusBar::MSWMetrics
& wxStatusBar::MSWGetMetrics()
381 static MSWMetrics s_metrics
= { 0, 0 };
382 if ( !s_metrics
.textMargin
)
384 // Grip size should be self explanatory (the only problem with it is
385 // that it's hard coded as we don't know how to find its size using
386 // API) but the margin might merit an explanation: Windows offsets the
387 // text drawn in status bar panes so we need to take this extra margin
388 // into account to make sure the text drawn by user fits inside the
389 // pane. Notice that it's not the value returned by SB_GETBORDERS
390 // which, at least on this Windows 2003 system, returns {0, 2, 2}
391 if ( wxUxThemeEngine::GetIfActive() )
393 s_metrics
.gripWidth
= 20;
394 s_metrics
.textMargin
= 8;
396 else // classic/unthemed look
398 s_metrics
.gripWidth
= 18;
399 s_metrics
.textMargin
= 4;
406 void wxStatusBar::SetMinHeight(int height
)
408 // It looks like we need to count the border twice to really make the
409 // controls taking exactly height pixels fully fit in the status bar:
410 // at least under Windows 7 the checkbox in the custom status bar of the
411 // statbar sample gets truncated otherwise.
412 height
+= 4*GetBorderY();
414 // We need to set the size and not the size to reflect the height because
415 // wxFrame uses our size and not the minimal size as it assumes that the
416 // size of a status bar never changes anyhow.
419 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
, 0);
421 // we have to send a (dummy) WM_SIZE to redraw it now
422 SendMessage(GetHwnd(), WM_SIZE
, 0, 0);
425 bool wxStatusBar::GetFieldRect(int i
, wxRect
& rect
) const
427 wxCHECK_MSG( (i
>= 0) && ((size_t)i
< m_panes
.GetCount()), false,
428 "invalid statusbar field index" );
431 if ( !::SendMessage(GetHwnd(), SB_GETRECT
, i
, (LPARAM
)&r
) )
433 wxLogLastError("SendMessage(SB_GETRECT)");
437 wxUxThemeHandle
theme(const_cast<wxStatusBar
*>(this), L
"Status");
440 // by default Windows has a 2 pixel border to the right of the left
441 // divider (or it could be a bug) but it looks wrong so remove it
447 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme
, NULL
,
453 wxCopyRECTToRect(r
, rect
);
458 wxSize
wxStatusBar::DoGetBestSize() const
460 const MSWBorders borders
= MSWGetBorders();
464 for ( size_t i
= 0; i
< m_panes
.GetCount(); ++i
)
467 m_bSameWidthForAllPanes
? DEFAULT_FIELD_WIDTH
: m_panes
[i
].GetWidth();
468 if ( widthField
>= 0 )
474 // variable width field, its width is really a proportion
475 // related to the other fields
476 width
+= -widthField
*DEFAULT_FIELD_WIDTH
;
479 // add the space between fields
480 width
+= borders
.between
;
485 // still need something even for an empty status bar
486 width
= 2*DEFAULT_FIELD_WIDTH
;
489 // calculate height: by default it should be just big enough to show text
490 // (see SetMinHeight() for the explanation of 4 factor)
491 int height
= GetCharHeight();
492 height
+= 4*borders
.vert
;
494 wxSize
best(width
, height
);
499 void wxStatusBar::DoMoveWindow(int x
, int y
, int width
, int height
)
501 if ( GetParent()->IsSizeDeferred() )
503 wxWindowMSW::DoMoveWindow(x
, y
, width
, height
);
507 // parent pos/size isn't deferred so do it now but don't send
508 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
509 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
510 // if other windows are size deferred
511 ::SetWindowPos(GetHwnd(), NULL
, x
, y
, width
, height
,
512 SWP_NOZORDER
| SWP_NOOWNERZORDER
| SWP_NOACTIVATE
514 | SWP_NOCOPYBITS
| SWP_NOSENDCHANGING
519 // we have to trigger wxSizeEvent if there are children window in status
520 // bar because GetFieldRect returned incorrect (not updated) values up to
521 // here, which almost certainly resulted in incorrectly redrawn statusbar
522 if ( m_children
.GetCount() > 0 )
524 wxSizeEvent
event(GetClientSize(), m_windowId
);
525 event
.SetEventObject(this);
526 HandleWindowEvent(event
);
530 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
532 wxStatusBarBase::SetStatusStyles(n
, styles
);
534 if (n
!= (int)m_panes
.GetCount())
537 for (int i
= 0; i
< n
; i
++)
546 style
= SBT_NOBORDERS
;
554 // The SB_SETTEXT message is both used to set the field's text as well as
555 // the fields' styles.
556 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
557 wxString text
= GetStatusText(i
);
558 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.wx_str()))
560 wxLogLastError("StatusBar_SetText");
566 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
569 if ( nMsg
== WM_WINDOWPOSCHANGING
)
571 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
576 // we need real window coords and not wx client coords
577 AdjustForParentClientOrigin(x
, y
);
587 if ( nMsg
== WM_NCLBUTTONDOWN
)
589 // if hit-test is on gripper then send message to TLW to begin
590 // resizing. It is possible to send this message to any window.
591 if ( wParam
== HTBOTTOMRIGHT
)
595 for ( win
= GetParent(); win
; win
= win
->GetParent() )
597 if ( win
->IsTopLevel() )
599 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
609 if ( nMsg
== WM_SIZE
)
611 MSWUpdateFieldsWidths();
613 if ( HasFlag(wxSTB_ELLIPSIZE_START
) ||
614 HasFlag(wxSTB_ELLIPSIZE_MIDDLE
) ||
615 HasFlag(wxSTB_ELLIPSIZE_END
) )
617 for (int i
=0; i
<GetFieldsCount(); i
++)
619 // re-set the field text, in case we need to ellipsize
620 // (or de-ellipsize) some parts of it
621 DoUpdateStatusText(i
);
626 return wxStatusBarBase::MSWWindowProc(nMsg
, wParam
, lParam
);
630 bool wxStatusBar::MSWProcessMessage(WXMSG
* pMsg
)
632 if ( HasFlag(wxSTB_SHOW_TIPS
) )
634 // for a tooltip to be shown, we need to relay mouse events to it;
635 // this is typically done by wxWindowMSW::MSWProcessMessage but only
636 // if wxWindow::m_tooltip pointer is non-NULL.
637 // Since wxStatusBar has multiple tooltips for a single HWND, it keeps
638 // wxWindow::m_tooltip == NULL and then relays mouse events here:
639 MSG
*msg
= (MSG
*)pMsg
;
640 if ( msg
->message
== WM_MOUSEMOVE
)
641 wxToolTip::RelayEvent(pMsg
);
644 return wxWindow::MSWProcessMessage(pMsg
);
647 bool wxStatusBar::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
* WXUNUSED(result
))
649 if ( HasFlag(wxSTB_SHOW_TIPS
) )
651 // see comment in wxStatusBar::MSWProcessMessage for more info;
652 // basically we need to override wxWindow::MSWOnNotify because
653 // we have wxWindow::m_tooltip always NULL but we still use tooltips...
655 NMHDR
* hdr
= (NMHDR
*)lParam
;
658 if (hdr
->idFrom
< m_tooltips
.size() && m_tooltips
[hdr
->idFrom
])
659 str
= m_tooltips
[hdr
->idFrom
]->GetTip();
661 if ( HandleTooltipNotify(hdr
->code
, lParam
, str
))
670 #endif // wxUSE_TOOLTIPS
672 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR