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