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