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