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