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