]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statusbar.cpp
fix memory leak; allocate the DC before SetFont() is called on the status bar
[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 #endif
31
32 #include "wx/msw/private.h"
33 #include <windowsx.h>
34
35 #if wxUSE_UXTHEME
36 #include "wx/msw/uxtheme.h"
37 #endif
38
39 // no idea for a default width, just choose something
40 #define DEFAULT_FIELD_WIDTH 25
41
42 // ----------------------------------------------------------------------------
43 // macros
44 // ----------------------------------------------------------------------------
45
46 // windowsx.h and commctrl.h don't define those, so we do it here
47 #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w)
48 #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
49 #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
50 #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
51
52 // ============================================================================
53 // implementation
54 // ============================================================================
55
56 // ----------------------------------------------------------------------------
57 // wxStatusBar class
58 // ----------------------------------------------------------------------------
59
60 wxStatusBar::wxStatusBar()
61 {
62 SetParent(NULL);
63 m_hWnd = 0;
64 m_windowId = 0;
65 m_pDC = NULL;
66 }
67
68 bool wxStatusBar::Create(wxWindow *parent,
69 wxWindowID id,
70 long style,
71 const wxString& name)
72 {
73 wxCHECK_MSG( parent, false, wxT("status bar must have a parent") );
74
75 SetName(name);
76 SetWindowStyleFlag(style);
77 SetParent(parent);
78
79 parent->AddChild(this);
80
81 m_windowId = id == wxID_ANY ? NewControlId() : id;
82
83 DWORD wstyle = WS_CHILD | WS_VISIBLE;
84
85 if ( style & wxCLIP_SIBLINGS )
86 wstyle |= WS_CLIPSIBLINGS;
87
88 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
89 // (at least in the version of comctl32.dll I'm using), and the only way to
90 // turn it off is to use CCS_TOP style - as we position the status bar
91 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxST_SIZEGRIP
92 // is not given
93 if ( !(style & wxST_SIZEGRIP) )
94 {
95 wstyle |= CCS_TOP;
96 }
97 else
98 {
99 #ifndef __WXWINCE__
100 // may be some versions of comctl32.dll do need it - anyhow, it won't
101 // do any harm
102 wstyle |= SBARS_SIZEGRIP;
103 #endif
104 }
105
106 m_hWnd = CreateWindow
107 (
108 STATUSCLASSNAME,
109 _T(""),
110 wstyle,
111 0, 0, 0, 0,
112 GetHwndOf(parent),
113 (HMENU)wxUIntToPtr(m_windowId.GetValue()),
114 wxGetInstance(),
115 NULL
116 );
117 if ( m_hWnd == 0 )
118 {
119 wxLogSysError(_("Failed to create a status bar."));
120
121 return false;
122 }
123
124 SetFieldsCount(1);
125 SubclassWin(m_hWnd);
126
127 // cache the DC instance used by UpdateFieldText:
128 // NOTE: create the DC before calling InheritAttributes() since
129 // it may result in a call to our SetFont()
130 m_pDC = new wxClientDC(this);
131
132 InheritAttributes();
133
134 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR));
135
136 // we must refresh the frame size when the statusbar is created, because
137 // its client area might change
138 //
139 // notice that we must post the event, not send it, as the frame doesn't
140 // know that we're its status bar yet so laying it out right now wouldn't
141 // work correctly, we need to wait until we return to the main loop
142 PostSizeEventToParent();
143
144 return true;
145 }
146
147 wxStatusBar::~wxStatusBar()
148 {
149 // we must refresh the frame size when the statusbar is deleted but the
150 // frame is not - otherwise statusbar leaves a hole in the place it used to
151 // occupy
152 PostSizeEventToParent();
153
154 wxDELETE(m_pDC);
155 }
156
157 bool wxStatusBar::SetFont(const wxFont& font)
158 {
159 if (!wxWindow::SetFont(font))
160 return false;
161
162 if (m_pDC) m_pDC->SetFont(font);
163 return true;
164 }
165
166 void wxStatusBar::SetFieldsCount(int nFields, const int *widths)
167 {
168 // this is a Windows limitation
169 wxASSERT_MSG( (nFields > 0) && (nFields < 255), _T("too many fields") );
170
171 wxStatusBarBase::SetFieldsCount(nFields, widths);
172
173 SetFieldsWidth();
174 }
175
176 void wxStatusBar::SetStatusWidths(int n, const int widths[])
177 {
178 wxStatusBarBase::SetStatusWidths(n, widths);
179
180 SetFieldsWidth();
181 }
182
183 void wxStatusBar::SetFieldsWidth()
184 {
185 if ( m_panes.IsEmpty() )
186 return;
187
188 int aBorders[3];
189 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
190
191 int extraWidth = aBorders[2]; // space between fields
192
193 wxArrayInt widthsAbs =
194 CalculateAbsWidths(GetClientSize().x - extraWidth*(m_panes.GetCount() - 1));
195
196 int *pWidths = new int[m_panes.GetCount()];
197
198 int nCurPos = 0;
199 for ( size_t i = 0; i < m_panes.GetCount(); i++ ) {
200 nCurPos += widthsAbs[i] + extraWidth;
201 pWidths[i] = nCurPos;
202 }
203
204 if ( !StatusBar_SetParts(GetHwnd(), m_panes.GetCount(), pWidths) ) {
205 wxLogLastError(wxT("StatusBar_SetParts"));
206 }
207
208 delete [] pWidths;
209 }
210
211 void wxStatusBar::SetStatusText(const wxString& strText, int nField)
212 {
213 wxCHECK_RET( (nField >= 0) && ((size_t)nField < m_panes.GetCount()),
214 _T("invalid statusbar field index") );
215
216 if ( strText == GetStatusText(nField) )
217 {
218 // don't call StatusBar_SetText() to avoid flicker
219 return;
220 }
221
222 wxStatusBarBase::SetStatusText(strText, nField);
223
224 UpdateFieldText(nField);
225 }
226
227 void wxStatusBar::UpdateFieldText(int nField)
228 {
229 if (!m_pDC)
230 return;
231
232 // Get field style, if any
233 int style;
234 switch(m_panes[nField].nStyle)
235 {
236 case wxSB_RAISED:
237 style = SBT_POPOUT;
238 break;
239 case wxSB_FLAT:
240 style = SBT_NOBORDERS;
241 break;
242
243 case wxSB_NORMAL:
244 default:
245 style = 0;
246 break;
247 }
248
249 wxRect rc;
250 GetFieldRect(nField, rc);
251
252 int margin;
253 if (nField == GetFieldsCount()-1)
254 margin = -6; // windows reports a smaller rect for the last field; enlarge it
255 else
256 margin = 4;
257
258 // do we need to ellipsize this string?
259 wxString ellipsizedStr =
260 wxControl::Ellipsize(GetStatusText(nField), *m_pDC,
261 GetLayoutDirection() == wxLayout_RightToLeft ? wxELLIPSIZE_START : wxELLIPSIZE_END,
262 rc.GetWidth() - margin, // leave a small margin
263 wxELLIPSIZE_EXPAND_TAB);
264
265 // Pass both field number and style. MSDN library doesn't mention
266 // that nField and style have to be 'ORed'
267 if ( !StatusBar_SetText(GetHwnd(), nField | style, ellipsizedStr.wx_str()) )
268 {
269 wxLogLastError(wxT("StatusBar_SetText"));
270 }
271 }
272
273 int wxStatusBar::GetBorderX() const
274 {
275 int aBorders[3];
276 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
277
278 return aBorders[0];
279 }
280
281 int wxStatusBar::GetBorderY() const
282 {
283 int aBorders[3];
284 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
285
286 return aBorders[1];
287 }
288
289 void wxStatusBar::SetMinHeight(int height)
290 {
291 SendMessage(GetHwnd(), SB_SETMINHEIGHT, height + 2*GetBorderY(), 0);
292
293 // have to send a (dummy) WM_SIZE to redraw it now
294 SendMessage(GetHwnd(), WM_SIZE, 0, 0);
295 }
296
297 bool wxStatusBar::GetFieldRect(int i, wxRect& rect) const
298 {
299 wxCHECK_MSG( (i >= 0) && ((size_t)i < m_panes.GetCount()), false,
300 _T("invalid statusbar field index") );
301
302 RECT r;
303 if ( !::SendMessage(GetHwnd(), SB_GETRECT, i, (LPARAM)&r) )
304 {
305 wxLogLastError(wxT("SendMessage(SB_GETRECT)"));
306 }
307
308 #if wxUSE_UXTHEME
309 wxUxThemeHandle theme((wxStatusBar *)this, L"Status"); // const_cast
310 if ( theme )
311 {
312 // by default Windows has a 2 pixel border to the right of the left
313 // divider (or it could be a bug) but it looks wrong so remove it
314 if ( i != 0 )
315 {
316 r.left -= 2;
317 }
318
319 wxUxThemeEngine::Get()->GetThemeBackgroundContentRect(theme, NULL,
320 1 /* SP_PANE */, 0,
321 &r, &r);
322 }
323 #endif
324
325 wxCopyRECTToRect(r, rect);
326
327 return true;
328 }
329
330 wxSize wxStatusBar::DoGetBestSize() const
331 {
332 int borders[3];
333 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)borders);
334
335 // calculate width
336 int width = 0;
337 for ( size_t i = 0; i < m_panes.GetCount(); ++i )
338 {
339 int widthField =
340 m_bSameWidthForAllPanes ? DEFAULT_FIELD_WIDTH : m_panes[i].nWidth;
341 if ( widthField >= 0 )
342 {
343 width += widthField;
344 }
345 else
346 {
347 // variable width field, its width is really a proportion
348 // related to the other fields
349 width += -widthField*DEFAULT_FIELD_WIDTH;
350 }
351
352 // add the space between fields
353 width += borders[2];
354 }
355
356 if ( !width )
357 {
358 // still need something even for an empty status bar
359 width = 2*DEFAULT_FIELD_WIDTH;
360 }
361
362 // calculate height
363 int height;
364 wxGetCharSize(GetHWND(), NULL, &height, GetFont());
365 height = EDIT_HEIGHT_FROM_CHAR_HEIGHT(height);
366 height += borders[1];
367
368 wxSize best(width, height);
369 CacheBestSize(best);
370 return best;
371 }
372
373 void wxStatusBar::DoMoveWindow(int x, int y, int width, int height)
374 {
375 if ( GetParent()->IsSizeDeferred() )
376 {
377 wxWindowMSW::DoMoveWindow(x, y, width, height);
378 }
379 else
380 {
381 // parent pos/size isn't deferred so do it now but don't send
382 // WM_WINDOWPOSCHANGING since we don't want to change pos/size later
383 // we must use SWP_NOCOPYBITS here otherwise it paints incorrectly
384 // if other windows are size deferred
385 ::SetWindowPos(GetHwnd(), NULL, x, y, width, height,
386 SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE
387 #ifndef __WXWINCE__
388 | SWP_NOCOPYBITS | SWP_NOSENDCHANGING
389 #endif
390 );
391 }
392
393 // adjust fields widths to the new size
394 SetFieldsWidth();
395
396 // we have to trigger wxSizeEvent if there are children window in status
397 // bar because GetFieldRect returned incorrect (not updated) values up to
398 // here, which almost certainly resulted in incorrectly redrawn statusbar
399 if ( m_children.GetCount() > 0 )
400 {
401 wxSizeEvent event(GetClientSize(), m_windowId);
402 event.SetEventObject(this);
403 HandleWindowEvent(event);
404 }
405 }
406
407 void wxStatusBar::SetStatusStyles(int n, const int styles[])
408 {
409 wxStatusBarBase::SetStatusStyles(n, styles);
410
411 if (n != (int)m_panes.GetCount())
412 return;
413
414 for (int i = 0; i < n; i++)
415 {
416 int style;
417 switch(styles[i])
418 {
419 case wxSB_RAISED:
420 style = SBT_POPOUT;
421 break;
422 case wxSB_FLAT:
423 style = SBT_NOBORDERS;
424 break;
425 case wxSB_NORMAL:
426 default:
427 style = 0;
428 break;
429 }
430 // The SB_SETTEXT message is both used to set the field's text as well as
431 // the fields' styles. MSDN library doesn't mention
432 // that nField and style have to be 'ORed'
433 wxString text = GetStatusText(i);
434 if (!StatusBar_SetText(GetHwnd(), style | i, text.wx_str()))
435 {
436 wxLogLastError(wxT("StatusBar_SetText"));
437 }
438 }
439 }
440
441 WXLRESULT
442 wxStatusBar::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
443 {
444 #ifndef __WXWINCE__
445 if ( nMsg == WM_WINDOWPOSCHANGING )
446 {
447 WINDOWPOS *lpPos = (WINDOWPOS *)lParam;
448 int x, y, w, h;
449 GetPosition(&x, &y);
450 GetSize(&w, &h);
451
452 // we need real window coords and not wx client coords
453 AdjustForParentClientOrigin(x, y);
454
455 lpPos->x = x;
456 lpPos->y = y;
457 lpPos->cx = w;
458 lpPos->cy = h;
459
460 return 0;
461 }
462
463 if ( nMsg == WM_NCLBUTTONDOWN )
464 {
465 // if hit-test is on gripper then send message to TLW to begin
466 // resizing. It is possible to send this message to any window.
467 if ( wParam == HTBOTTOMRIGHT )
468 {
469 wxWindow *win;
470
471 for ( win = GetParent(); win; win = win->GetParent() )
472 {
473 if ( win->IsTopLevel() )
474 {
475 SendMessage(GetHwndOf(win), WM_NCLBUTTONDOWN,
476 wParam, lParam);
477
478 return 0;
479 }
480 }
481 }
482 }
483 #endif
484
485 if ( nMsg == WM_SIZE )
486 {
487 for (int i=0; i<GetFieldsCount(); i++)
488 UpdateFieldText(i);
489 // re-set the field text, in case we need to ellipsize
490 // (or de-ellipsize) some parts of it
491 }
492
493 return wxStatusBarBase::MSWWindowProc(nMsg, wParam, lParam);
494 }
495
496 #endif // wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR