]>
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 // 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."));
127 // cache the DC instance used by UpdateFieldText:
128 // NOTE: create the DC before calling InheritAttributes() since
129 // it may result in a call to our SetFont()
130 m_pDC
= new wxClientDC(this);
134 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR
));
136 // we must refresh the frame size when the statusbar is created, because
137 // its client area might change
139 // notice that we must post the event, not send it, as the frame doesn't
140 // know that we're its status bar yet so laying it out right now wouldn't
141 // work correctly, we need to wait until we return to the main loop
142 PostSizeEventToParent();
147 wxStatusBar::~wxStatusBar()
149 // we must refresh the frame size when the statusbar is deleted but the
150 // frame is not - otherwise statusbar leaves a hole in the place it used to
152 PostSizeEventToParent();
157 bool wxStatusBar::SetFont(const wxFont
& font
)
159 if (!wxWindow::SetFont(font
))
162 if (m_pDC
) m_pDC
->SetFont(font
);
166 void wxStatusBar::SetFieldsCount(int nFields
, const int *widths
)
168 // this is a Windows limitation
169 wxASSERT_MSG( (nFields
> 0) && (nFields
< 255), _T("too many fields") );
171 wxStatusBarBase::SetFieldsCount(nFields
, widths
);
176 void wxStatusBar::SetStatusWidths(int n
, const int widths
[])
178 wxStatusBarBase::SetStatusWidths(n
, widths
);
183 void wxStatusBar::SetFieldsWidth()
185 if ( m_panes
.IsEmpty() )
189 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
191 int extraWidth
= aBorders
[2]; // space between fields
193 wxArrayInt widthsAbs
=
194 CalculateAbsWidths(GetClientSize().x
- extraWidth
*(m_panes
.GetCount() - 1));
196 int *pWidths
= new int[m_panes
.GetCount()];
199 for ( size_t i
= 0; i
< m_panes
.GetCount(); i
++ ) {
200 nCurPos
+= widthsAbs
[i
] + extraWidth
;
201 pWidths
[i
] = nCurPos
;
204 if ( !StatusBar_SetParts(GetHwnd(), m_panes
.GetCount(), pWidths
) ) {
205 wxLogLastError(wxT("StatusBar_SetParts"));
211 void wxStatusBar::SetStatusText(const wxString
& strText
, int nField
)
213 wxCHECK_RET( (nField
>= 0) && ((size_t)nField
< m_panes
.GetCount()),
214 _T("invalid statusbar field index") );
216 if ( strText
== GetStatusText(nField
) )
218 // don't call StatusBar_SetText() to avoid flicker
222 wxStatusBarBase::SetStatusText(strText
, nField
);
224 UpdateFieldText(nField
);
227 void wxStatusBar::UpdateFieldText(int nField
)
232 // Get field style, if any
234 switch(m_panes
[nField
].nStyle
)
240 style
= SBT_NOBORDERS
;
250 GetFieldRect(nField
, rc
);
253 if (nField
== GetFieldsCount()-1)
254 margin
= -6; // windows reports a smaller rect for the last field; enlarge it
258 // do we need to ellipsize this string?
259 wxString ellipsizedStr
=
260 wxControl::Ellipsize(GetStatusText(nField
), *m_pDC
,
261 GetLayoutDirection() == wxLayout_RightToLeft
? wxELLIPSIZE_START
: wxELLIPSIZE_END
,
262 rc
.GetWidth() - margin
, // leave a small margin
263 wxELLIPSIZE_EXPAND_TAB
);
265 // Pass both field number and style. MSDN library doesn't mention
266 // that nField and style have to be 'ORed'
267 if ( !StatusBar_SetText(GetHwnd(), nField
| style
, ellipsizedStr
.wx_str()) )
269 wxLogLastError(wxT("StatusBar_SetText"));
273 int wxStatusBar::GetBorderX() const
276 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
281 int wxStatusBar::GetBorderY() const
284 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)aBorders
);
289 void wxStatusBar::SetMinHeight(int height
)
291 SendMessage(GetHwnd(), SB_SETMINHEIGHT
, height
+ 2*GetBorderY(), 0);
293 // have to send a (dummy) WM_SIZE to redraw it now
294 SendMessage(GetHwnd(), WM_SIZE
, 0, 0);
297 bool wxStatusBar::GetFieldRect(int i
, wxRect
& rect
) const
299 wxCHECK_MSG( (i
>= 0) && ((size_t)i
< m_panes
.GetCount()), false,
300 _T("invalid statusbar field index") );
303 if ( !::SendMessage(GetHwnd(), SB_GETRECT
, i
, (LPARAM
)&r
) )
305 wxLogLastError(wxT("SendMessage(SB_GETRECT)"));
309 wxUxThemeHandle
theme((wxStatusBar
*)this, L
"Status"); // const_cast
312 // by default Windows has a 2 pixel border to the right of the left
313 // divider (or it could be a bug) but it looks wrong so remove it
319 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme
, NULL
,
325 wxCopyRECTToRect(r
, rect
);
330 wxSize
wxStatusBar::DoGetBestSize() const
333 SendMessage(GetHwnd(), SB_GETBORDERS
, 0, (LPARAM
)borders
);
337 for ( size_t i
= 0; i
< m_panes
.GetCount(); ++i
)
340 m_bSameWidthForAllPanes
? DEFAULT_FIELD_WIDTH
: m_panes
[i
].nWidth
;
341 if ( widthField
>= 0 )
347 // variable width field, its width is really a proportion
348 // related to the other fields
349 width
+= -widthField
*DEFAULT_FIELD_WIDTH
;
352 // add the space between fields
358 // still need something even for an empty status bar
359 width
= 2*DEFAULT_FIELD_WIDTH
;
364 wxGetCharSize(GetHWND(), NULL
, &height
, GetFont());
365 height
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(height
);
366 height
+= borders
[1];
368 wxSize
best(width
, height
);
373 void wxStatusBar::DoMoveWindow(int x
, int y
, int width
, int height
)
375 if ( GetParent()->IsSizeDeferred() )
377 wxWindowMSW::DoMoveWindow(x
, y
, width
, height
);
381 // parent pos/size isn't deferred so do it now but don't send
382 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
383 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
384 // if other windows are size deferred
385 ::SetWindowPos(GetHwnd(), NULL
, x
, y
, width
, height
,
386 SWP_NOZORDER
| SWP_NOOWNERZORDER
| SWP_NOACTIVATE
388 | SWP_NOCOPYBITS
| SWP_NOSENDCHANGING
393 // adjust fields widths to the new size
396 // we have to trigger wxSizeEvent if there are children window in status
397 // bar because GetFieldRect returned incorrect (not updated) values up to
398 // here, which almost certainly resulted in incorrectly redrawn statusbar
399 if ( m_children
.GetCount() > 0 )
401 wxSizeEvent
event(GetClientSize(), m_windowId
);
402 event
.SetEventObject(this);
403 HandleWindowEvent(event
);
407 void wxStatusBar::SetStatusStyles(int n
, const int styles
[])
409 wxStatusBarBase::SetStatusStyles(n
, styles
);
411 if (n
!= (int)m_panes
.GetCount())
414 for (int i
= 0; i
< n
; i
++)
423 style
= SBT_NOBORDERS
;
430 // The SB_SETTEXT message is both used to set the field's text as well as
431 // the fields' styles. MSDN library doesn't mention
432 // that nField and style have to be 'ORed'
433 wxString text
= GetStatusText(i
);
434 if (!StatusBar_SetText(GetHwnd(), style
| i
, text
.wx_str()))
436 wxLogLastError(wxT("StatusBar_SetText"));
442 wxStatusBar::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
445 if ( nMsg
== WM_WINDOWPOSCHANGING
)
447 WINDOWPOS
*lpPos
= (WINDOWPOS
*)lParam
;
452 // we need real window coords and not wx client coords
453 AdjustForParentClientOrigin(x
, y
);
463 if ( nMsg
== WM_NCLBUTTONDOWN
)
465 // if hit-test is on gripper then send message to TLW to begin
466 // resizing. It is possible to send this message to any window.
467 if ( wParam
== HTBOTTOMRIGHT
)
471 for ( win
= GetParent(); win
; win
= win
->GetParent() )
473 if ( win
->IsTopLevel() )
475 SendMessage(GetHwndOf(win
), WM_NCLBUTTONDOWN
,
485 if ( nMsg
== WM_SIZE
)
487 for (int i
=0; i
<GetFieldsCount(); i
++)
489 // re-set the field text, in case we need to ellipsize
490 // (or de-ellipsize) some parts of it
493 return wxStatusBarBase::MSWWindowProc(nMsg
, wParam
, lParam
);
496 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR