]>
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"
32 #include "wx/msw/private.h"
36 #include "wx/msw/uxtheme.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // windowsx.h and commctrl.h don't define those, so we do it here
44 #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w)
45 #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
46 #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
47 #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 wxStatusBar::wxStatusBar()
64 bool wxStatusBar::Create(wxWindow
*parent
,
69 wxCHECK_MSG( parent
, false, wxT("status bar must have a parent") );
72 SetWindowStyleFlag(style
);
75 parent
->AddChild(this);
77 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
79 DWORD wstyle
= WS_CHILD
| WS_VISIBLE
;
81 if ( style
& wxCLIP_SIBLINGS
)
82 wstyle
|= WS_CLIPSIBLINGS
;
84 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
85 // (at least in the version of comctl32.dll I'm using), and the only way to
86 // turn it off is to use CCS_TOP style - as we position the status bar
87 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxST_SIZEGRIP
89 if ( !(style
& wxST_SIZEGRIP
) )
96 // may be some versions of comctl32.dll do need it - anyhow, it won't
98 wstyle
|= SBARS_SIZEGRIP
;
102 m_hWnd
= CreateWindow
109 (HMENU
)wxUIntToPtr(m_windowId
.GetValue()),
115 wxLogSysError(_("Failed to create a status bar."));
124 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
126 // we must refresh the frame size when the statusbar is created, because
127 // its client area might change
129 // notice that we must post the event, not send it, as the frame doesn't
130 // know that we're its status bar yet so laying it out right now wouldn't
131 // work correctly, we need to wait until we return to the main loop
132 PostSizeEventToParent();
137 wxStatusBar::~wxStatusBar()
139 // we must refresh the frame size when the statusbar is deleted but the
140 // frame is not - otherwise statusbar leaves a hole in the place it used to
142 PostSizeEventToParent();
145 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
147 // this is a Windows limitation
148 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), _T("too many fields") );
150 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
155 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
157 wxStatusBarBase::SetStatusWidths(n
, widths
);
162 void wxStatusBar::SetFieldsWidth()
164 if ( m_panes
.IsEmpty() )
168 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
170 int extraWidth
= aBorders
[2]; // space between fields
172 wxArrayInt widthsAbs
=
173 CalculateAbsWidths(GetClientSize().x
- extraWidth
*(m_panes
.GetCount() - 1));
175 int *pWidths
= new int[m_panes
.GetCount()];
178 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ ) {
179 nCurPos
+= widthsAbs
[i
] + extraWidth
;
180 pWidths
[i
] = nCurPos
;
183 if ( !StatusBar_SetParts(GetHwnd(), m_panes
.GetCount(), pWidths
) ) {
184 wxLogLastError(wxT("StatusBar_SetParts"));
190 void wxStatusBar::SetStatusText(const wxString
& strText
, int nField
)
192 wxCHECK_RET( (nField
>= 0) && ((size_t)nField
< m_panes
.GetCount()),
193 _T("invalid statusbar field index") );
195 if ( strText
== GetStatusText(nField
) )
197 // don't call StatusBar_SetText() to avoid flicker
201 // Get field style, if any
203 switch(m_panes
[nField
].nStyle
)
209 style
= SBT_NOBORDERS
;
218 // Pass both field number and style. MSDN library doesn't mention
219 // that nField and style have to be 'ORed'
220 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, strText
.wx_str()) )
222 wxLogLastError(wxT("StatusBar_SetText"));
226 wxString
wxStatusBar::GetStatusText(int nField
) const
228 wxCHECK_MSG( (nField
>= 0) && ((size_t)nField
< m_panes
.GetCount()), wxEmptyString
,
229 _T("invalid statusbar field index") );
232 int len
= StatusBar_GetTextLen(GetHwnd(), nField
);
235 StatusBar_GetText(GetHwnd(), nField
, wxStringBuffer(str
, len
));
241 int wxStatusBar::GetBorderX() const
244 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
249 int wxStatusBar::GetBorderY() const
252 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
257 void wxStatusBar::SetMinHeight(int height
)
259 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
+ 2*GetBorderY(), 0);
261 // have to send a (dummy) WM_SIZE to redraw it now
262 SendMessage(GetHwnd(), WM_SIZE
, 0, 0);
265 bool wxStatusBar::GetFieldRect(int i
, wxRect
& rect
) const
267 wxCHECK_MSG( (i
>= 0) && ((size_t)i
< m_panes
.GetCount()), false,
268 _T("invalid statusbar field index") );
271 if ( !::SendMessage(GetHwnd(), SB_GETRECT
, i
, (LPARAM
)&r
) )
273 wxLogLastError(wxT("SendMessage(SB_GETRECT)"));
277 wxUxThemeHandle
theme((wxStatusBar
*)this, L
"Status"); // const_cast
280 // by default Windows has a 2 pixel border to the right of the left
281 // divider (or it could be a bug) but it looks wrong so remove it
287 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme
, NULL
,
293 wxCopyRECTToRect(r
, rect
);
298 // no idea for a default width, just choose something
299 #define DEFAULT_FIELD_WIDTH 25
301 wxSize
wxStatusBar::DoGetBestSize() const
304 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)borders
);
308 for ( size_t i
= 0; i
< m_panes
.GetCount(); ++i
)
311 m_bSameWidthForAllPanes
? DEFAULT_FIELD_WIDTH
: m_panes
[i
].nWidth
;
312 if ( widthField
>= 0 )
318 // variable width field, its width is really a proportion
319 // related to the other fields
320 width
+= -widthField
*DEFAULT_FIELD_WIDTH
;
323 // add the space between fields
329 // still need something even for an empty status bar
330 width
= 2*DEFAULT_FIELD_WIDTH
;
336 wxGetCharSize(GetHWND(), NULL
, &height
, GetFont());
337 height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(height
);
338 height
+= borders
[1];
340 wxSize
best(width
, height
);
345 void wxStatusBar::DoMoveWindow(int x
, int y
, int width
, int height
)
347 if ( GetParent()->IsSizeDeferred() )
349 wxWindowMSW::DoMoveWindow(x
, y
, width
, height
);
353 // parent pos/size isn't deferred so do it now but don't send
354 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
355 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
356 // if other windows are size deferred
357 ::SetWindowPos(GetHwnd(), NULL
, x
, y
, width
, height
,
358 SWP_NOZORDER
| SWP_NOOWNERZORDER
| SWP_NOACTIVATE
360 | SWP_NOCOPYBITS
| SWP_NOSENDCHANGING
365 // adjust fields widths to the new size
368 // we have to trigger wxSizeEvent if there are children window in status
369 // bar because GetFieldRect returned incorrect (not updated) values up to
370 // here, which almost certainly resulted in incorrectly redrawn statusbar
371 if ( m_children
.GetCount() > 0 )
373 wxSizeEvent
event(GetClientSize(), m_windowId
);
374 event
.SetEventObject(this);
375 HandleWindowEvent(event
);
379 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
381 wxStatusBarBase::SetStatusStyles(n
, styles
);
383 if (n
!= (int)m_panes
.GetCount())
386 for (int i
= 0; i
< n
; i
++)
395 style
= SBT_NOBORDERS
;
402 // The SB_SETTEXT message is both used to set the field's text as well as
403 // the fields' styles. MSDN library doesn't mention
404 // that nField and style have to be 'ORed'
405 wxString text
= GetStatusText(i
);
406 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.wx_str()))
408 wxLogLastError(wxT("StatusBar_SetText"));
414 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
417 if ( nMsg
== WM_WINDOWPOSCHANGING
)
419 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
424 // we need real window coords and not wx client coords
425 AdjustForParentClientOrigin(x
, y
);
435 if ( nMsg
== WM_NCLBUTTONDOWN
)
437 // if hit-test is on gripper then send message to TLW to begin
438 // resizing. It is possible to send this message to any window.
439 if ( wParam
== HTBOTTOMRIGHT
)
443 for ( win
= GetParent(); win
; win
= win
->GetParent() )
445 if ( win
->IsTopLevel() )
447 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
457 return wxStatusBarBase::MSWWindowProc(nMsg
, wParam
, lParam
);
460 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR