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