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