]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statusbar.cpp
Skip VarFileInfo block when using windres.
[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 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
20
21 #include "wx/statusbr.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
25 #include "wx/frame.h"
26 #include "wx/settings.h"
27 #include "wx/dcclient.h"
28 #include "wx/intl.h"
29 #include "wx/log.h"
30 #include "wx/control.h"
31 #endif
32
33 #include "wx/msw/private.h"
34 #include "wx/tooltip.h"
35 #include <windowsx.h>
36
37 #if wxUSE_UXTHEME
38 #include "wx/msw/uxtheme.h"
39 #endif
40
41 // no idea for a default width, just choose something
42 #define DEFAULT_FIELD_WIDTH 25
43
44 // ----------------------------------------------------------------------------
45 // macros
46 // ----------------------------------------------------------------------------
47
48 // windowsx.h and commctrl.h don't define those, so we do it here
49 #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w)
50 #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
51 #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
52 #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // wxStatusBar class
60 // ----------------------------------------------------------------------------
61
62 wxStatusBar::wxStatusBar()
63 {
64 SetParent(NULL);
65 m_hWnd = 0;
66 m_windowId = 0;
67 m_pDC = NULL;
68 }
69
70 bool wxStatusBar::Create(wxWindow *parent,
71 wxWindowID id,
72 long style,
73 const wxString& name)
74 {
75 wxCHECK_MSG( parent, false, "status bar must have a parent" );
76
77 SetName(name);
78 SetWindowStyleFlag(style);
79 SetParent(parent);
80
81 parent->AddChild(this);
82
83 m_windowId = id == wxID_ANY ? NewControlId() : id;
84
85 DWORD wstyle = WS_CHILD | WS_VISIBLE;
86
87 if ( style & wxCLIP_SIBLINGS )
88 wstyle |= WS_CLIPSIBLINGS;
89
90 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
91 // (at least in the version of comctl32.dll I'm using), and the only way to
92 // turn it off is to use CCS_TOP style - as we position the status bar
93 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxSTB_SIZEGRIP
94 // is not given
95 if ( !(style & wxSTB_SIZEGRIP) )
96 {
97 wstyle |= CCS_TOP;
98 }
99 else
100 {
101 #ifndef __WXWINCE__
102 // may be some versions of comctl32.dll do need it - anyhow, it won't
103 // do any harm
104 wstyle |= SBARS_SIZEGRIP;
105 #endif
106 }
107
108 m_hWnd = CreateWindow
109 (
110 STATUSCLASSNAME,
111 wxT(""),
112 wstyle,
113 0, 0, 0, 0,
114 GetHwndOf(parent),
115 (HMENU)wxUIntToPtr(m_windowId.GetValue()),
116 wxGetInstance(),
117 NULL
118 );
119 if ( m_hWnd == 0 )
120 {
121 wxLogSysError(_("Failed to create a status bar."));
122
123 return false;
124 }
125
126 SetFieldsCount(1);
127 SubclassWin(m_hWnd);
128
129 // cache the DC instance used by UpdateFieldText:
130 // NOTE: create the DC before calling InheritAttributes() since
131 // it may result in a call to our SetFont()
132 m_pDC = new wxClientDC(this);
133
134 InheritAttributes();
135
136 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR));
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 if (m_tooltips[i])
160 {
161 delete m_tooltips[i];
162 m_tooltips[i] = NULL;
163 }
164 }
165
166 wxDELETE(m_pDC);
167 }
168
169 bool wxStatusBar::SetFont(const wxFont& font)
170 {
171 if (!wxWindow::SetFont(font))
172 return false;
173
174 if (m_pDC) m_pDC->SetFont(font);
175 return true;
176 }
177
178 void wxStatusBar::SetFieldsCount(int nFields, const int *widths)
179 {
180 // this is a Windows limitation
181 wxASSERT_MSG( (nFields > 0) && (nFields < 255), "too many fields" );
182
183 wxStatusBarBase::SetFieldsCount(nFields, widths);
184
185 SetFieldsWidth();
186
187 // keep in synch also our m_tooltips array
188
189 // reset all current tooltips
190 for (size_t i=0; i<m_tooltips.size(); i++)
191 {
192 if (m_tooltips[i])
193 {
194 delete m_tooltips[i];
195 m_tooltips[i] = NULL;
196 }
197 }
198
199 // shrink/expand the array:
200 m_tooltips.resize(m_panes.GetCount(), NULL);
201 }
202
203 void wxStatusBar::SetStatusWidths(int n, const int widths[])
204 {
205 wxStatusBarBase::SetStatusWidths(n, widths);
206
207 SetFieldsWidth();
208 }
209
210 void wxStatusBar::SetFieldsWidth()
211 {
212 if ( m_panes.IsEmpty() )
213 return;
214
215 int aBorders[3];
216 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
217
218 int extraWidth = aBorders[2]; // space between fields
219
220
221 // distribute the available space (client width) among the various fields:
222
223 wxArrayInt widthsAbs =
224 CalculateAbsWidths(GetClientSize().x - extraWidth*(m_panes.GetCount() - 1));
225
226
227 // update the field widths in the native control:
228
229 int *pWidths = new int[m_panes.GetCount()];
230
231 int nCurPos = 0;
232 for ( size_t i = 0; i < m_panes.GetCount(); i++ )
233 {
234 nCurPos += widthsAbs[i] + extraWidth;
235 pWidths[i] = nCurPos;
236 }
237
238 if ( !StatusBar_SetParts(GetHwnd(), m_panes.GetCount(), pWidths) )
239 {
240 wxLogLastError("StatusBar_SetParts");
241 }
242
243 delete [] pWidths;
244
245
246 // FIXME: we may want to call UpdateFieldText() here since we may need to (de)ellipsize status texts
247 }
248
249 void wxStatusBar::SetStatusText(const wxString& strText, int nField)
250 {
251 wxCHECK_RET( (nField >= 0) && ((size_t)nField < m_panes.GetCount()),
252 "invalid statusbar field index" );
253
254 if ( strText == GetStatusText(nField) )
255 {
256 // don't call StatusBar_SetText() to avoid flicker
257 return;
258 }
259
260 wxStatusBarBase::SetStatusText(strText, nField);
261
262 UpdateFieldText(nField);
263 }
264
265 void wxStatusBar::UpdateFieldText(int nField)
266 {
267 if (!m_pDC)
268 return;
269
270 // Get field style, if any
271 int style;
272 switch(m_panes[nField].GetStyle())
273 {
274 case wxSB_RAISED:
275 style = SBT_POPOUT;
276 break;
277 case wxSB_FLAT:
278 style = SBT_NOBORDERS;
279 break;
280
281 case wxSB_NORMAL:
282 default:
283 style = 0;
284 break;
285 }
286
287 wxRect rc;
288 GetFieldRect(nField, rc);
289
290 int margin;
291 if (nField == GetFieldsCount()-1)
292 margin = -6; // windows reports a smaller rect for the last field; enlarge it
293 else
294 margin = 4;
295
296 int maxWidth = rc.GetWidth() - margin; // leave a small margin
297 wxString text = GetStatusText(nField);
298
299 // do we need to ellipsize this string?
300 wxEllipsizeMode ellmode = (wxEllipsizeMode)-1;
301 if (HasFlag(wxSTB_ELLIPSIZE_START)) ellmode = wxELLIPSIZE_START;
302 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE)) ellmode = wxELLIPSIZE_MIDDLE;
303 else if (HasFlag(wxSTB_ELLIPSIZE_END)) ellmode = wxELLIPSIZE_END;
304
305 if (ellmode == (wxEllipsizeMode)-1)
306 {
307 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
308 // we don't ellipsize the text but just truncate it
309 if (HasFlag(wxSTB_SHOW_TIPS))
310 SetEllipsizedFlag(nField, m_pDC->GetTextExtent(text).GetWidth() > maxWidth);
311 }
312 else
313 {
314 text = wxControl::Ellipsize(text,
315 *m_pDC,
316 ellmode,
317 maxWidth,
318 wxELLIPSIZE_EXPAND_TAB);
319
320 // update the ellipsization status for this pane; this is used later to
321 // decide whether a tooltip should be shown or not for this pane
322 // (if we have wxSTB_SHOW_TIPS)
323 SetEllipsizedFlag(nField, text != GetStatusText(nField));
324 }
325
326 // Set the status text in the native control passing both field number and style.
327 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
328 if ( !StatusBar_SetText(GetHwnd(), nField | style, text.wx_str()) )
329 {
330 wxLogLastError("StatusBar_SetText");
331 }
332
333 if (HasFlag(wxSTB_SHOW_TIPS))
334 {
335 wxASSERT(m_tooltips.size() == m_panes.GetCount());
336
337 if (m_tooltips[nField])
338 {
339 if (GetField(nField).IsEllipsized())
340 {
341 // update the rect of this tooltip:
342 m_tooltips[nField]->SetRect(rc);
343
344 // update also the text:
345 m_tooltips[nField]->SetTip(GetStatusText(nField));
346 }
347 else
348 {
349 // delete the tooltip associated with this pane; it's not needed anymore
350 delete m_tooltips[nField];
351 m_tooltips[nField] = NULL;
352 }
353 }
354 else
355 {
356 // create a new tooltip for this pane if needed
357 if (GetField(nField).IsEllipsized())
358 m_tooltips[nField] = new wxToolTip(this, nField, GetStatusText(nField), rc);
359 //else: leave m_tooltips[nField]==NULL
360 }
361 }
362 }
363
364 int wxStatusBar::GetBorderX() const
365 {
366 int aBorders[3];
367 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
368
369 return aBorders[0];
370 }
371
372 int wxStatusBar::GetBorderY() const
373 {
374 int aBorders[3];
375 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
376
377 return aBorders[1];
378 }
379
380 void wxStatusBar::SetMinHeight(int height)
381 {
382 SendMessage(GetHwnd(), SB_SETMINHEIGHT, height + 2*GetBorderY(), 0);
383
384 // we have to send a (dummy) WM_SIZE to redraw it now
385 SendMessage(GetHwnd(), WM_SIZE, 0, 0);
386 }
387
388 bool wxStatusBar::GetFieldRect(int i, wxRect& rect) const
389 {
390 wxCHECK_MSG( (i >= 0) && ((size_t)i < m_panes.GetCount()), false,
391 "invalid statusbar field index" );
392
393 RECT r;
394 if ( !::SendMessage(GetHwnd(), SB_GETRECT, i, (LPARAM)&r) )
395 {
396 wxLogLastError("SendMessage(SB_GETRECT)");
397 }
398
399 #if wxUSE_UXTHEME
400 wxUxThemeHandle theme(const_cast<wxStatusBar*>(this), L"Status");
401 if ( theme )
402 {
403 // by default Windows has a 2 pixel border to the right of the left
404 // divider (or it could be a bug) but it looks wrong so remove it
405 if ( i != 0 )
406 {
407 r.left -= 2;
408 }
409
410 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme, NULL,
411 1 /* SP_PANE */, 0,
412 &r, &r);
413 }
414 #endif
415
416 wxCopyRECTToRect(r, rect);
417
418 return true;
419 }
420
421 wxSize wxStatusBar::DoGetBestSize() const
422 {
423 int borders[3];
424 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)borders);
425
426 // calculate width
427 int width = 0;
428 for ( size_t i = 0; i < m_panes.GetCount(); ++i )
429 {
430 int widthField =
431 m_bSameWidthForAllPanes ? DEFAULT_FIELD_WIDTH : m_panes[i].GetWidth();
432 if ( widthField >= 0 )
433 {
434 width += widthField;
435 }
436 else
437 {
438 // variable width field, its width is really a proportion
439 // related to the other fields
440 width += -widthField*DEFAULT_FIELD_WIDTH;
441 }
442
443 // add the space between fields
444 width += borders[2];
445 }
446
447 if ( !width )
448 {
449 // still need something even for an empty status bar
450 width = 2*DEFAULT_FIELD_WIDTH;
451 }
452
453 // calculate height
454 int height;
455 wxGetCharSize(GetHWND(), NULL, &height, GetFont());
456 height = EDIT_HEIGHT_FROM_CHAR_HEIGHT(height);
457 height += borders[1];
458
459 wxSize best(width, height);
460 CacheBestSize(best);
461 return best;
462 }
463
464 void wxStatusBar::DoMoveWindow(int x, int y, int width, int height)
465 {
466 if ( GetParent()->IsSizeDeferred() )
467 {
468 wxWindowMSW::DoMoveWindow(x, y, width, height);
469 }
470 else
471 {
472 // parent pos/size isn't deferred so do it now but don't send
473 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
474 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
475 // if other windows are size deferred
476 ::SetWindowPos(GetHwnd(), NULL, x, y, width, height,
477 SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE
478 #ifndef __WXWINCE__
479 | SWP_NOCOPYBITS | SWP_NOSENDCHANGING
480 #endif
481 );
482 }
483
484 // adjust fields widths to the new size
485 SetFieldsWidth();
486
487 // we have to trigger wxSizeEvent if there are children window in status
488 // bar because GetFieldRect returned incorrect (not updated) values up to
489 // here, which almost certainly resulted in incorrectly redrawn statusbar
490 if ( m_children.GetCount() > 0 )
491 {
492 wxSizeEvent event(GetClientSize(), m_windowId);
493 event.SetEventObject(this);
494 HandleWindowEvent(event);
495 }
496 }
497
498 void wxStatusBar::SetStatusStyles(int n, const int styles[])
499 {
500 wxStatusBarBase::SetStatusStyles(n, styles);
501
502 if (n != (int)m_panes.GetCount())
503 return;
504
505 for (int i = 0; i < n; i++)
506 {
507 int style;
508 switch(styles[i])
509 {
510 case wxSB_RAISED:
511 style = SBT_POPOUT;
512 break;
513 case wxSB_FLAT:
514 style = SBT_NOBORDERS;
515 break;
516 case wxSB_NORMAL:
517 default:
518 style = 0;
519 break;
520 }
521
522 // The SB_SETTEXT message is both used to set the field's text as well as
523 // the fields' styles.
524 // NOTE: MSDN library doesn't mention that nField and style have to be 'ORed'
525 wxString text = GetStatusText(i);
526 if (!StatusBar_SetText(GetHwnd(), style | i, text.wx_str()))
527 {
528 wxLogLastError("StatusBar_SetText");
529 }
530 }
531 }
532
533 WXLRESULT
534 wxStatusBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
535 {
536 #ifndef __WXWINCE__
537 if ( nMsg == WM_WINDOWPOSCHANGING )
538 {
539 WINDOWPOS *lpPos = (WINDOWPOS *)lParam;
540 int x, y, w, h;
541 GetPosition(&x, &y);
542 GetSize(&w, &h);
543
544 // we need real window coords and not wx client coords
545 AdjustForParentClientOrigin(x, y);
546
547 lpPos->x = x;
548 lpPos->y = y;
549 lpPos->cx = w;
550 lpPos->cy = h;
551
552 return 0;
553 }
554
555 if ( nMsg == WM_NCLBUTTONDOWN )
556 {
557 // if hit-test is on gripper then send message to TLW to begin
558 // resizing. It is possible to send this message to any window.
559 if ( wParam == HTBOTTOMRIGHT )
560 {
561 wxWindow *win;
562
563 for ( win = GetParent(); win; win = win->GetParent() )
564 {
565 if ( win->IsTopLevel() )
566 {
567 SendMessage(GetHwndOf(win), WM_NCLBUTTONDOWN,
568 wParam, lParam);
569
570 return 0;
571 }
572 }
573 }
574 }
575 #endif
576
577 bool needsEllipsization = HasFlag(wxSTB_ELLIPSIZE_START) ||
578 HasFlag(wxSTB_ELLIPSIZE_MIDDLE) ||
579 HasFlag(wxSTB_ELLIPSIZE_END);
580 if ( nMsg == WM_SIZE && needsEllipsization )
581 {
582 for (int i=0; i<GetFieldsCount(); i++)
583 UpdateFieldText(i);
584 // re-set the field text, in case we need to ellipsize
585 // (or de-ellipsize) some parts of it
586 }
587
588 return wxStatusBarBase::MSWWindowProc(nMsg, wParam, lParam);
589 }
590
591 #if wxUSE_TOOLTIPS
592 bool wxStatusBar::MSWProcessMessage(WXMSG* pMsg)
593 {
594 if ( HasFlag(wxSTB_SHOW_TIPS) )
595 {
596 // for a tooltip to be shown, we need to relay mouse events to it;
597 // this is typically done by wxWindowMSW::MSWProcessMessage but only
598 // if wxWindow::m_tooltip pointer is non-NULL.
599 // Since wxStatusBar has multiple tooltips for a single HWND, it keeps
600 // wxWindow::m_tooltip == NULL and then relays mouse events here:
601 MSG *msg = (MSG *)pMsg;
602 if ( msg->message == WM_MOUSEMOVE )
603 wxToolTip::RelayEvent(pMsg);
604 }
605
606 return wxWindow::MSWProcessMessage(pMsg);
607 }
608
609 bool wxStatusBar::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM* WXUNUSED(result))
610 {
611 if ( HasFlag(wxSTB_SHOW_TIPS) )
612 {
613 // see comment in wxStatusBar::MSWProcessMessage for more info;
614 // basically we need to override wxWindow::MSWOnNotify because
615 // we have wxWindow::m_tooltip always NULL but we still use tooltips...
616
617 NMHDR* hdr = (NMHDR *)lParam;
618
619 wxString str;
620 if (hdr->idFrom < m_tooltips.size() && m_tooltips[hdr->idFrom])
621 str = m_tooltips[hdr->idFrom]->GetTip();
622
623 if ( HandleTooltipNotify(hdr->code, lParam, str))
624 {
625 // processed
626 return true;
627 }
628 }
629
630 return false;
631 }
632 #endif // wxUSE_TOOLTIPS
633
634 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR