]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/statusbr.cpp
wxRegion fixes (off by one)
[wxWidgets.git] / src / generic / statusbr.cpp
... / ...
CommitLineData
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 wxUSE_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/statusbr.h"
33
34// we only have to do it here when we use wxStatusBarGeneric in addition to the
35// standard wxStatusBar class, if wxStatusBarGeneric is the same as
36// wxStatusBar, then the corresponding IMPLEMENT_DYNAMIC_CLASS is already in
37// common/statbar.cpp
38#if defined(__WXMAC__) || \
39 (defined(wxUSE_NATIVE_STATUSBAR) && wxUSE_NATIVE_STATUSBAR)
40 #include "wx/generic/statusbr.h"
41
42 IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric, wxWindow)
43#endif // wxUSE_NATIVE_STATUSBAR
44
45BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow)
46 EVT_PAINT(wxStatusBarGeneric::OnPaint)
47 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged)
48END_EVENT_TABLE()
49
50// Default status border dimensions
51#define wxTHICK_LINE_BORDER 2
52#define wxTHICK_LINE_WIDTH 1
53
54wxStatusBarGeneric::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
63wxStatusBarGeneric::~wxStatusBarGeneric()
64{
65# ifdef __WXMSW__
66 SetFont(wxNullFont);
67# endif // MSW
68
69 if ( m_statusStrings )
70 delete[] m_statusStrings;
71}
72
73bool wxStatusBarGeneric::Create(wxWindow *parent,
74 wxWindowID id,
75 long style,
76 const wxString& name)
77{
78 m_statusWidths = (int *) NULL;
79 m_statusStrings = (wxString *) NULL;
80 m_nFields = 0;
81 m_borderX = wxTHICK_LINE_BORDER;
82 m_borderY = wxTHICK_LINE_BORDER;
83
84 bool success = wxWindow::Create(parent, id,
85 wxDefaultPosition, wxDefaultSize,
86 style | wxTAB_TRAVERSAL, name);
87
88 // Don't wish this to be found as a child
89#ifndef __WXMAC__
90 parent->GetChildren().DeleteObject(this);
91#endif
92 InitColours();
93
94 SetFont(m_defaultStatusBarFont);
95
96 // Set the height according to the font and the border size
97 wxClientDC dc(this);
98 dc.SetFont(GetFont());
99
100 wxCoord y;
101 dc.GetTextExtent(_T("X"), NULL, &y );
102
103 int height = (int)( (11*y)/10 + 2*GetBorderY());
104
105 SetSize(-1, -1, -1, height);
106
107 return success;
108}
109
110void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths)
111{
112 if ( number != m_nFields )
113 {
114 m_nFields = number;
115
116 delete[] m_statusStrings;
117 m_statusStrings = new wxString[number];
118 }
119
120 SetStatusWidths(number, widths);
121}
122
123void wxStatusBarGeneric::SetStatusText(const wxString& text, int number)
124{
125 wxCHECK_RET( (number >= 0) && (number < m_nFields),
126 _T("invalid status bar field index") );
127
128 m_statusStrings[number] = text;
129
130 Refresh();
131}
132
133wxString wxStatusBarGeneric::GetStatusText(int n) const
134{
135 wxCHECK_MSG( (n >= 0) && (n < m_nFields), wxEmptyString,
136 _T("invalid status bar field index") );
137
138 return m_statusStrings[n];
139}
140
141void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[])
142{
143 // only set status widths, when n == number of statuswindows
144 wxCHECK_RET( n == m_nFields, _T("status bar field count mismatch") );
145
146 // delete the old widths in any case - this function may be used to reset
147 // the widths to the default (all equal)
148 delete [] m_statusWidths;
149
150 if ( !widths_field )
151 {
152 // not an error, see the comment above
153 m_statusWidths = (int *)NULL;
154
155 return;
156 }
157
158 int i;
159
160 // VZ: this doesn't do anything as is_variable is unused later
161#if 0
162 // when one window (minimum) is variable (width <= 0)
163 bool is_variable = FALSE;
164 for (i = 0; i < m_nFields; i++)
165 {
166 if (widths_field[i] <= 0)
167 is_variable = TRUE;
168 }
169#endif // 0
170
171 // set widths
172 m_statusWidths = new int[n];
173 for (i = 0; i < m_nFields; i++)
174 {
175 m_statusWidths[i] = widths_field[i];
176 }
177}
178
179void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
180{
181 wxPaintDC dc(this);
182
183
184 int i;
185 if ( GetFont().Ok() )
186 dc.SetFont(GetFont());
187 dc.SetBackgroundMode(wxTRANSPARENT);
188
189#ifdef __WXPM__
190 wxColour vColor;
191
192 vColor.InitFromName("GREY");
193 ::WinFillRect(dc.m_hPS, &dc.m_vRclPaint, vColor.GetPixel());
194#endif
195
196 for ( i = 0; i < m_nFields; i ++ )
197 DrawField(dc, i);
198
199#ifdef __WXMSW__
200 dc.SetFont(wxNullFont);
201#endif // MSW
202}
203
204void wxStatusBarGeneric::DrawFieldText(wxDC& dc, int i)
205{
206 int leftMargin = 2;
207
208 wxRect rect;
209 GetFieldRect(i, rect);
210
211 wxString text(GetStatusText(i));
212
213 long x, y;
214
215 dc.GetTextExtent(text, &x, &y);
216
217 int xpos = rect.x + leftMargin;
218 int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ;
219
220#if defined( __WXGTK__ ) || defined(__WXMAC__)
221 xpos++;
222 ypos++;
223#endif
224
225 dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height);
226
227 dc.DrawText(text, xpos, ypos);
228
229 dc.DestroyClippingRegion();
230}
231
232void wxStatusBarGeneric::DrawField(wxDC& dc, int i)
233{
234 wxRect rect;
235 GetFieldRect(i, rect);
236
237 // Draw border
238 // Have grey background, plus 3-d border -
239 // One black rectangle.
240 // Inside this, left and top sides - dark grey. Bottom and right -
241 // white.
242
243 dc.SetPen(m_hilightPen);
244
245#ifndef __WXPM__
246
247 // Right and bottom white lines
248 dc.DrawLine(rect.x + rect.width, rect.y,
249 rect.x + rect.width, rect.y + rect.height);
250 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
251 rect.x, rect.y + rect.height);
252
253 dc.SetPen(m_mediumShadowPen);
254
255 // Left and top grey lines
256 dc.DrawLine(rect.x, rect.y + rect.height,
257 rect.x, rect.y);
258 dc.DrawLine(rect.x, rect.y,
259 rect.x + rect.width, rect.y);
260#else
261 // Right
262 dc.DrawLine(rect.x + rect.width, rect.y,
263 rect.x + rect.width, rect.y + rect.height + 2);
264 dc.SetPen(m_mediumShadowPen);
265 dc.DrawLine(rect.x + rect.width + 1, rect.y,
266 rect.x + rect.width + 1, rect.y + rect.height + 2);
267 dc.DrawLine(rect.x + rect.width + 2, rect.y,
268 rect.x + rect.width + 2, rect.y + rect.height + 2);
269 // Top
270 dc.DrawLine(rect.x + rect.width + 2, rect.y,
271 rect.x - 2, rect.y);
272 dc.DrawLine(rect.x + rect.width + 1, rect.y - 1,
273 rect.x - 2, rect.y - 1);
274 dc.SetPen(m_hilightPen);
275 dc.DrawLine(rect.x + rect.width, rect.y - 2,
276 rect.x - 2, rect.y - 2);
277
278#endif
279
280 DrawFieldText(dc, i);
281}
282
283 // Get the position and size of the field's internal bounding rectangle
284bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
285{
286 wxCHECK_MSG( (n >= 0) && (n < m_nFields), FALSE,
287 _T("invalid status bar field index") );
288
289 int width, height;
290#ifdef __WXPM__
291 GetSize(&width, &height);
292#else
293 GetClientSize(&width, &height);
294#endif
295
296 int i;
297 int sum_of_nonvar = 0;
298 int num_of_var = 0;
299 bool do_same_width = FALSE;
300
301 int fieldWidth = 0;
302 int fieldPosition = 0;
303
304 if (m_statusWidths)
305 {
306 // if sum(not variable Windows) > c_width - (20 points per variable_window)
307 // then do_same_width = TRUE;
308 for (i = 0; i < m_nFields; i++)
309 {
310 if (m_statusWidths[i] > 0) sum_of_nonvar += m_statusWidths[i];
311 else num_of_var++;
312 }
313 if (sum_of_nonvar > (width - 20*num_of_var)) do_same_width = TRUE;
314 }
315 else do_same_width = TRUE;
316 if (do_same_width)
317 {
318 for (i = 0; i < m_nFields; i++)
319 {
320 fieldWidth = (int)(width/m_nFields);
321 fieldPosition = i*fieldWidth;
322 if ( i == n )
323 break;
324 }
325 }
326 else // no_same_width
327 {
328 int *tempwidth = new int[m_nFields];
329 int temppos = 0;
330 for (i = 0; i < m_nFields; i++)
331 {
332 if (m_statusWidths[i] > 0) tempwidth[i] = m_statusWidths[i];
333 else tempwidth[i] = (width - sum_of_nonvar) / num_of_var;
334 }
335 for (i = 0; i < m_nFields; i++)
336 {
337 fieldWidth = tempwidth[i];
338 fieldPosition = temppos;
339
340 temppos += tempwidth[i];
341
342 if ( i == n )
343 break;
344 }
345 delete [] tempwidth;
346 }
347
348 rect.x = fieldPosition + wxTHICK_LINE_BORDER;
349 rect.y = wxTHICK_LINE_BORDER;
350
351 rect.width = fieldWidth - 2 * wxTHICK_LINE_BORDER ;
352 rect.height = height - 2 * wxTHICK_LINE_BORDER ;
353
354 return TRUE;
355}
356
357// Initialize colours
358void wxStatusBarGeneric::InitColours()
359{
360 // Shadow colours
361#if defined(__WIN95__)
362 wxColour mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW));
363 m_mediumShadowPen = wxPen(mediumShadowColour, 1, wxSOLID);
364
365 wxColour hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT));
366 m_hilightPen = wxPen(hilightColour, 1, wxSOLID);
367#elif defined(__WXPM__)
368 m_mediumShadowPen = wxPen("LIGHT GREY", 1, wxSOLID);
369 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
370#else
371 m_mediumShadowPen = wxPen("GREY", 1, wxSOLID);
372 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
373#endif
374
375 m_defaultStatusBarFont = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
376 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
377}
378
379// Responds to colour changes, and passes event on to children.
380void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event)
381{
382 InitColours();
383 Refresh();
384
385 // Propagate the event to the non-top-level children
386 wxWindow::OnSysColourChanged(event);
387}
388
389void wxStatusBarGeneric::SetMinHeight(int height)
390{
391 // check that this min height is not less than minimal height for the
392 // current font
393 wxClientDC dc(this);
394 wxCoord y;
395 dc.GetTextExtent( _T("X"), NULL, &y );
396
397 if ( height > (11*y)/10 )
398 {
399 SetSize(-1, -1, -1, height + 2*m_borderY);
400 }
401}
402
403#endif // wxUSE_STATUSBAR