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