]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statusbar.cpp
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 DoUpdateStatusText:
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();
156 // delete existing tooltips
157 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
161 delete m_tooltips
[i
];
162 m_tooltips
[i
] = NULL
;
169 bool wxStatusBar::SetFont(const wxFont
& font
)
171 if (!wxWindow::SetFont(font
))
174 if (m_pDC
) m_pDC
->SetFont(font
);
178 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
180 // this is a Windows limitation
181 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), "too many fields" );
183 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
185 MSWUpdateFieldsWidths();
187 // keep in synch also our m_tooltips array
189 // reset all current tooltips
190 for (size_t i
=0; i
<m_tooltips
.size(); i
++)
194 delete m_tooltips
[i
];
195 m_tooltips
[i
] = NULL
;
199 // shrink/expand the array:
200 m_tooltips
.resize(m_panes
.GetCount(), NULL
);
203 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
205 wxStatusBarBase::SetStatusWidths(n
, widths
);
207 MSWUpdateFieldsWidths();
210 void wxStatusBar::MSWUpdateFieldsWidths()
212 if ( m_panes
.IsEmpty() )
216 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
218 int extraWidth
= aBorders
[2]; // space between fields
221 // distribute the available space (client width) among the various fields:
223 wxArrayInt widthsAbs
=
224 CalculateAbsWidths(GetClientSize().x
- extraWidth
*(m_panes
.GetCount() - 1));
227 // update the field widths in the native control:
229 int *pWidths
= new int[m_panes
.GetCount()];
232 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ )
234 nCurPos
+= widthsAbs
[i
] + extraWidth
;
235 pWidths
[i
] = nCurPos
;
238 if ( !StatusBar_SetParts(GetHwnd(), m_panes
.GetCount(), pWidths
) )
240 wxLogLastError("StatusBar_SetParts");
246 // FIXME: we may want to call DoUpdateStatusText() here since we may need to (de)ellipsize status texts
249 void wxStatusBar::DoUpdateStatusText(int nField
)
254 // Get field style, if any
256 switch(m_panes
[nField
].GetStyle())
262 style
= SBT_NOBORDERS
;
272 GetFieldRect(nField
, rc
);
275 if (nField
== GetFieldsCount()-1)
276 margin
= -6; // windows reports a smaller rect for the last field; enlarge it
280 int maxWidth
= rc
.GetWidth() - margin
; // leave a small margin
281 wxString text
= GetStatusText(nField
);
283 // do we need to ellipsize this string?
284 wxEllipsizeMode ellmode
= (wxEllipsizeMode
)-1;
285 if (HasFlag(wxSTB_ELLIPSIZE_START
)) ellmode
= wxELLIPSIZE_START
;
286 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE
)) ellmode
= wxELLIPSIZE_MIDDLE
;
287 else if (HasFlag(wxSTB_ELLIPSIZE_END
)) ellmode
= wxELLIPSIZE_END
;
289 if (ellmode
== (wxEllipsizeMode
)-1)
291 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
292 // we don't ellipsize the text but just truncate it
293 if (HasFlag(wxSTB_SHOW_TIPS
))
294 SetEllipsizedFlag(nField
, m_pDC
->GetTextExtent(text
).GetWidth() > maxWidth
);
298 text
= wxControl::Ellipsize(text
,
302 wxELLIPSIZE_EXPAND_TAB
);
304 // update the ellipsization status for this pane; this is used later to
305 // decide whether a tooltip should be shown or not for this pane
306 // (if we have wxSTB_SHOW_TIPS)
307 SetEllipsizedFlag(nField
, text
!= GetStatusText(nField
));
310 // Set the status text in the native control passing both field number and style.
311 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
312 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, text
.wx_str()) )
314 wxLogLastError("StatusBar_SetText");
317 if (HasFlag(wxSTB_SHOW_TIPS
))
319 wxASSERT(m_tooltips
.size() == m_panes
.GetCount());
321 if (m_tooltips
[nField
])
323 if (GetField(nField
).IsEllipsized())
325 // update the rect of this tooltip:
326 m_tooltips
[nField
]->SetRect(rc
);
328 // update also the text:
329 m_tooltips
[nField
]->SetTip(GetStatusText(nField
));
333 // delete the tooltip associated with this pane; it's not needed anymore
334 delete m_tooltips
[nField
];
335 m_tooltips
[nField
] = NULL
;
340 // create a new tooltip for this pane if needed
341 if (GetField(nField
).IsEllipsized())
342 m_tooltips
[nField
] = new wxToolTip(this, nField
, GetStatusText(nField
), rc
);
343 //else: leave m_tooltips[nField]==NULL
348 int wxStatusBar::GetBorderX() const
351 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
356 int wxStatusBar::GetBorderY() const
359 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
364 void wxStatusBar::SetMinHeight(int height
)
366 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
+ 2*GetBorderY(), 0);
368 // we have to send a (dummy) WM_SIZE to redraw it now
369 SendMessage(GetHwnd(), WM_SIZE
, 0, 0);
372 bool wxStatusBar::GetFieldRect(int i
, wxRect
& rect
) const
374 wxCHECK_MSG( (i
>= 0) && ((size_t)i
< m_panes
.GetCount()), false,
375 "invalid statusbar field index" );
378 if ( !::SendMessage(GetHwnd(), SB_GETRECT
, i
, (LPARAM
)&r
) )
380 wxLogLastError("SendMessage(SB_GETRECT)");
384 wxUxThemeHandle
theme(const_cast<wxStatusBar
*>(this), L
"Status");
387 // by default Windows has a 2 pixel border to the right of the left
388 // divider (or it could be a bug) but it looks wrong so remove it
394 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme
, NULL
,
400 wxCopyRECTToRect(r
, rect
);
405 wxSize
wxStatusBar::DoGetBestSize() const
408 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)borders
);
412 for ( size_t i
= 0; i
< m_panes
.GetCount(); ++i
)
415 m_bSameWidthForAllPanes
? DEFAULT_FIELD_WIDTH
: m_panes
[i
].GetWidth();
416 if ( widthField
>= 0 )
422 // variable width field, its width is really a proportion
423 // related to the other fields
424 width
+= -widthField
*DEFAULT_FIELD_WIDTH
;
427 // add the space between fields
433 // still need something even for an empty status bar
434 width
= 2*DEFAULT_FIELD_WIDTH
;
439 wxGetCharSize(GetHWND(), NULL
, &height
, GetFont());
440 height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(height
);
441 height
+= borders
[1];
443 wxSize
best(width
, height
);
448 void wxStatusBar::DoMoveWindow(int x
, int y
, int width
, int height
)
450 if ( GetParent()->IsSizeDeferred() )
452 wxWindowMSW::DoMoveWindow(x
, y
, width
, height
);
456 // parent pos/size isn't deferred so do it now but don't send
457 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
458 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
459 // if other windows are size deferred
460 ::SetWindowPos(GetHwnd(), NULL
, x
, y
, width
, height
,
461 SWP_NOZORDER
| SWP_NOOWNERZORDER
| SWP_NOACTIVATE
463 | SWP_NOCOPYBITS
| SWP_NOSENDCHANGING
468 // adjust fields widths to the new size
469 MSWUpdateFieldsWidths();
471 // we have to trigger wxSizeEvent if there are children window in status
472 // bar because GetFieldRect returned incorrect (not updated) values up to
473 // here, which almost certainly resulted in incorrectly redrawn statusbar
474 if ( m_children
.GetCount() > 0 )
476 wxSizeEvent
event(GetClientSize(), m_windowId
);
477 event
.SetEventObject(this);
478 HandleWindowEvent(event
);
482 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
484 wxStatusBarBase::SetStatusStyles(n
, styles
);
486 if (n
!= (int)m_panes
.GetCount())
489 for (int i
= 0; i
< n
; i
++)
498 style
= SBT_NOBORDERS
;
506 // The SB_SETTEXT message is both used to set the field's text as well as
507 // the fields' styles.
508 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
509 wxString text
= GetStatusText(i
);
510 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.wx_str()))
512 wxLogLastError("StatusBar_SetText");
518 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
521 if ( nMsg
== WM_WINDOWPOSCHANGING
)
523 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
528 // we need real window coords and not wx client coords
529 AdjustForParentClientOrigin(x
, y
);
539 if ( nMsg
== WM_NCLBUTTONDOWN
)
541 // if hit-test is on gripper then send message to TLW to begin
542 // resizing. It is possible to send this message to any window.
543 if ( wParam
== HTBOTTOMRIGHT
)
547 for ( win
= GetParent(); win
; win
= win
->GetParent() )
549 if ( win
->IsTopLevel() )
551 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
561 bool needsEllipsization
= HasFlag(wxSTB_ELLIPSIZE_START
) ||
562 HasFlag(wxSTB_ELLIPSIZE_MIDDLE
) ||
563 HasFlag(wxSTB_ELLIPSIZE_END
);
564 if ( nMsg
== WM_SIZE
&& needsEllipsization
)
566 for (int i
=0; i
<GetFieldsCount(); i
++)
567 DoUpdateStatusText(i
);
568 // re-set the field text, in case we need to ellipsize
569 // (or de-ellipsize) some parts of it
572 return wxStatusBarBase::MSWWindowProc(nMsg
, wParam
, lParam
);
576 bool wxStatusBar::MSWProcessMessage(WXMSG
* pMsg
)
578 if ( HasFlag(wxSTB_SHOW_TIPS
) )
580 // for a tooltip to be shown, we need to relay mouse events to it;
581 // this is typically done by wxWindowMSW::MSWProcessMessage but only
582 // if wxWindow::m_tooltip pointer is non-NULL.
583 // Since wxStatusBar has multiple tooltips for a single HWND, it keeps
584 // wxWindow::m_tooltip == NULL and then relays mouse events here:
585 MSG
*msg
= (MSG
*)pMsg
;
586 if ( msg
->message
== WM_MOUSEMOVE
)
587 wxToolTip::RelayEvent(pMsg
);
590 return wxWindow::MSWProcessMessage(pMsg
);
593 bool wxStatusBar::MSWOnNotify(int WXUNUSED(idCtrl
), WXLPARAM lParam
, WXLPARAM
* WXUNUSED(result
))
595 if ( HasFlag(wxSTB_SHOW_TIPS
) )
597 // see comment in wxStatusBar::MSWProcessMessage for more info;
598 // basically we need to override wxWindow::MSWOnNotify because
599 // we have wxWindow::m_tooltip always NULL but we still use tooltips...
601 NMHDR
* hdr
= (NMHDR
*)lParam
;
604 if (hdr
->idFrom
< m_tooltips
.size() && m_tooltips
[hdr
->idFrom
])
605 str
= m_tooltips
[hdr
->idFrom
]->GetTip();
607 if ( HandleTooltipNotify(hdr
->code
, lParam
, str
))
616 #endif // wxUSE_TOOLTIPS
618 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR