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