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