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