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