]>
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 | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #if wxUSE_STATUSBAR | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/setup.h" | |
23 | #include "wx/frame.h" | |
24 | #include "wx/settings.h" | |
25 | #include "wx/dcclient.h" | |
26 | #endif | |
27 | ||
28 | #ifdef __WXGTK20__ | |
29 | #include "wx/gtk/private.h" | |
30 | #include "wx/gtk/win_gtk.h" | |
31 | #endif | |
32 | ||
33 | #include "wx/statusbr.h" | |
34 | ||
35 | // we only have to do it here when we use wxStatusBarGeneric in addition to the | |
36 | // standard wxStatusBar class, if wxStatusBarGeneric is the same as | |
37 | // wxStatusBar, then the corresponding IMPLEMENT_DYNAMIC_CLASS is already in | |
38 | // common/statbar.cpp | |
39 | #if defined(__WXMAC__) || \ | |
40 | (defined(wxUSE_NATIVE_STATUSBAR) && wxUSE_NATIVE_STATUSBAR) | |
41 | #include "wx/generic/statusbr.h" | |
42 | ||
43 | IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric, wxWindow) | |
44 | #endif // wxUSE_NATIVE_STATUSBAR | |
45 | ||
46 | BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow) | |
47 | EVT_PAINT(wxStatusBarGeneric::OnPaint) | |
48 | EVT_LEFT_DOWN(wxStatusBarGeneric::OnLeftDown) | |
49 | EVT_RIGHT_DOWN(wxStatusBarGeneric::OnRightDown) | |
50 | EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged) | |
51 | END_EVENT_TABLE() | |
52 | ||
53 | // Default status border dimensions | |
54 | #define wxTHICK_LINE_BORDER 2 | |
55 | ||
56 | void wxStatusBarGeneric::Init() | |
57 | { | |
58 | m_borderX = wxTHICK_LINE_BORDER; | |
59 | m_borderY = wxTHICK_LINE_BORDER; | |
60 | } | |
61 | ||
62 | wxStatusBarGeneric::~wxStatusBarGeneric() | |
63 | { | |
64 | } | |
65 | ||
66 | bool wxStatusBarGeneric::Create(wxWindow *parent, | |
67 | wxWindowID id, | |
68 | long style, | |
69 | const wxString& name) | |
70 | { | |
71 | if ( !wxWindow::Create(parent, id, | |
72 | wxDefaultPosition, wxDefaultSize, | |
73 | style | wxTAB_TRAVERSAL, name) ) | |
74 | return false; | |
75 | ||
76 | // The status bar should have a themed background | |
77 | SetThemeEnabled( true ); | |
78 | ||
79 | InitColours(); | |
80 | ||
81 | #ifdef __WXPM__ | |
82 | SetFont(*wxSMALL_FONT); | |
83 | #endif | |
84 | ||
85 | // Set the height according to the font and the border size | |
86 | wxClientDC dc(this); | |
87 | dc.SetFont(GetFont()); | |
88 | ||
89 | wxCoord y; | |
90 | dc.GetTextExtent(_T("X"), NULL, &y ); | |
91 | ||
92 | int height = (int)( (11*y)/10 + 2*GetBorderY()); | |
93 | ||
94 | SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height); | |
95 | ||
96 | SetFieldsCount(1); | |
97 | ||
98 | return true; | |
99 | } | |
100 | ||
101 | ||
102 | wxSize wxStatusBarGeneric::DoGetBestSize() const | |
103 | { | |
104 | int width, height; | |
105 | ||
106 | // best width is the width of the parent | |
107 | GetParent()->GetClientSize(&width, NULL); | |
108 | ||
109 | // best height is as calculated above in Create | |
110 | wxClientDC dc((wxWindow*)this); | |
111 | dc.SetFont(GetFont()); | |
112 | wxCoord y; | |
113 | dc.GetTextExtent(_T("X"), NULL, &y ); | |
114 | height = (int)( (11*y)/10 + 2*GetBorderY()); | |
115 | ||
116 | return wxSize(width, height); | |
117 | } | |
118 | ||
119 | void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths) | |
120 | { | |
121 | wxASSERT_MSG( number >= 0, _T("negative number of fields in wxStatusBar?") ); | |
122 | ||
123 | int i; | |
124 | for(i = m_nFields; i < number; ++i) | |
125 | m_statusStrings.Add( wxEmptyString ); | |
126 | ||
127 | for (i = m_nFields - 1; i >= number; --i) | |
128 | m_statusStrings.RemoveAt(i); | |
129 | ||
130 | // forget the old cached pixel widths | |
131 | m_widthsAbs.Empty(); | |
132 | ||
133 | wxStatusBarBase::SetFieldsCount(number, widths); | |
134 | ||
135 | wxASSERT_MSG( m_nFields == (int)m_statusStrings.GetCount(), | |
136 | _T("This really should never happen, can we do away with m_nFields here?") ); | |
137 | } | |
138 | ||
139 | void wxStatusBarGeneric::SetStatusText(const wxString& text, int number) | |
140 | { | |
141 | wxCHECK_RET( (number >= 0) && (number < m_nFields), | |
142 | _T("invalid status bar field index") ); | |
143 | ||
144 | wxString oldText = m_statusStrings[number]; | |
145 | if (oldText != text) | |
146 | { | |
147 | m_statusStrings[number] = text; | |
148 | ||
149 | wxRect rect; | |
150 | GetFieldRect(number, rect); | |
151 | ||
152 | Refresh( true, &rect ); | |
153 | } | |
154 | } | |
155 | ||
156 | wxString wxStatusBarGeneric::GetStatusText(int n) const | |
157 | { | |
158 | wxCHECK_MSG( (n >= 0) && (n < m_nFields), wxEmptyString, | |
159 | _T("invalid status bar field index") ); | |
160 | ||
161 | return m_statusStrings[n]; | |
162 | } | |
163 | ||
164 | void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[]) | |
165 | { | |
166 | // only set status widths, when n == number of statuswindows | |
167 | wxCHECK_RET( n == m_nFields, _T("status bar field count mismatch") ); | |
168 | ||
169 | // delete the old widths in any case - this function may be used to reset | |
170 | // the widths to the default (all equal) | |
171 | // MBN: this is incompatible with at least wxMSW and wxMAC and not | |
172 | // documented, but let's keep it for now | |
173 | ReinitWidths(); | |
174 | ||
175 | // forget the old cached pixel widths | |
176 | m_widthsAbs.Empty(); | |
177 | ||
178 | if ( !widths_field ) | |
179 | { | |
180 | // not an error, see the comment above | |
181 | Refresh(); | |
182 | return; | |
183 | } | |
184 | ||
185 | wxStatusBarBase::SetStatusWidths(n, widths_field); | |
186 | } | |
187 | ||
188 | void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) ) | |
189 | { | |
190 | wxPaintDC dc(this); | |
191 | ||
192 | #ifdef __WXGTK20__ | |
193 | // Draw grip first | |
194 | if (HasFlag( wxST_SIZEGRIP )) | |
195 | { | |
196 | int width, height; | |
197 | GetClientSize(&width, &height); | |
198 | ||
199 | gtk_paint_resize_grip( m_widget->style, | |
200 | GTK_PIZZA(m_wxwindow)->bin_window, | |
201 | (GtkStateType) GTK_WIDGET_STATE (m_widget), | |
202 | NULL, | |
203 | m_widget, | |
204 | "statusbar", | |
205 | GDK_WINDOW_EDGE_SOUTH_EAST, | |
206 | width-height-2, 1, height-2, height-3 ); | |
207 | ||
208 | } | |
209 | #endif | |
210 | ||
211 | if (GetFont().Ok()) | |
212 | dc.SetFont(GetFont()); | |
213 | ||
214 | dc.SetBackgroundMode(wxTRANSPARENT); | |
215 | ||
216 | #ifdef __WXPM__ | |
217 | wxColour vColor; | |
218 | ||
219 | vColor = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR); | |
220 | ::WinFillRect(dc.m_hPS, &dc.m_vRclPaint, vColor.GetPixel()); | |
221 | #endif | |
222 | ||
223 | for (int i = 0; i < m_nFields; i ++) | |
224 | DrawField(dc, i); | |
225 | } | |
226 | ||
227 | void wxStatusBarGeneric::DrawFieldText(wxDC& dc, int i) | |
228 | { | |
229 | int leftMargin = 2; | |
230 | ||
231 | wxRect rect; | |
232 | GetFieldRect(i, rect); | |
233 | ||
234 | wxString text(GetStatusText(i)); | |
235 | ||
236 | long x, y; | |
237 | ||
238 | dc.GetTextExtent(text, &x, &y); | |
239 | ||
240 | int xpos = rect.x + leftMargin; | |
241 | int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ; | |
242 | ||
243 | #if defined( __WXGTK__ ) || defined(__WXMAC__) | |
244 | xpos++; | |
245 | ypos++; | |
246 | #endif | |
247 | ||
248 | dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height); | |
249 | ||
250 | dc.DrawText(text, xpos, ypos); | |
251 | ||
252 | dc.DestroyClippingRegion(); | |
253 | } | |
254 | ||
255 | void wxStatusBarGeneric::DrawField(wxDC& dc, int i) | |
256 | { | |
257 | wxRect rect; | |
258 | GetFieldRect(i, rect); | |
259 | ||
260 | int style = wxSB_NORMAL; | |
261 | if (m_statusStyles) | |
262 | style = m_statusStyles[i]; | |
263 | ||
264 | if (style != wxSB_FLAT) | |
265 | { | |
266 | // Draw border | |
267 | // For wxSB_NORMAL: | |
268 | // Have grey background, plus 3-d border - | |
269 | // One black rectangle. | |
270 | // Inside this, left and top sides - dark grey. Bottom and right - | |
271 | // white. | |
272 | // Reverse it for wxSB_RAISED | |
273 | ||
274 | dc.SetPen((style == wxSB_RAISED) ? m_mediumShadowPen : m_hilightPen); | |
275 | ||
276 | #ifndef __WXPM__ | |
277 | ||
278 | // Right and bottom lines | |
279 | dc.DrawLine(rect.x + rect.width, rect.y, | |
280 | rect.x + rect.width, rect.y + rect.height); | |
281 | dc.DrawLine(rect.x + rect.width, rect.y + rect.height, | |
282 | rect.x, rect.y + rect.height); | |
283 | ||
284 | dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen); | |
285 | ||
286 | // Left and top lines | |
287 | dc.DrawLine(rect.x, rect.y + rect.height, | |
288 | rect.x, rect.y); | |
289 | dc.DrawLine(rect.x, rect.y, | |
290 | rect.x + rect.width, rect.y); | |
291 | #else | |
292 | ||
293 | dc.DrawLine(rect.x + rect.width, rect.height + 2, | |
294 | rect.x, rect.height + 2); | |
295 | dc.DrawLine(rect.x + rect.width, rect.y, | |
296 | rect.x + rect.width, rect.y + rect.height); | |
297 | ||
298 | dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen); | |
299 | dc.DrawLine(rect.x, rect.y, | |
300 | rect.x + rect.width, rect.y); | |
301 | dc.DrawLine(rect.x, rect.y + rect.height, | |
302 | rect.x, rect.y); | |
303 | ||
304 | #endif | |
305 | } | |
306 | ||
307 | DrawFieldText(dc, i); | |
308 | } | |
309 | ||
310 | // Get the position and size of the field's internal bounding rectangle | |
311 | bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const | |
312 | { | |
313 | wxCHECK_MSG( (n >= 0) && (n < m_nFields), false, | |
314 | _T("invalid status bar field index") ); | |
315 | ||
316 | // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ) | |
317 | int width, height; | |
318 | #ifdef __WXPM__ | |
319 | GetSize(&width, &height); | |
320 | #else | |
321 | GetClientSize(&width, &height); | |
322 | #endif | |
323 | ||
324 | // we cache m_widthsAbs between calls and recompute it if client | |
325 | // width has changed (or when it is initially empty) | |
326 | if ( m_widthsAbs.IsEmpty() || (m_lastClientWidth != width) ) | |
327 | { | |
328 | wxConstCast(this, wxStatusBarGeneric)-> | |
329 | m_widthsAbs = CalculateAbsWidths(width); | |
330 | // remember last width for which we have recomputed the widths in pixels | |
331 | wxConstCast(this, wxStatusBarGeneric)-> | |
332 | m_lastClientWidth = width; | |
333 | } | |
334 | ||
335 | rect.x = 0; | |
336 | for ( int i = 0; i < n; i++ ) | |
337 | { | |
338 | rect.x += m_widthsAbs[i]; | |
339 | } | |
340 | ||
341 | rect.x += m_borderX; | |
342 | rect.y = m_borderY; | |
343 | ||
344 | rect.width = m_widthsAbs[n] - 2*m_borderX; | |
345 | rect.height = height - 2*m_borderY; | |
346 | ||
347 | return true; | |
348 | } | |
349 | ||
350 | // Initialize colours | |
351 | void wxStatusBarGeneric::InitColours() | |
352 | { | |
353 | // Shadow colours | |
354 | #if defined(__WIN95__) || defined(__WXMAC__) | |
355 | wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW)); | |
356 | m_mediumShadowPen = wxPen(mediumShadowColour, 1, wxSOLID); | |
357 | ||
358 | wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT)); | |
359 | m_hilightPen = wxPen(hilightColour, 1, wxSOLID); | |
360 | #elif defined(__WXPM__) | |
361 | m_mediumShadowPen = wxPen(wxColour(127, 127, 127), 1, wxSOLID); | |
362 | m_hilightPen = wxPen(_T("WHITE"), 1, wxSOLID); | |
363 | ||
364 | wxColour vColour; | |
365 | ||
366 | vColour.Set(wxString(_T("LIGHT GREY"))); | |
367 | SetBackgroundColour(vColour); | |
368 | vColour.Set(wxString(_T("BLACK"))); | |
369 | SetForegroundColour(vColour); | |
370 | #else | |
371 | m_mediumShadowPen = wxPen("GREY", 1, wxSOLID); | |
372 | m_hilightPen = wxPen("WHITE", 1, wxSOLID); | |
373 | #endif | |
374 | } | |
375 | ||
376 | // Responds to colour changes, and passes event on to children. | |
377 | void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event) | |
378 | { | |
379 | InitColours(); | |
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( wxT("X"), NULL, &y ); | |
392 | ||
393 | if ( height > (11*y)/10 ) | |
394 | { | |
395 | SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height + 2*m_borderY); | |
396 | } | |
397 | } | |
398 | ||
399 | void wxStatusBarGeneric::OnLeftDown(wxMouseEvent& event) | |
400 | { | |
401 | #ifdef __WXGTK20__ | |
402 | int width, height; | |
403 | GetClientSize(&width, &height); | |
404 | ||
405 | if (HasFlag( wxST_SIZEGRIP ) && (event.GetX() > width-height)) | |
406 | { | |
407 | GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget ); | |
408 | ||
409 | if (!GTK_IS_WINDOW (ancestor)) | |
410 | return; | |
411 | ||
412 | GdkWindow *source = GTK_PIZZA(m_wxwindow)->bin_window; | |
413 | ||
414 | int org_x = 0; | |
415 | int org_y = 0; | |
416 | gdk_window_get_origin( source, &org_x, &org_y ); | |
417 | ||
418 | gtk_window_begin_resize_drag (GTK_WINDOW (ancestor), | |
419 | GDK_WINDOW_EDGE_SOUTH_EAST, | |
420 | 1, | |
421 | org_x + event.GetX(), | |
422 | org_y + event.GetY(), | |
423 | 0); | |
424 | } | |
425 | else | |
426 | { | |
427 | event.Skip( true ); | |
428 | } | |
429 | #else | |
430 | event.Skip( true ); | |
431 | #endif | |
432 | } | |
433 | ||
434 | void wxStatusBarGeneric::OnRightDown(wxMouseEvent& event) | |
435 | { | |
436 | #ifdef __WXGTK20__ | |
437 | int width, height; | |
438 | GetClientSize(&width, &height); | |
439 | ||
440 | if (HasFlag( wxST_SIZEGRIP ) && (event.GetX() > width-height)) | |
441 | { | |
442 | GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget ); | |
443 | ||
444 | if (!GTK_IS_WINDOW (ancestor)) | |
445 | return; | |
446 | ||
447 | GdkWindow *source = GTK_PIZZA(m_wxwindow)->bin_window; | |
448 | ||
449 | int org_x = 0; | |
450 | int org_y = 0; | |
451 | gdk_window_get_origin( source, &org_x, &org_y ); | |
452 | ||
453 | gtk_window_begin_move_drag (GTK_WINDOW (ancestor), | |
454 | 2, | |
455 | org_x + event.GetX(), | |
456 | org_y + event.GetY(), | |
457 | 0); | |
458 | } | |
459 | else | |
460 | { | |
461 | event.Skip( true ); | |
462 | } | |
463 | #else | |
464 | event.Skip( true ); | |
465 | #endif | |
466 | } | |
467 | ||
468 | #endif // wxUSE_STATUSBAR |