]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/statbr95.cpp | |
3 | // Purpose: native implementation of wxStatusBar | |
4 | // Author: Vadim Zeitlin | |
ed791986 | 5 | // Modified by: |
2bda0e17 KB |
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 | ||
cfe780fb JS |
12 | #ifdef __GNUG__ |
13 | #pragma implementation "statbr95.h" | |
14 | #endif | |
15 | ||
2bda0e17 KB |
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 | ||
ed791986 | 30 | #if defined(__WIN95__) && wxUSE_NATIVE_STATUSBAR |
2432b92d | 31 | |
608eeb0f | 32 | #include "wx/intl.h" |
2bda0e17 | 33 | #include "wx/log.h" |
ed791986 | 34 | #include "wx/statusbr.h" |
2bda0e17 | 35 | |
42e69d6b VZ |
36 | #include "wx/msw/private.h" |
37 | #include <windowsx.h> | |
2bda0e17 | 38 | |
c42404a5 VZ |
39 | #if !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) |
40 | #include <commctrl.h> | |
ce3ed50d JS |
41 | #endif |
42 | ||
ed791986 VZ |
43 | // ---------------------------------------------------------------------------- |
44 | // wxWindows macros | |
45 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 46 | |
ed791986 VZ |
47 | IMPLEMENT_DYNAMIC_CLASS(wxStatusBar95, wxWindow); |
48 | IMPLEMENT_DYNAMIC_CLASS(wxStatusBar, wxStatusBar95) | |
2bda0e17 | 49 | |
ed791986 | 50 | BEGIN_EVENT_TABLE(wxStatusBar95, wxWindow) |
2bda0e17 | 51 | EVT_SIZE(wxStatusBar95::OnSize) |
ed791986 | 52 | END_EVENT_TABLE() |
2bda0e17 KB |
53 | |
54 | // ---------------------------------------------------------------------------- | |
55 | // macros | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | // windowsx.h and commctrl.h don't define those, so we do it here | |
59 | #define StatusBar_SetParts(h, n, w) SendMessage(h, SB_SETPARTS, (WPARAM)n, (LPARAM)w) | |
837e5743 | 60 | #define StatusBar_SetText(h, n, t) SendMessage(h, SB_SETTEXT, (WPARAM)n, (LPARAM)(LPCTSTR)t) |
2bda0e17 | 61 | #define StatusBar_GetTextLen(h, n) LOWORD(SendMessage(h, SB_GETTEXTLENGTH, (WPARAM)n, 0)) |
837e5743 | 62 | #define StatusBar_GetText(h, n, s) LOWORD(SendMessage(h, SB_GETTEXT, (WPARAM)n, (LPARAM)(LPTSTR)s)) |
2bda0e17 | 63 | |
ed791986 VZ |
64 | // ---------------------------------------------------------------------------- |
65 | // | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
457e6c54 JS |
68 | // static WNDPROC gs_wndprocStatBar = NULL; |
69 | static WXFARPROC gs_wndprocStatBar = (WXFARPROC) NULL; | |
ed791986 VZ |
70 | |
71 | LRESULT APIENTRY wxStatusBarProc(HWND hwnd, | |
72 | UINT message, | |
73 | WPARAM wParam, | |
74 | LPARAM lParam) | |
75 | { | |
76 | if ( message == WM_COMMAND ) | |
77 | { | |
78 | wxStatusBar95 *sb = (wxStatusBar95 *)GetWindowLong(hwnd, GWL_USERDATA); | |
79 | sb->MSWWindowProc(message, wParam, lParam); | |
80 | } | |
81 | ||
66e33344 | 82 | return ::CallWindowProc(CASTWNDPROC gs_wndprocStatBar, hwnd, message, wParam, lParam); |
ed791986 | 83 | } |
2bda0e17 KB |
84 | |
85 | // ============================================================================ | |
86 | // implementation | |
87 | // ============================================================================ | |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // wxStatusBar95 class | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | wxStatusBar95::wxStatusBar95() | |
94 | { | |
95 | SetParent(NULL); | |
96 | m_hWnd = 0; | |
97 | m_windowId = 0; | |
98 | } | |
99 | ||
ed791986 VZ |
100 | bool wxStatusBar95::Create(wxWindow *parent, |
101 | wxWindowID id, | |
102 | long style, | |
103 | const wxString& name) | |
2bda0e17 | 104 | { |
ed791986 VZ |
105 | SetName(name); |
106 | SetParent(parent); | |
107 | ||
108 | if (id == -1) | |
109 | m_windowId = NewControlId(); | |
110 | else | |
111 | m_windowId = id; | |
112 | ||
113 | DWORD wstyle = WS_CHILD | WS_VISIBLE; | |
114 | if ( style & wxST_SIZEGRIP ) | |
115 | wstyle |= SBARS_SIZEGRIP; | |
116 | ||
117 | m_hWnd = (WXHWND)CreateStatusWindow(wstyle, | |
118 | wxEmptyString, | |
119 | GetHwndOf(parent), | |
120 | m_windowId); | |
121 | if ( m_hWnd == 0 ) | |
122 | { | |
123 | wxLogSysError(_("Failed to create a status bar.")); | |
124 | ||
125 | return FALSE; | |
126 | } | |
2bda0e17 | 127 | |
ed791986 VZ |
128 | // for some reason, subclassing in the usual way doesn't work at all - many |
129 | // strange things start happening (status bar is not positioned correctly, | |
130 | // all methods fail...) | |
131 | // SubclassWin(m_hWnd); | |
132 | ||
133 | // but we want to process the messages from it still, so must subclass it | |
457e6c54 | 134 | gs_wndprocStatBar = (WXFARPROC)GetWindowLong(GetHwnd(), GWL_WNDPROC); |
ed791986 VZ |
135 | SetWindowLong(GetHwnd(), GWL_WNDPROC, (LONG)wxStatusBarProc); |
136 | SetWindowLong(GetHwnd(), GWL_USERDATA, (LONG)this); | |
2bda0e17 | 137 | |
ed791986 VZ |
138 | return TRUE; |
139 | } | |
2bda0e17 | 140 | |
ed791986 VZ |
141 | wxStatusBar95::~wxStatusBar95() |
142 | { | |
143 | delete [] m_statusWidths; | |
2bda0e17 KB |
144 | } |
145 | ||
8b9518ee | 146 | void wxStatusBar95::CopyFieldsWidth(const int widths[]) |
2bda0e17 KB |
147 | { |
148 | if (widths && !m_statusWidths) | |
149 | m_statusWidths = new int[m_nFields]; | |
150 | ||
151 | if ( widths != NULL ) { | |
152 | for ( int i = 0; i < m_nFields; i++ ) | |
153 | m_statusWidths[i] = widths[i]; | |
154 | } | |
155 | else { | |
156 | delete [] m_statusWidths; | |
157 | m_statusWidths = NULL; | |
158 | } | |
159 | } | |
160 | ||
790ad94f | 161 | void wxStatusBar95::SetFieldsCount(int nFields, const int *widths) |
2bda0e17 | 162 | { |
ed791986 VZ |
163 | // this is Windows limitation |
164 | wxASSERT_MSG( (nFields > 0) && (nFields < 255), _T("too many fields") ); | |
2bda0e17 KB |
165 | |
166 | m_nFields = nFields; | |
167 | ||
168 | CopyFieldsWidth(widths); | |
169 | SetFieldsWidth(); | |
170 | } | |
171 | ||
8b9518ee | 172 | void wxStatusBar95::SetStatusWidths(int n, const int widths[]) |
2bda0e17 | 173 | { |
ed791986 | 174 | wxASSERT_MSG( n == m_nFields, _T("field number mismatch") ); |
2bda0e17 KB |
175 | |
176 | CopyFieldsWidth(widths); | |
177 | SetFieldsWidth(); | |
178 | } | |
179 | ||
180 | void wxStatusBar95::SetFieldsWidth() | |
181 | { | |
3175c712 VZ |
182 | if ( !m_nFields ) |
183 | return; | |
184 | ||
ed791986 VZ |
185 | int aBorders[3]; |
186 | SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders); | |
2bda0e17 | 187 | |
ed791986 | 188 | int extraWidth = aBorders[2]; // space between fields |
2bda0e17 | 189 | |
ed791986 | 190 | int *pWidths = new int[m_nFields]; |
2bda0e17 | 191 | |
ed791986 VZ |
192 | int nWindowWidth, y; |
193 | GetClientSize(&nWindowWidth, &y); | |
2bda0e17 | 194 | |
ed791986 VZ |
195 | if ( m_statusWidths == NULL ) { |
196 | // default: all fields have the same width | |
197 | int nWidth = nWindowWidth / m_nFields; | |
198 | for ( int i = 0; i < m_nFields; i++ ) | |
199 | pWidths[i] = (i + 1) * nWidth; | |
2bda0e17 | 200 | } |
ed791986 VZ |
201 | else { |
202 | // -1 doesn't mean the same thing for wxWindows and Win32, recalc | |
203 | int nTotalWidth = 0, | |
204 | nVarCount = 0, | |
205 | i; | |
206 | for ( i = 0; i < m_nFields; i++ ) { | |
207 | if ( m_statusWidths[i] == -1 ) | |
208 | nVarCount++; | |
209 | else | |
210 | nTotalWidth += m_statusWidths[i] + extraWidth; | |
211 | } | |
212 | ||
213 | if ( nVarCount == 0 ) { | |
214 | wxFAIL_MSG( _T("at least one field must be of variable width") ); | |
215 | ||
216 | nVarCount++; | |
217 | } | |
218 | ||
219 | int nVarWidth = (nWindowWidth - nTotalWidth) / nVarCount; | |
220 | ||
221 | // do fill the array | |
222 | int nCurPos = 0; | |
223 | for ( i = 0; i < m_nFields; i++ ) { | |
224 | if ( m_statusWidths[i] == -1 ) | |
225 | nCurPos += nVarWidth; | |
226 | else | |
227 | nCurPos += m_statusWidths[i] + extraWidth; | |
228 | pWidths[i] = nCurPos; | |
229 | } | |
2bda0e17 | 230 | } |
2bda0e17 | 231 | |
ed791986 VZ |
232 | if ( !StatusBar_SetParts(GetHwnd(), m_nFields, pWidths) ) { |
233 | wxLogLastError(wxT("StatusBar_SetParts")); | |
234 | } | |
2bda0e17 | 235 | |
ed791986 | 236 | delete [] pWidths; |
2bda0e17 KB |
237 | } |
238 | ||
debe6624 | 239 | void wxStatusBar95::SetStatusText(const wxString& strText, int nField) |
2bda0e17 | 240 | { |
ed791986 VZ |
241 | wxCHECK_RET( (nField >= 0) && (nField < m_nFields), |
242 | _T("invalid statusbar field index") ); | |
243 | ||
244 | if ( !StatusBar_SetText(GetHwnd(), nField, strText) ) { | |
223d09f6 | 245 | wxLogLastError(wxT("StatusBar_SetText")); |
2bda0e17 KB |
246 | } |
247 | } | |
248 | ||
249 | wxString wxStatusBar95::GetStatusText(int nField) const | |
250 | { | |
ed791986 VZ |
251 | wxCHECK_MSG( (nField >= 0) && (nField < m_nFields), wxEmptyString, |
252 | _T("invalid statusbar field index") ); | |
2bda0e17 | 253 | |
ed791986 VZ |
254 | wxString str; |
255 | int len = StatusBar_GetTextLen(GetHwnd(), nField); | |
b7346a70 JS |
256 | if (len > 0) |
257 | { | |
ed791986 VZ |
258 | StatusBar_GetText(GetHwnd(), nField, str.GetWriteBuf(len)); |
259 | str.UngetWriteBuf(); | |
b7346a70 | 260 | } |
2bda0e17 KB |
261 | return str; |
262 | } | |
263 | ||
ed791986 VZ |
264 | int wxStatusBar95::GetBorderX() const |
265 | { | |
266 | int aBorders[3]; | |
267 | SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders); | |
268 | ||
269 | return aBorders[0]; | |
270 | } | |
271 | ||
272 | int wxStatusBar95::GetBorderY() const | |
273 | { | |
274 | int aBorders[3]; | |
275 | SendMessage(GetHwnd(), SB_GETBORDERS, 0, (LPARAM)aBorders); | |
276 | ||
277 | return aBorders[1]; | |
278 | } | |
279 | ||
280 | void wxStatusBar95::SetMinHeight(int height) | |
281 | { | |
282 | SendMessage(GetHwnd(), SB_SETMINHEIGHT, height + 2*GetBorderY(), 0); | |
283 | ||
284 | // have to send a (dummy) WM_SIZE to redraw it now | |
285 | SendMessage(GetHwnd(), WM_SIZE, 0, 0); | |
286 | } | |
287 | ||
288 | bool wxStatusBar95::GetFieldRect(int i, wxRect& rect) const | |
289 | { | |
290 | wxCHECK_MSG( (i >= 0) && (i < m_nFields), FALSE, | |
291 | _T("invalid statusbar field index") ); | |
292 | ||
293 | RECT r; | |
294 | if ( !::SendMessage(GetHwnd(), SB_GETRECT, i, (LPARAM)&r) ) | |
295 | { | |
296 | wxLogLastError("SendMessage(SB_GETRECT)"); | |
297 | } | |
298 | ||
299 | wxCopyRECTToRect(r, rect); | |
300 | ||
301 | return TRUE; | |
302 | } | |
303 | ||
2bda0e17 KB |
304 | void wxStatusBar95::OnSize(wxSizeEvent& event) |
305 | { | |
ed791986 VZ |
306 | FORWARD_WM_SIZE(GetHwnd(), SIZE_RESTORED, |
307 | event.GetSize().x, event.GetSize().y, | |
2bda0e17 KB |
308 | SendMessage); |
309 | ||
310 | // adjust fields widths to the new size | |
311 | SetFieldsWidth(); | |
312 | } | |
313 | ||
ed791986 | 314 | #endif // __WIN95__ && wxUSE_NATIVE_STATUSBAR |
ba681060 | 315 |