]>
Commit | Line | Data |
---|---|---|
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 | ||
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_statusStrings ) | |
70 | delete[] m_statusStrings; | |
71 | } | |
72 | ||
73 | bool 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 | ||
110 | void 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 | ||
123 | void 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 | #ifdef __WXMAC__ | |
131 | int major,minor; | |
132 | wxGetOsVersion( &major, &minor ); | |
133 | ||
134 | if (major >= 10) | |
135 | { | |
136 | Refresh(); | |
137 | return; | |
138 | } | |
139 | #endif | |
140 | ||
141 | wxRect rect; | |
142 | GetFieldRect(number, rect); | |
143 | wxClientDC dc(this); | |
144 | ||
145 | dc.SetBackground( wxBrush(GetBackgroundColour(), wxSOLID) ); | |
146 | dc.SetClippingRegion( rect.x+1, rect.y+1, rect.width-1, rect.height-1 ); | |
147 | dc.Clear(); | |
148 | dc.DestroyClippingRegion(); | |
149 | dc.SetFont(GetFont()); | |
150 | DrawFieldText( dc, number ); | |
151 | } | |
152 | ||
153 | wxString wxStatusBarGeneric::GetStatusText(int n) const | |
154 | { | |
155 | wxCHECK_MSG( (n >= 0) && (n < m_nFields), wxEmptyString, | |
156 | _T("invalid status bar field index") ); | |
157 | ||
158 | return m_statusStrings[n]; | |
159 | } | |
160 | ||
161 | void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[]) | |
162 | { | |
163 | // only set status widths, when n == number of statuswindows | |
164 | wxCHECK_RET( n == m_nFields, _T("status bar field count mismatch") ); | |
165 | ||
166 | // delete the old widths in any case - this function may be used to reset | |
167 | // the widths to the default (all equal) | |
168 | delete [] m_statusWidths; | |
169 | ||
170 | if ( !widths_field ) | |
171 | { | |
172 | // not an error, see the comment above | |
173 | m_statusWidths = (int *)NULL; | |
174 | Refresh(); | |
175 | return; | |
176 | } | |
177 | ||
178 | int i; | |
179 | ||
180 | // VZ: this doesn't do anything as is_variable is unused later | |
181 | #if 0 | |
182 | // when one window (minimum) is variable (width <= 0) | |
183 | bool is_variable = FALSE; | |
184 | for (i = 0; i < m_nFields; i++) | |
185 | { | |
186 | if (widths_field[i] <= 0) | |
187 | is_variable = TRUE; | |
188 | } | |
189 | #endif // 0 | |
190 | ||
191 | // set widths | |
192 | m_statusWidths = new int[n]; | |
193 | for (i = 0; i < m_nFields; i++) | |
194 | { | |
195 | m_statusWidths[i] = widths_field[i]; | |
196 | } | |
197 | Refresh(); | |
198 | } | |
199 | ||
200 | void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) ) | |
201 | { | |
202 | wxPaintDC dc(this); | |
203 | ||
204 | ||
205 | int i; | |
206 | if ( GetFont().Ok() ) | |
207 | dc.SetFont(GetFont()); | |
208 | dc.SetBackgroundMode(wxTRANSPARENT); | |
209 | ||
210 | #ifdef __WXPM__ | |
211 | wxColour vColor; | |
212 | ||
213 | vColor.InitFromName("LIGHT GREY"); | |
214 | ::WinFillRect(dc.m_hPS, &dc.m_vRclPaint, vColor.GetPixel()); | |
215 | #endif | |
216 | ||
217 | for ( i = 0; i < m_nFields; i ++ ) | |
218 | DrawField(dc, i); | |
219 | ||
220 | #ifdef __WXMSW__ | |
221 | dc.SetFont(wxNullFont); | |
222 | #endif // MSW | |
223 | } | |
224 | ||
225 | void wxStatusBarGeneric::DrawFieldText(wxDC& dc, int i) | |
226 | { | |
227 | int leftMargin = 2; | |
228 | ||
229 | wxRect rect; | |
230 | GetFieldRect(i, rect); | |
231 | ||
232 | wxString text(GetStatusText(i)); | |
233 | ||
234 | long x, y; | |
235 | ||
236 | dc.GetTextExtent(text, &x, &y); | |
237 | ||
238 | int xpos = rect.x + leftMargin; | |
239 | int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ; | |
240 | ||
241 | #if defined( __WXGTK__ ) || defined(__WXMAC__) | |
242 | xpos++; | |
243 | ypos++; | |
244 | #endif | |
245 | ||
246 | dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height); | |
247 | ||
248 | dc.DrawText(text, xpos, ypos); | |
249 | ||
250 | dc.DestroyClippingRegion(); | |
251 | } | |
252 | ||
253 | void wxStatusBarGeneric::DrawField(wxDC& dc, int i) | |
254 | { | |
255 | wxRect rect; | |
256 | GetFieldRect(i, rect); | |
257 | ||
258 | // Draw border | |
259 | // Have grey background, plus 3-d border - | |
260 | // One black rectangle. | |
261 | // Inside this, left and top sides - dark grey. Bottom and right - | |
262 | // white. | |
263 | ||
264 | dc.SetPen(m_hilightPen); | |
265 | ||
266 | #ifndef __WXPM__ | |
267 | ||
268 | // Right and bottom white lines | |
269 | dc.DrawLine(rect.x + rect.width, rect.y, | |
270 | rect.x + rect.width, rect.y + rect.height); | |
271 | dc.DrawLine(rect.x + rect.width, rect.y + rect.height, | |
272 | rect.x, rect.y + rect.height); | |
273 | ||
274 | dc.SetPen(m_mediumShadowPen); | |
275 | ||
276 | // Left and top grey lines | |
277 | dc.DrawLine(rect.x, rect.y + rect.height, | |
278 | rect.x, rect.y); | |
279 | dc.DrawLine(rect.x, rect.y, | |
280 | rect.x + rect.width, rect.y); | |
281 | #else | |
282 | // Right | |
283 | dc.DrawLine(rect.x + rect.width, rect.y, | |
284 | rect.x + rect.width, rect.y + rect.height + 2); | |
285 | dc.SetPen(m_mediumShadowPen); | |
286 | dc.DrawLine(rect.x + rect.width + 1, rect.y, | |
287 | rect.x + rect.width + 1, rect.y + rect.height + 2); | |
288 | dc.DrawLine(rect.x + rect.width + 2, rect.y, | |
289 | rect.x + rect.width + 2, rect.y + rect.height + 2); | |
290 | // Top | |
291 | dc.DrawLine(rect.x + rect.width + 2, rect.y, | |
292 | rect.x - 2, rect.y); | |
293 | dc.DrawLine(rect.x + rect.width + 1, rect.y - 1, | |
294 | rect.x - 2, rect.y - 1); | |
295 | dc.SetPen(m_hilightPen); | |
296 | dc.DrawLine(rect.x + rect.width, rect.y - 2, | |
297 | rect.x - 2, rect.y - 2); | |
298 | ||
299 | #endif | |
300 | ||
301 | DrawFieldText(dc, i); | |
302 | } | |
303 | ||
304 | // Get the position and size of the field's internal bounding rectangle | |
305 | bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const | |
306 | { | |
307 | wxCHECK_MSG( (n >= 0) && (n < m_nFields), FALSE, | |
308 | _T("invalid status bar field index") ); | |
309 | ||
310 | int width, height; | |
311 | #ifdef __WXPM__ | |
312 | GetSize(&width, &height); | |
313 | #else | |
314 | GetClientSize(&width, &height); | |
315 | #endif | |
316 | ||
317 | int i; | |
318 | int sum_of_nonvar = 0; | |
319 | int num_of_var = 0; | |
320 | bool do_same_width = FALSE; | |
321 | ||
322 | int fieldWidth = 0; | |
323 | int fieldPosition = 0; | |
324 | ||
325 | if (m_statusWidths) | |
326 | { | |
327 | // if sum(not variable Windows) > c_width - (20 points per variable_window) | |
328 | // then do_same_width = TRUE; | |
329 | for (i = 0; i < m_nFields; i++) | |
330 | { | |
331 | if (m_statusWidths[i] > 0) sum_of_nonvar += m_statusWidths[i]; | |
332 | else num_of_var++; | |
333 | } | |
334 | if (sum_of_nonvar > (width - 20*num_of_var)) do_same_width = TRUE; | |
335 | } | |
336 | else do_same_width = TRUE; | |
337 | if (do_same_width) | |
338 | { | |
339 | for (i = 0; i < m_nFields; i++) | |
340 | { | |
341 | fieldWidth = (int)(width/m_nFields); | |
342 | fieldPosition = i*fieldWidth; | |
343 | if ( i == n ) | |
344 | break; | |
345 | } | |
346 | } | |
347 | else // no_same_width | |
348 | { | |
349 | int *tempwidth = new int[m_nFields]; | |
350 | int temppos = 0; | |
351 | for (i = 0; i < m_nFields; i++) | |
352 | { | |
353 | if (m_statusWidths[i] > 0) tempwidth[i] = m_statusWidths[i]; | |
354 | else tempwidth[i] = (width - sum_of_nonvar) / num_of_var; | |
355 | } | |
356 | for (i = 0; i < m_nFields; i++) | |
357 | { | |
358 | fieldWidth = tempwidth[i]; | |
359 | fieldPosition = temppos; | |
360 | ||
361 | temppos += tempwidth[i]; | |
362 | ||
363 | if ( i == n ) | |
364 | break; | |
365 | } | |
366 | delete [] tempwidth; | |
367 | } | |
368 | ||
369 | rect.x = fieldPosition + wxTHICK_LINE_BORDER; | |
370 | rect.y = wxTHICK_LINE_BORDER; | |
371 | ||
372 | rect.width = fieldWidth - 2 * wxTHICK_LINE_BORDER ; | |
373 | rect.height = height - 2 * wxTHICK_LINE_BORDER ; | |
374 | ||
375 | return TRUE; | |
376 | } | |
377 | ||
378 | // Initialize colours | |
379 | void wxStatusBarGeneric::InitColours() | |
380 | { | |
381 | // Shadow colours | |
382 | #if defined(__WIN95__) | |
383 | wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)); | |
384 | m_mediumShadowPen = wxPen(mediumShadowColour, 1, wxSOLID); | |
385 | ||
386 | wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT)); | |
387 | m_hilightPen = wxPen(hilightColour, 1, wxSOLID); | |
388 | #elif defined(__WXPM__) | |
389 | m_mediumShadowPen = wxPen("DARK GREY", 1, wxSOLID); | |
390 | m_hilightPen = wxPen("WHITE", 1, wxSOLID); | |
391 | ||
392 | wxColour vColour; | |
393 | ||
394 | vColour.Set(wxString("LIGHT GREY")); | |
395 | SetBackgroundColour(vColour); | |
396 | vColour.Set(wxString("BLACK")); | |
397 | SetForegroundColour(vColour); | |
398 | m_defaultStatusBarFont = *wxSMALL_FONT; | |
399 | #else | |
400 | m_mediumShadowPen = wxPen("GREY", 1, wxSOLID); | |
401 | m_hilightPen = wxPen("WHITE", 1, wxSOLID); | |
402 | #endif | |
403 | ||
404 | #ifndef __WXPM__ | |
405 | m_defaultStatusBarFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); | |
406 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); | |
407 | #endif | |
408 | } | |
409 | ||
410 | // Responds to colour changes, and passes event on to children. | |
411 | void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event) | |
412 | { | |
413 | InitColours(); | |
414 | Refresh(); | |
415 | ||
416 | // Propagate the event to the non-top-level children | |
417 | wxWindow::OnSysColourChanged(event); | |
418 | } | |
419 | ||
420 | void wxStatusBarGeneric::SetMinHeight(int height) | |
421 | { | |
422 | // check that this min height is not less than minimal height for the | |
423 | // current font | |
424 | wxClientDC dc(this); | |
425 | wxCoord y; | |
426 | dc.GetTextExtent( _T("X"), NULL, &y ); | |
427 | ||
428 | if ( height > (11*y)/10 ) | |
429 | { | |
430 | SetSize(-1, -1, -1, height + 2*m_borderY); | |
431 | } | |
432 | } | |
433 | ||
434 | #endif // wxUSE_STATUSBAR |