]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statusbar.cpp
b057c0e260c3a5e8e0befdba236017396293e1ad
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 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
21 #include "wx/statusbr.h"
24 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
26 #include "wx/settings.h"
27 #include "wx/dcclient.h"
30 #include "wx/control.h"
33 #include "wx/msw/private.h"
34 #include "wx/tooltip.h"
38 #include "wx/msw/uxtheme.h"
41 // no idea for a default width, just choose something
42 #define DEFAULT_FIELD_WIDTH 25
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // windowsx.h and commctrl.h don't define those, so we do it here
49 #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w)
50 #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
51 #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
52 #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 wxStatusBar::wxStatusBar()
70 bool wxStatusBar::Create(wxWindow
*parent
,
75 wxCHECK_MSG( parent
, false, "status bar must have a parent" );
78 SetWindowStyleFlag(style
);
81 parent
->AddChild(this);
83 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
85 DWORD wstyle
= WS_CHILD
| WS_VISIBLE
;
87 if ( style
& wxCLIP_SIBLINGS
)
88 wstyle
|= WS_CLIPSIBLINGS
;
90 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
91 // (at least in the version of comctl32.dll I'm using), and the only way to
92 // turn it off is to use CCS_TOP style - as we position the status bar
93 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxSTB_SIZEGRIP
95 if ( !(style
& wxSTB_SIZEGRIP
) )
102 // may be some versions of comctl32.dll do need it - anyhow, it won't
104 wstyle
|= SBARS_SIZEGRIP
;
108 m_hWnd
= CreateWindow
115 (HMENU
)wxUIntToPtr(m_windowId
.GetValue()),
121 wxLogSysError(_("Failed to create a status bar."));
129 // cache the DC instance used by UpdateFieldText:
130 // NOTE: create the DC before calling InheritAttributes() since
131 // it may result in a call to our SetFont()
132 m_pDC
= new wxClientDC(this);
136 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
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();
159 bool wxStatusBar::SetFont(const wxFont
& font
)
161 if (!wxWindow::SetFont(font
))
164 if (m_pDC
) m_pDC
->SetFont(font
);
168 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
170 // this is a Windows limitation
171 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), "too many fields" );
173 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
177 // keep in synch also our m_tooltips array
179 // reset all current tooltips
180 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
182 delete m_tooltips
[i
];
183 m_tooltips
[i
] = NULL
;
186 // shrink/expand the array:
187 m_tooltips
.resize(m_panes
.GetCount(), NULL
);
190 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
192 wxStatusBarBase::SetStatusWidths(n
, widths
);
197 void wxStatusBar::SetFieldsWidth()
199 if ( m_panes
.IsEmpty() )
203 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
205 int extraWidth
= aBorders
[2]; // space between fields
208 // distribute the available space (client width) among the various fields:
210 wxArrayInt widthsAbs
=
211 CalculateAbsWidths(GetClientSize().x
- extraWidth
*(m_panes
.GetCount() - 1));
214 // update the field widths in the native control:
216 int *pWidths
= new int[m_panes
.GetCount()];
219 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ )
221 nCurPos
+= widthsAbs
[i
] + extraWidth
;
222 pWidths
[i
] = nCurPos
;
225 if ( !StatusBar_SetParts(GetHwnd(), m_panes
.GetCount(), pWidths
) )
226 wxLogLastError("StatusBar_SetParts");
231 // FIXME: we may want to call UpdateFieldText() here since we may need to (de)ellipsize status texts
234 void wxStatusBar::SetStatusText(const wxString
& strText
, int nField
)
236 wxCHECK_RET( (nField
>= 0) && ((size_t)nField
< m_panes
.GetCount()),
237 "invalid statusbar field index" );
239 if ( strText
== GetStatusText(nField
) )
241 // don't call StatusBar_SetText() to avoid flicker
245 wxStatusBarBase::SetStatusText(strText
, nField
);
247 UpdateFieldText(nField
);
250 void wxStatusBar::UpdateFieldText(int nField
)
255 // Get field style, if any
257 switch(m_panes
[nField
].GetStyle())
263 style
= SBT_NOBORDERS
;
273 GetFieldRect(nField
, rc
);
276 if (nField
== GetFieldsCount()-1)
277 margin
= -6; // windows reports a smaller rect for the last field; enlarge it
281 int maxWidth
= rc
.GetWidth() - margin
; // leave a small margin
282 wxString text
= GetStatusText(nField
);
284 // do we need to ellipsize this string?
285 wxEllipsizeMode ellmode
= (wxEllipsizeMode
)-1;
286 if (HasFlag(wxSTB_ELLIPSIZE_START
)) ellmode
= wxELLIPSIZE_START
;
287 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE
)) ellmode
= wxELLIPSIZE_MIDDLE
;
288 else if (HasFlag(wxSTB_ELLIPSIZE_END
)) ellmode
= wxELLIPSIZE_END
;
290 if (ellmode
== (wxEllipsizeMode
)-1)
292 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
293 // we don't ellipsize the text but just truncate it
294 if (HasFlag(wxSTB_SHOW_TIPS
))
295 SetEllipsizedFlag(nField
, m_pDC
->GetTextExtent(text
).GetWidth() > maxWidth
);
299 text
= wxControl::Ellipsize(text
,
303 wxELLIPSIZE_EXPAND_TAB
);
305 // update the ellipsization status for this pane; this is used later to
306 // decide whether a tooltip should be shown or not for this pane
307 // (if we have wxSTB_SHOW_TIPS)
308 SetEllipsizedFlag(nField
, text
!= GetStatusText(nField
));
311 // Set the status text in the native control passing both field number and style.
312 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
313 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, text
.wx_str()) )
314 wxLogLastError("StatusBar_SetText");
316 if (HasFlag(wxSTB_SHOW_TIPS
))
318 wxASSERT(m_tooltips
.size() == m_panes
.GetCount());
320 if (m_tooltips
[nField
])
322 if (GetField(nField
).IsEllipsized())
324 // update the rect of this tooltip:
325 m_tooltips
[nField
]->SetRect(rc
);
327 // update also the text:
328 m_tooltips
[nField
]->SetTip(GetStatusText(nField
));
332 // delete the tooltip associated with this pane; it's not needed anymore
333 delete m_tooltips
[nField
];
334 m_tooltips
[nField
] = NULL
;
339 // create a new tooltip for this pane if needed
340 if (GetField(nField
).IsEllipsized())
341 m_tooltips
[nField
] = new wxToolTip(this, nField
, GetStatusText(nField
), rc
);
342 //else: leave m_tooltips[nField]==NULL
347 int wxStatusBar::GetBorderX() const
350 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
355 int wxStatusBar::GetBorderY() const
358 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
363 void wxStatusBar::SetMinHeight(int height
)
365 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
+ 2*GetBorderY(), 0);
367 // we have to send a (dummy) WM_SIZE to redraw it now
368 SendMessage(GetHwnd(), WM_SIZE
, 0, 0);
371 bool wxStatusBar::GetFieldRect(int i
, wxRect
& rect
) const
373 wxCHECK_MSG( (i
>= 0) && ((size_t)i
< m_panes
.GetCount()), false,
374 "invalid statusbar field index" );
377 if ( !::SendMessage(GetHwnd(), SB_GETRECT
, i
, (LPARAM
)&r
) )
378 wxLogLastError("SendMessage(SB_GETRECT)");
381 wxUxThemeHandle
theme(const_cast<wxStatusBar
*>(this), L
"Status");
384 // by default Windows has a 2 pixel border to the right of the left
385 // divider (or it could be a bug) but it looks wrong so remove it
391 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme
, NULL
,
397 wxCopyRECTToRect(r
, rect
);
402 wxSize
wxStatusBar::DoGetBestSize() const
405 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)borders
);
409 for ( size_t i
= 0; i
< m_panes
.GetCount(); ++i
)
412 m_bSameWidthForAllPanes
? DEFAULT_FIELD_WIDTH
: m_panes
[i
].GetWidth();
413 if ( widthField
>= 0 )
419 // variable width field, its width is really a proportion
420 // related to the other fields
421 width
+= -widthField
*DEFAULT_FIELD_WIDTH
;
424 // add the space between fields
430 // still need something even for an empty status bar
431 width
= 2*DEFAULT_FIELD_WIDTH
;
436 wxGetCharSize(GetHWND(), NULL
, &height
, GetFont());
437 height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(height
);
438 height
+= borders
[1];
440 wxSize
best(width
, height
);
445 void wxStatusBar::DoMoveWindow(int x
, int y
, int width
, int height
)
447 if ( GetParent()->IsSizeDeferred() )
449 wxWindowMSW::DoMoveWindow(x
, y
, width
, height
);
453 // parent pos/size isn't deferred so do it now but don't send
454 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
455 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
456 // if other windows are size deferred
457 ::SetWindowPos(GetHwnd(), NULL
, x
, y
, width
, height
,
458 SWP_NOZORDER
| SWP_NOOWNERZORDER
| SWP_NOACTIVATE
460 | SWP_NOCOPYBITS
| SWP_NOSENDCHANGING
465 // adjust fields widths to the new size
468 // we have to trigger wxSizeEvent if there are children window in status
469 // bar because GetFieldRect returned incorrect (not updated) values up to
470 // here, which almost certainly resulted in incorrectly redrawn statusbar
471 if ( m_children
.GetCount() > 0 )
473 wxSizeEvent
event(GetClientSize(), m_windowId
);
474 event
.SetEventObject(this);
475 HandleWindowEvent(event
);
479 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
481 wxStatusBarBase::SetStatusStyles(n
, styles
);
483 if (n
!= (int)m_panes
.GetCount())
486 for (int i
= 0; i
< n
; i
++)
495 style
= SBT_NOBORDERS
;
503 // The SB_SETTEXT message is both used to set the field's text as well as
504 // the fields' styles.
505 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
506 wxString text
= GetStatusText(i
);
507 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.wx_str()))
508 wxLogLastError("StatusBar_SetText");
513 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
516 if ( nMsg
== WM_WINDOWPOSCHANGING
)
518 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
523 // we need real window coords and not wx client coords
524 AdjustForParentClientOrigin(x
, y
);
534 if ( nMsg
== WM_NCLBUTTONDOWN
)
536 // if hit-test is on gripper then send message to TLW to begin
537 // resizing. It is possible to send this message to any window.
538 if ( wParam
== HTBOTTOMRIGHT
)
542 for ( win
= GetParent(); win
; win
= win
->GetParent() )
544 if ( win
->IsTopLevel() )
546 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
556 bool needsEllipsization
= HasFlag(wxSTB_ELLIPSIZE_START
) ||
557 HasFlag(wxSTB_ELLIPSIZE_MIDDLE
) ||
558 HasFlag(wxSTB_ELLIPSIZE_END
);
559 if ( nMsg
== WM_SIZE
&& needsEllipsization
)
561 for (int i
=0; i
<GetFieldsCount(); i
++)
563 // re-set the field text, in case we need to ellipsize
564 // (or de-ellipsize) some parts of it
567 return wxStatusBarBase::MSWWindowProc(nMsg
, wParam
, lParam
);
571 bool wxStatusBar::MSWProcessMessage(WXMSG
* pMsg
)
573 if ( HasFlag(wxSTB_SHOW_TIPS
) )
575 // for a tooltip to be shown, we need to relay mouse events to it;
576 // this is typically done by wxWindowMSW::MSWProcessMessage but only
577 // if wxWindow::m_tooltip pointer is non-NULL.
578 // Since wxStatusBar has multiple tooltips for a single HWND, it keeps
579 // wxWindow::m_tooltip == NULL and then relays mouse events here:
580 MSG
*msg
= (MSG
*)pMsg
;
581 if ( msg
->message
== WM_MOUSEMOVE
)
582 wxToolTip::RelayEvent(pMsg
);
585 return wxWindow::MSWProcessMessage(pMsg
);
588 bool wxStatusBar::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
* WXUNUSED(result
))
590 if ( HasFlag(wxSTB_SHOW_TIPS
) )
592 // see comment in wxStatusBar::MSWProcessMessage for more info;
593 // basically we need to override wxWindow::MSWOnNotify because
594 // we have wxWindow::m_tooltip always NULL but we still use tooltips...
596 NMHDR
* hdr
= (NMHDR
*)lParam
;
599 if (hdr
->idFrom
< m_tooltips
.size() && m_tooltips
[hdr
->idFrom
])
600 str
= m_tooltips
[hdr
->idFrom
]->GetTip();
602 if ( HandleTooltipNotify(hdr
->code
, lParam
, str
))
611 #endif // wxUSE_TOOLTIPS
613 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR