]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statusbar.cpp
Add support for IEC and SI size units to wxFileName::GetHumanReadableSize().
[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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
28
29 #include "wx/statusbr.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/frame.h"
34 #include "wx/settings.h"
35 #include "wx/dcclient.h"
36 #include "wx/intl.h"
37 #include "wx/log.h"
38 #include "wx/control.h"
39 #endif
40
41 #include "wx/msw/private.h"
42 #include "wx/tooltip.h"
43 #include <windowsx.h>
44
45 #if wxUSE_UXTHEME
46 #include "wx/msw/uxtheme.h"
47 #endif
48
49 // ----------------------------------------------------------------------------
50 // constants
51 // ----------------------------------------------------------------------------
52
53 namespace
54 {
55
56 // no idea for a default width, just choose something
57 static const int DEFAULT_FIELD_WIDTH = 25;
58
59 } // anonymous namespace
60
61 // ----------------------------------------------------------------------------
62 // macros
63 // ----------------------------------------------------------------------------
64
65 // windowsx.h and commctrl.h don't define those, so we do it here
66 #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w)
67 #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
68 #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
69 #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
70
71 // ============================================================================
72 // implementation
73 // ============================================================================
74
75 // ----------------------------------------------------------------------------
76 // wxStatusBar class
77 // ----------------------------------------------------------------------------
78
79 wxStatusBar::wxStatusBar()
80 {
81 SetParent(NULL);
82 m_hWnd = 0;
83 m_windowId = 0;
84 m_pDC = NULL;
85 }
86
87 bool wxStatusBar::Create(wxWindow *parent,
88 wxWindowID id,
89 long style,
90 const wxString& name)
91 {
92 wxCHECK_MSG( parent, false, "status bar must have a parent" );
93
94 SetName(name);
95 SetWindowStyleFlag(style);
96 SetParent(parent);
97
98 parent->AddChild(this);
99
100 m_windowId = id == wxID_ANY ? NewControlId() : id;
101
102 DWORD wstyle = WS_CHILD | WS_VISIBLE;
103
104 if ( style & wxCLIP_SIBLINGS )
105 wstyle |= WS_CLIPSIBLINGS;
106
107 // wxSTB_SIZEGRIP is part of our default style but it doesn't make sense to
108 // show size grip if this is the status bar of a non-resizeable TLW so turn
109 // it off in such case
110 if ( parent->IsTopLevel() && !parent->HasFlag(wxRESIZE_BORDER) )
111 style &= ~wxSTB_SIZEGRIP;
112
113 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
114 // (at least in the version of comctl32.dll I'm using), and the only way to
115 // turn it off is to use CCS_TOP style - as we position the status bar
116 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxSTB_SIZEGRIP
117 // is not given
118 if ( !(style & wxSTB_SIZEGRIP) )
119 {
120 wstyle |= CCS_TOP;
121 }
122 else
123 {
124 #ifndef __WXWINCE__
125 // may be some versions of comctl32.dll do need it - anyhow, it won't
126 // do any harm
127 wstyle |= SBARS_SIZEGRIP;
128 #endif
129 }
130
131 m_hWnd = CreateWindow
132 (
133 STATUSCLASSNAME,
134 wxT(""),
135 wstyle,
136 0, 0, 0, 0,
137 GetHwndOf(parent),
138 (HMENU)wxUIntToPtr(m_windowId.GetValue()),
139 wxGetInstance(),
140 NULL
141 );
142 if ( m_hWnd == 0 )
143 {
144 wxLogSysError(_("Failed to create a status bar."));
145
146 return false;
147 }
148
149 SetFieldsCount(1);
150 SubclassWin(m_hWnd);
151
152 // cache the DC instance used by DoUpdateStatusText:
153 // NOTE: create the DC before calling InheritAttributes() since
154 // it may result in a call to our SetFont()
155 m_pDC = new wxClientDC(this);
156
157 InheritAttributes();
158
159 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR));
160
161 // we must refresh the frame size when the statusbar is created, because
162 // its client area might change
163 //
164 // notice that we must post the event, not send it, as the frame doesn't
165 // know that we're its status bar yet so laying it out right now wouldn't
166 // work correctly, we need to wait until we return to the main loop
167 PostSizeEventToParent();
168
169 return true;
170 }
171
172 wxStatusBar::~wxStatusBar()
173 {
174 // we must refresh the frame size when the statusbar is deleted but the
175 // frame is not - otherwise statusbar leaves a hole in the place it used to
176 // occupy
177 PostSizeEventToParent();
178
179 // delete existing tooltips
180 for (size_t i=0; i<m_tooltips.size(); i++)
181 {
182 if (m_tooltips[i])
183 {
184 delete m_tooltips[i];
185 m_tooltips[i] = NULL;
186 }
187 }
188
189 wxDELETE(m_pDC);
190 }
191
192 bool wxStatusBar::SetFont(const wxFont& font)
193 {
194 if (!wxWindow::SetFont(font))
195 return false;
196
197 if (m_pDC) m_pDC->SetFont(font);
198 return true;
199 }
200
201 void wxStatusBar::SetFieldsCount(int nFields, const int *widths)
202 {
203 // this is a Windows limitation
204 wxASSERT_MSG( (nFields > 0) && (nFields < 255), "too many fields" );
205
206 wxStatusBarBase::SetFieldsCount(nFields, widths);
207
208 MSWUpdateFieldsWidths();
209
210 // keep in synch also our m_tooltips array
211
212 // reset all current tooltips
213 for (size_t i=0; i<m_tooltips.size(); i++)
214 {
215 if (m_tooltips[i])
216 {
217 delete m_tooltips[i];
218 m_tooltips[i] = NULL;
219 }
220 }
221
222 // shrink/expand the array:
223 m_tooltips.resize(m_panes.GetCount(), NULL);
224 }
225
226 void wxStatusBar::SetStatusWidths(int n, const int widths[])
227 {
228 wxStatusBarBase::SetStatusWidths(n, widths);
229
230 MSWUpdateFieldsWidths();
231 }
232
233 void wxStatusBar::MSWUpdateFieldsWidths()
234 {
235 if ( m_panes.IsEmpty() )
236 return;
237
238 const int count = m_panes.GetCount();
239
240 const int extraWidth = MSWGetBorderWidth() + MSWGetMetrics().textMargin;
241
242 // compute the effectively available amount of space:
243 int widthAvailable = GetClientSize().x; // start with the entire width
244 widthAvailable -= extraWidth*(count - 1); // extra space between fields
245 widthAvailable -= MSWGetMetrics().textMargin; // and for the last field
246
247 if ( HasFlag(wxSTB_SIZEGRIP) )
248 widthAvailable -= MSWGetMetrics().gripWidth;
249
250 // distribute the available space (client width) among the various fields:
251
252 wxArrayInt widthsAbs = CalculateAbsWidths(widthAvailable);
253
254
255 // update the field widths in the native control:
256
257 int *pWidths = new int[count];
258
259 int nCurPos = 0;
260 for ( int i = 0; i < count; i++ )
261 {
262 nCurPos += widthsAbs[i] + extraWidth;
263 pWidths[i] = nCurPos;
264 }
265
266 if ( !StatusBar_SetParts(GetHwnd(), count, pWidths) )
267 {
268 wxLogLastError("StatusBar_SetParts");
269 }
270
271 delete [] pWidths;
272
273
274 // FIXME: we may want to call DoUpdateStatusText() here since we may need to (de)ellipsize status texts
275 }
276
277 void wxStatusBar::DoUpdateStatusText(int nField)
278 {
279 if (!m_pDC)
280 return;
281
282 // Get field style, if any
283 int style;
284 switch(m_panes[nField].GetStyle())
285 {
286 case wxSB_RAISED:
287 style = SBT_POPOUT;
288 break;
289 case wxSB_FLAT:
290 style = SBT_NOBORDERS;
291 break;
292
293 case wxSB_NORMAL:
294 default:
295 style = 0;
296 break;
297 }
298
299 wxRect rc;
300 GetFieldRect(nField, rc);
301
302 const int maxWidth = rc.GetWidth() - MSWGetMetrics().textMargin;
303
304 wxString text = GetStatusText(nField);
305
306 // do we need to ellipsize this string?
307 wxEllipsizeMode ellmode = (wxEllipsizeMode)-1;
308 if (HasFlag(wxSTB_ELLIPSIZE_START)) ellmode = wxELLIPSIZE_START;
309 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE)) ellmode = wxELLIPSIZE_MIDDLE;
310 else if (HasFlag(wxSTB_ELLIPSIZE_END)) ellmode = wxELLIPSIZE_END;
311
312 if (ellmode == (wxEllipsizeMode)-1)
313 {
314 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
315 // we don't ellipsize the text but just truncate it
316 if (HasFlag(wxSTB_SHOW_TIPS))
317 SetEllipsizedFlag(nField, m_pDC->GetTextExtent(text).GetWidth() > maxWidth);
318 }
319 else
320 {
321 text = wxControl::Ellipsize(text,
322 *m_pDC,
323 ellmode,
324 maxWidth,
325 wxELLIPSIZE_FLAGS_EXPAND_TABS);
326
327 // update the ellipsization status for this pane; this is used later to
328 // decide whether a tooltip should be shown or not for this pane
329 // (if we have wxSTB_SHOW_TIPS)
330 SetEllipsizedFlag(nField, text != GetStatusText(nField));
331 }
332
333 // Set the status text in the native control passing both field number and style.
334 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
335 if ( !StatusBar_SetText(GetHwnd(), nField | style, text.wx_str()) )
336 {
337 wxLogLastError("StatusBar_SetText");
338 }
339
340 if (HasFlag(wxSTB_SHOW_TIPS))
341 {
342 wxASSERT(m_tooltips.size() == m_panes.GetCount());
343
344 if (m_tooltips[nField])
345 {
346 if (GetField(nField).IsEllipsized())
347 {
348 // update the rect of this tooltip:
349 m_tooltips[nField]->SetRect(rc);
350
351 // update also the text:
352 m_tooltips[nField]->SetTip(GetStatusText(nField));
353 }
354 else
355 {
356 // delete the tooltip associated with this pane; it's not needed anymore
357 delete m_tooltips[nField];
358 m_tooltips[nField] = NULL;
359 }
360 }
361 else
362 {
363 // create a new tooltip for this pane if needed
364 if (GetField(nField).IsEllipsized())
365 m_tooltips[nField] = new wxToolTip(this, nField, GetStatusText(nField), rc);
366 //else: leave m_tooltips[nField]==NULL
367 }
368 }
369 }
370
371 wxStatusBar::MSWBorders wxStatusBar::MSWGetBorders() const
372 {
373 int aBorders[3];
374 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
375
376 MSWBorders borders;
377 borders.horz = aBorders[0];
378 borders.vert = aBorders[1];
379 borders.between = aBorders[2];
380 return borders;
381 }
382
383 int wxStatusBar::GetBorderX() const
384 {
385 return MSWGetBorders().horz;
386 }
387
388 int wxStatusBar::GetBorderY() const
389 {
390 return MSWGetBorders().vert;
391 }
392
393 int wxStatusBar::MSWGetBorderWidth() const
394 {
395 return MSWGetBorders().between;
396 }
397
398 /* static */
399 const wxStatusBar::MSWMetrics& wxStatusBar::MSWGetMetrics()
400 {
401 static MSWMetrics s_metrics = { 0 };
402 if ( !s_metrics.textMargin )
403 {
404 // Grip size should be self explanatory (the only problem with it is
405 // that it's hard coded as we don't know how to find its size using
406 // API) but the margin might merit an explanation: Windows offsets the
407 // text drawn in status bar panes so we need to take this extra margin
408 // into account to make sure the text drawn by user fits inside the
409 // pane. Notice that it's not the value returned by SB_GETBORDERS
410 // which, at least on this Windows 2003 system, returns {0, 2, 2}
411 if ( wxUxThemeEngine::GetIfActive() )
412 {
413 s_metrics.gripWidth = 20;
414 s_metrics.textMargin = 8;
415 }
416 else // classic/unthemed look
417 {
418 s_metrics.gripWidth = 18;
419 s_metrics.textMargin = 4;
420 }
421 }
422
423 return s_metrics;
424 }
425
426 void wxStatusBar::SetMinHeight(int height)
427 {
428 SendMessage(GetHwnd(), SB_SETMINHEIGHT, height + 2*GetBorderY(), 0);
429
430 // we have to send a (dummy) WM_SIZE to redraw it now
431 SendMessage(GetHwnd(), WM_SIZE, 0, 0);
432 }
433
434 bool wxStatusBar::GetFieldRect(int i, wxRect& rect) const
435 {
436 wxCHECK_MSG( (i >= 0) && ((size_t)i < m_panes.GetCount()), false,
437 "invalid statusbar field index" );
438
439 RECT r;
440 if ( !::SendMessage(GetHwnd(), SB_GETRECT, i, (LPARAM)&r) )
441 {
442 wxLogLastError("SendMessage(SB_GETRECT)");
443 }
444
445 #if wxUSE_UXTHEME
446 wxUxThemeHandle theme(const_cast<wxStatusBar*>(this), L"Status");
447 if ( theme )
448 {
449 // by default Windows has a 2 pixel border to the right of the left
450 // divider (or it could be a bug) but it looks wrong so remove it
451 if ( i != 0 )
452 {
453 r.left -= 2;
454 }
455
456 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme, NULL,
457 1 /* SP_PANE */, 0,
458 &r, &r);
459 }
460 #endif
461
462 wxCopyRECTToRect(r, rect);
463
464 // Windows seems to under-report the size of the last field rectangle,
465 // presumably in order to prevent the buggy applications from overflowing
466 // onto the size grip but we want to return the real size to wx users
467 if ( HasFlag(wxSTB_SIZEGRIP) && i == (int)m_panes.GetCount() - 1 )
468 {
469 rect.width += MSWGetMetrics().gripWidth - MSWGetBorderWidth();
470 }
471
472 return true;
473 }
474
475 wxSize wxStatusBar::DoGetBestSize() const
476 {
477 const MSWBorders borders = MSWGetBorders();
478
479 // calculate width
480 int width = 0;
481 for ( size_t i = 0; i < m_panes.GetCount(); ++i )
482 {
483 int widthField =
484 m_bSameWidthForAllPanes ? DEFAULT_FIELD_WIDTH : m_panes[i].GetWidth();
485 if ( widthField >= 0 )
486 {
487 width += widthField;
488 }
489 else
490 {
491 // variable width field, its width is really a proportion
492 // related to the other fields
493 width += -widthField*DEFAULT_FIELD_WIDTH;
494 }
495
496 // add the space between fields
497 width += borders.between;
498 }
499
500 if ( !width )
501 {
502 // still need something even for an empty status bar
503 width = 2*DEFAULT_FIELD_WIDTH;
504 }
505
506 // calculate height
507 int height;
508 wxGetCharSize(GetHWND(), NULL, &height, GetFont());
509 height = EDIT_HEIGHT_FROM_CHAR_HEIGHT(height);
510 height += borders.vert;
511
512 wxSize best(width, height);
513 CacheBestSize(best);
514 return best;
515 }
516
517 void wxStatusBar::DoMoveWindow(int x, int y, int width, int height)
518 {
519 if ( GetParent()->IsSizeDeferred() )
520 {
521 wxWindowMSW::DoMoveWindow(x, y, width, height);
522 }
523 else
524 {
525 // parent pos/size isn't deferred so do it now but don't send
526 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
527 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
528 // if other windows are size deferred
529 ::SetWindowPos(GetHwnd(), NULL, x, y, width, height,
530 SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE
531 #ifndef __WXWINCE__
532 | SWP_NOCOPYBITS | SWP_NOSENDCHANGING
533 #endif
534 );
535 }
536
537 // adjust fields widths to the new size
538 MSWUpdateFieldsWidths();
539
540 // we have to trigger wxSizeEvent if there are children window in status
541 // bar because GetFieldRect returned incorrect (not updated) values up to
542 // here, which almost certainly resulted in incorrectly redrawn statusbar
543 if ( m_children.GetCount() > 0 )
544 {
545 wxSizeEvent event(GetClientSize(), m_windowId);
546 event.SetEventObject(this);
547 HandleWindowEvent(event);
548 }
549 }
550
551 void wxStatusBar::SetStatusStyles(int n, const int styles[])
552 {
553 wxStatusBarBase::SetStatusStyles(n, styles);
554
555 if (n != (int)m_panes.GetCount())
556 return;
557
558 for (int i = 0; i < n; i++)
559 {
560 int style;
561 switch(styles[i])
562 {
563 case wxSB_RAISED:
564 style = SBT_POPOUT;
565 break;
566 case wxSB_FLAT:
567 style = SBT_NOBORDERS;
568 break;
569 case wxSB_NORMAL:
570 default:
571 style = 0;
572 break;
573 }
574
575 // The SB_SETTEXT message is both used to set the field's text as well as
576 // the fields' styles.
577 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
578 wxString text = GetStatusText(i);
579 if (!StatusBar_SetText(GetHwnd(), style | i, text.wx_str()))
580 {
581 wxLogLastError("StatusBar_SetText");
582 }
583 }
584 }
585
586 WXLRESULT
587 wxStatusBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
588 {
589 #ifndef __WXWINCE__
590 if ( nMsg == WM_WINDOWPOSCHANGING )
591 {
592 WINDOWPOS *lpPos = (WINDOWPOS *)lParam;
593 int x, y, w, h;
594 GetPosition(&x, &y);
595 GetSize(&w, &h);
596
597 // we need real window coords and not wx client coords
598 AdjustForParentClientOrigin(x, y);
599
600 lpPos->x = x;
601 lpPos->y = y;
602 lpPos->cx = w;
603 lpPos->cy = h;
604
605 return 0;
606 }
607
608 if ( nMsg == WM_NCLBUTTONDOWN )
609 {
610 // if hit-test is on gripper then send message to TLW to begin
611 // resizing. It is possible to send this message to any window.
612 if ( wParam == HTBOTTOMRIGHT )
613 {
614 wxWindow *win;
615
616 for ( win = GetParent(); win; win = win->GetParent() )
617 {
618 if ( win->IsTopLevel() )
619 {
620 SendMessage(GetHwndOf(win), WM_NCLBUTTONDOWN,
621 wParam, lParam);
622
623 return 0;
624 }
625 }
626 }
627 }
628 #endif
629
630 bool needsEllipsization = HasFlag(wxSTB_ELLIPSIZE_START) ||
631 HasFlag(wxSTB_ELLIPSIZE_MIDDLE) ||
632 HasFlag(wxSTB_ELLIPSIZE_END);
633 if ( nMsg == WM_SIZE && needsEllipsization )
634 {
635 for (int i=0; i<GetFieldsCount(); i++)
636 DoUpdateStatusText(i);
637 // re-set the field text, in case we need to ellipsize
638 // (or de-ellipsize) some parts of it
639 }
640
641 return wxStatusBarBase::MSWWindowProc(nMsg, wParam, lParam);
642 }
643
644 #if wxUSE_TOOLTIPS
645 bool wxStatusBar::MSWProcessMessage(WXMSG* pMsg)
646 {
647 if ( HasFlag(wxSTB_SHOW_TIPS) )
648 {
649 // for a tooltip to be shown, we need to relay mouse events to it;
650 // this is typically done by wxWindowMSW::MSWProcessMessage but only
651 // if wxWindow::m_tooltip pointer is non-NULL.
652 // Since wxStatusBar has multiple tooltips for a single HWND, it keeps
653 // wxWindow::m_tooltip == NULL and then relays mouse events here:
654 MSG *msg = (MSG *)pMsg;
655 if ( msg->message == WM_MOUSEMOVE )
656 wxToolTip::RelayEvent(pMsg);
657 }
658
659 return wxWindow::MSWProcessMessage(pMsg);
660 }
661
662 bool wxStatusBar::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM* WXUNUSED(result))
663 {
664 if ( HasFlag(wxSTB_SHOW_TIPS) )
665 {
666 // see comment in wxStatusBar::MSWProcessMessage for more info;
667 // basically we need to override wxWindow::MSWOnNotify because
668 // we have wxWindow::m_tooltip always NULL but we still use tooltips...
669
670 NMHDR* hdr = (NMHDR *)lParam;
671
672 wxString str;
673 if (hdr->idFrom < m_tooltips.size() && m_tooltips[hdr->idFrom])
674 str = m_tooltips[hdr->idFrom]->GetTip();
675
676 if ( HandleTooltipNotify(hdr->code, lParam, str))
677 {
678 // processed
679 return true;
680 }
681 }
682
683 return false;
684 }
685 #endif // wxUSE_TOOLTIPS
686
687 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR