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