]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbr95.cpp
20df26db30f1e1bee4828e56210f4edd2abafdf4
[wxWidgets.git] / src / msw / statbr95.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "statbr95.h"
14 #endif
15
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/setup.h"
25 #include "wx/frame.h"
26 #include "wx/settings.h"
27 #include "wx/dcclient.h"
28 #endif
29
30 #if defined(__WIN95__) && wxUSE_NATIVE_STATUSBAR
31
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #include "wx/statusbr.h"
35
36 #include "wx/msw/private.h"
37 #include <windowsx.h>
38
39 #if !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
40 #include <commctrl.h>
41 #endif
42
43 // ----------------------------------------------------------------------------
44 // wxWindows macros
45 // ----------------------------------------------------------------------------
46
47 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar95, wxWindow);
48 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar, wxStatusBar95)
49
50 // ----------------------------------------------------------------------------
51 // macros
52 // ----------------------------------------------------------------------------
53
54 // windowsx.h and commctrl.h don't define those, so we do it here
55 #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w)
56 #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
57 #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
58 #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
59
60 // ----------------------------------------------------------------------------
61 //
62 // ----------------------------------------------------------------------------
63
64 // static WNDPROC gs_wndprocStatBar = NULL;
65 static WXFARPROC gs_wndprocStatBar = (WXFARPROC) NULL;
66
67 LRESULT APIENTRY wxStatusBarProc(HWND hwnd,
68 UINT message,
69 WPARAM wParam,
70 LPARAM lParam)
71 {
72 switch (message) {
73 case WM_COMMAND:
74 case WM_SIZE:
75 case WM_MOVE:
76 case WM_MOUSEMOVE:
77 case WM_MOUSEMOVE:
78 case WM_LBUTTONUP:
79 case WM_LBUTTONDBLCLK:
80 case WM_RBUTTONDOWN:
81 case WM_RBUTTONUP:
82 case WM_RBUTTONDBLCLK:
83 case WM_MBUTTONDOWN:
84 case WM_MBUTTONUP:
85 case WM_MBUTTONDBLCLK:
86 wxStatusBar95 *sb = (wxStatusBar95 *)GetWindowLong(hwnd, GWL_USERDATA);
87 sb->MSWWindowProc(message, wParam, lParam);
88 break;
89 }
90
91 return ::CallWindowProc(CASTWNDPROC gs_wndprocStatBar, hwnd, message, wParam, lParam);
92 }
93
94 // ============================================================================
95 // implementation
96 // ============================================================================
97
98 // ----------------------------------------------------------------------------
99 // wxStatusBar95 class
100 // ----------------------------------------------------------------------------
101
102 wxStatusBar95::wxStatusBar95()
103 {
104 SetParent(NULL);
105 m_hWnd = 0;
106 m_windowId = 0;
107 }
108
109 bool wxStatusBar95::Create(wxWindow *parent,
110 wxWindowID id,
111 long style,
112 const wxString& name)
113 {
114 wxCHECK_MSG( parent, FALSE, wxT("status bar must have a parent") );
115
116 SetName(name);
117 SetWindowStyleFlag(style);
118 SetParent(parent);
119
120 parent->AddChild(this);
121
122 m_windowId = id == -1 ? NewControlId() : id;
123
124 DWORD wstyle = WS_CHILD | WS_VISIBLE;
125
126 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
127 // (at least in the version of comctl32.dll I'm using), and the only way to
128 // turn it off is to use CCS_TOP style - as we position the status bar
129 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxST_SIZEGRIP
130 // is not given
131 if ( !(style & wxST_SIZEGRIP) )
132 {
133 wstyle |= CCS_TOP;
134 }
135 else
136 {
137 // may be some versions of comctl32.dll do need it - anyhow, it won't
138 // do any harm
139 wstyle |= SBARS_SIZEGRIP;
140 }
141
142 m_hWnd = (WXHWND)CreateStatusWindow(wstyle,
143 wxEmptyString,
144 GetHwndOf(parent),
145 m_windowId);
146 if ( m_hWnd == 0 )
147 {
148 wxLogSysError(_("Failed to create a status bar."));
149
150 return FALSE;
151 }
152
153 SetFieldsCount(1);
154
155 // we can't subclass this window as usual because the status bar window
156 // proc processes WM_SIZE and WM_PAINT specially
157 // SubclassWin(m_hWnd);
158
159 // but we want to process the messages from it still, so do custom
160 // subclassing here
161 gs_wndprocStatBar = (WXFARPROC)GetWindowLong(GetHwnd(), GWL_WNDPROC);
162 SetWindowLong(GetHwnd(), GWL_WNDPROC, (LONG)wxStatusBarProc);
163 SetWindowLong(GetHwnd(), GWL_USERDATA, (LONG)this);
164
165 return TRUE;
166 }
167
168 wxStatusBar95::~wxStatusBar95()
169 {
170 delete [] m_statusWidths;
171 }
172
173 void wxStatusBar95::CopyFieldsWidth(const int widths[])
174 {
175 if (widths && !m_statusWidths)
176 m_statusWidths = new int[m_nFields];
177
178 if ( widths != NULL ) {
179 for ( int i = 0; i < m_nFields; i++ )
180 m_statusWidths[i] = widths[i];
181 }
182 else {
183 delete [] m_statusWidths;
184 m_statusWidths = NULL;
185 }
186 }
187
188 void wxStatusBar95::SetFieldsCount(int nFields, const int *widths)
189 {
190 // this is Windows limitation
191 wxASSERT_MSG( (nFields > 0) && (nFields < 255), _T("too many fields") );
192
193 m_nFields = nFields;
194
195 CopyFieldsWidth(widths);
196 SetFieldsWidth();
197 }
198
199 void wxStatusBar95::SetStatusWidths(int n, const int widths[])
200 {
201 wxASSERT_MSG( n == m_nFields, _T("field number mismatch") );
202
203 CopyFieldsWidth(widths);
204 SetFieldsWidth();
205 }
206
207 void wxStatusBar95::SetFieldsWidth()
208 {
209 if ( !m_nFields )
210 return;
211
212 int aBorders[3];
213 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
214
215 int extraWidth = aBorders[2]; // space between fields
216
217 int *pWidths = new int[m_nFields];
218
219 int nWindowWidth, y;
220 GetClientSize(&nWindowWidth, &y);
221
222 if ( m_statusWidths == NULL ) {
223 // default: all fields have the same width
224 int nWidth = nWindowWidth / m_nFields;
225 for ( int i = 0; i < m_nFields; i++ )
226 pWidths[i] = (i + 1) * nWidth;
227 }
228 else {
229 // -1 doesn't mean the same thing for wxWindows and Win32, recalc
230 int nTotalWidth = 0,
231 nVarCount = 0,
232 i;
233 for ( i = 0; i < m_nFields; i++ ) {
234 if ( m_statusWidths[i] == -1 )
235 nVarCount++;
236 else
237 nTotalWidth += m_statusWidths[i] + extraWidth;
238 }
239
240 if ( nVarCount == 0 ) {
241 wxFAIL_MSG( _T("at least one field must be of variable width") );
242
243 nVarCount++;
244 }
245
246 int nVarWidth = (nWindowWidth - nTotalWidth) / nVarCount;
247
248 // do fill the array
249 int nCurPos = 0;
250 for ( i = 0; i < m_nFields; i++ ) {
251 if ( m_statusWidths[i] == -1 )
252 nCurPos += nVarWidth;
253 else
254 nCurPos += m_statusWidths[i] + extraWidth;
255 pWidths[i] = nCurPos;
256 }
257 }
258
259 if ( !StatusBar_SetParts(GetHwnd(), m_nFields, pWidths) ) {
260 wxLogLastError(wxT("StatusBar_SetParts"));
261 }
262
263 delete [] pWidths;
264 }
265
266 void wxStatusBar95::SetStatusText(const wxString& strText, int nField)
267 {
268 wxCHECK_RET( (nField >= 0) && (nField < m_nFields),
269 _T("invalid statusbar field index") );
270
271 if ( !StatusBar_SetText(GetHwnd(), nField, strText) ) {
272 wxLogLastError(wxT("StatusBar_SetText"));
273 }
274 }
275
276 wxString wxStatusBar95::GetStatusText(int nField) const
277 {
278 wxCHECK_MSG( (nField >= 0) && (nField < m_nFields), wxEmptyString,
279 _T("invalid statusbar field index") );
280
281 wxString str;
282 int len = StatusBar_GetTextLen(GetHwnd(), nField);
283 if (len > 0)
284 {
285 StatusBar_GetText(GetHwnd(), nField, str.GetWriteBuf(len));
286 str.UngetWriteBuf();
287 }
288 return str;
289 }
290
291 int wxStatusBar95::GetBorderX() const
292 {
293 int aBorders[3];
294 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
295
296 return aBorders[0];
297 }
298
299 int wxStatusBar95::GetBorderY() const
300 {
301 int aBorders[3];
302 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
303
304 return aBorders[1];
305 }
306
307 void wxStatusBar95::SetMinHeight(int height)
308 {
309 SendMessage(GetHwnd(), SB_SETMINHEIGHT, height + 2*GetBorderY(), 0);
310
311 // have to send a (dummy) WM_SIZE to redraw it now
312 SendMessage(GetHwnd(), WM_SIZE, 0, 0);
313 }
314
315 bool wxStatusBar95::GetFieldRect(int i, wxRect& rect) const
316 {
317 wxCHECK_MSG( (i >= 0) && (i < m_nFields), FALSE,
318 _T("invalid statusbar field index") );
319
320 RECT r;
321 if ( !::SendMessage(GetHwnd(), SB_GETRECT, i, (LPARAM)&r) )
322 {
323 wxLogLastError("SendMessage(SB_GETRECT)");
324 }
325
326 wxCopyRECTToRect(r, rect);
327
328 return TRUE;
329 }
330
331 void wxStatusBar95::DoMoveWindow(int x, int y, int width, int height)
332 {
333 // the status bar wnd proc must be forwarded the WM_SIZE message whenever
334 // the stat bar position/size is changed because it normally positions the
335 // control itself along bottom or top side of the parent window - failing
336 // to do so will result in nasty visual effects
337 FORWARD_WM_SIZE(GetHwnd(), SIZE_RESTORED, x, y, SendMessage);
338
339 // but now, when the standard status bar wnd proc did all it wanted to do,
340 // move the status bar to its correct location - usually this call may be
341 // omitted because for normal status bars (positioned along the bottom
342 // edge) the position is already set correctly, but if the user wants to
343 // position them in some exotic location, this is really needed
344 wxWindow::DoMoveWindow(x, y, width, height);
345
346 // adjust fields widths to the new size
347 SetFieldsWidth();
348 }
349
350 #endif // __WIN95__ && wxUSE_NATIVE_STATUSBAR
351