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