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