1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/statusbar.cpp
3 // Purpose: native implementation of wxStatusBar
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
28 #include "wx/statusbr.h"
31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/settings.h"
34 #include "wx/dcclient.h"
37 #include "wx/control.h"
40 #include "wx/msw/private.h"
41 #include "wx/tooltip.h"
45 #include "wx/msw/uxtheme.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 // no idea for a default width, just choose something
56 static const int DEFAULT_FIELD_WIDTH
= 25;
58 } // anonymous namespace
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // windowsx.h and commctrl.h don't define those, so we do it here
65 #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w)
66 #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
67 #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
68 #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
70 // ============================================================================
72 // ============================================================================
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 wxStatusBar::wxStatusBar()
86 WXDWORD
wxStatusBar::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
88 WXDWORD msStyle
= wxStatusBarBase::MSWGetStyle(style
, exstyle
);
90 // wxSTB_SIZEGRIP is part of our default style but it doesn't make sense to
91 // show size grip if this is the status bar of a non-resizable TLW so turn
92 // it off in such case
93 wxWindow
* const parent
= GetParent();
94 wxCHECK_MSG( parent
, msStyle
, wxS("Status bar must have a parent") );
95 if ( parent
->IsTopLevel() && !parent
->HasFlag(wxRESIZE_BORDER
) )
96 style
&= ~wxSTB_SIZEGRIP
;
98 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
99 // (at least in the version of comctl32.dll I'm using), and the only way to
100 // turn it off is to use CCS_TOP style - as we position the status bar
101 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxSTB_SIZEGRIP
103 if ( !(style
& wxSTB_SIZEGRIP
) )
110 // may be some versions of comctl32.dll do need it - anyhow, it won't
112 msStyle
|= SBARS_SIZEGRIP
;
119 bool wxStatusBar::Create(wxWindow
*parent
,
122 const wxString
& name
)
124 if ( !CreateControl(parent
, id
, wxDefaultPosition
, wxDefaultSize
,
125 style
, wxDefaultValidator
, name
) )
128 if ( !MSWCreateControl(STATUSCLASSNAME
, wxString(),
129 wxDefaultPosition
, wxDefaultSize
) )
134 // cache the DC instance used by DoUpdateStatusText:
135 m_pDC
= new wxClientDC(this);
137 // we must refresh the frame size when the statusbar is created, because
138 // its client area might change
140 // notice that we must post the event, not send it, as the frame doesn't
141 // know that we're its status bar yet so laying it out right now wouldn't
142 // work correctly, we need to wait until we return to the main loop
143 PostSizeEventToParent();
148 wxStatusBar::~wxStatusBar()
150 // we must refresh the frame size when the statusbar is deleted but the
151 // frame is not - otherwise statusbar leaves a hole in the place it used to
153 PostSizeEventToParent();
156 // delete existing tooltips
157 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
159 wxDELETE(m_tooltips
[i
]);
161 #endif // wxUSE_TOOLTIPS
166 bool wxStatusBar::SetFont(const wxFont
& font
)
168 if (!wxWindow::SetFont(font
))
171 if (m_pDC
) m_pDC
->SetFont(font
);
175 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
177 // this is a Windows limitation
178 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), "too many fields" );
180 // keep in synch also our m_tooltips array
183 // reset all current tooltips
184 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
186 wxDELETE(m_tooltips
[i
]);
189 // shrink/expand the array:
190 m_tooltips
.resize(nFields
, NULL
);
191 #endif // wxUSE_TOOLTIPS
193 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
195 MSWUpdateFieldsWidths();
198 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
200 wxStatusBarBase::SetStatusWidths(n
, widths
);
202 MSWUpdateFieldsWidths();
205 void wxStatusBar::MSWUpdateFieldsWidths()
207 if ( m_panes
.IsEmpty() )
210 const int count
= m_panes
.GetCount();
212 const int extraWidth
= MSWGetBorderWidth() + MSWGetMetrics().textMargin
;
214 // compute the effectively available amount of space:
215 int widthAvailable
= GetClientSize().x
; // start with the entire width
216 widthAvailable
-= extraWidth
*(count
- 1); // extra space between fields
217 widthAvailable
-= MSWGetMetrics().textMargin
; // and for the last field
219 // Deal with the grip: we shouldn't overflow onto the space occupied by it
220 // so the effectively available space is smaller.
221 const int gripWidth
= HasFlag(wxSTB_SIZEGRIP
) ? MSWGetMetrics().gripWidth
223 widthAvailable
-= gripWidth
;
225 // distribute the available space (client width) among the various fields:
227 wxArrayInt widthsAbs
= CalculateAbsWidths(widthAvailable
);
230 // update the field widths in the native control:
232 int *pWidths
= new int[count
];
236 for ( i
= 0; i
< count
; i
++ )
238 nCurPos
+= widthsAbs
[i
] + extraWidth
;
239 pWidths
[i
] = nCurPos
;
242 // The total width of the panes passed to Windows must be equal to the
243 // total width available, including the grip. Otherwise we get an extra
244 // separator line just before it.
245 pWidths
[count
- 1] += gripWidth
;
247 if ( !StatusBar_SetParts(GetHwnd(), count
, pWidths
) )
249 wxLogLastError("StatusBar_SetParts");
252 // Now that all parts have been created, set their text.
253 for ( i
= 0; i
< count
; i
++ )
255 DoUpdateStatusText(i
);
261 void wxStatusBar::DoUpdateStatusText(int nField
)
266 // Get field style, if any
268 switch(m_panes
[nField
].GetStyle())
274 style
= SBT_NOBORDERS
;
285 GetFieldRect(nField
, rc
);
287 const int maxWidth
= rc
.GetWidth() - MSWGetMetrics().textMargin
;
289 wxString text
= GetStatusText(nField
);
291 // do we need to ellipsize this string?
292 wxEllipsizeMode ellmode
= (wxEllipsizeMode
)-1;
293 if (HasFlag(wxSTB_ELLIPSIZE_START
)) ellmode
= wxELLIPSIZE_START
;
294 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE
)) ellmode
= wxELLIPSIZE_MIDDLE
;
295 else if (HasFlag(wxSTB_ELLIPSIZE_END
)) ellmode
= wxELLIPSIZE_END
;
297 if (ellmode
== (wxEllipsizeMode
)-1)
299 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
300 // we don't ellipsize the text but just truncate it
301 if (HasFlag(wxSTB_SHOW_TIPS
))
302 SetEllipsizedFlag(nField
, m_pDC
->GetTextExtent(text
).GetWidth() > maxWidth
);
306 text
= wxControl::Ellipsize(text
,
310 wxELLIPSIZE_FLAGS_EXPAND_TABS
);
312 // update the ellipsization status for this pane; this is used later to
313 // decide whether a tooltip should be shown or not for this pane
314 // (if we have wxSTB_SHOW_TIPS)
315 SetEllipsizedFlag(nField
, text
!= GetStatusText(nField
));
318 // Set the status text in the native control passing both field number and style.
319 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
320 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, text
.t_str()) )
322 wxLogLastError("StatusBar_SetText");
326 if (HasFlag(wxSTB_SHOW_TIPS
))
328 wxASSERT(m_tooltips
.size() == m_panes
.GetCount());
330 if (m_tooltips
[nField
])
332 if (GetField(nField
).IsEllipsized())
334 // update the rect of this tooltip:
335 m_tooltips
[nField
]->SetRect(rc
);
337 // update also the text:
338 m_tooltips
[nField
]->SetTip(GetStatusText(nField
));
342 // delete the tooltip associated with this pane; it's not needed anymore
343 wxDELETE(m_tooltips
[nField
]);
348 // create a new tooltip for this pane if needed
349 if (GetField(nField
).IsEllipsized())
350 m_tooltips
[nField
] = new wxToolTip(this, nField
, GetStatusText(nField
), rc
);
351 //else: leave m_tooltips[nField]==NULL
354 #endif // wxUSE_TOOLTIPS
357 wxStatusBar::MSWBorders
wxStatusBar::MSWGetBorders() const
360 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
363 borders
.horz
= aBorders
[0];
364 borders
.vert
= aBorders
[1];
365 borders
.between
= aBorders
[2];
369 int wxStatusBar::GetBorderX() const
371 return MSWGetBorders().horz
;
374 int wxStatusBar::GetBorderY() const
376 return MSWGetBorders().vert
;
379 int wxStatusBar::MSWGetBorderWidth() const
381 return MSWGetBorders().between
;
385 const wxStatusBar::MSWMetrics
& wxStatusBar::MSWGetMetrics()
387 static MSWMetrics s_metrics
= { 0, 0 };
388 if ( !s_metrics
.textMargin
)
390 // Grip size should be self explanatory (the only problem with it is
391 // that it's hard coded as we don't know how to find its size using
392 // API) but the margin might merit an explanation: Windows offsets the
393 // text drawn in status bar panes so we need to take this extra margin
394 // into account to make sure the text drawn by user fits inside the
395 // pane. Notice that it's not the value returned by SB_GETBORDERS
396 // which, at least on this Windows 2003 system, returns {0, 2, 2}
398 if ( wxUxThemeEngine::GetIfActive() )
400 s_metrics
.gripWidth
= 20;
401 s_metrics
.textMargin
= 8;
403 else // classic/unthemed look
404 #endif // wxUSE_UXTHEME
406 s_metrics
.gripWidth
= 18;
407 s_metrics
.textMargin
= 4;
414 void wxStatusBar::SetMinHeight(int height
)
416 // It looks like we need to count the border twice to really make the
417 // controls taking exactly height pixels fully fit in the status bar:
418 // at least under Windows 7 the checkbox in the custom status bar of the
419 // statbar sample gets truncated otherwise.
420 height
+= 4*GetBorderY();
422 // We need to set the size and not the size to reflect the height because
423 // wxFrame uses our size and not the minimal size as it assumes that the
424 // size of a status bar never changes anyhow.
427 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
, 0);
429 // we have to send a (dummy) WM_SIZE to redraw it now
430 SendMessage(GetHwnd(), WM_SIZE
, 0, 0);
433 bool wxStatusBar::GetFieldRect(int i
, wxRect
& rect
) const
435 wxCHECK_MSG( (i
>= 0) && ((size_t)i
< m_panes
.GetCount()), false,
436 "invalid statusbar field index" );
439 if ( !::SendMessage(GetHwnd(), SB_GETRECT
, i
, (LPARAM
)&r
) )
441 wxLogLastError("SendMessage(SB_GETRECT)");
445 wxUxThemeHandle
theme(const_cast<wxStatusBar
*>(this), L
"Status");
448 // by default Windows has a 2 pixel border to the right of the left
449 // divider (or it could be a bug) but it looks wrong so remove it
455 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme
, NULL
,
461 wxCopyRECTToRect(r
, rect
);
466 wxSize
wxStatusBar::DoGetBestSize() const
468 const MSWBorders borders
= MSWGetBorders();
472 for ( size_t i
= 0; i
< m_panes
.GetCount(); ++i
)
475 m_bSameWidthForAllPanes
? DEFAULT_FIELD_WIDTH
: m_panes
[i
].GetWidth();
476 if ( widthField
>= 0 )
482 // variable width field, its width is really a proportion
483 // related to the other fields
484 width
+= -widthField
*DEFAULT_FIELD_WIDTH
;
487 // add the space between fields
488 width
+= borders
.between
;
493 // still need something even for an empty status bar
494 width
= 2*DEFAULT_FIELD_WIDTH
;
497 // calculate height: by default it should be just big enough to show text
498 // (see SetMinHeight() for the explanation of 4 factor)
499 int height
= GetCharHeight();
500 height
+= 4*borders
.vert
;
502 wxSize
best(width
, height
);
507 void wxStatusBar::DoMoveWindow(int x
, int y
, int width
, int height
)
509 if ( GetParent()->IsSizeDeferred() )
511 wxWindowMSW::DoMoveWindow(x
, y
, width
, height
);
515 // parent pos/size isn't deferred so do it now but don't send
516 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
517 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
518 // if other windows are size deferred
519 ::SetWindowPos(GetHwnd(), NULL
, x
, y
, width
, height
,
520 SWP_NOZORDER
| SWP_NOOWNERZORDER
| SWP_NOACTIVATE
522 | SWP_NOCOPYBITS
| SWP_NOSENDCHANGING
527 // we have to trigger wxSizeEvent if there are children window in status
528 // bar because GetFieldRect returned incorrect (not updated) values up to
529 // here, which almost certainly resulted in incorrectly redrawn statusbar
530 if ( m_children
.GetCount() > 0 )
532 wxSizeEvent
event(GetClientSize(), m_windowId
);
533 event
.SetEventObject(this);
534 HandleWindowEvent(event
);
538 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
540 wxStatusBarBase::SetStatusStyles(n
, styles
);
542 if (n
!= (int)m_panes
.GetCount())
545 for (int i
= 0; i
< n
; i
++)
554 style
= SBT_NOBORDERS
;
563 // The SB_SETTEXT message is both used to set the field's text as well as
564 // the fields' styles.
565 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
566 wxString text
= GetStatusText(i
);
567 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.t_str()))
569 wxLogLastError("StatusBar_SetText");
575 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
578 if ( nMsg
== WM_WINDOWPOSCHANGING
)
580 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
585 // we need real window coords and not wx client coords
586 AdjustForParentClientOrigin(x
, y
);
596 if ( nMsg
== WM_NCLBUTTONDOWN
)
598 // if hit-test is on gripper then send message to TLW to begin
599 // resizing. It is possible to send this message to any window.
600 if ( wParam
== HTBOTTOMRIGHT
)
604 for ( win
= GetParent(); win
; win
= win
->GetParent() )
606 if ( win
->IsTopLevel() )
608 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
618 if ( nMsg
== WM_SIZE
)
620 MSWUpdateFieldsWidths();
622 if ( HasFlag(wxSTB_ELLIPSIZE_START
) ||
623 HasFlag(wxSTB_ELLIPSIZE_MIDDLE
) ||
624 HasFlag(wxSTB_ELLIPSIZE_END
) )
626 for (int i
=0; i
<GetFieldsCount(); i
++)
628 // re-set the field text, in case we need to ellipsize
629 // (or de-ellipsize) some parts of it
630 DoUpdateStatusText(i
);
635 return wxStatusBarBase::MSWWindowProc(nMsg
, wParam
, lParam
);
639 bool wxStatusBar::MSWProcessMessage(WXMSG
* pMsg
)
641 if ( HasFlag(wxSTB_SHOW_TIPS
) )
643 // for a tooltip to be shown, we need to relay mouse events to it;
644 // this is typically done by wxWindowMSW::MSWProcessMessage but only
645 // if wxWindow::m_tooltip pointer is non-NULL.
646 // Since wxStatusBar has multiple tooltips for a single HWND, it keeps
647 // wxWindow::m_tooltip == NULL and then relays mouse events here:
648 MSG
*msg
= (MSG
*)pMsg
;
649 if ( msg
->message
== WM_MOUSEMOVE
)
650 wxToolTip::RelayEvent(pMsg
);
653 return wxWindow::MSWProcessMessage(pMsg
);
656 bool wxStatusBar::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
* WXUNUSED(result
))
658 if ( HasFlag(wxSTB_SHOW_TIPS
) )
660 // see comment in wxStatusBar::MSWProcessMessage for more info;
661 // basically we need to override wxWindow::MSWOnNotify because
662 // we have wxWindow::m_tooltip always NULL but we still use tooltips...
664 NMHDR
* hdr
= (NMHDR
*)lParam
;
667 if (hdr
->idFrom
< m_tooltips
.size() && m_tooltips
[hdr
->idFrom
])
668 str
= m_tooltips
[hdr
->idFrom
]->GetTip();
670 if ( HandleTooltipNotify(hdr
->code
, lParam
, str
))
679 #endif // wxUSE_TOOLTIPS
681 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR