]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbr95.cpp
regenerated from the templates
[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(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
40 #include <commctrl.h>
41 #endif
42
43 // ----------------------------------------------------------------------------
44 // wxWindows macros
45 // ----------------------------------------------------------------------------
46
47 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar95, wxWindow);
48
49 // ----------------------------------------------------------------------------
50 // macros
51 // ----------------------------------------------------------------------------
52
53 // windowsx.h and commctrl.h don't define those, so we do it here
54 #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w)
55 #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t)
56 #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0))
57 #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s))
58
59 // ============================================================================
60 // implementation
61 // ============================================================================
62
63 // ----------------------------------------------------------------------------
64 // wxStatusBar95 class
65 // ----------------------------------------------------------------------------
66
67 wxStatusBar95::wxStatusBar95()
68 {
69 SetParent(NULL);
70 m_hWnd = 0;
71 m_windowId = 0;
72 }
73
74 bool wxStatusBar95::Create(wxWindow *parent,
75 wxWindowID id,
76 long style,
77 const wxString& name)
78 {
79 wxCHECK_MSG( parent, FALSE, wxT("status bar must have a parent") );
80
81 SetName(name);
82 SetWindowStyleFlag(style);
83 SetParent(parent);
84
85 parent->AddChild(this);
86
87 m_windowId = id == -1 ? NewControlId() : id;
88
89 DWORD wstyle = WS_CHILD | WS_VISIBLE;
90
91 if ( style & wxCLIP_SIBLINGS )
92 wstyle |= WS_CLIPSIBLINGS;
93
94 // setting SBARS_SIZEGRIP is perfectly useless: it's always on by default
95 // (at least in the version of comctl32.dll I'm using), and the only way to
96 // turn it off is to use CCS_TOP style - as we position the status bar
97 // manually anyhow (see DoMoveWindow), use CCS_TOP style if wxST_SIZEGRIP
98 // is not given
99 if ( !(style & wxST_SIZEGRIP) )
100 {
101 wstyle |= CCS_TOP;
102 }
103 else
104 {
105 // may be some versions of comctl32.dll do need it - anyhow, it won't
106 // do any harm
107 wstyle |= SBARS_SIZEGRIP;
108 }
109
110 m_hWnd = (WXHWND)CreateStatusWindow(wstyle,
111 wxEmptyString,
112 GetHwndOf(parent),
113 m_windowId);
114 if ( m_hWnd == 0 )
115 {
116 wxLogSysError(_("Failed to create a status bar."));
117
118 return FALSE;
119 }
120
121 SetFieldsCount(1);
122 SubclassWin(m_hWnd);
123
124 return TRUE;
125 }
126
127 wxStatusBar95::~wxStatusBar95()
128 {
129 }
130
131 void wxStatusBar95::CopyFieldsWidth(const int widths[])
132 {
133 if (widths && !m_statusWidths)
134 m_statusWidths = new int[m_nFields];
135
136 if ( widths != NULL )
137 {
138 for ( int i = 0; i < m_nFields; i++ )
139 m_statusWidths[i] = widths[i];
140 }
141 else // no widths
142 {
143 delete [] m_statusWidths;
144 m_statusWidths = NULL;
145 }
146 }
147
148 void wxStatusBar95::SetFieldsCount(int nFields, const int *widths)
149 {
150 // this is a Windows limitation
151 wxASSERT_MSG( (nFields > 0) && (nFields < 255), _T("too many fields") );
152
153 m_nFields = nFields;
154
155 CopyFieldsWidth(widths);
156 SetFieldsWidth();
157 }
158
159 void wxStatusBar95::SetStatusWidths(int WXUNUSED_UNLESS_DEBUG(n), const int widths[])
160 {
161 wxASSERT_MSG( n == m_nFields, _T("field number mismatch") );
162
163 CopyFieldsWidth(widths);
164 SetFieldsWidth();
165 }
166
167 void wxStatusBar95::SetFieldsWidth()
168 {
169 if ( !m_nFields )
170 return;
171
172 int aBorders[3];
173 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
174
175 int extraWidth = aBorders[2]; // space between fields
176
177 int *pWidths = new int[m_nFields];
178
179 int nWindowWidth, y;
180 GetClientSize(&nWindowWidth, &y);
181
182 if ( m_statusWidths == NULL ) {
183 // default: all fields have the same width
184 int nWidth = nWindowWidth / m_nFields;
185 for ( int i = 0; i < m_nFields; i++ )
186 pWidths[i] = (i + 1) * nWidth;
187 }
188 else {
189 // -1 doesn't mean the same thing for wxWindows and Win32, recalc
190 int nTotalWidth = 0,
191 nVarCount = 0,
192 i;
193 for ( i = 0; i < m_nFields; i++ ) {
194 if ( m_statusWidths[i] == -1 )
195 nVarCount++;
196 else
197 nTotalWidth += m_statusWidths[i] + extraWidth;
198 }
199
200 if ( nVarCount == 0 ) {
201 wxFAIL_MSG( _T("at least one field must be of variable width") );
202
203 nVarCount++;
204 }
205
206 int nVarWidth = (nWindowWidth - nTotalWidth) / nVarCount;
207
208 // do fill the array
209 int nCurPos = 0;
210 for ( i = 0; i < m_nFields; i++ ) {
211 if ( m_statusWidths[i] == -1 )
212 nCurPos += nVarWidth;
213 else
214 nCurPos += m_statusWidths[i] + extraWidth;
215 pWidths[i] = nCurPos;
216 }
217 }
218
219 if ( !StatusBar_SetParts(GetHwnd(), m_nFields, pWidths) ) {
220 wxLogLastError(wxT("StatusBar_SetParts"));
221 }
222
223 delete [] pWidths;
224 }
225
226 void wxStatusBar95::SetStatusText(const wxString& strText, int nField)
227 {
228 wxCHECK_RET( (nField >= 0) && (nField < m_nFields),
229 _T("invalid statusbar field index") );
230
231 if ( !StatusBar_SetText(GetHwnd(), nField, strText) )
232 {
233 wxLogLastError(wxT("StatusBar_SetText"));
234 }
235 }
236
237 wxString wxStatusBar95::GetStatusText(int nField) const
238 {
239 wxCHECK_MSG( (nField >= 0) && (nField < m_nFields), wxEmptyString,
240 _T("invalid statusbar field index") );
241
242 wxString str;
243 int len = StatusBar_GetTextLen(GetHwnd(), nField);
244 if ( len > 0 )
245 {
246 StatusBar_GetText(GetHwnd(), nField, str.GetWriteBuf(len));
247 str.UngetWriteBuf();
248 }
249
250 return str;
251 }
252
253 int wxStatusBar95::GetBorderX() const
254 {
255 int aBorders[3];
256 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
257
258 return aBorders[0];
259 }
260
261 int wxStatusBar95::GetBorderY() const
262 {
263 int aBorders[3];
264 SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders);
265
266 return aBorders[1];
267 }
268
269 void wxStatusBar95::SetMinHeight(int height)
270 {
271 SendMessage(GetHwnd(), SB_SETMINHEIGHT, height + 2*GetBorderY(), 0);
272
273 // have to send a (dummy) WM_SIZE to redraw it now
274 SendMessage(GetHwnd(), WM_SIZE, 0, 0);
275 }
276
277 bool wxStatusBar95::GetFieldRect(int i, wxRect& rect) const
278 {
279 wxCHECK_MSG( (i >= 0) && (i < m_nFields), FALSE,
280 _T("invalid statusbar field index") );
281
282 RECT r;
283 if ( !::SendMessage(GetHwnd(), SB_GETRECT, i, (LPARAM)&r) )
284 {
285 wxLogLastError(wxT("SendMessage(SB_GETRECT)"));
286 }
287
288 wxCopyRECTToRect(r, rect);
289
290 return TRUE;
291 }
292
293 void wxStatusBar95::DoMoveWindow(int x, int y, int width, int height)
294 {
295 // the status bar wnd proc must be forwarded the WM_SIZE message whenever
296 // the stat bar position/size is changed because it normally positions the
297 // control itself along bottom or top side of the parent window - failing
298 // to do so will result in nasty visual effects
299 FORWARD_WM_SIZE(GetHwnd(), SIZE_RESTORED, x, y, SendMessage);
300
301 // but now, when the standard status bar wnd proc did all it wanted to do,
302 // move the status bar to its correct location - usually this call may be
303 // omitted because for normal status bars (positioned along the bottom
304 // edge) the position is already set correctly, but if the user wants to
305 // position them in some exotic location, this is really needed
306 wxWindow::DoMoveWindow(x, y, width, height);
307
308 // adjust fields widths to the new size
309 SetFieldsWidth();
310
311 // we have to trigger wxSizeEvent if there are children window in status
312 // bar because GetFieldRect returned incorrect (not updated) values up to
313 // here, which almost certainly resulted in incorrectly redrawn statusbar
314 if ( m_children.GetCount() > 0 )
315 {
316 wxSizeEvent event(GetClientSize(), m_windowId);
317 event.SetEventObject(this);
318 GetEventHandler()->ProcessEvent(event);
319 }
320 }
321
322 #endif // __WIN95__ && wxUSE_NATIVE_STATUSBAR
323