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 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
108 // (at least in the version of comctl32.dll I'm using), and the only way to
109 // turn it off is to use CCS_TOP style - as we position the status bar
110 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxSTB_SIZEGRIP
112 if ( !(style
& wxSTB_SIZEGRIP
) )
119 // may be some versions of comctl32.dll do need it - anyhow, it won't
121 wstyle
|= SBARS_SIZEGRIP
;
125 m_hWnd
= CreateWindow
132 (HMENU
)wxUIntToPtr(m_windowId
.GetValue()),
138 wxLogSysError(_("Failed to create a status bar."));
146 // cache the DC instance used by DoUpdateStatusText:
147 // NOTE: create the DC before calling InheritAttributes() since
148 // it may result in a call to our SetFont()
149 m_pDC
= new wxClientDC(this);
153 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
155 // we must refresh the frame size when the statusbar is created, because
156 // its client area might change
158 // notice that we must post the event, not send it, as the frame doesn't
159 // know that we're its status bar yet so laying it out right now wouldn't
160 // work correctly, we need to wait until we return to the main loop
161 PostSizeEventToParent();
166 wxStatusBar::~wxStatusBar()
168 // we must refresh the frame size when the statusbar is deleted but the
169 // frame is not - otherwise statusbar leaves a hole in the place it used to
171 PostSizeEventToParent();
173 // delete existing tooltips
174 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
178 delete m_tooltips
[i
];
179 m_tooltips
[i
] = NULL
;
186 bool wxStatusBar::SetFont(const wxFont
& font
)
188 if (!wxWindow::SetFont(font
))
191 if (m_pDC
) m_pDC
->SetFont(font
);
195 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
197 // this is a Windows limitation
198 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), "too many fields" );
200 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
202 MSWUpdateFieldsWidths();
204 // keep in synch also our m_tooltips array
206 // reset all current tooltips
207 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
211 delete m_tooltips
[i
];
212 m_tooltips
[i
] = NULL
;
216 // shrink/expand the array:
217 m_tooltips
.resize(m_panes
.GetCount(), NULL
);
220 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
222 wxStatusBarBase::SetStatusWidths(n
, widths
);
224 MSWUpdateFieldsWidths();
227 void wxStatusBar::MSWUpdateFieldsWidths()
229 if ( m_panes
.IsEmpty() )
232 const int count
= m_panes
.GetCount();
234 const int extraWidth
= MSWGetBorderWidth() + MSWGetMetrics().textMargin
;
236 // compute the effectively available amount of space:
237 int widthAvailable
= GetClientSize().x
; // start with the entire width
238 widthAvailable
-= extraWidth
*(count
- 1); // extra space between fields
239 widthAvailable
-= MSWGetMetrics().textMargin
; // and for the last field
241 if ( HasFlag(wxSTB_SIZEGRIP
) )
242 widthAvailable
-= MSWGetMetrics().gripWidth
;
244 // distribute the available space (client width) among the various fields:
246 wxArrayInt widthsAbs
= CalculateAbsWidths(widthAvailable
);
249 // update the field widths in the native control:
251 int *pWidths
= new int[count
];
254 for ( int i
= 0; i
< count
; i
++ )
256 nCurPos
+= widthsAbs
[i
] + extraWidth
;
257 pWidths
[i
] = nCurPos
;
260 if ( !StatusBar_SetParts(GetHwnd(), count
, pWidths
) )
262 wxLogLastError("StatusBar_SetParts");
268 // FIXME: we may want to call DoUpdateStatusText() here since we may need to (de)ellipsize status texts
271 void wxStatusBar::DoUpdateStatusText(int nField
)
276 // Get field style, if any
278 switch(m_panes
[nField
].GetStyle())
284 style
= SBT_NOBORDERS
;
294 GetFieldRect(nField
, rc
);
296 const int maxWidth
= rc
.GetWidth() - MSWGetMetrics().textMargin
;
298 wxString text
= GetStatusText(nField
);
300 // do we need to ellipsize this string?
301 wxEllipsizeMode ellmode
= (wxEllipsizeMode
)-1;
302 if (HasFlag(wxSTB_ELLIPSIZE_START
)) ellmode
= wxELLIPSIZE_START
;
303 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE
)) ellmode
= wxELLIPSIZE_MIDDLE
;
304 else if (HasFlag(wxSTB_ELLIPSIZE_END
)) ellmode
= wxELLIPSIZE_END
;
306 if (ellmode
== (wxEllipsizeMode
)-1)
308 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
309 // we don't ellipsize the text but just truncate it
310 if (HasFlag(wxSTB_SHOW_TIPS
))
311 SetEllipsizedFlag(nField
, m_pDC
->GetTextExtent(text
).GetWidth() > maxWidth
);
315 text
= wxControl::Ellipsize(text
,
319 wxELLIPSIZE_FLAGS_EXPAND_TABS
);
321 // update the ellipsization status for this pane; this is used later to
322 // decide whether a tooltip should be shown or not for this pane
323 // (if we have wxSTB_SHOW_TIPS)
324 SetEllipsizedFlag(nField
, text
!= GetStatusText(nField
));
327 // Set the status text in the native control passing both field number and style.
328 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
329 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, text
.wx_str()) )
331 wxLogLastError("StatusBar_SetText");
334 if (HasFlag(wxSTB_SHOW_TIPS
))
336 wxASSERT(m_tooltips
.size() == m_panes
.GetCount());
338 if (m_tooltips
[nField
])
340 if (GetField(nField
).IsEllipsized())
342 // update the rect of this tooltip:
343 m_tooltips
[nField
]->SetRect(rc
);
345 // update also the text:
346 m_tooltips
[nField
]->SetTip(GetStatusText(nField
));
350 // delete the tooltip associated with this pane; it's not needed anymore
351 delete m_tooltips
[nField
];
352 m_tooltips
[nField
] = NULL
;
357 // create a new tooltip for this pane if needed
358 if (GetField(nField
).IsEllipsized())
359 m_tooltips
[nField
] = new wxToolTip(this, nField
, GetStatusText(nField
), rc
);
360 //else: leave m_tooltips[nField]==NULL
365 wxStatusBar::MSWBorders
wxStatusBar::MSWGetBorders() const
368 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
371 borders
.horz
= aBorders
[0];
372 borders
.vert
= aBorders
[1];
373 borders
.between
= aBorders
[2];
377 int wxStatusBar::GetBorderX() const
379 return MSWGetBorders().horz
;
382 int wxStatusBar::GetBorderY() const
384 return MSWGetBorders().vert
;
387 int wxStatusBar::MSWGetBorderWidth() const
389 return MSWGetBorders().between
;
393 const wxStatusBar::MSWMetrics
& wxStatusBar::MSWGetMetrics()
395 static MSWMetrics s_metrics
= { 0 };
396 if ( !s_metrics
.textMargin
)
398 // Grip size should be self explanatory (the only problem with it is
399 // that it's hard coded as we don't know how to find its size using
400 // API) but the margin might merit an explanation: Windows offsets the
401 // text drawn in status bar panes so we need to take this extra margin
402 // into account to make sure the text drawn by user fits inside the
403 // pane. Notice that it's not the value returned by SB_GETBORDERS
404 // which, at least on this Windows 2003 system, returns {0, 2, 2}
405 if ( wxUxThemeEngine::GetIfActive() )
407 s_metrics
.gripWidth
= 20;
408 s_metrics
.textMargin
= 8;
410 else // classic/unthemed look
412 s_metrics
.gripWidth
= 18;
413 s_metrics
.textMargin
= 4;
420 void wxStatusBar::SetMinHeight(int height
)
422 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
+ 2*GetBorderY(), 0);
424 // we have to send a (dummy) WM_SIZE to redraw it now
425 SendMessage(GetHwnd(), WM_SIZE
, 0, 0);
428 bool wxStatusBar::GetFieldRect(int i
, wxRect
& rect
) const
430 wxCHECK_MSG( (i
>= 0) && ((size_t)i
< m_panes
.GetCount()), false,
431 "invalid statusbar field index" );
434 if ( !::SendMessage(GetHwnd(), SB_GETRECT
, i
, (LPARAM
)&r
) )
436 wxLogLastError("SendMessage(SB_GETRECT)");
440 wxUxThemeHandle
theme(const_cast<wxStatusBar
*>(this), L
"Status");
443 // by default Windows has a 2 pixel border to the right of the left
444 // divider (or it could be a bug) but it looks wrong so remove it
450 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme
, NULL
,
456 wxCopyRECTToRect(r
, rect
);
458 // Windows seems to under-report the size of the last field rectangle,
459 // presumably in order to prevent the buggy applications from overflowing
460 // onto the size grip but we want to return the real size to wx users
461 if ( HasFlag(wxSTB_SIZEGRIP
) && i
== (int)m_panes
.GetCount() - 1 )
463 rect
.width
+= MSWGetMetrics().gripWidth
- MSWGetBorderWidth();
469 wxSize
wxStatusBar::DoGetBestSize() const
471 const MSWBorders borders
= MSWGetBorders();
475 for ( size_t i
= 0; i
< m_panes
.GetCount(); ++i
)
478 m_bSameWidthForAllPanes
? DEFAULT_FIELD_WIDTH
: m_panes
[i
].GetWidth();
479 if ( widthField
>= 0 )
485 // variable width field, its width is really a proportion
486 // related to the other fields
487 width
+= -widthField
*DEFAULT_FIELD_WIDTH
;
490 // add the space between fields
491 width
+= borders
.between
;
496 // still need something even for an empty status bar
497 width
= 2*DEFAULT_FIELD_WIDTH
;
502 wxGetCharSize(GetHWND(), NULL
, &height
, GetFont());
503 height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(height
);
504 height
+= borders
.vert
;
506 wxSize
best(width
, height
);
511 void wxStatusBar::DoMoveWindow(int x
, int y
, int width
, int height
)
513 if ( GetParent()->IsSizeDeferred() )
515 wxWindowMSW::DoMoveWindow(x
, y
, width
, height
);
519 // parent pos/size isn't deferred so do it now but don't send
520 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
521 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
522 // if other windows are size deferred
523 ::SetWindowPos(GetHwnd(), NULL
, x
, y
, width
, height
,
524 SWP_NOZORDER
| SWP_NOOWNERZORDER
| SWP_NOACTIVATE
526 | SWP_NOCOPYBITS
| SWP_NOSENDCHANGING
531 // adjust fields widths to the new size
532 MSWUpdateFieldsWidths();
534 // we have to trigger wxSizeEvent if there are children window in status
535 // bar because GetFieldRect returned incorrect (not updated) values up to
536 // here, which almost certainly resulted in incorrectly redrawn statusbar
537 if ( m_children
.GetCount() > 0 )
539 wxSizeEvent
event(GetClientSize(), m_windowId
);
540 event
.SetEventObject(this);
541 HandleWindowEvent(event
);
545 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
547 wxStatusBarBase::SetStatusStyles(n
, styles
);
549 if (n
!= (int)m_panes
.GetCount())
552 for (int i
= 0; i
< n
; i
++)
561 style
= SBT_NOBORDERS
;
569 // The SB_SETTEXT message is both used to set the field's text as well as
570 // the fields' styles.
571 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
572 wxString text
= GetStatusText(i
);
573 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.wx_str()))
575 wxLogLastError("StatusBar_SetText");
581 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
584 if ( nMsg
== WM_WINDOWPOSCHANGING
)
586 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
591 // we need real window coords and not wx client coords
592 AdjustForParentClientOrigin(x
, y
);
602 if ( nMsg
== WM_NCLBUTTONDOWN
)
604 // if hit-test is on gripper then send message to TLW to begin
605 // resizing. It is possible to send this message to any window.
606 if ( wParam
== HTBOTTOMRIGHT
)
610 for ( win
= GetParent(); win
; win
= win
->GetParent() )
612 if ( win
->IsTopLevel() )
614 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
624 bool needsEllipsization
= HasFlag(wxSTB_ELLIPSIZE_START
) ||
625 HasFlag(wxSTB_ELLIPSIZE_MIDDLE
) ||
626 HasFlag(wxSTB_ELLIPSIZE_END
);
627 if ( nMsg
== WM_SIZE
&& needsEllipsization
)
629 for (int i
=0; i
<GetFieldsCount(); i
++)
630 DoUpdateStatusText(i
);
631 // re-set the field text, in case we need to ellipsize
632 // (or de-ellipsize) some parts of it
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