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