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