]>
Commit | Line | Data |
---|---|---|
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 |
37 | IMPLEMENT_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 |
43 | BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow) |
44 | EVT_PAINT(wxStatusBarGeneric::OnPaint) | |
45 | EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged) | |
c801d85f | 46 | END_EVENT_TABLE() |
c801d85f KB |
47 | |
48 | // Default status border dimensions | |
49 | #define wxTHICK_LINE_BORDER 2 | |
50 | #define wxTHICK_LINE_WIDTH 1 | |
51 | ||
ed791986 | 52 | wxStatusBarGeneric::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 | 61 | wxStatusBarGeneric::~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 |
73 | bool 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 | 99 | void wxStatusBarGeneric::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 | |
ed791986 VZ |
115 | if ( widths ) |
116 | SetStatusWidths(number, widths); | |
c801d85f KB |
117 | } |
118 | ||
ed791986 | 119 | void wxStatusBarGeneric::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(); | |
c801d85f KB |
127 | } |
128 | ||
ed791986 | 129 | wxString wxStatusBarGeneric::GetStatusText(int n) const |
c801d85f | 130 | { |
ed791986 VZ |
131 | wxCHECK_MSG( (n >= 0) && (n < m_nFields), wxEmptyString, |
132 | _T("invalid status bar field index") ); | |
133 | ||
c801d85f KB |
134 | return m_statusStrings[n]; |
135 | } | |
136 | ||
ed791986 | 137 | void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[]) |
c801d85f KB |
138 | { |
139 | // only set status widths, when n == number of statuswindows | |
140 | if (n == m_nFields) | |
141 | { | |
142 | // only set status widths, | |
143 | // when one window (minimum) is variable (width <= 0) | |
144 | bool is_variable = FALSE; | |
145 | int i; | |
146 | for (i = 0; i < m_nFields; i++) | |
147 | { | |
148 | if (widths_field[i] <= 0) is_variable = TRUE; | |
149 | } | |
150 | ||
151 | // if there are old widths, delete them | |
152 | if (m_statusWidths) | |
153 | delete [] m_statusWidths; | |
154 | ||
155 | // set widths | |
156 | m_statusWidths = new int[n]; | |
157 | for (i = 0; i < m_nFields; i++) | |
158 | { | |
159 | m_statusWidths[i] = widths_field[i]; | |
160 | } | |
161 | } | |
162 | } | |
163 | ||
ed791986 | 164 | void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) ) |
c801d85f KB |
165 | { |
166 | wxPaintDC dc(this); | |
167 | ||
168 | int i; | |
c0ed460c JS |
169 | if ( GetFont().Ok() ) |
170 | dc.SetFont(GetFont()); | |
c801d85f KB |
171 | dc.SetBackgroundMode(wxTRANSPARENT); |
172 | ||
173 | for ( i = 0; i < m_nFields; i ++ ) | |
f51f94ea | 174 | DrawField(dc, i); |
e97f20a0 | 175 | |
f51f94ea VZ |
176 | # ifdef __WXMSW__ |
177 | dc.SetFont(wxNullFont); | |
178 | # endif // MSW | |
c801d85f KB |
179 | } |
180 | ||
ed791986 | 181 | void wxStatusBarGeneric::DrawFieldText(wxDC& dc, int i) |
c801d85f KB |
182 | { |
183 | int leftMargin = 2; | |
184 | ||
16e93305 | 185 | wxRect rect; |
c801d85f KB |
186 | GetFieldRect(i, rect); |
187 | ||
188 | wxString text(GetStatusText(i)); | |
189 | ||
190 | long x, y; | |
191 | ||
192 | dc.GetTextExtent(text, &x, &y); | |
193 | ||
194 | int xpos = rect.x + leftMargin; | |
195 | int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ; | |
ed791986 | 196 | |
7c74e7fe | 197 | #if defined( __WXGTK__ ) || defined(__WXMAC__) |
92976ab6 RR |
198 | xpos++; |
199 | ypos++; | |
200 | #endif | |
c801d85f KB |
201 | |
202 | dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height); | |
203 | ||
204 | dc.DrawText(text, xpos, ypos); | |
205 | ||
206 | dc.DestroyClippingRegion(); | |
207 | } | |
208 | ||
ed791986 | 209 | void wxStatusBarGeneric::DrawField(wxDC& dc, int i) |
c801d85f | 210 | { |
16e93305 | 211 | wxRect rect; |
c801d85f KB |
212 | GetFieldRect(i, rect); |
213 | ||
214 | // Draw border | |
215 | // Have grey background, plus 3-d border - | |
216 | // One black rectangle. | |
217 | // Inside this, left and top sides - dark grey. Bottom and right - | |
218 | // white. | |
219 | ||
f51f94ea | 220 | dc.SetPen(m_hilightPen); |
c801d85f KB |
221 | |
222 | // Right and bottom white lines | |
223 | dc.DrawLine(rect.x + rect.width, rect.y, | |
224 | rect.x + rect.width, rect.y + rect.height); | |
225 | dc.DrawLine(rect.x + rect.width, rect.y + rect.height, | |
f51f94ea | 226 | rect.x, rect.y + rect.height); |
c801d85f | 227 | |
f51f94ea | 228 | dc.SetPen(m_mediumShadowPen); |
c801d85f KB |
229 | |
230 | // Left and top grey lines | |
231 | dc.DrawLine(rect.x, rect.y + rect.height, | |
f51f94ea | 232 | rect.x, rect.y); |
c801d85f | 233 | dc.DrawLine(rect.x, rect.y, |
f51f94ea | 234 | rect.x + rect.width, rect.y); |
c801d85f | 235 | |
f51f94ea | 236 | DrawFieldText(dc, i); |
c801d85f KB |
237 | } |
238 | ||
239 | // Get the position and size of the field's internal bounding rectangle | |
ed791986 | 240 | bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const |
c801d85f | 241 | { |
ed791986 VZ |
242 | wxCHECK_MSG( (n >= 0) && (n < m_nFields), FALSE, |
243 | _T("invalid status bar field index") ); | |
c801d85f KB |
244 | |
245 | int width, height; | |
246 | GetClientSize(&width, &height); | |
247 | ||
248 | int i; | |
249 | int sum_of_nonvar = 0; | |
250 | int num_of_var = 0; | |
251 | bool do_same_width = FALSE; | |
252 | ||
253 | int fieldWidth = 0; | |
254 | int fieldPosition = 0; | |
255 | ||
256 | if (m_statusWidths) | |
257 | { | |
258 | // if sum(not variable Windows) > c_width - (20 points per variable_window) | |
259 | // then do_same_width = TRUE; | |
260 | for (i = 0; i < m_nFields; i++) | |
261 | { | |
262 | if (m_statusWidths[i] > 0) sum_of_nonvar += m_statusWidths[i]; | |
263 | else num_of_var++; | |
264 | } | |
265 | if (sum_of_nonvar > (width - 20*num_of_var)) do_same_width = TRUE; | |
266 | } | |
267 | else do_same_width = TRUE; | |
268 | if (do_same_width) | |
269 | { | |
270 | for (i = 0; i < m_nFields; i++) | |
271 | { | |
272 | fieldWidth = (int)(width/m_nFields); | |
f51f94ea VZ |
273 | fieldPosition = i*fieldWidth; |
274 | if ( i == n ) | |
275 | break; | |
c801d85f KB |
276 | } |
277 | } | |
278 | else // no_same_width | |
279 | { | |
280 | int *tempwidth = new int[m_nFields]; | |
281 | int temppos = 0; | |
282 | for (i = 0; i < m_nFields; i++) | |
283 | { | |
284 | if (m_statusWidths[i] > 0) tempwidth[i] = m_statusWidths[i]; | |
285 | else tempwidth[i] = (width - sum_of_nonvar) / num_of_var; | |
286 | } | |
287 | for (i = 0; i < m_nFields; i++) | |
288 | { | |
f51f94ea VZ |
289 | fieldWidth = tempwidth[i]; |
290 | fieldPosition = temppos; | |
c801d85f | 291 | |
f51f94ea | 292 | temppos += tempwidth[i]; |
c801d85f | 293 | |
f51f94ea VZ |
294 | if ( i == n ) |
295 | break; | |
c801d85f KB |
296 | } |
297 | delete [] tempwidth; | |
298 | } | |
299 | ||
300 | rect.x = fieldPosition + wxTHICK_LINE_BORDER; | |
f51f94ea | 301 | rect.y = wxTHICK_LINE_BORDER; |
c801d85f | 302 | |
f51f94ea VZ |
303 | rect.width = fieldWidth - 2 * wxTHICK_LINE_BORDER ; |
304 | rect.height = height - 2 * wxTHICK_LINE_BORDER ; | |
c801d85f | 305 | |
f51f94ea | 306 | return TRUE; |
c801d85f KB |
307 | } |
308 | ||
309 | // Initialize colours | |
ed791986 | 310 | void wxStatusBarGeneric::InitColours() |
c801d85f KB |
311 | { |
312 | // Shadow colours | |
313 | #if defined(__WIN95__) | |
314 | wxColour mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW)); | |
315 | m_mediumShadowPen = wxPen(mediumShadowColour, 1, wxSOLID); | |
316 | ||
317 | wxColour hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT)); | |
318 | m_hilightPen = wxPen(hilightColour, 1, wxSOLID); | |
319 | #else | |
320 | m_mediumShadowPen = wxPen("GREY", 1, wxSOLID); | |
321 | m_hilightPen = wxPen("WHITE", 1, wxSOLID); | |
322 | #endif | |
323 | ||
16c1f7f3 | 324 | m_defaultStatusBarFont = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT); |
c801d85f KB |
325 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); |
326 | } | |
327 | ||
328 | // Responds to colour changes, and passes event on to children. | |
ed791986 | 329 | void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event) |
c801d85f KB |
330 | { |
331 | InitColours(); | |
332 | Refresh(); | |
333 | ||
334 | // Propagate the event to the non-top-level children | |
335 | wxWindow::OnSysColourChanged(event); | |
336 | } | |
337 | ||
ed791986 VZ |
338 | void wxStatusBarGeneric::SetMinHeight(int height) |
339 | { | |
340 | // check that this min height is not less than minimal height for the | |
341 | // current font | |
342 | wxClientDC dc(this); | |
343 | wxCoord y; | |
344 | dc.GetTextExtent( _T("X"), NULL, &y ); | |
345 | ||
346 | if ( height > (11*y)/10 ) | |
347 | { | |
348 | SetSize(-1, -1, -1, height + 2*m_borderY); | |
349 | } | |
350 | } | |
351 | ||
352 | //#endif // Win32 && wxUSE_NATIVE_STATUSBAR |