]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statusbar.cpp
applying patch, fixes #10523
[wxWidgets.git] / src / msw / statusbar.cpp
CommitLineData
2bda0e17 1///////////////////////////////////////////////////////////////////////////////
936f6353 2// Name: src/msw/statusbar.cpp
2bda0e17
KB
3// Purpose: native implementation of wxStatusBar
4// Author: Vadim Zeitlin
ed791986 5// Modified by:
2bda0e17
KB
6// Created: 04.04.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10///////////////////////////////////////////////////////////////////////////////
11
2bda0e17
KB
12// for compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
88a7a4e1
WS
19#if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
20
21#include "wx/statusbr.h"
22
2bda0e17 23#ifndef WX_PRECOMP
57bd4c60 24 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
88a7a4e1
WS
25 #include "wx/frame.h"
26 #include "wx/settings.h"
27 #include "wx/dcclient.h"
28 #include "wx/intl.h"
e4db172a 29 #include "wx/log.h"
cbbf47f3 30 #include "wx/control.h"
2bda0e17
KB
31#endif
32
42e69d6b
VZ
33#include "wx/msw/private.h"
34#include <windowsx.h>
2bda0e17 35
1feb5443
JG
36#if wxUSE_UXTHEME
37 #include "wx/msw/uxtheme.h"
38#endif
39
0cd15959
FM
40// no idea for a default width, just choose something
41#define DEFAULT_FIELD_WIDTH 25
42
2bda0e17
KB
43// ----------------------------------------------------------------------------
44// macros
45// ----------------------------------------------------------------------------
46
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)
837e5743 49#define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
2bda0e17 50#define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
837e5743 51#define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
2bda0e17 52
2bda0e17
KB
53// ============================================================================
54// implementation
55// ============================================================================
56
57// ----------------------------------------------------------------------------
936f6353 58// wxStatusBar class
2bda0e17
KB
59// ----------------------------------------------------------------------------
60
936f6353 61wxStatusBar::wxStatusBar()
2bda0e17 62{
b3d008db
VZ
63 SetParent(NULL);
64 m_hWnd = 0;
65 m_windowId = 0;
0cd15959 66 m_pDC = NULL;
2bda0e17
KB
67}
68
936f6353
VZ
69bool wxStatusBar::Create(wxWindow *parent,
70 wxWindowID id,
71 long style,
72 const wxString& name)
2bda0e17 73{
57f4f925 74 wxCHECK_MSG( parent, false, wxT("status bar must have a parent") );
cbc66a27 75
ed791986 76 SetName(name);
cbc66a27 77 SetWindowStyleFlag(style);
ed791986
VZ
78 SetParent(parent);
79
cbc66a27
VZ
80 parent->AddChild(this);
81
57f4f925 82 m_windowId = id == wxID_ANY ? NewControlId() : id;
ed791986 83
b0766406
JS
84 DWORD wstyle = WS_CHILD | WS_VISIBLE;
85
86 if ( style & wxCLIP_SIBLINGS )
87 wstyle |= WS_CLIPSIBLINGS;
88
43b5058d
VZ
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
93 // is not given
94 if ( !(style & wxST_SIZEGRIP) )
95 {
96 wstyle |= CCS_TOP;
97 }
98 else
99 {
4676948b 100#ifndef __WXWINCE__
43b5058d
VZ
101 // may be some versions of comctl32.dll do need it - anyhow, it won't
102 // do any harm
ed791986 103 wstyle |= SBARS_SIZEGRIP;
4676948b 104#endif
43b5058d 105 }
ed791986 106
2a11c826
VZ
107 m_hWnd = CreateWindow
108 (
109 STATUSCLASSNAME,
110 _T(""),
111 wstyle,
112 0, 0, 0, 0,
113 GetHwndOf(parent),
dca0f651 114 (HMENU)wxUIntToPtr(m_windowId.GetValue()),
2a11c826
VZ
115 wxGetInstance(),
116 NULL
117 );
ed791986
VZ
118 if ( m_hWnd == 0 )
119 {
120 wxLogSysError(_("Failed to create a status bar."));
121
57f4f925 122 return false;
ed791986 123 }
2bda0e17 124
c6e7d14f 125 SetFieldsCount(1);
b3d008db 126 SubclassWin(m_hWnd);
80255b7e
FM
127
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);
132
e0176dd9 133 InheritAttributes();
2bda0e17 134
7d6d3bf3 135 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR));
c07103f2 136
c10d3a54
VS
137 // we must refresh the frame size when the statusbar is created, because
138 // its client area might change
ecdc1183
VZ
139 //
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();
7d6d3bf3 144
57f4f925 145 return true;
ed791986 146}
2bda0e17 147
936f6353 148wxStatusBar::~wxStatusBar()
ed791986 149{
6b5c56bd
VS
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
152 // occupy
ecdc1183 153 PostSizeEventToParent();
80255b7e
FM
154
155 wxDELETE(m_pDC);
2bda0e17
KB
156}
157
0cd15959
FM
158bool wxStatusBar::SetFont(const wxFont& font)
159{
160 if (!wxWindow::SetFont(font))
161 return false;
162
80255b7e 163 if (m_pDC) m_pDC->SetFont(font);
0cd15959
FM
164 return true;
165}
166
936f6353 167void wxStatusBar::SetFieldsCount(int nFields, const int *widths)
2bda0e17 168{
f6bcfd97
BP
169 // this is a Windows limitation
170 wxASSERT_MSG( (nFields > 0) && (nFields < 255), _T("too many fields") );
2bda0e17 171
498fd97d 172 wxStatusBarBase::SetFieldsCount(nFields, widths);
27d2cd5c
VZ
173
174 SetFieldsWidth();
2bda0e17
KB
175}
176
936f6353 177void wxStatusBar::SetStatusWidths(int n, const int widths[])
2bda0e17 178{
498fd97d 179 wxStatusBarBase::SetStatusWidths(n, widths);
2bda0e17 180
f6bcfd97 181 SetFieldsWidth();
2bda0e17
KB
182}
183
936f6353 184void wxStatusBar::SetFieldsWidth()
2bda0e17 185{
7b6fefbe 186 if ( m_panes.IsEmpty() )
3175c712
VZ
187 return;
188
ed791986
VZ
189 int aBorders[3];
190 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
2bda0e17 191
ed791986 192 int extraWidth = aBorders[2]; // space between fields
2bda0e17 193
498fd97d 194 wxArrayInt widthsAbs =
7b6fefbe 195 CalculateAbsWidths(GetClientSize().x - extraWidth*(m_panes.GetCount() - 1));
2bda0e17 196
7b6fefbe 197 int *pWidths = new int[m_panes.GetCount()];
2bda0e17 198
498fd97d 199 int nCurPos = 0;
a37d94d3 200 for ( size_t i = 0; i < m_panes.GetCount(); i++ ) {
498fd97d
VZ
201 nCurPos += widthsAbs[i] + extraWidth;
202 pWidths[i] = nCurPos;
2bda0e17 203 }
2bda0e17 204
7b6fefbe 205 if ( !StatusBar_SetParts(GetHwnd(), m_panes.GetCount(), pWidths) ) {
ed791986
VZ
206 wxLogLastError(wxT("StatusBar_SetParts"));
207 }
2bda0e17 208
ed791986 209 delete [] pWidths;
2bda0e17
KB
210}
211
936f6353 212void wxStatusBar::SetStatusText(const wxString& strText, int nField)
2bda0e17 213{
a37d94d3 214 wxCHECK_RET( (nField >= 0) && ((size_t)nField < m_panes.GetCount()),
ed791986
VZ
215 _T("invalid statusbar field index") );
216
823b6635
VZ
217 if ( strText == GetStatusText(nField) )
218 {
219 // don't call StatusBar_SetText() to avoid flicker
220 return;
221 }
222
0cd15959
FM
223 wxStatusBarBase::SetStatusText(strText, nField);
224
225 UpdateFieldText(nField);
226}
227
228void wxStatusBar::UpdateFieldText(int nField)
229{
230 if (!m_pDC)
231 return;
232
c2919ab3
VZ
233 // Get field style, if any
234 int style;
7b6fefbe 235 switch(m_panes[nField].nStyle)
c2919ab3 236 {
7b6fefbe
FM
237 case wxSB_RAISED:
238 style = SBT_POPOUT;
239 break;
240 case wxSB_FLAT:
241 style = SBT_NOBORDERS;
242 break;
243
244 case wxSB_NORMAL:
245 default:
c2919ab3 246 style = 0;
7b6fefbe
FM
247 break;
248 }
c2919ab3 249
0cd15959
FM
250 wxRect rc;
251 GetFieldRect(nField, rc);
252
253 int margin;
254 if (nField == GetFieldsCount()-1)
255 margin = -6; // windows reports a smaller rect for the last field; enlarge it
256 else
257 margin = 4;
258
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);
265
c2919ab3
VZ
266 // Pass both field number and style. MSDN library doesn't mention
267 // that nField and style have to be 'ORed'
0cd15959 268 if ( !StatusBar_SetText(GetHwnd(), nField | style, ellipsizedStr.wx_str()) )
b3d008db
VZ
269 {
270 wxLogLastError(wxT("StatusBar_SetText"));
271 }
2bda0e17
KB
272}
273
936f6353 274int wxStatusBar::GetBorderX() const
ed791986
VZ
275{
276 int aBorders[3];
277 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
278
279 return aBorders[0];
280}
281
936f6353 282int wxStatusBar::GetBorderY() const
ed791986
VZ
283{
284 int aBorders[3];
285 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
286
287 return aBorders[1];
288}
289
936f6353 290void wxStatusBar::SetMinHeight(int height)
ed791986
VZ
291{
292 SendMessage(GetHwnd(), SB_SETMINHEIGHT, height + 2*GetBorderY(), 0);
293
294 // have to send a (dummy) WM_SIZE to redraw it now
295 SendMessage(GetHwnd(), WM_SIZE, 0, 0);
296}
297
936f6353 298bool wxStatusBar::GetFieldRect(int i, wxRect& rect) const
ed791986 299{
a37d94d3 300 wxCHECK_MSG( (i >= 0) && ((size_t)i < m_panes.GetCount()), false,
ed791986
VZ
301 _T("invalid statusbar field index") );
302
303 RECT r;
304 if ( !::SendMessage(GetHwnd(), SB_GETRECT, i, (LPARAM)&r) )
305 {
f6bcfd97 306 wxLogLastError(wxT("SendMessage(SB_GETRECT)"));
ed791986
VZ
307 }
308
1feb5443 309#if wxUSE_UXTHEME
936f6353 310 wxUxThemeHandle theme((wxStatusBar *)this, L"Status"); // const_cast
1feb5443
JG
311 if ( theme )
312 {
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
315 if ( i != 0 )
316 {
317 r.left -= 2;
318 }
319
320 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme, NULL,
321 1 /* SP_PANE */, 0,
322 &r, &r);
323 }
324#endif
325
ed791986
VZ
326 wxCopyRECTToRect(r, rect);
327
57f4f925 328 return true;
ed791986
VZ
329}
330
936f6353 331wxSize wxStatusBar::DoGetBestSize() const
c0ac3149
VZ
332{
333 int borders[3];
334 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)borders);
335
336 // calculate width
337 int width = 0;
a37d94d3 338 for ( size_t i = 0; i < m_panes.GetCount(); ++i )
c0ac3149 339 {
7b6fefbe
FM
340 int widthField =
341 m_bSameWidthForAllPanes ? DEFAULT_FIELD_WIDTH : m_panes[i].nWidth;
c0ac3149
VZ
342 if ( widthField >= 0 )
343 {
e408d9ca 344 width += widthField;
c0ac3149
VZ
345 }
346 else
347 {
348 // variable width field, its width is really a proportion
349 // related to the other fields
350 width += -widthField*DEFAULT_FIELD_WIDTH;
351 }
352
353 // add the space between fields
354 width += borders[2];
355 }
356
357 if ( !width )
358 {
359 // still need something even for an empty status bar
360 width = 2*DEFAULT_FIELD_WIDTH;
361 }
362
c0ac3149
VZ
363 // calculate height
364 int height;
365 wxGetCharSize(GetHWND(), NULL, &height, GetFont());
366 height = EDIT_HEIGHT_FROM_CHAR_HEIGHT(height);
367 height += borders[1];
368
369 wxSize best(width, height);
370 CacheBestSize(best);
371 return best;
372}
373
936f6353 374void wxStatusBar::DoMoveWindow(int x, int y, int width, int height)
2bda0e17 375{
c07103f2
VZ
376 if ( GetParent()->IsSizeDeferred() )
377 {
378 wxWindowMSW::DoMoveWindow(x, y, width, height);
379 }
380 else
381 {
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
dd28827a
JG
384 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
385 // if other windows are size deferred
c07103f2 386 ::SetWindowPos(GetHwnd(), NULL, x, y, width, height,
99cc862c 387 SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE
521bf4ff 388#ifndef __WXWINCE__
99cc862c
JS
389 | SWP_NOCOPYBITS | SWP_NOSENDCHANGING
390#endif
391 );
c07103f2 392 }
43b5058d
VZ
393
394 // adjust fields widths to the new size
395 SetFieldsWidth();
dafa4925 396
57f4f925 397 // we have to trigger wxSizeEvent if there are children window in status
dafa4925
VS
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 )
401 {
402 wxSizeEvent event(GetClientSize(), m_windowId);
403 event.SetEventObject(this);
937013e0 404 HandleWindowEvent(event);
dafa4925 405 }
2bda0e17
KB
406}
407
936f6353 408void wxStatusBar::SetStatusStyles(int n, const int styles[])
c2919ab3
VZ
409{
410 wxStatusBarBase::SetStatusStyles(n, styles);
411
a37d94d3 412 if (n != (int)m_panes.GetCount())
c2919ab3
VZ
413 return;
414
415 for (int i = 0; i < n; i++)
416 {
417 int style;
418 switch(styles[i])
419 {
420 case wxSB_RAISED:
421 style = SBT_POPOUT;
422 break;
423 case wxSB_FLAT:
424 style = SBT_NOBORDERS;
425 break;
426 case wxSB_NORMAL:
427 default:
428 style = 0;
429 break;
430 }
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);
e0a050e3 435 if (!StatusBar_SetText(GetHwnd(), style | i, text.wx_str()))
c2919ab3
VZ
436 {
437 wxLogLastError(wxT("StatusBar_SetText"));
438 }
439 }
440}
441
c07103f2 442WXLRESULT
936f6353 443wxStatusBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
c07103f2 444{
e78e8583 445#ifndef __WXWINCE__
c07103f2
VZ
446 if ( nMsg == WM_WINDOWPOSCHANGING )
447 {
448 WINDOWPOS *lpPos = (WINDOWPOS *)lParam;
449 int x, y, w, h;
450 GetPosition(&x, &y);
451 GetSize(&w, &h);
452
4ce18ecb
VZ
453 // we need real window coords and not wx client coords
454 AdjustForParentClientOrigin(x, y);
455
c07103f2
VZ
456 lpPos->x = x;
457 lpPos->y = y;
458 lpPos->cx = w;
459 lpPos->cy = h;
460
461 return 0;
462 }
e78e8583 463
c07103f2
VZ
464 if ( nMsg == WM_NCLBUTTONDOWN )
465 {
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 )
469 {
470 wxWindow *win;
471
472 for ( win = GetParent(); win; win = win->GetParent() )
473 {
474 if ( win->IsTopLevel() )
475 {
476 SendMessage(GetHwndOf(win), WM_NCLBUTTONDOWN,
477 wParam, lParam);
478
479 return 0;
480 }
481 }
482 }
483 }
e78e8583 484#endif
c07103f2 485
0cd15959
FM
486 if ( nMsg == WM_SIZE )
487 {
488 for (int i=0; i<GetFieldsCount(); i++)
489 UpdateFieldText(i);
490 // re-set the field text, in case we need to ellipsize
491 // (or de-ellipsize) some parts of it
492 }
493
c07103f2
VZ
494 return wxStatusBarBase::MSWWindowProc(nMsg, wParam, lParam);
495}
496
a71d815b 497#endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR