save the current status text for each pane inside wxStatusBarPane: native controls...
[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 // no idea for a default width, just choose something
40 #define DEFAULT_FIELD_WIDTH 25
41
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)
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))
51
52 // ============================================================================
53 // implementation
54 // ============================================================================
55
56 // ----------------------------------------------------------------------------
57 // wxStatusBar class
58 // ----------------------------------------------------------------------------
59
60 wxStatusBar::wxStatusBar()
61 {
62 SetParent(NULL);
63 m_hWnd = 0;
64 m_windowId = 0;
65 m_pDC = NULL;
66 }
67
68 bool wxStatusBar::Create(wxWindow *parent,
69 wxWindowID id,
70 long style,
71 const wxString& name)
72 {
73 wxCHECK_MSG( parent, false, wxT("status bar must have a parent") );
74
75 SetName(name);
76 SetWindowStyleFlag(style);
77 SetParent(parent);
78
79 parent->AddChild(this);
80
81 m_windowId = id == wxID_ANY ? NewControlId() : id;
82
83 DWORD wstyle = WS_CHILD | WS_VISIBLE;
84
85 if ( style & wxCLIP_SIBLINGS )
86 wstyle |= WS_CLIPSIBLINGS;
87
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 {
99 #ifndef __WXWINCE__
100 // may be some versions of comctl32.dll do need it - anyhow, it won't
101 // do any harm
102 wstyle |= SBARS_SIZEGRIP;
103 #endif
104 }
105
106 m_hWnd = CreateWindow
107 (
108 STATUSCLASSNAME,
109 _T(""),
110 wstyle,
111 0, 0, 0, 0,
112 GetHwndOf(parent),
113 (HMENU)wxUIntToPtr(m_windowId.GetValue()),
114 wxGetInstance(),
115 NULL
116 );
117 if ( m_hWnd == 0 )
118 {
119 wxLogSysError(_("Failed to create a status bar."));
120
121 return false;
122 }
123
124 SetFieldsCount(1);
125 SubclassWin(m_hWnd);
126 InheritAttributes();
127
128 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR));
129
130 // we must refresh the frame size when the statusbar is created, because
131 // its client area might change
132 //
133 // notice that we must post the event, not send it, as the frame doesn't
134 // know that we're its status bar yet so laying it out right now wouldn't
135 // work correctly, we need to wait until we return to the main loop
136 PostSizeEventToParent();
137
138 // cache the DC instance used by UpdateFieldText
139 m_pDC = new wxClientDC(this);
140
141 return true;
142 }
143
144 wxStatusBar::~wxStatusBar()
145 {
146 // we must refresh the frame size when the statusbar is deleted but the
147 // frame is not - otherwise statusbar leaves a hole in the place it used to
148 // occupy
149 PostSizeEventToParent();
150 }
151
152 bool wxStatusBar::SetFont(const wxFont& font)
153 {
154 if (!wxWindow::SetFont(font))
155 return false;
156
157 m_pDC->SetFont(font);
158 return true;
159 }
160
161 void wxStatusBar::SetFieldsCount(int nFields, const int *widths)
162 {
163 // this is a Windows limitation
164 wxASSERT_MSG( (nFields > 0) && (nFields < 255), _T("too many fields") );
165
166 wxStatusBarBase::SetFieldsCount(nFields, widths);
167
168 SetFieldsWidth();
169 }
170
171 void wxStatusBar::SetStatusWidths(int n, const int widths[])
172 {
173 wxStatusBarBase::SetStatusWidths(n, widths);
174
175 SetFieldsWidth();
176 }
177
178 void wxStatusBar::SetFieldsWidth()
179 {
180 if ( m_panes.IsEmpty() )
181 return;
182
183 int aBorders[3];
184 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
185
186 int extraWidth = aBorders[2]; // space between fields
187
188 wxArrayInt widthsAbs =
189 CalculateAbsWidths(GetClientSize().x - extraWidth*(m_panes.GetCount() - 1));
190
191 int *pWidths = new int[m_panes.GetCount()];
192
193 int nCurPos = 0;
194 for ( size_t i = 0; i < m_panes.GetCount(); i++ ) {
195 nCurPos += widthsAbs[i] + extraWidth;
196 pWidths[i] = nCurPos;
197 }
198
199 if ( !StatusBar_SetParts(GetHwnd(), m_panes.GetCount(), pWidths) ) {
200 wxLogLastError(wxT("StatusBar_SetParts"));
201 }
202
203 delete [] pWidths;
204 }
205
206 void wxStatusBar::SetStatusText(const wxString& strText, int nField)
207 {
208 wxCHECK_RET( (nField >= 0) && ((size_t)nField < m_panes.GetCount()),
209 _T("invalid statusbar field index") );
210
211 if ( strText == GetStatusText(nField) )
212 {
213 // don't call StatusBar_SetText() to avoid flicker
214 return;
215 }
216
217 wxStatusBarBase::SetStatusText(strText, nField);
218
219 UpdateFieldText(nField);
220 }
221
222 void wxStatusBar::UpdateFieldText(int nField)
223 {
224 if (!m_pDC)
225 return;
226
227 // Get field style, if any
228 int style;
229 switch(m_panes[nField].nStyle)
230 {
231 case wxSB_RAISED:
232 style = SBT_POPOUT;
233 break;
234 case wxSB_FLAT:
235 style = SBT_NOBORDERS;
236 break;
237
238 case wxSB_NORMAL:
239 default:
240 style = 0;
241 break;
242 }
243
244 wxRect rc;
245 GetFieldRect(nField, rc);
246
247 int margin;
248 if (nField == GetFieldsCount()-1)
249 margin = -6; // windows reports a smaller rect for the last field; enlarge it
250 else
251 margin = 4;
252
253 // do we need to ellipsize this string?
254 wxString ellipsizedStr =
255 wxControl::Ellipsize(GetStatusText(nField), *m_pDC,
256 GetLayoutDirection() == wxLayout_RightToLeft ? wxELLIPSIZE_START : wxELLIPSIZE_END,
257 rc.GetWidth() - margin, // leave a small margin
258 wxELLIPSIZE_EXPAND_TAB);
259
260 // Pass both field number and style. MSDN library doesn't mention
261 // that nField and style have to be 'ORed'
262 if ( !StatusBar_SetText(GetHwnd(), nField | style, ellipsizedStr.wx_str()) )
263 {
264 wxLogLastError(wxT("StatusBar_SetText"));
265 }
266 }
267
268 int wxStatusBar::GetBorderX() const
269 {
270 int aBorders[3];
271 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
272
273 return aBorders[0];
274 }
275
276 int wxStatusBar::GetBorderY() const
277 {
278 int aBorders[3];
279 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
280
281 return aBorders[1];
282 }
283
284 void wxStatusBar::SetMinHeight(int height)
285 {
286 SendMessage(GetHwnd(), SB_SETMINHEIGHT, height + 2*GetBorderY(), 0);
287
288 // have to send a (dummy) WM_SIZE to redraw it now
289 SendMessage(GetHwnd(), WM_SIZE, 0, 0);
290 }
291
292 bool wxStatusBar::GetFieldRect(int i, wxRect& rect) const
293 {
294 wxCHECK_MSG( (i >= 0) && ((size_t)i < m_panes.GetCount()), false,
295 _T("invalid statusbar field index") );
296
297 RECT r;
298 if ( !::SendMessage(GetHwnd(), SB_GETRECT, i, (LPARAM)&r) )
299 {
300 wxLogLastError(wxT("SendMessage(SB_GETRECT)"));
301 }
302
303 #if wxUSE_UXTHEME
304 wxUxThemeHandle theme((wxStatusBar *)this, L"Status"); // const_cast
305 if ( theme )
306 {
307 // by default Windows has a 2 pixel border to the right of the left
308 // divider (or it could be a bug) but it looks wrong so remove it
309 if ( i != 0 )
310 {
311 r.left -= 2;
312 }
313
314 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme, NULL,
315 1 /* SP_PANE */, 0,
316 &r, &r);
317 }
318 #endif
319
320 wxCopyRECTToRect(r, rect);
321
322 return true;
323 }
324
325 wxSize wxStatusBar::DoGetBestSize() const
326 {
327 int borders[3];
328 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)borders);
329
330 // calculate width
331 int width = 0;
332 for ( size_t i = 0; i < m_panes.GetCount(); ++i )
333 {
334 int widthField =
335 m_bSameWidthForAllPanes ? DEFAULT_FIELD_WIDTH : m_panes[i].nWidth;
336 if ( widthField >= 0 )
337 {
338 width += widthField;
339 }
340 else
341 {
342 // variable width field, its width is really a proportion
343 // related to the other fields
344 width += -widthField*DEFAULT_FIELD_WIDTH;
345 }
346
347 // add the space between fields
348 width += borders[2];
349 }
350
351 if ( !width )
352 {
353 // still need something even for an empty status bar
354 width = 2*DEFAULT_FIELD_WIDTH;
355 }
356
357 // calculate height
358 int height;
359 wxGetCharSize(GetHWND(), NULL, &height, GetFont());
360 height = EDIT_HEIGHT_FROM_CHAR_HEIGHT(height);
361 height += borders[1];
362
363 wxSize best(width, height);
364 CacheBestSize(best);
365 return best;
366 }
367
368 void wxStatusBar::DoMoveWindow(int x, int y, int width, int height)
369 {
370 if ( GetParent()->IsSizeDeferred() )
371 {
372 wxWindowMSW::DoMoveWindow(x, y, width, height);
373 }
374 else
375 {
376 // parent pos/size isn't deferred so do it now but don't send
377 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
378 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
379 // if other windows are size deferred
380 ::SetWindowPos(GetHwnd(), NULL, x, y, width, height,
381 SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE
382 #ifndef __WXWINCE__
383 | SWP_NOCOPYBITS | SWP_NOSENDCHANGING
384 #endif
385 );
386 }
387
388 // adjust fields widths to the new size
389 SetFieldsWidth();
390
391 // we have to trigger wxSizeEvent if there are children window in status
392 // bar because GetFieldRect returned incorrect (not updated) values up to
393 // here, which almost certainly resulted in incorrectly redrawn statusbar
394 if ( m_children.GetCount() > 0 )
395 {
396 wxSizeEvent event(GetClientSize(), m_windowId);
397 event.SetEventObject(this);
398 HandleWindowEvent(event);
399 }
400 }
401
402 void wxStatusBar::SetStatusStyles(int n, const int styles[])
403 {
404 wxStatusBarBase::SetStatusStyles(n, styles);
405
406 if (n != (int)m_panes.GetCount())
407 return;
408
409 for (int i = 0; i < n; i++)
410 {
411 int style;
412 switch(styles[i])
413 {
414 case wxSB_RAISED:
415 style = SBT_POPOUT;
416 break;
417 case wxSB_FLAT:
418 style = SBT_NOBORDERS;
419 break;
420 case wxSB_NORMAL:
421 default:
422 style = 0;
423 break;
424 }
425 // The SB_SETTEXT message is both used to set the field's text as well as
426 // the fields' styles. MSDN library doesn't mention
427 // that nField and style have to be 'ORed'
428 wxString text = GetStatusText(i);
429 if (!StatusBar_SetText(GetHwnd(), style | i, text.wx_str()))
430 {
431 wxLogLastError(wxT("StatusBar_SetText"));
432 }
433 }
434 }
435
436 WXLRESULT
437 wxStatusBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
438 {
439 #ifndef __WXWINCE__
440 if ( nMsg == WM_WINDOWPOSCHANGING )
441 {
442 WINDOWPOS *lpPos = (WINDOWPOS *)lParam;
443 int x, y, w, h;
444 GetPosition(&x, &y);
445 GetSize(&w, &h);
446
447 // we need real window coords and not wx client coords
448 AdjustForParentClientOrigin(x, y);
449
450 lpPos->x = x;
451 lpPos->y = y;
452 lpPos->cx = w;
453 lpPos->cy = h;
454
455 return 0;
456 }
457
458 if ( nMsg == WM_NCLBUTTONDOWN )
459 {
460 // if hit-test is on gripper then send message to TLW to begin
461 // resizing. It is possible to send this message to any window.
462 if ( wParam == HTBOTTOMRIGHT )
463 {
464 wxWindow *win;
465
466 for ( win = GetParent(); win; win = win->GetParent() )
467 {
468 if ( win->IsTopLevel() )
469 {
470 SendMessage(GetHwndOf(win), WM_NCLBUTTONDOWN,
471 wParam, lParam);
472
473 return 0;
474 }
475 }
476 }
477 }
478 #endif
479
480 if ( nMsg == WM_SIZE )
481 {
482 for (int i=0; i<GetFieldsCount(); i++)
483 UpdateFieldText(i);
484 // re-set the field text, in case we need to ellipsize
485 // (or de-ellipsize) some parts of it
486 }
487
488 return wxStatusBarBase::MSWWindowProc(nMsg, wParam, lParam);
489 }
490
491 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR