]> git.saurik.com Git - wxWidgets.git/blame - src/generic/statusbr.cpp
removed (wrong?) assert
[wxWidgets.git] / src / generic / statusbr.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: statusbr.cpp
3// Purpose: wxStatusBar 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
f51f94ea 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
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#include "wx/generic/statusbr.h"
31
2049ba38 32#ifdef __WXMSW__
c801d85f
KB
33#include <windows.h>
34
35#ifdef DrawText
36#undef DrawText
37#endif
38
39#endif
40
41#if !USE_SHARED_LIBRARY
42IMPLEMENT_DYNAMIC_CLASS(wxStatusBar, wxWindow)
43
44BEGIN_EVENT_TABLE(wxStatusBar, wxWindow)
f51f94ea 45 EVT_PAINT(wxStatusBar::OnPaint)
c801d85f
KB
46 EVT_SYS_COLOUR_CHANGED(wxStatusBar::OnSysColourChanged)
47END_EVENT_TABLE()
48#endif
49
50// Default status border dimensions
51#define wxTHICK_LINE_BORDER 2
52#define wxTHICK_LINE_WIDTH 1
53
54wxStatusBar::wxStatusBar(void)
55{
c67daf87
UR
56 m_statusWidths = (int *) NULL;
57 m_statusStrings = (wxString *) NULL;
c801d85f
KB
58 m_nFields = 0;
59 m_borderX = wxTHICK_LINE_BORDER;
60 m_borderY = wxTHICK_LINE_BORDER;
61}
62
63wxStatusBar::~wxStatusBar(void)
64{
f51f94ea
VZ
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;
c801d85f
KB
73}
74
debe6624 75bool wxStatusBar::Create(wxWindow *parent, wxWindowID id,
c801d85f
KB
76 const wxPoint& pos,
77 const wxSize& size,
debe6624 78 long style,
c801d85f
KB
79 const wxString& name)
80{
c67daf87
UR
81 m_statusWidths = (int *) NULL;
82 m_statusStrings = (wxString *) NULL;
c801d85f
KB
83 m_nFields = 0;
84 m_borderX = wxTHICK_LINE_BORDER;
85 m_borderY = wxTHICK_LINE_BORDER;
86
b292e2f5 87 bool success = wxWindow::Create(parent, id, pos, size, style | wxTAB_TRAVERSAL, name);
c801d85f
KB
88
89 // Don't wish this to be found as a child
c0ed460c 90 parent->GetChildren().DeleteObject(this);
c801d85f
KB
91
92 InitColours();
93
94 SetFont(m_defaultStatusBarFont);
95
96 return success;
97}
98
8b9518ee 99void wxStatusBar::SetFieldsCount(int number, const int widths[])
c801d85f
KB
100{
101 m_nFields = number;
102
103 if ( m_statusWidths )
f51f94ea 104 delete[] m_statusWidths;
c801d85f
KB
105
106 if ( m_statusStrings )
f51f94ea 107 delete[] m_statusStrings;
c801d85f
KB
108
109 m_statusStrings = new wxString[number];
110
111 int i;
112 for (i = 0; i < number; i++)
f51f94ea 113 m_statusStrings[i] = "";
c801d85f 114
f51f94ea
VZ
115 if ( widths )
116 SetStatusWidths(number, widths);
c801d85f
KB
117}
118
debe6624 119void wxStatusBar::SetStatusText(const wxString& text, int number)
c801d85f
KB
120{
121 if ((number < 0) || (number >= m_nFields))
122 return;
123
124 m_statusStrings[number] = text;
125
126 Refresh();
127
2049ba38 128#ifdef __WXMSW__
c801d85f
KB
129 // For some reason, this can cause major GDI problems - graphics
130 // all over the place. E.g. in print previewing.
131// ::UpdateWindow((HWND) GetHWND());
132#endif
133}
134
debe6624 135wxString wxStatusBar::GetStatusText(int n) const
c801d85f
KB
136{
137 if ((n < 0) || (n >= m_nFields))
138 return wxString("");
139 else
140 return m_statusStrings[n];
141}
142
8b9518ee 143void wxStatusBar::SetStatusWidths(int n, const int widths_field[])
c801d85f
KB
144{
145 // only set status widths, when n == number of statuswindows
146 if (n == m_nFields)
147 {
148 // only set status widths,
149 // when one window (minimum) is variable (width <= 0)
150 bool is_variable = FALSE;
151 int i;
152 for (i = 0; i < m_nFields; i++)
153 {
154 if (widths_field[i] <= 0) is_variable = TRUE;
155 }
156
157 // if there are old widths, delete them
158 if (m_statusWidths)
159 delete [] m_statusWidths;
160
161 // set widths
162 m_statusWidths = new int[n];
163 for (i = 0; i < m_nFields; i++)
164 {
165 m_statusWidths[i] = widths_field[i];
166 }
167 }
168}
169
170void wxStatusBar::OnPaint(wxPaintEvent& WXUNUSED(event) )
171{
172 wxPaintDC dc(this);
173
174 int i;
c0ed460c
JS
175 if ( GetFont().Ok() )
176 dc.SetFont(GetFont());
c801d85f
KB
177 dc.SetBackgroundMode(wxTRANSPARENT);
178
179 for ( i = 0; i < m_nFields; i ++ )
f51f94ea 180 DrawField(dc, i);
e97f20a0 181
f51f94ea
VZ
182# ifdef __WXMSW__
183 dc.SetFont(wxNullFont);
184# endif // MSW
c801d85f
KB
185}
186
debe6624 187void wxStatusBar::DrawFieldText(wxDC& dc, int i)
c801d85f
KB
188{
189 int leftMargin = 2;
190
16e93305 191 wxRect rect;
c801d85f
KB
192 GetFieldRect(i, rect);
193
194 wxString text(GetStatusText(i));
195
196 long x, y;
197
198 dc.GetTextExtent(text, &x, &y);
199
200 int xpos = rect.x + leftMargin;
201 int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ;
92976ab6
RR
202
203#ifdef __WXGTK__
204 xpos++;
205 ypos++;
206#endif
c801d85f
KB
207
208 dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height);
209
210 dc.DrawText(text, xpos, ypos);
211
212 dc.DestroyClippingRegion();
213}
214
debe6624 215void wxStatusBar::DrawField(wxDC& dc, int i)
c801d85f 216{
16e93305 217 wxRect rect;
c801d85f
KB
218 GetFieldRect(i, rect);
219
220 // Draw border
221 // Have grey background, plus 3-d border -
222 // One black rectangle.
223 // Inside this, left and top sides - dark grey. Bottom and right -
224 // white.
225
f51f94ea 226 dc.SetPen(m_hilightPen);
c801d85f
KB
227
228 // Right and bottom white lines
229 dc.DrawLine(rect.x + rect.width, rect.y,
230 rect.x + rect.width, rect.y + rect.height);
231 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
f51f94ea 232 rect.x, rect.y + rect.height);
c801d85f 233
f51f94ea 234 dc.SetPen(m_mediumShadowPen);
c801d85f
KB
235
236 // Left and top grey lines
237 dc.DrawLine(rect.x, rect.y + rect.height,
f51f94ea 238 rect.x, rect.y);
c801d85f 239 dc.DrawLine(rect.x, rect.y,
f51f94ea 240 rect.x + rect.width, rect.y);
c801d85f 241
f51f94ea 242 DrawFieldText(dc, i);
c801d85f
KB
243}
244
245 // Get the position and size of the field's internal bounding rectangle
16e93305 246bool wxStatusBar::GetFieldRect(int n, wxRect& rect) const
c801d85f
KB
247{
248 if ((n < 0) || (n >= m_nFields))
249 return FALSE;
250
251 int width, height;
252 GetClientSize(&width, &height);
253
254 int i;
255 int sum_of_nonvar = 0;
256 int num_of_var = 0;
257 bool do_same_width = FALSE;
258
259 int fieldWidth = 0;
260 int fieldPosition = 0;
261
262 if (m_statusWidths)
263 {
264 // if sum(not variable Windows) > c_width - (20 points per variable_window)
265 // then do_same_width = TRUE;
266 for (i = 0; i < m_nFields; i++)
267 {
268 if (m_statusWidths[i] > 0) sum_of_nonvar += m_statusWidths[i];
269 else num_of_var++;
270 }
271 if (sum_of_nonvar > (width - 20*num_of_var)) do_same_width = TRUE;
272 }
273 else do_same_width = TRUE;
274 if (do_same_width)
275 {
276 for (i = 0; i < m_nFields; i++)
277 {
278 fieldWidth = (int)(width/m_nFields);
f51f94ea
VZ
279 fieldPosition = i*fieldWidth;
280 if ( i == n )
281 break;
c801d85f
KB
282 }
283 }
284 else // no_same_width
285 {
286 int *tempwidth = new int[m_nFields];
287 int temppos = 0;
288 for (i = 0; i < m_nFields; i++)
289 {
290 if (m_statusWidths[i] > 0) tempwidth[i] = m_statusWidths[i];
291 else tempwidth[i] = (width - sum_of_nonvar) / num_of_var;
292 }
293 for (i = 0; i < m_nFields; i++)
294 {
f51f94ea
VZ
295 fieldWidth = tempwidth[i];
296 fieldPosition = temppos;
c801d85f 297
f51f94ea 298 temppos += tempwidth[i];
c801d85f 299
f51f94ea
VZ
300 if ( i == n )
301 break;
c801d85f
KB
302 }
303 delete [] tempwidth;
304 }
305
306 rect.x = fieldPosition + wxTHICK_LINE_BORDER;
f51f94ea 307 rect.y = wxTHICK_LINE_BORDER;
c801d85f 308
f51f94ea
VZ
309 rect.width = fieldWidth - 2 * wxTHICK_LINE_BORDER ;
310 rect.height = height - 2 * wxTHICK_LINE_BORDER ;
c801d85f 311
f51f94ea 312 return TRUE;
c801d85f
KB
313}
314
315// Initialize colours
316void wxStatusBar::InitColours(void)
317{
318 // Shadow colours
319#if defined(__WIN95__)
320 wxColour mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW));
321 m_mediumShadowPen = wxPen(mediumShadowColour, 1, wxSOLID);
322
323 wxColour hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT));
324 m_hilightPen = wxPen(hilightColour, 1, wxSOLID);
325#else
326 m_mediumShadowPen = wxPen("GREY", 1, wxSOLID);
327 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
328#endif
329
16c1f7f3 330 m_defaultStatusBarFont = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
c801d85f
KB
331 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
332}
333
334// Responds to colour changes, and passes event on to children.
335void wxStatusBar::OnSysColourChanged(wxSysColourChangedEvent& event)
336{
337 InitColours();
338 Refresh();
339
340 // Propagate the event to the non-top-level children
341 wxWindow::OnSysColourChanged(event);
342}
343