]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statusbar.cpp
0dcc35899c083630817b7566c10d04a8bb2f1309
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 // no idea for a default width, just choose something
40 #define DEFAULT_FIELD_WIDTH 25
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // windowsx.h and commctrl.h don't define those, so we do it here
47 #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w)
48 #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
49 #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
50 #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 wxStatusBar::wxStatusBar()
68 bool wxStatusBar::Create(wxWindow
*parent
,
73 wxCHECK_MSG( parent
, false, wxT("status bar must have a parent") );
76 SetWindowStyleFlag(style
);
79 parent
->AddChild(this);
81 m_windowId
= id
== wxID_ANY
? NewControlId() : id
;
83 DWORD wstyle
= WS_CHILD
| WS_VISIBLE
;
85 if ( style
& wxCLIP_SIBLINGS
)
86 wstyle
|= WS_CLIPSIBLINGS
;
88 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
89 // (at least in the version of comctl32.dll I'm using), and the only way to
90 // turn it off is to use CCS_TOP style - as we position the status bar
91 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxST_SIZEGRIP
93 if ( !(style
& wxST_SIZEGRIP
) )
100 // may be some versions of comctl32.dll do need it - anyhow, it won't
102 wstyle
|= SBARS_SIZEGRIP
;
106 m_hWnd
= CreateWindow
113 (HMENU
)wxUIntToPtr(m_windowId
.GetValue()),
119 wxLogSysError(_("Failed to create a status bar."));
128 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
130 // we must refresh the frame size when the statusbar is created, because
131 // its client area might change
133 // notice that we must post the event, not send it, as the frame doesn't
134 // know that we're its status bar yet so laying it out right now wouldn't
135 // work correctly, we need to wait until we return to the main loop
136 PostSizeEventToParent();
138 // cache the DC instance used by UpdateFieldText
139 m_pDC
= new wxClientDC(this);
144 wxStatusBar::~wxStatusBar()
146 // we must refresh the frame size when the statusbar is deleted but the
147 // frame is not - otherwise statusbar leaves a hole in the place it used to
149 PostSizeEventToParent();
152 bool wxStatusBar::SetFont(const wxFont
& font
)
154 if (!wxWindow::SetFont(font
))
157 m_pDC
->SetFont(font
);
161 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
163 // this is a Windows limitation
164 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), _T("too many fields") );
166 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
171 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
173 wxStatusBarBase::SetStatusWidths(n
, widths
);
178 void wxStatusBar::SetFieldsWidth()
180 if ( m_panes
.IsEmpty() )
184 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
186 int extraWidth
= aBorders
[2]; // space between fields
188 wxArrayInt widthsAbs
=
189 CalculateAbsWidths(GetClientSize().x
- extraWidth
*(m_panes
.GetCount() - 1));
191 int *pWidths
= new int[m_panes
.GetCount()];
194 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ ) {
195 nCurPos
+= widthsAbs
[i
] + extraWidth
;
196 pWidths
[i
] = nCurPos
;
199 if ( !StatusBar_SetParts(GetHwnd(), m_panes
.GetCount(), pWidths
) ) {
200 wxLogLastError(wxT("StatusBar_SetParts"));
206 void wxStatusBar::SetStatusText(const wxString
& strText
, int nField
)
208 wxCHECK_RET( (nField
>= 0) && ((size_t)nField
< m_panes
.GetCount()),
209 _T("invalid statusbar field index") );
211 if ( strText
== GetStatusText(nField
) )
213 // don't call StatusBar_SetText() to avoid flicker
217 wxStatusBarBase::SetStatusText(strText
, nField
);
219 UpdateFieldText(nField
);
222 void wxStatusBar::UpdateFieldText(int nField
)
227 // Get field style, if any
229 switch(m_panes
[nField
].nStyle
)
235 style
= SBT_NOBORDERS
;
245 GetFieldRect(nField
, rc
);
248 if (nField
== GetFieldsCount()-1)
249 margin
= -6; // windows reports a smaller rect for the last field; enlarge it
253 // do we need to ellipsize this string?
254 wxString ellipsizedStr
=
255 wxControl::Ellipsize(GetStatusText(nField
), *m_pDC
,
256 GetLayoutDirection() == wxLayout_RightToLeft
? wxELLIPSIZE_START
: wxELLIPSIZE_END
,
257 rc
.GetWidth() - margin
, // leave a small margin
258 wxELLIPSIZE_EXPAND_TAB
);
260 // Pass both field number and style. MSDN library doesn't mention
261 // that nField and style have to be 'ORed'
262 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, ellipsizedStr
.wx_str()) )
264 wxLogLastError(wxT("StatusBar_SetText"));
268 int wxStatusBar::GetBorderX() const
271 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
276 int wxStatusBar::GetBorderY() const
279 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
284 void wxStatusBar::SetMinHeight(int height
)
286 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
+ 2*GetBorderY(), 0);
288 // have to send a (dummy) WM_SIZE to redraw it now
289 SendMessage(GetHwnd(), WM_SIZE
, 0, 0);
292 bool wxStatusBar::GetFieldRect(int i
, wxRect
& rect
) const
294 wxCHECK_MSG( (i
>= 0) && ((size_t)i
< m_panes
.GetCount()), false,
295 _T("invalid statusbar field index") );
298 if ( !::SendMessage(GetHwnd(), SB_GETRECT
, i
, (LPARAM
)&r
) )
300 wxLogLastError(wxT("SendMessage(SB_GETRECT)"));
304 wxUxThemeHandle
theme((wxStatusBar
*)this, L
"Status"); // const_cast
307 // by default Windows has a 2 pixel border to the right of the left
308 // divider (or it could be a bug) but it looks wrong so remove it
314 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme
, NULL
,
320 wxCopyRECTToRect(r
, rect
);
325 wxSize
wxStatusBar::DoGetBestSize() const
328 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)borders
);
332 for ( size_t i
= 0; i
< m_panes
.GetCount(); ++i
)
335 m_bSameWidthForAllPanes
? DEFAULT_FIELD_WIDTH
: m_panes
[i
].nWidth
;
336 if ( widthField
>= 0 )
342 // variable width field, its width is really a proportion
343 // related to the other fields
344 width
+= -widthField
*DEFAULT_FIELD_WIDTH
;
347 // add the space between fields
353 // still need something even for an empty status bar
354 width
= 2*DEFAULT_FIELD_WIDTH
;
359 wxGetCharSize(GetHWND(), NULL
, &height
, GetFont());
360 height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(height
);
361 height
+= borders
[1];
363 wxSize
best(width
, height
);
368 void wxStatusBar::DoMoveWindow(int x
, int y
, int width
, int height
)
370 if ( GetParent()->IsSizeDeferred() )
372 wxWindowMSW::DoMoveWindow(x
, y
, width
, height
);
376 // parent pos/size isn't deferred so do it now but don't send
377 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
378 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
379 // if other windows are size deferred
380 ::SetWindowPos(GetHwnd(), NULL
, x
, y
, width
, height
,
381 SWP_NOZORDER
| SWP_NOOWNERZORDER
| SWP_NOACTIVATE
383 | SWP_NOCOPYBITS
| SWP_NOSENDCHANGING
388 // adjust fields widths to the new size
391 // we have to trigger wxSizeEvent if there are children window in status
392 // bar because GetFieldRect returned incorrect (not updated) values up to
393 // here, which almost certainly resulted in incorrectly redrawn statusbar
394 if ( m_children
.GetCount() > 0 )
396 wxSizeEvent
event(GetClientSize(), m_windowId
);
397 event
.SetEventObject(this);
398 HandleWindowEvent(event
);
402 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
404 wxStatusBarBase::SetStatusStyles(n
, styles
);
406 if (n
!= (int)m_panes
.GetCount())
409 for (int i
= 0; i
< n
; i
++)
418 style
= SBT_NOBORDERS
;
425 // The SB_SETTEXT message is both used to set the field's text as well as
426 // the fields' styles. MSDN library doesn't mention
427 // that nField and style have to be 'ORed'
428 wxString text
= GetStatusText(i
);
429 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.wx_str()))
431 wxLogLastError(wxT("StatusBar_SetText"));
437 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
440 if ( nMsg
== WM_WINDOWPOSCHANGING
)
442 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
447 // we need real window coords and not wx client coords
448 AdjustForParentClientOrigin(x
, y
);
458 if ( nMsg
== WM_NCLBUTTONDOWN
)
460 // if hit-test is on gripper then send message to TLW to begin
461 // resizing. It is possible to send this message to any window.
462 if ( wParam
== HTBOTTOMRIGHT
)
466 for ( win
= GetParent(); win
; win
= win
->GetParent() )
468 if ( win
->IsTopLevel() )
470 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
480 if ( nMsg
== WM_SIZE
)
482 for (int i
=0; i
<GetFieldsCount(); i
++)
484 // re-set the field text, in case we need to ellipsize
485 // (or de-ellipsize) some parts of it
488 return wxStatusBarBase::MSWWindowProc(nMsg
, wParam
, lParam
);
491 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR