]>
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"
37 #include "wx/msw/uxtheme.h"
40 // no idea for a default width, just choose something
41 #define DEFAULT_FIELD_WIDTH 25
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // windowsx.h and commctrl.h don't define those, so we do it here
48 #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w)
49 #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
50 #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
51 #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
53 // ============================================================================
55 // ============================================================================
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 wxStatusBar::wxStatusBar()
69 bool wxStatusBar::Create(wxWindow
*parent
,
74 wxCHECK_MSG( parent
, false, wxT("status bar must have a parent") );
77 SetWindowStyleFlag(style
);
80 parent
->AddChild(this);
82 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
84 DWORD wstyle
= WS_CHILD
| WS_VISIBLE
;
86 if ( style
& wxCLIP_SIBLINGS
)
87 wstyle
|= WS_CLIPSIBLINGS
;
89 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
90 // (at least in the version of comctl32.dll I'm using), and the only way to
91 // turn it off is to use CCS_TOP style - as we position the status bar
92 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxST_SIZEGRIP
94 if ( !(style
& wxST_SIZEGRIP
) )
101 // may be some versions of comctl32.dll do need it - anyhow, it won't
103 wstyle
|= SBARS_SIZEGRIP
;
107 m_hWnd
= CreateWindow
114 (HMENU
)wxUIntToPtr(m_windowId
.GetValue()),
120 wxLogSysError(_("Failed to create a status bar."));
128 // cache the DC instance used by UpdateFieldText:
129 // NOTE: create the DC before calling InheritAttributes() since
130 // it may result in a call to our SetFont()
131 m_pDC
= new wxClientDC(this);
135 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
137 // we must refresh the frame size when the statusbar is created, because
138 // its client area might change
140 // notice that we must post the event, not send it, as the frame doesn't
141 // know that we're its status bar yet so laying it out right now wouldn't
142 // work correctly, we need to wait until we return to the main loop
143 PostSizeEventToParent();
148 wxStatusBar::~wxStatusBar()
150 // we must refresh the frame size when the statusbar is deleted but the
151 // frame is not - otherwise statusbar leaves a hole in the place it used to
153 PostSizeEventToParent();
158 bool wxStatusBar::SetFont(const wxFont
& font
)
160 if (!wxWindow::SetFont(font
))
163 if (m_pDC
) m_pDC
->SetFont(font
);
167 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
169 // this is a Windows limitation
170 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), _T("too many fields") );
172 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
177 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
179 wxStatusBarBase::SetStatusWidths(n
, widths
);
184 void wxStatusBar::SetFieldsWidth()
186 if ( m_panes
.IsEmpty() )
190 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
192 int extraWidth
= aBorders
[2]; // space between fields
194 wxArrayInt widthsAbs
=
195 CalculateAbsWidths(GetClientSize().x
- extraWidth
*(m_panes
.GetCount() - 1));
197 int *pWidths
= new int[m_panes
.GetCount()];
200 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ ) {
201 nCurPos
+= widthsAbs
[i
] + extraWidth
;
202 pWidths
[i
] = nCurPos
;
205 if ( !StatusBar_SetParts(GetHwnd(), m_panes
.GetCount(), pWidths
) ) {
206 wxLogLastError(wxT("StatusBar_SetParts"));
212 void wxStatusBar::SetStatusText(const wxString
& strText
, int nField
)
214 wxCHECK_RET( (nField
>= 0) && ((size_t)nField
< m_panes
.GetCount()),
215 _T("invalid statusbar field index") );
217 if ( strText
== GetStatusText(nField
) )
219 // don't call StatusBar_SetText() to avoid flicker
223 wxStatusBarBase::SetStatusText(strText
, nField
);
225 UpdateFieldText(nField
);
228 void wxStatusBar::UpdateFieldText(int nField
)
233 // Get field style, if any
235 switch(m_panes
[nField
].nStyle
)
241 style
= SBT_NOBORDERS
;
251 GetFieldRect(nField
, rc
);
254 if (nField
== GetFieldsCount()-1)
255 margin
= -6; // windows reports a smaller rect for the last field; enlarge it
259 // do we need to ellipsize this string?
260 wxString ellipsizedStr
=
261 wxControl::Ellipsize(GetStatusText(nField
), *m_pDC
,
262 GetLayoutDirection() == wxLayout_RightToLeft
? wxELLIPSIZE_START
: wxELLIPSIZE_END
,
263 rc
.GetWidth() - margin
, // leave a small margin
264 wxELLIPSIZE_EXPAND_TAB
);
266 // Pass both field number and style. MSDN library doesn't mention
267 // that nField and style have to be 'ORed'
268 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, ellipsizedStr
.wx_str()) )
270 wxLogLastError(wxT("StatusBar_SetText"));
274 int wxStatusBar::GetBorderX() const
277 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
282 int wxStatusBar::GetBorderY() const
285 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
290 void wxStatusBar::SetMinHeight(int height
)
292 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
+ 2*GetBorderY(), 0);
294 // have to send a (dummy) WM_SIZE to redraw it now
295 SendMessage(GetHwnd(), WM_SIZE
, 0, 0);
298 bool wxStatusBar::GetFieldRect(int i
, wxRect
& rect
) const
300 wxCHECK_MSG( (i
>= 0) && ((size_t)i
< m_panes
.GetCount()), false,
301 _T("invalid statusbar field index") );
304 if ( !::SendMessage(GetHwnd(), SB_GETRECT
, i
, (LPARAM
)&r
) )
306 wxLogLastError(wxT("SendMessage(SB_GETRECT)"));
310 wxUxThemeHandle
theme((wxStatusBar
*)this, L
"Status"); // const_cast
313 // by default Windows has a 2 pixel border to the right of the left
314 // divider (or it could be a bug) but it looks wrong so remove it
320 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme
, NULL
,
326 wxCopyRECTToRect(r
, rect
);
331 wxSize
wxStatusBar::DoGetBestSize() const
334 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)borders
);
338 for ( size_t i
= 0; i
< m_panes
.GetCount(); ++i
)
341 m_bSameWidthForAllPanes
? DEFAULT_FIELD_WIDTH
: m_panes
[i
].nWidth
;
342 if ( widthField
>= 0 )
348 // variable width field, its width is really a proportion
349 // related to the other fields
350 width
+= -widthField
*DEFAULT_FIELD_WIDTH
;
353 // add the space between fields
359 // still need something even for an empty status bar
360 width
= 2*DEFAULT_FIELD_WIDTH
;
365 wxGetCharSize(GetHWND(), NULL
, &height
, GetFont());
366 height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(height
);
367 height
+= borders
[1];
369 wxSize
best(width
, height
);
374 void wxStatusBar::DoMoveWindow(int x
, int y
, int width
, int height
)
376 if ( GetParent()->IsSizeDeferred() )
378 wxWindowMSW::DoMoveWindow(x
, y
, width
, height
);
382 // parent pos/size isn't deferred so do it now but don't send
383 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
384 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
385 // if other windows are size deferred
386 ::SetWindowPos(GetHwnd(), NULL
, x
, y
, width
, height
,
387 SWP_NOZORDER
| SWP_NOOWNERZORDER
| SWP_NOACTIVATE
389 | SWP_NOCOPYBITS
| SWP_NOSENDCHANGING
394 // adjust fields widths to the new size
397 // we have to trigger wxSizeEvent if there are children window in status
398 // bar because GetFieldRect returned incorrect (not updated) values up to
399 // here, which almost certainly resulted in incorrectly redrawn statusbar
400 if ( m_children
.GetCount() > 0 )
402 wxSizeEvent
event(GetClientSize(), m_windowId
);
403 event
.SetEventObject(this);
404 HandleWindowEvent(event
);
408 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
410 wxStatusBarBase::SetStatusStyles(n
, styles
);
412 if (n
!= (int)m_panes
.GetCount())
415 for (int i
= 0; i
< n
; i
++)
424 style
= SBT_NOBORDERS
;
431 // The SB_SETTEXT message is both used to set the field's text as well as
432 // the fields' styles. MSDN library doesn't mention
433 // that nField and style have to be 'ORed'
434 wxString text
= GetStatusText(i
);
435 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.wx_str()))
437 wxLogLastError(wxT("StatusBar_SetText"));
443 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
446 if ( nMsg
== WM_WINDOWPOSCHANGING
)
448 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
453 // we need real window coords and not wx client coords
454 AdjustForParentClientOrigin(x
, y
);
464 if ( nMsg
== WM_NCLBUTTONDOWN
)
466 // if hit-test is on gripper then send message to TLW to begin
467 // resizing. It is possible to send this message to any window.
468 if ( wParam
== HTBOTTOMRIGHT
)
472 for ( win
= GetParent(); win
; win
= win
->GetParent() )
474 if ( win
->IsTopLevel() )
476 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
486 if ( nMsg
== WM_SIZE
)
488 for (int i
=0; i
<GetFieldsCount(); i
++)
490 // re-set the field text, in case we need to ellipsize
491 // (or de-ellipsize) some parts of it
494 return wxStatusBarBase::MSWWindowProc(nMsg
, wParam
, lParam
);
497 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR