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