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