X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/936f635341e9b92b37db93cc7781d564babbe25f..69d31e313035d5e22d9400ec946f6007f710910c:/src/msw/statusbar.cpp?ds=inline diff --git a/src/msw/statusbar.cpp b/src/msw/statusbar.cpp index aef8e5c567..8b58819757 100644 --- a/src/msw/statusbar.cpp +++ b/src/msw/statusbar.cpp @@ -27,15 +27,20 @@ #include "wx/dcclient.h" #include "wx/intl.h" #include "wx/log.h" + #include "wx/control.h" #endif #include "wx/msw/private.h" +#include "wx/tooltip.h" #include #if wxUSE_UXTHEME #include "wx/msw/uxtheme.h" #endif +// no idea for a default width, just choose something +#define DEFAULT_FIELD_WIDTH 25 + // ---------------------------------------------------------------------------- // macros // ---------------------------------------------------------------------------- @@ -59,6 +64,7 @@ wxStatusBar::wxStatusBar() SetParent(NULL); m_hWnd = 0; m_windowId = 0; + m_pDC = NULL; } bool wxStatusBar::Create(wxWindow *parent, @@ -66,7 +72,7 @@ bool wxStatusBar::Create(wxWindow *parent, long style, const wxString& name) { - wxCHECK_MSG( parent, false, wxT("status bar must have a parent") ); + wxCHECK_MSG( parent, false, "status bar must have a parent" ); SetName(name); SetWindowStyleFlag(style); @@ -84,9 +90,9 @@ bool wxStatusBar::Create(wxWindow *parent, // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default // (at least in the version of comctl32.dll I'm using), and the only way to // turn it off is to use CCS_TOP style - as we position the status bar - // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxST_SIZEGRIP + // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxSTB_SIZEGRIP // is not given - if ( !(style & wxST_SIZEGRIP) ) + if ( !(style & wxSTB_SIZEGRIP) ) { wstyle |= CCS_TOP; } @@ -102,7 +108,7 @@ bool wxStatusBar::Create(wxWindow *parent, m_hWnd = CreateWindow ( STATUSCLASSNAME, - _T(""), + wxT(""), wstyle, 0, 0, 0, 0, GetHwndOf(parent), @@ -119,17 +125,23 @@ bool wxStatusBar::Create(wxWindow *parent, SetFieldsCount(1); SubclassWin(m_hWnd); + + // cache the DC instance used by DoUpdateStatusText: + // NOTE: create the DC before calling InheritAttributes() since + // it may result in a call to our SetFont() + m_pDC = new wxClientDC(this); + InheritAttributes(); SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR)); // we must refresh the frame size when the statusbar is created, because // its client area might change - wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); - if ( frame ) - { - frame->SendSizeEvent(); - } + // + // notice that we must post the event, not send it, as the frame doesn't + // know that we're its status bar yet so laying it out right now wouldn't + // work correctly, we need to wait until we return to the main loop + PostSizeEventToParent(); return true; } @@ -139,33 +151,65 @@ wxStatusBar::~wxStatusBar() // we must refresh the frame size when the statusbar is deleted but the // frame is not - otherwise statusbar leaves a hole in the place it used to // occupy - wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); - if ( frame && !frame->IsBeingDeleted() ) + PostSizeEventToParent(); + + // delete existing tooltips + for (size_t i=0; iSendSizeEvent(); + if (m_tooltips[i]) + { + delete m_tooltips[i]; + m_tooltips[i] = NULL; + } } + + wxDELETE(m_pDC); +} + +bool wxStatusBar::SetFont(const wxFont& font) +{ + if (!wxWindow::SetFont(font)) + return false; + + if (m_pDC) m_pDC->SetFont(font); + return true; } void wxStatusBar::SetFieldsCount(int nFields, const int *widths) { // this is a Windows limitation - wxASSERT_MSG( (nFields > 0) && (nFields < 255), _T("too many fields") ); + wxASSERT_MSG( (nFields > 0) && (nFields < 255), "too many fields" ); wxStatusBarBase::SetFieldsCount(nFields, widths); - SetFieldsWidth(); + MSWUpdateFieldsWidths(); + + // keep in synch also our m_tooltips array + + // reset all current tooltips + for (size_t i=0; i= 0) && (nField < m_nFields), - _T("invalid statusbar field index") ); - - if ( strText == GetStatusText(nField) ) - { - // don't call StatusBar_SetText() to avoid flicker - return; - } + if (!m_pDC) + return; // Get field style, if any int style; - if (m_statusStyles) + switch(m_panes[nField].GetStyle()) { - switch(m_statusStyles[nField]) - { - case wxSB_RAISED: - style = SBT_POPOUT; - break; - case wxSB_FLAT: - style = SBT_NOBORDERS; - break; - case wxSB_NORMAL: - default: - style = 0; - break; - } + case wxSB_RAISED: + style = SBT_POPOUT; + break; + case wxSB_FLAT: + style = SBT_NOBORDERS; + break; + + case wxSB_NORMAL: + default: + style = 0; + break; } + + wxRect rc; + GetFieldRect(nField, rc); + + int margin; + if (nField == GetFieldsCount()-1) + margin = -6; // windows reports a smaller rect for the last field; enlarge it else - style = 0; + margin = 4; + + int maxWidth = rc.GetWidth() - margin; // leave a small margin + wxString text = GetStatusText(nField); - // Pass both field number and style. MSDN library doesn't mention - // that nField and style have to be 'ORed' - if ( !StatusBar_SetText(GetHwnd(), nField | style, strText.wx_str()) ) + // do we need to ellipsize this string? + wxEllipsizeMode ellmode = (wxEllipsizeMode)-1; + if (HasFlag(wxSTB_ELLIPSIZE_START)) ellmode = wxELLIPSIZE_START; + else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE)) ellmode = wxELLIPSIZE_MIDDLE; + else if (HasFlag(wxSTB_ELLIPSIZE_END)) ellmode = wxELLIPSIZE_END; + + if (ellmode == (wxEllipsizeMode)-1) { - wxLogLastError(wxT("StatusBar_SetText")); + // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if + // we don't ellipsize the text but just truncate it + if (HasFlag(wxSTB_SHOW_TIPS)) + SetEllipsizedFlag(nField, m_pDC->GetTextExtent(text).GetWidth() > maxWidth); + } + else + { + text = wxControl::Ellipsize(text, + *m_pDC, + ellmode, + maxWidth, + wxELLIPSIZE_EXPAND_TAB); + + // update the ellipsization status for this pane; this is used later to + // decide whether a tooltip should be shown or not for this pane + // (if we have wxSTB_SHOW_TIPS) + SetEllipsizedFlag(nField, text != GetStatusText(nField)); } -} - -wxString wxStatusBar::GetStatusText(int nField) const -{ - wxCHECK_MSG( (nField >= 0) && (nField < m_nFields), wxEmptyString, - _T("invalid statusbar field index") ); - wxString str; - int len = StatusBar_GetTextLen(GetHwnd(), nField); - if ( len > 0 ) + // Set the status text in the native control passing both field number and style. + // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed' + if ( !StatusBar_SetText(GetHwnd(), nField | style, text.wx_str()) ) { - StatusBar_GetText(GetHwnd(), nField, wxStringBuffer(str, len)); + wxLogLastError("StatusBar_SetText"); } - return str; + if (HasFlag(wxSTB_SHOW_TIPS)) + { + wxASSERT(m_tooltips.size() == m_panes.GetCount()); + + if (m_tooltips[nField]) + { + if (GetField(nField).IsEllipsized()) + { + // update the rect of this tooltip: + m_tooltips[nField]->SetRect(rc); + + // update also the text: + m_tooltips[nField]->SetTip(GetStatusText(nField)); + } + else + { + // delete the tooltip associated with this pane; it's not needed anymore + delete m_tooltips[nField]; + m_tooltips[nField] = NULL; + } + } + else + { + // create a new tooltip for this pane if needed + if (GetField(nField).IsEllipsized()) + m_tooltips[nField] = new wxToolTip(this, nField, GetStatusText(nField), rc); + //else: leave m_tooltips[nField]==NULL + } + } } int wxStatusBar::GetBorderX() const @@ -266,23 +365,23 @@ void wxStatusBar::SetMinHeight(int height) { SendMessage(GetHwnd(), SB_SETMINHEIGHT, height + 2*GetBorderY(), 0); - // have to send a (dummy) WM_SIZE to redraw it now + // we have to send a (dummy) WM_SIZE to redraw it now SendMessage(GetHwnd(), WM_SIZE, 0, 0); } bool wxStatusBar::GetFieldRect(int i, wxRect& rect) const { - wxCHECK_MSG( (i >= 0) && (i < m_nFields), false, - _T("invalid statusbar field index") ); + wxCHECK_MSG( (i >= 0) && ((size_t)i < m_panes.GetCount()), false, + "invalid statusbar field index" ); RECT r; if ( !::SendMessage(GetHwnd(), SB_GETRECT, i, (LPARAM)&r) ) { - wxLogLastError(wxT("SendMessage(SB_GETRECT)")); + wxLogLastError("SendMessage(SB_GETRECT)"); } #if wxUSE_UXTHEME - wxUxThemeHandle theme((wxStatusBar *)this, L"Status"); // const_cast + wxUxThemeHandle theme(const_cast(this), L"Status"); if ( theme ) { // by default Windows has a 2 pixel border to the right of the left @@ -303,9 +402,6 @@ bool wxStatusBar::GetFieldRect(int i, wxRect& rect) const return true; } -// no idea for a default width, just choose something -#define DEFAULT_FIELD_WIDTH 25 - wxSize wxStatusBar::DoGetBestSize() const { int borders[3]; @@ -313,10 +409,10 @@ wxSize wxStatusBar::DoGetBestSize() const // calculate width int width = 0; - for ( int i = 0; i < m_nFields; ++i ) + for ( size_t i = 0; i < m_panes.GetCount(); ++i ) { - int widthField = m_statusWidths ? m_statusWidths[i] - : DEFAULT_FIELD_WIDTH; + int widthField = + m_bSameWidthForAllPanes ? DEFAULT_FIELD_WIDTH : m_panes[i].GetWidth(); if ( widthField >= 0 ) { width += widthField; @@ -338,7 +434,6 @@ wxSize wxStatusBar::DoGetBestSize() const width = 2*DEFAULT_FIELD_WIDTH; } - // calculate height int height; wxGetCharSize(GetHWND(), NULL, &height, GetFont()); @@ -371,7 +466,7 @@ void wxStatusBar::DoMoveWindow(int x, int y, int width, int height) } // adjust fields widths to the new size - SetFieldsWidth(); + MSWUpdateFieldsWidths(); // we have to trigger wxSizeEvent if there are children window in status // bar because GetFieldRect returned incorrect (not updated) values up to @@ -388,7 +483,7 @@ void wxStatusBar::SetStatusStyles(int n, const int styles[]) { wxStatusBarBase::SetStatusStyles(n, styles); - if (n != m_nFields) + if (n != (int)m_panes.GetCount()) return; for (int i = 0; i < n; i++) @@ -407,13 +502,14 @@ void wxStatusBar::SetStatusStyles(int n, const int styles[]) style = 0; break; } + // The SB_SETTEXT message is both used to set the field's text as well as - // the fields' styles. MSDN library doesn't mention - // that nField and style have to be 'ORed' + // the fields' styles. + // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed' wxString text = GetStatusText(i); if (!StatusBar_SetText(GetHwnd(), style | i, text.wx_str())) { - wxLogLastError(wxT("StatusBar_SetText")); + wxLogLastError("StatusBar_SetText"); } } } @@ -462,7 +558,61 @@ wxStatusBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) } #endif + bool needsEllipsization = HasFlag(wxSTB_ELLIPSIZE_START) || + HasFlag(wxSTB_ELLIPSIZE_MIDDLE) || + HasFlag(wxSTB_ELLIPSIZE_END); + if ( nMsg == WM_SIZE && needsEllipsization ) + { + for (int i=0; imessage == WM_MOUSEMOVE ) + wxToolTip::RelayEvent(pMsg); + } + + return wxWindow::MSWProcessMessage(pMsg); +} + +bool wxStatusBar::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM* WXUNUSED(result)) +{ + if ( HasFlag(wxSTB_SHOW_TIPS) ) + { + // see comment in wxStatusBar::MSWProcessMessage for more info; + // basically we need to override wxWindow::MSWOnNotify because + // we have wxWindow::m_tooltip always NULL but we still use tooltips... + + NMHDR* hdr = (NMHDR *)lParam; + + wxString str; + if (hdr->idFrom < m_tooltips.size() && m_tooltips[hdr->idFrom]) + str = m_tooltips[hdr->idFrom]->GetTip(); + + if ( HandleTooltipNotify(hdr->code, lParam, str)) + { + // processed + return true; + } + } + + return false; +} +#endif // wxUSE_TOOLTIPS + #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR