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();
157 // delete existing tooltips
158 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
160 wxDELETE(m_tooltips
[i
]);
162 #endif // wxUSE_TOOLTIPS
167 bool wxStatusBar::SetFont(const wxFont
& font
)
169 if (!wxWindow::SetFont(font
))
172 if (m_pDC
) m_pDC
->SetFont(font
);
176 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
178 // this is a Windows limitation
179 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), "too many fields" );
181 // keep in synch also our m_tooltips array
184 // reset all current tooltips
185 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
187 wxDELETE(m_tooltips
[i
]);
190 // shrink/expand the array:
191 m_tooltips
.resize(nFields
, NULL
);
192 #endif // wxUSE_TOOLTIPS
194 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
196 MSWUpdateFieldsWidths();
199 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
201 wxStatusBarBase::SetStatusWidths(n
, widths
);
203 MSWUpdateFieldsWidths();
206 void wxStatusBar::MSWUpdateFieldsWidths()
208 if ( m_panes
.IsEmpty() )
211 const int count
= m_panes
.GetCount();
213 const int extraWidth
= MSWGetBorderWidth() + MSWGetMetrics().textMargin
;
215 // compute the effectively available amount of space:
216 int widthAvailable
= GetClientSize().x
; // start with the entire width
217 widthAvailable
-= extraWidth
*(count
- 1); // extra space between fields
218 widthAvailable
-= MSWGetMetrics().textMargin
; // and for the last field
220 // Deal with the grip: we shouldn't overflow onto the space occupied by it
221 // so the effectively available space is smaller.
222 const int gripWidth
= HasFlag(wxSTB_SIZEGRIP
) ? MSWGetMetrics().gripWidth
224 widthAvailable
-= gripWidth
;
226 // distribute the available space (client width) among the various fields:
228 wxArrayInt widthsAbs
= CalculateAbsWidths(widthAvailable
);
231 // update the field widths in the native control:
233 int *pWidths
= new int[count
];
237 for ( i
= 0; i
< count
; i
++ )
239 nCurPos
+= widthsAbs
[i
] + extraWidth
;
240 pWidths
[i
] = nCurPos
;
243 // The total width of the panes passed to Windows must be equal to the
244 // total width available, including the grip. Otherwise we get an extra
245 // separator line just before it.
246 pWidths
[count
- 1] += gripWidth
;
248 if ( !StatusBar_SetParts(GetHwnd(), count
, pWidths
) )
250 wxLogLastError("StatusBar_SetParts");
253 // Now that all parts have been created, set their text.
254 for ( i
= 0; i
< count
; i
++ )
256 DoUpdateStatusText(i
);
262 void wxStatusBar::DoUpdateStatusText(int nField
)
267 // Get field style, if any
269 switch(m_panes
[nField
].GetStyle())
275 style
= SBT_NOBORDERS
;
286 GetFieldRect(nField
, rc
);
288 const int maxWidth
= rc
.GetWidth() - MSWGetMetrics().textMargin
;
290 wxString text
= GetStatusText(nField
);
292 // do we need to ellipsize this string?
293 wxEllipsizeMode ellmode
= (wxEllipsizeMode
)-1;
294 if (HasFlag(wxSTB_ELLIPSIZE_START
)) ellmode
= wxELLIPSIZE_START
;
295 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE
)) ellmode
= wxELLIPSIZE_MIDDLE
;
296 else if (HasFlag(wxSTB_ELLIPSIZE_END
)) ellmode
= wxELLIPSIZE_END
;
298 if (ellmode
== (wxEllipsizeMode
)-1)
300 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
301 // we don't ellipsize the text but just truncate it
302 if (HasFlag(wxSTB_SHOW_TIPS
))
303 SetEllipsizedFlag(nField
, m_pDC
->GetTextExtent(text
).GetWidth() > maxWidth
);
307 text
= wxControl::Ellipsize(text
,
311 wxELLIPSIZE_FLAGS_EXPAND_TABS
);
313 // update the ellipsization status for this pane; this is used later to
314 // decide whether a tooltip should be shown or not for this pane
315 // (if we have wxSTB_SHOW_TIPS)
316 SetEllipsizedFlag(nField
, text
!= GetStatusText(nField
));
319 // Set the status text in the native control passing both field number and style.
320 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
321 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, text
.t_str()) )
323 wxLogLastError("StatusBar_SetText");
327 if (HasFlag(wxSTB_SHOW_TIPS
))
329 wxASSERT(m_tooltips
.size() == m_panes
.GetCount());
331 if (m_tooltips
[nField
])
333 if (GetField(nField
).IsEllipsized())
335 // update the rect of this tooltip:
336 m_tooltips
[nField
]->SetRect(rc
);
338 // update also the text:
339 m_tooltips
[nField
]->SetTip(GetStatusText(nField
));
343 // delete the tooltip associated with this pane; it's not needed anymore
344 wxDELETE(m_tooltips
[nField
]);
349 // create a new tooltip for this pane if needed
350 if (GetField(nField
).IsEllipsized())
351 m_tooltips
[nField
] = new wxToolTip(this, nField
, GetStatusText(nField
), rc
);
352 //else: leave m_tooltips[nField]==NULL
355 #endif // wxUSE_TOOLTIPS
358 wxStatusBar::MSWBorders
wxStatusBar::MSWGetBorders() const
361 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
364 borders
.horz
= aBorders
[0];
365 borders
.vert
= aBorders
[1];
366 borders
.between
= aBorders
[2];
370 int wxStatusBar::GetBorderX() const
372 return MSWGetBorders().horz
;
375 int wxStatusBar::GetBorderY() const
377 return MSWGetBorders().vert
;
380 int wxStatusBar::MSWGetBorderWidth() const
382 return MSWGetBorders().between
;
386 const wxStatusBar::MSWMetrics
& wxStatusBar::MSWGetMetrics()
388 static MSWMetrics s_metrics
= { 0, 0 };
389 if ( !s_metrics
.textMargin
)
391 // Grip size should be self explanatory (the only problem with it is
392 // that it's hard coded as we don't know how to find its size using
393 // API) but the margin might merit an explanation: Windows offsets the
394 // text drawn in status bar panes so we need to take this extra margin
395 // into account to make sure the text drawn by user fits inside the
396 // pane. Notice that it's not the value returned by SB_GETBORDERS
397 // which, at least on this Windows 2003 system, returns {0, 2, 2}
399 if ( wxUxThemeEngine::GetIfActive() )
401 s_metrics
.gripWidth
= 20;
402 s_metrics
.textMargin
= 8;
404 else // classic/unthemed look
405 #endif // wxUSE_UXTHEME
407 s_metrics
.gripWidth
= 18;
408 s_metrics
.textMargin
= 4;
415 void wxStatusBar::SetMinHeight(int height
)
417 // It looks like we need to count the border twice to really make the
418 // controls taking exactly height pixels fully fit in the status bar:
419 // at least under Windows 7 the checkbox in the custom status bar of the
420 // statbar sample gets truncated otherwise.
421 height
+= 4*GetBorderY();
423 // We need to set the size and not the size to reflect the height because
424 // wxFrame uses our size and not the minimal size as it assumes that the
425 // size of a status bar never changes anyhow.
428 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
, 0);
430 // we have to send a (dummy) WM_SIZE to redraw it now
431 SendMessage(GetHwnd(), WM_SIZE
, 0, 0);
434 bool wxStatusBar::GetFieldRect(int i
, wxRect
& rect
) const
436 wxCHECK_MSG( (i
>= 0) && ((size_t)i
< m_panes
.GetCount()), false,
437 "invalid statusbar field index" );
440 if ( !::SendMessage(GetHwnd(), SB_GETRECT
, i
, (LPARAM
)&r
) )
442 wxLogLastError("SendMessage(SB_GETRECT)");
446 wxUxThemeHandle
theme(const_cast<wxStatusBar
*>(this), L
"Status");
449 // by default Windows has a 2 pixel border to the right of the left
450 // divider (or it could be a bug) but it looks wrong so remove it
456 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme
, NULL
,
462 wxCopyRECTToRect(r
, rect
);
467 wxSize
wxStatusBar::DoGetBestSize() const
469 const MSWBorders borders
= MSWGetBorders();
473 for ( size_t i
= 0; i
< m_panes
.GetCount(); ++i
)
476 m_bSameWidthForAllPanes
? DEFAULT_FIELD_WIDTH
: m_panes
[i
].GetWidth();
477 if ( widthField
>= 0 )
483 // variable width field, its width is really a proportion
484 // related to the other fields
485 width
+= -widthField
*DEFAULT_FIELD_WIDTH
;
488 // add the space between fields
489 width
+= borders
.between
;
494 // still need something even for an empty status bar
495 width
= 2*DEFAULT_FIELD_WIDTH
;
498 // calculate height: by default it should be just big enough to show text
499 // (see SetMinHeight() for the explanation of 4 factor)
500 int height
= GetCharHeight();
501 height
+= 4*borders
.vert
;
503 wxSize
best(width
, height
);
508 void wxStatusBar::DoMoveWindow(int x
, int y
, int width
, int height
)
510 if ( GetParent()->IsSizeDeferred() )
512 wxWindowMSW::DoMoveWindow(x
, y
, width
, height
);
516 // parent pos/size isn't deferred so do it now but don't send
517 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
518 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
519 // if other windows are size deferred
520 ::SetWindowPos(GetHwnd(), NULL
, x
, y
, width
, height
,
521 SWP_NOZORDER
| SWP_NOOWNERZORDER
| SWP_NOACTIVATE
523 | SWP_NOCOPYBITS
| SWP_NOSENDCHANGING
528 // we have to trigger wxSizeEvent if there are children window in status
529 // bar because GetFieldRect returned incorrect (not updated) values up to
530 // here, which almost certainly resulted in incorrectly redrawn statusbar
531 if ( m_children
.GetCount() > 0 )
533 wxSizeEvent
event(GetClientSize(), m_windowId
);
534 event
.SetEventObject(this);
535 HandleWindowEvent(event
);
539 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
541 wxStatusBarBase::SetStatusStyles(n
, styles
);
543 if (n
!= (int)m_panes
.GetCount())
546 for (int i
= 0; i
< n
; i
++)
555 style
= SBT_NOBORDERS
;
564 // The SB_SETTEXT message is both used to set the field's text as well as
565 // the fields' styles.
566 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
567 wxString text
= GetStatusText(i
);
568 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.t_str()))
570 wxLogLastError("StatusBar_SetText");
576 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
579 if ( nMsg
== WM_WINDOWPOSCHANGING
)
581 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
586 // we need real window coords and not wx client coords
587 AdjustForParentClientOrigin(x
, y
);
597 if ( nMsg
== WM_NCLBUTTONDOWN
)
599 // if hit-test is on gripper then send message to TLW to begin
600 // resizing. It is possible to send this message to any window.
601 if ( wParam
== HTBOTTOMRIGHT
)
605 for ( win
= GetParent(); win
; win
= win
->GetParent() )
607 if ( win
->IsTopLevel() )
609 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
619 if ( nMsg
== WM_SIZE
)
621 MSWUpdateFieldsWidths();
623 if ( HasFlag(wxSTB_ELLIPSIZE_START
) ||
624 HasFlag(wxSTB_ELLIPSIZE_MIDDLE
) ||
625 HasFlag(wxSTB_ELLIPSIZE_END
) )
627 for (int i
=0; i
<GetFieldsCount(); i
++)
629 // re-set the field text, in case we need to ellipsize
630 // (or de-ellipsize) some parts of it
631 DoUpdateStatusText(i
);
636 return wxStatusBarBase::MSWWindowProc(nMsg
, wParam
, lParam
);
640 bool wxStatusBar::MSWProcessMessage(WXMSG
* pMsg
)
642 if ( HasFlag(wxSTB_SHOW_TIPS
) )
644 // for a tooltip to be shown, we need to relay mouse events to it;
645 // this is typically done by wxWindowMSW::MSWProcessMessage but only
646 // if wxWindow::m_tooltip pointer is non-NULL.
647 // Since wxStatusBar has multiple tooltips for a single HWND, it keeps
648 // wxWindow::m_tooltip == NULL and then relays mouse events here:
649 MSG
*msg
= (MSG
*)pMsg
;
650 if ( msg
->message
== WM_MOUSEMOVE
)
651 wxToolTip::RelayEvent(pMsg
);
654 return wxWindow::MSWProcessMessage(pMsg
);
657 bool wxStatusBar::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
* WXUNUSED(result
))
659 if ( HasFlag(wxSTB_SHOW_TIPS
) )
661 // see comment in wxStatusBar::MSWProcessMessage for more info;
662 // basically we need to override wxWindow::MSWOnNotify because
663 // we have wxWindow::m_tooltip always NULL but we still use tooltips...
665 NMHDR
* hdr
= (NMHDR
*)lParam
;
668 if (hdr
->idFrom
< m_tooltips
.size() && m_tooltips
[hdr
->idFrom
])
669 str
= m_tooltips
[hdr
->idFrom
]->GetTip();
671 if ( HandleTooltipNotify(hdr
->code
, lParam
, str
))
680 #endif // wxUSE_TOOLTIPS
682 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR