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