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