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