SetStatusFields type in implementation now matches header
[wxWidgets.git] / src / generic / statusbr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/statusbr.cpp
3 // Purpose: wxStatusBarGeneric class implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "statusbr.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 //#if !defined(__WIN32__) || !wxUSE_NATIVE_STATUSBAR
24
25 #ifndef WX_PRECOMP
26 #include "wx/setup.h"
27 #include "wx/frame.h"
28 #include "wx/settings.h"
29 #include "wx/dcclient.h"
30 #endif
31
32 #include "wx/generic/statusbr.h"
33
34 #ifdef __WXMSW__
35 #include <windows.h>
36 #include "wx/msw/winundef.h"
37 #endif
38
39 IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric, wxWindow)
40
41 #if !defined(__WIN32__) || !wxUSE_NATIVE_STATUSBAR
42 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar, wxStatusBarGeneric)
43 #endif // Win32 && wxUSE_NATIVE_STATUSBAR
44
45 BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow)
46 EVT_PAINT(wxStatusBarGeneric::OnPaint)
47 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged)
48 END_EVENT_TABLE()
49
50 // Default status border dimensions
51 #define wxTHICK_LINE_BORDER 2
52 #define wxTHICK_LINE_WIDTH 1
53
54 wxStatusBarGeneric::wxStatusBarGeneric()
55 {
56 m_statusWidths = (int *) NULL;
57 m_statusStrings = (wxString *) NULL;
58 m_nFields = 0;
59 m_borderX = wxTHICK_LINE_BORDER;
60 m_borderY = wxTHICK_LINE_BORDER;
61 }
62
63 wxStatusBarGeneric::~wxStatusBarGeneric()
64 {
65 # ifdef __WXMSW__
66 SetFont(wxNullFont);
67 # endif // MSW
68
69 if ( m_statusWidths )
70 delete[] m_statusWidths;
71 if ( m_statusStrings )
72 delete[] m_statusStrings;
73 }
74
75 bool wxStatusBarGeneric::Create(wxWindow *parent,
76 wxWindowID id,
77 long style,
78 const wxString& name)
79 {
80 m_statusWidths = (int *) NULL;
81 m_statusStrings = (wxString *) NULL;
82 m_nFields = 0;
83 m_borderX = wxTHICK_LINE_BORDER;
84 m_borderY = wxTHICK_LINE_BORDER;
85
86 bool success = wxWindow::Create(parent, id,
87 wxDefaultPosition, wxDefaultSize,
88 style | wxTAB_TRAVERSAL, name);
89
90 // Don't wish this to be found as a child
91 #ifndef __WXMAC__
92 parent->GetChildren().DeleteObject(this);
93 #endif
94 InitColours();
95
96 SetFont(m_defaultStatusBarFont);
97
98 return success;
99 }
100
101 void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths)
102 {
103 m_nFields = number;
104
105 if ( m_statusWidths )
106 delete[] m_statusWidths;
107
108 if ( m_statusStrings )
109 delete[] m_statusStrings;
110
111 m_statusStrings = new wxString[number];
112
113 int i;
114 for (i = 0; i < number; i++)
115 m_statusStrings[i] = "";
116
117 if ( widths )
118 SetStatusWidths(number, widths);
119 }
120
121 void wxStatusBarGeneric::SetStatusText(const wxString& text, int number)
122 {
123 if ((number < 0) || (number >= m_nFields))
124 return;
125
126 m_statusStrings[number] = text;
127
128 Refresh();
129 }
130
131 wxString wxStatusBarGeneric::GetStatusText(int n) const
132 {
133 wxCHECK_MSG( (n >= 0) && (n < m_nFields), wxEmptyString,
134 _T("invalid status bar field index") );
135
136 return m_statusStrings[n];
137 }
138
139 void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[])
140 {
141 // only set status widths, when n == number of statuswindows
142 if (n == m_nFields)
143 {
144 // only set status widths,
145 // when one window (minimum) is variable (width <= 0)
146 bool is_variable = FALSE;
147 int i;
148 for (i = 0; i < m_nFields; i++)
149 {
150 if (widths_field[i] <= 0) is_variable = TRUE;
151 }
152
153 // if there are old widths, delete them
154 if (m_statusWidths)
155 delete [] m_statusWidths;
156
157 // set widths
158 m_statusWidths = new int[n];
159 for (i = 0; i < m_nFields; i++)
160 {
161 m_statusWidths[i] = widths_field[i];
162 }
163 }
164 }
165
166 void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
167 {
168 wxPaintDC dc(this);
169
170 int i;
171 if ( GetFont().Ok() )
172 dc.SetFont(GetFont());
173 dc.SetBackgroundMode(wxTRANSPARENT);
174
175 for ( i = 0; i < m_nFields; i ++ )
176 DrawField(dc, i);
177
178 # ifdef __WXMSW__
179 dc.SetFont(wxNullFont);
180 # endif // MSW
181 }
182
183 void wxStatusBarGeneric::DrawFieldText(wxDC& dc, int i)
184 {
185 int leftMargin = 2;
186
187 wxRect rect;
188 GetFieldRect(i, rect);
189
190 wxString text(GetStatusText(i));
191
192 long x, y;
193
194 dc.GetTextExtent(text, &x, &y);
195
196 int xpos = rect.x + leftMargin;
197 int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ;
198
199 #if defined( __WXGTK__ ) || defined(__WXMAC__)
200 xpos++;
201 ypos++;
202 #endif
203
204 dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height);
205
206 dc.DrawText(text, xpos, ypos);
207
208 dc.DestroyClippingRegion();
209 }
210
211 void wxStatusBarGeneric::DrawField(wxDC& dc, int i)
212 {
213 wxRect rect;
214 GetFieldRect(i, rect);
215
216 // Draw border
217 // Have grey background, plus 3-d border -
218 // One black rectangle.
219 // Inside this, left and top sides - dark grey. Bottom and right -
220 // white.
221
222 dc.SetPen(m_hilightPen);
223
224 // Right and bottom white lines
225 dc.DrawLine(rect.x + rect.width, rect.y,
226 rect.x + rect.width, rect.y + rect.height);
227 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
228 rect.x, rect.y + rect.height);
229
230 dc.SetPen(m_mediumShadowPen);
231
232 // Left and top grey lines
233 dc.DrawLine(rect.x, rect.y + rect.height,
234 rect.x, rect.y);
235 dc.DrawLine(rect.x, rect.y,
236 rect.x + rect.width, rect.y);
237
238 DrawFieldText(dc, i);
239 }
240
241 // Get the position and size of the field's internal bounding rectangle
242 bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
243 {
244 wxCHECK_MSG( (n >= 0) && (n < m_nFields), FALSE,
245 _T("invalid status bar field index") );
246
247 int width, height;
248 GetClientSize(&width, &height);
249
250 int i;
251 int sum_of_nonvar = 0;
252 int num_of_var = 0;
253 bool do_same_width = FALSE;
254
255 int fieldWidth = 0;
256 int fieldPosition = 0;
257
258 if (m_statusWidths)
259 {
260 // if sum(not variable Windows) > c_width - (20 points per variable_window)
261 // then do_same_width = TRUE;
262 for (i = 0; i < m_nFields; i++)
263 {
264 if (m_statusWidths[i] > 0) sum_of_nonvar += m_statusWidths[i];
265 else num_of_var++;
266 }
267 if (sum_of_nonvar > (width - 20*num_of_var)) do_same_width = TRUE;
268 }
269 else do_same_width = TRUE;
270 if (do_same_width)
271 {
272 for (i = 0; i < m_nFields; i++)
273 {
274 fieldWidth = (int)(width/m_nFields);
275 fieldPosition = i*fieldWidth;
276 if ( i == n )
277 break;
278 }
279 }
280 else // no_same_width
281 {
282 int *tempwidth = new int[m_nFields];
283 int temppos = 0;
284 for (i = 0; i < m_nFields; i++)
285 {
286 if (m_statusWidths[i] > 0) tempwidth[i] = m_statusWidths[i];
287 else tempwidth[i] = (width - sum_of_nonvar) / num_of_var;
288 }
289 for (i = 0; i < m_nFields; i++)
290 {
291 fieldWidth = tempwidth[i];
292 fieldPosition = temppos;
293
294 temppos += tempwidth[i];
295
296 if ( i == n )
297 break;
298 }
299 delete [] tempwidth;
300 }
301
302 rect.x = fieldPosition + wxTHICK_LINE_BORDER;
303 rect.y = wxTHICK_LINE_BORDER;
304
305 rect.width = fieldWidth - 2 * wxTHICK_LINE_BORDER ;
306 rect.height = height - 2 * wxTHICK_LINE_BORDER ;
307
308 return TRUE;
309 }
310
311 // Initialize colours
312 void wxStatusBarGeneric::InitColours()
313 {
314 // Shadow colours
315 #if defined(__WIN95__)
316 wxColour mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW));
317 m_mediumShadowPen = wxPen(mediumShadowColour, 1, wxSOLID);
318
319 wxColour hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT));
320 m_hilightPen = wxPen(hilightColour, 1, wxSOLID);
321 #else
322 m_mediumShadowPen = wxPen("GREY", 1, wxSOLID);
323 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
324 #endif
325
326 m_defaultStatusBarFont = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
327 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
328 }
329
330 // Responds to colour changes, and passes event on to children.
331 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event)
332 {
333 InitColours();
334 Refresh();
335
336 // Propagate the event to the non-top-level children
337 wxWindow::OnSysColourChanged(event);
338 }
339
340 void wxStatusBarGeneric::SetMinHeight(int height)
341 {
342 // check that this min height is not less than minimal height for the
343 // current font
344 wxClientDC dc(this);
345 wxCoord y;
346 dc.GetTextExtent( _T("X"), NULL, &y );
347
348 if ( height > (11*y)/10 )
349 {
350 SetSize(-1, -1, -1, height + 2*m_borderY);
351 }
352 }
353
354 //#endif // Win32 && wxUSE_NATIVE_STATUSBAR