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