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 bool wxStatusBar::Create(wxWindow
*parent
,
92 wxCHECK_MSG( parent
, false, "status bar must have a parent" );
95 SetWindowStyleFlag(style
);
98 parent
->AddChild(this);
100 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
102 DWORD wstyle
= WS_CHILD
| WS_VISIBLE
;
104 if ( style
& wxCLIP_SIBLINGS
)
105 wstyle
|= WS_CLIPSIBLINGS
;
107 // wxSTB_SIZEGRIP is part of our default style but it doesn't make sense to
108 // show size grip if this is the status bar of a non-resizeable TLW so turn
109 // it off in such case
110 if ( parent
->IsTopLevel() && !parent
->HasFlag(wxRESIZE_BORDER
) )
111 style
&= ~wxSTB_SIZEGRIP
;
113 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
114 // (at least in the version of comctl32.dll I'm using), and the only way to
115 // turn it off is to use CCS_TOP style - as we position the status bar
116 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxSTB_SIZEGRIP
118 if ( !(style
& wxSTB_SIZEGRIP
) )
125 // may be some versions of comctl32.dll do need it - anyhow, it won't
127 wstyle
|= SBARS_SIZEGRIP
;
131 m_hWnd
= CreateWindow
138 (HMENU
)wxUIntToPtr(m_windowId
.GetValue()),
144 wxLogSysError(_("Failed to create a status bar."));
152 // cache the DC instance used by DoUpdateStatusText:
153 // NOTE: create the DC before calling InheritAttributes() since
154 // it may result in a call to our SetFont()
155 m_pDC
= new wxClientDC(this);
159 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
161 // we must refresh the frame size when the statusbar is created, because
162 // its client area might change
164 // notice that we must post the event, not send it, as the frame doesn't
165 // know that we're its status bar yet so laying it out right now wouldn't
166 // work correctly, we need to wait until we return to the main loop
167 PostSizeEventToParent();
172 wxStatusBar::~wxStatusBar()
174 // we must refresh the frame size when the statusbar is deleted but the
175 // frame is not - otherwise statusbar leaves a hole in the place it used to
177 PostSizeEventToParent();
179 // delete existing tooltips
180 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
184 delete m_tooltips
[i
];
185 m_tooltips
[i
] = NULL
;
192 bool wxStatusBar::SetFont(const wxFont
& font
)
194 if (!wxWindow::SetFont(font
))
197 if (m_pDC
) m_pDC
->SetFont(font
);
201 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
203 // this is a Windows limitation
204 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), "too many fields" );
206 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
208 MSWUpdateFieldsWidths();
210 // keep in synch also our m_tooltips array
212 // reset all current tooltips
213 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
217 delete m_tooltips
[i
];
218 m_tooltips
[i
] = NULL
;
222 // shrink/expand the array:
223 m_tooltips
.resize(m_panes
.GetCount(), NULL
);
226 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
228 wxStatusBarBase::SetStatusWidths(n
, widths
);
230 MSWUpdateFieldsWidths();
233 void wxStatusBar::MSWUpdateFieldsWidths()
235 if ( m_panes
.IsEmpty() )
238 const int count
= m_panes
.GetCount();
240 const int extraWidth
= MSWGetBorderWidth() + MSWGetMetrics().textMargin
;
242 // compute the effectively available amount of space:
243 int widthAvailable
= GetClientSize().x
; // start with the entire width
244 widthAvailable
-= extraWidth
*(count
- 1); // extra space between fields
245 widthAvailable
-= MSWGetMetrics().textMargin
; // and for the last field
247 if ( HasFlag(wxSTB_SIZEGRIP
) )
248 widthAvailable
-= MSWGetMetrics().gripWidth
;
250 // distribute the available space (client width) among the various fields:
252 wxArrayInt widthsAbs
= CalculateAbsWidths(widthAvailable
);
255 // update the field widths in the native control:
257 int *pWidths
= new int[count
];
260 for ( int i
= 0; i
< count
; i
++ )
262 nCurPos
+= widthsAbs
[i
] + extraWidth
;
263 pWidths
[i
] = nCurPos
;
266 if ( !StatusBar_SetParts(GetHwnd(), count
, pWidths
) )
268 wxLogLastError("StatusBar_SetParts");
274 // FIXME: we may want to call DoUpdateStatusText() here since we may need to (de)ellipsize status texts
277 void wxStatusBar::DoUpdateStatusText(int nField
)
282 // Get field style, if any
284 switch(m_panes
[nField
].GetStyle())
290 style
= SBT_NOBORDERS
;
300 GetFieldRect(nField
, rc
);
302 const int maxWidth
= rc
.GetWidth() - MSWGetMetrics().textMargin
;
304 wxString text
= GetStatusText(nField
);
306 // do we need to ellipsize this string?
307 wxEllipsizeMode ellmode
= (wxEllipsizeMode
)-1;
308 if (HasFlag(wxSTB_ELLIPSIZE_START
)) ellmode
= wxELLIPSIZE_START
;
309 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE
)) ellmode
= wxELLIPSIZE_MIDDLE
;
310 else if (HasFlag(wxSTB_ELLIPSIZE_END
)) ellmode
= wxELLIPSIZE_END
;
312 if (ellmode
== (wxEllipsizeMode
)-1)
314 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
315 // we don't ellipsize the text but just truncate it
316 if (HasFlag(wxSTB_SHOW_TIPS
))
317 SetEllipsizedFlag(nField
, m_pDC
->GetTextExtent(text
).GetWidth() > maxWidth
);
321 text
= wxControl::Ellipsize(text
,
325 wxELLIPSIZE_FLAGS_EXPAND_TABS
);
327 // update the ellipsization status for this pane; this is used later to
328 // decide whether a tooltip should be shown or not for this pane
329 // (if we have wxSTB_SHOW_TIPS)
330 SetEllipsizedFlag(nField
, text
!= GetStatusText(nField
));
333 // Set the status text in the native control passing both field number and style.
334 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
335 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, text
.wx_str()) )
337 wxLogLastError("StatusBar_SetText");
340 if (HasFlag(wxSTB_SHOW_TIPS
))
342 wxASSERT(m_tooltips
.size() == m_panes
.GetCount());
344 if (m_tooltips
[nField
])
346 if (GetField(nField
).IsEllipsized())
348 // update the rect of this tooltip:
349 m_tooltips
[nField
]->SetRect(rc
);
351 // update also the text:
352 m_tooltips
[nField
]->SetTip(GetStatusText(nField
));
356 // delete the tooltip associated with this pane; it's not needed anymore
357 delete m_tooltips
[nField
];
358 m_tooltips
[nField
] = NULL
;
363 // create a new tooltip for this pane if needed
364 if (GetField(nField
).IsEllipsized())
365 m_tooltips
[nField
] = new wxToolTip(this, nField
, GetStatusText(nField
), rc
);
366 //else: leave m_tooltips[nField]==NULL
371 wxStatusBar::MSWBorders
wxStatusBar::MSWGetBorders() const
374 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
377 borders
.horz
= aBorders
[0];
378 borders
.vert
= aBorders
[1];
379 borders
.between
= aBorders
[2];
383 int wxStatusBar::GetBorderX() const
385 return MSWGetBorders().horz
;
388 int wxStatusBar::GetBorderY() const
390 return MSWGetBorders().vert
;
393 int wxStatusBar::MSWGetBorderWidth() const
395 return MSWGetBorders().between
;
399 const wxStatusBar::MSWMetrics
& wxStatusBar::MSWGetMetrics()
401 static MSWMetrics s_metrics
= { 0 };
402 if ( !s_metrics
.textMargin
)
404 // Grip size should be self explanatory (the only problem with it is
405 // that it's hard coded as we don't know how to find its size using
406 // API) but the margin might merit an explanation: Windows offsets the
407 // text drawn in status bar panes so we need to take this extra margin
408 // into account to make sure the text drawn by user fits inside the
409 // pane. Notice that it's not the value returned by SB_GETBORDERS
410 // which, at least on this Windows 2003 system, returns {0, 2, 2}
411 if ( wxUxThemeEngine::GetIfActive() )
413 s_metrics
.gripWidth
= 20;
414 s_metrics
.textMargin
= 8;
416 else // classic/unthemed look
418 s_metrics
.gripWidth
= 18;
419 s_metrics
.textMargin
= 4;
426 void wxStatusBar::SetMinHeight(int height
)
428 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
+ 2*GetBorderY(), 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
);
464 // Windows seems to under-report the size of the last field rectangle,
465 // presumably in order to prevent the buggy applications from overflowing
466 // onto the size grip but we want to return the real size to wx users
467 if ( HasFlag(wxSTB_SIZEGRIP
) && i
== (int)m_panes
.GetCount() - 1 )
469 rect
.width
+= MSWGetMetrics().gripWidth
- MSWGetBorderWidth();
475 wxSize
wxStatusBar::DoGetBestSize() const
477 const MSWBorders borders
= MSWGetBorders();
481 for ( size_t i
= 0; i
< m_panes
.GetCount(); ++i
)
484 m_bSameWidthForAllPanes
? DEFAULT_FIELD_WIDTH
: m_panes
[i
].GetWidth();
485 if ( widthField
>= 0 )
491 // variable width field, its width is really a proportion
492 // related to the other fields
493 width
+= -widthField
*DEFAULT_FIELD_WIDTH
;
496 // add the space between fields
497 width
+= borders
.between
;
502 // still need something even for an empty status bar
503 width
= 2*DEFAULT_FIELD_WIDTH
;
508 wxGetCharSize(GetHWND(), NULL
, &height
, GetFont());
509 height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(height
);
510 height
+= borders
.vert
;
512 wxSize
best(width
, height
);
517 void wxStatusBar::DoMoveWindow(int x
, int y
, int width
, int height
)
519 if ( GetParent()->IsSizeDeferred() )
521 wxWindowMSW::DoMoveWindow(x
, y
, width
, height
);
525 // parent pos/size isn't deferred so do it now but don't send
526 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
527 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
528 // if other windows are size deferred
529 ::SetWindowPos(GetHwnd(), NULL
, x
, y
, width
, height
,
530 SWP_NOZORDER
| SWP_NOOWNERZORDER
| SWP_NOACTIVATE
532 | SWP_NOCOPYBITS
| SWP_NOSENDCHANGING
537 // adjust fields widths to the new size
538 MSWUpdateFieldsWidths();
540 // we have to trigger wxSizeEvent if there are children window in status
541 // bar because GetFieldRect returned incorrect (not updated) values up to
542 // here, which almost certainly resulted in incorrectly redrawn statusbar
543 if ( m_children
.GetCount() > 0 )
545 wxSizeEvent
event(GetClientSize(), m_windowId
);
546 event
.SetEventObject(this);
547 HandleWindowEvent(event
);
551 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
553 wxStatusBarBase::SetStatusStyles(n
, styles
);
555 if (n
!= (int)m_panes
.GetCount())
558 for (int i
= 0; i
< n
; i
++)
567 style
= SBT_NOBORDERS
;
575 // The SB_SETTEXT message is both used to set the field's text as well as
576 // the fields' styles.
577 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
578 wxString text
= GetStatusText(i
);
579 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.wx_str()))
581 wxLogLastError("StatusBar_SetText");
587 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
590 if ( nMsg
== WM_WINDOWPOSCHANGING
)
592 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
597 // we need real window coords and not wx client coords
598 AdjustForParentClientOrigin(x
, y
);
608 if ( nMsg
== WM_NCLBUTTONDOWN
)
610 // if hit-test is on gripper then send message to TLW to begin
611 // resizing. It is possible to send this message to any window.
612 if ( wParam
== HTBOTTOMRIGHT
)
616 for ( win
= GetParent(); win
; win
= win
->GetParent() )
618 if ( win
->IsTopLevel() )
620 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
630 bool needsEllipsization
= HasFlag(wxSTB_ELLIPSIZE_START
) ||
631 HasFlag(wxSTB_ELLIPSIZE_MIDDLE
) ||
632 HasFlag(wxSTB_ELLIPSIZE_END
);
633 if ( nMsg
== WM_SIZE
&& needsEllipsization
)
635 for (int i
=0; i
<GetFieldsCount(); i
++)
636 DoUpdateStatusText(i
);
637 // re-set the field text, in case we need to ellipsize
638 // (or de-ellipsize) some parts of it
641 return wxStatusBarBase::MSWWindowProc(nMsg
, wParam
, lParam
);
645 bool wxStatusBar::MSWProcessMessage(WXMSG
* pMsg
)
647 if ( HasFlag(wxSTB_SHOW_TIPS
) )
649 // for a tooltip to be shown, we need to relay mouse events to it;
650 // this is typically done by wxWindowMSW::MSWProcessMessage but only
651 // if wxWindow::m_tooltip pointer is non-NULL.
652 // Since wxStatusBar has multiple tooltips for a single HWND, it keeps
653 // wxWindow::m_tooltip == NULL and then relays mouse events here:
654 MSG
*msg
= (MSG
*)pMsg
;
655 if ( msg
->message
== WM_MOUSEMOVE
)
656 wxToolTip::RelayEvent(pMsg
);
659 return wxWindow::MSWProcessMessage(pMsg
);
662 bool wxStatusBar::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
* WXUNUSED(result
))
664 if ( HasFlag(wxSTB_SHOW_TIPS
) )
666 // see comment in wxStatusBar::MSWProcessMessage for more info;
667 // basically we need to override wxWindow::MSWOnNotify because
668 // we have wxWindow::m_tooltip always NULL but we still use tooltips...
670 NMHDR
* hdr
= (NMHDR
*)lParam
;
673 if (hdr
->idFrom
< m_tooltips
.size() && m_tooltips
[hdr
->idFrom
])
674 str
= m_tooltips
[hdr
->idFrom
]->GetTip();
676 if ( HandleTooltipNotify(hdr
->code
, lParam
, str
))
685 #endif // wxUSE_TOOLTIPS
687 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR