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
++)
182 wxDELETE(m_tooltips
[i
]);
188 bool wxStatusBar::SetFont(const wxFont
& font
)
190 if (!wxWindow::SetFont(font
))
193 if (m_pDC
) m_pDC
->SetFont(font
);
197 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
199 // this is a Windows limitation
200 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), "too many fields" );
202 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
204 MSWUpdateFieldsWidths();
206 // keep in synch also our m_tooltips array
208 // reset all current tooltips
209 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
211 wxDELETE(m_tooltips
[i
]);
214 // shrink/expand the array:
215 m_tooltips
.resize(m_panes
.GetCount(), NULL
);
218 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
220 wxStatusBarBase::SetStatusWidths(n
, widths
);
222 MSWUpdateFieldsWidths();
225 void wxStatusBar::MSWUpdateFieldsWidths()
227 if ( m_panes
.IsEmpty() )
230 const int count
= m_panes
.GetCount();
232 const int extraWidth
= MSWGetBorderWidth() + MSWGetMetrics().textMargin
;
234 // compute the effectively available amount of space:
235 int widthAvailable
= GetClientSize().x
; // start with the entire width
236 widthAvailable
-= extraWidth
*(count
- 1); // extra space between fields
237 widthAvailable
-= MSWGetMetrics().textMargin
; // and for the last field
239 if ( HasFlag(wxSTB_SIZEGRIP
) )
240 widthAvailable
-= MSWGetMetrics().gripWidth
;
242 // distribute the available space (client width) among the various fields:
244 wxArrayInt widthsAbs
= CalculateAbsWidths(widthAvailable
);
247 // update the field widths in the native control:
249 int *pWidths
= new int[count
];
252 for ( int i
= 0; i
< count
; i
++ )
254 nCurPos
+= widthsAbs
[i
] + extraWidth
;
255 pWidths
[i
] = nCurPos
;
258 if ( !StatusBar_SetParts(GetHwnd(), count
, pWidths
) )
260 wxLogLastError("StatusBar_SetParts");
266 // FIXME: we may want to call DoUpdateStatusText() here since we may need to (de)ellipsize status texts
269 void wxStatusBar::DoUpdateStatusText(int nField
)
274 // Get field style, if any
276 switch(m_panes
[nField
].GetStyle())
282 style
= SBT_NOBORDERS
;
292 GetFieldRect(nField
, rc
);
294 const int maxWidth
= rc
.GetWidth() - MSWGetMetrics().textMargin
;
296 wxString text
= GetStatusText(nField
);
298 // do we need to ellipsize this string?
299 wxEllipsizeMode ellmode
= (wxEllipsizeMode
)-1;
300 if (HasFlag(wxSTB_ELLIPSIZE_START
)) ellmode
= wxELLIPSIZE_START
;
301 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE
)) ellmode
= wxELLIPSIZE_MIDDLE
;
302 else if (HasFlag(wxSTB_ELLIPSIZE_END
)) ellmode
= wxELLIPSIZE_END
;
304 if (ellmode
== (wxEllipsizeMode
)-1)
306 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
307 // we don't ellipsize the text but just truncate it
308 if (HasFlag(wxSTB_SHOW_TIPS
))
309 SetEllipsizedFlag(nField
, m_pDC
->GetTextExtent(text
).GetWidth() > maxWidth
);
313 text
= wxControl::Ellipsize(text
,
317 wxELLIPSIZE_FLAGS_EXPAND_TABS
);
319 // update the ellipsization status for this pane; this is used later to
320 // decide whether a tooltip should be shown or not for this pane
321 // (if we have wxSTB_SHOW_TIPS)
322 SetEllipsizedFlag(nField
, text
!= GetStatusText(nField
));
325 // Set the status text in the native control passing both field number and style.
326 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
327 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, text
.wx_str()) )
329 wxLogLastError("StatusBar_SetText");
332 if (HasFlag(wxSTB_SHOW_TIPS
))
334 wxASSERT(m_tooltips
.size() == m_panes
.GetCount());
336 if (m_tooltips
[nField
])
338 if (GetField(nField
).IsEllipsized())
340 // update the rect of this tooltip:
341 m_tooltips
[nField
]->SetRect(rc
);
343 // update also the text:
344 m_tooltips
[nField
]->SetTip(GetStatusText(nField
));
348 // delete the tooltip associated with this pane; it's not needed anymore
349 wxDELETE(m_tooltips
[nField
]);
354 // create a new tooltip for this pane if needed
355 if (GetField(nField
).IsEllipsized())
356 m_tooltips
[nField
] = new wxToolTip(this, nField
, GetStatusText(nField
), rc
);
357 //else: leave m_tooltips[nField]==NULL
362 wxStatusBar::MSWBorders
wxStatusBar::MSWGetBorders() const
365 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
368 borders
.horz
= aBorders
[0];
369 borders
.vert
= aBorders
[1];
370 borders
.between
= aBorders
[2];
374 int wxStatusBar::GetBorderX() const
376 return MSWGetBorders().horz
;
379 int wxStatusBar::GetBorderY() const
381 return MSWGetBorders().vert
;
384 int wxStatusBar::MSWGetBorderWidth() const
386 return MSWGetBorders().between
;
390 const wxStatusBar::MSWMetrics
& wxStatusBar::MSWGetMetrics()
392 static MSWMetrics s_metrics
= { 0 };
393 if ( !s_metrics
.textMargin
)
395 // Grip size should be self explanatory (the only problem with it is
396 // that it's hard coded as we don't know how to find its size using
397 // API) but the margin might merit an explanation: Windows offsets the
398 // text drawn in status bar panes so we need to take this extra margin
399 // into account to make sure the text drawn by user fits inside the
400 // pane. Notice that it's not the value returned by SB_GETBORDERS
401 // which, at least on this Windows 2003 system, returns {0, 2, 2}
402 if ( wxUxThemeEngine::GetIfActive() )
404 s_metrics
.gripWidth
= 20;
405 s_metrics
.textMargin
= 8;
407 else // classic/unthemed look
409 s_metrics
.gripWidth
= 18;
410 s_metrics
.textMargin
= 4;
417 void wxStatusBar::SetMinHeight(int height
)
419 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
+ 2*GetBorderY(), 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
);
455 // Windows seems to under-report the size of the last field rectangle,
456 // presumably in order to prevent the buggy applications from overflowing
457 // onto the size grip but we want to return the real size to wx users
458 if ( HasFlag(wxSTB_SIZEGRIP
) && i
== (int)m_panes
.GetCount() - 1 )
460 rect
.width
+= MSWGetMetrics().gripWidth
- MSWGetBorderWidth();
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
;
499 wxGetCharSize(GetHWND(), NULL
, &height
, GetFont());
500 height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(height
);
501 height
+= 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 // adjust fields widths to the new size
529 MSWUpdateFieldsWidths();
531 // we have to trigger wxSizeEvent if there are children window in status
532 // bar because GetFieldRect returned incorrect (not updated) values up to
533 // here, which almost certainly resulted in incorrectly redrawn statusbar
534 if ( m_children
.GetCount() > 0 )
536 wxSizeEvent
event(GetClientSize(), m_windowId
);
537 event
.SetEventObject(this);
538 HandleWindowEvent(event
);
542 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
544 wxStatusBarBase::SetStatusStyles(n
, styles
);
546 if (n
!= (int)m_panes
.GetCount())
549 for (int i
= 0; i
< n
; i
++)
558 style
= SBT_NOBORDERS
;
566 // The SB_SETTEXT message is both used to set the field's text as well as
567 // the fields' styles.
568 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
569 wxString text
= GetStatusText(i
);
570 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.wx_str()))
572 wxLogLastError("StatusBar_SetText");
578 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
581 if ( nMsg
== WM_WINDOWPOSCHANGING
)
583 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
588 // we need real window coords and not wx client coords
589 AdjustForParentClientOrigin(x
, y
);
599 if ( nMsg
== WM_NCLBUTTONDOWN
)
601 // if hit-test is on gripper then send message to TLW to begin
602 // resizing. It is possible to send this message to any window.
603 if ( wParam
== HTBOTTOMRIGHT
)
607 for ( win
= GetParent(); win
; win
= win
->GetParent() )
609 if ( win
->IsTopLevel() )
611 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
621 bool needsEllipsization
= HasFlag(wxSTB_ELLIPSIZE_START
) ||
622 HasFlag(wxSTB_ELLIPSIZE_MIDDLE
) ||
623 HasFlag(wxSTB_ELLIPSIZE_END
);
624 if ( nMsg
== WM_SIZE
&& needsEllipsization
)
626 for (int i
=0; i
<GetFieldsCount(); i
++)
627 DoUpdateStatusText(i
);
628 // re-set the field text, in case we need to ellipsize
629 // (or de-ellipsize) some parts of it
632 return wxStatusBarBase::MSWWindowProc(nMsg
, wParam
, lParam
);
636 bool wxStatusBar::MSWProcessMessage(WXMSG
* pMsg
)
638 if ( HasFlag(wxSTB_SHOW_TIPS
) )
640 // for a tooltip to be shown, we need to relay mouse events to it;
641 // this is typically done by wxWindowMSW::MSWProcessMessage but only
642 // if wxWindow::m_tooltip pointer is non-NULL.
643 // Since wxStatusBar has multiple tooltips for a single HWND, it keeps
644 // wxWindow::m_tooltip == NULL and then relays mouse events here:
645 MSG
*msg
= (MSG
*)pMsg
;
646 if ( msg
->message
== WM_MOUSEMOVE
)
647 wxToolTip::RelayEvent(pMsg
);
650 return wxWindow::MSWProcessMessage(pMsg
);
653 bool wxStatusBar::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
* WXUNUSED(result
))
655 if ( HasFlag(wxSTB_SHOW_TIPS
) )
657 // see comment in wxStatusBar::MSWProcessMessage for more info;
658 // basically we need to override wxWindow::MSWOnNotify because
659 // we have wxWindow::m_tooltip always NULL but we still use tooltips...
661 NMHDR
* hdr
= (NMHDR
*)lParam
;
664 if (hdr
->idFrom
< m_tooltips
.size() && m_tooltips
[hdr
->idFrom
])
665 str
= m_tooltips
[hdr
->idFrom
]->GetTip();
667 if ( HandleTooltipNotify(hdr
->code
, lParam
, str
))
676 #endif // wxUSE_TOOLTIPS
678 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR