]> git.saurik.com Git - wxWidgets.git/blob - src/generic/statusbr.cpp
#define wxUSE_STD_STRING to 0 rather than #undef it, patch 1255570 from Jan van Dijk
[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
60 void wxStatusBarGeneric::Init()
61 {
62 m_borderX = wxTHICK_LINE_BORDER;
63 m_borderY = wxTHICK_LINE_BORDER;
64 }
65
66 wxStatusBarGeneric::~wxStatusBarGeneric()
67 {
68 }
69
70 bool wxStatusBarGeneric::Create(wxWindow *parent,
71 wxWindowID id,
72 long style,
73 const wxString& name)
74 {
75 if ( !wxWindow::Create(parent, id,
76 wxDefaultPosition, wxDefaultSize,
77 style | wxTAB_TRAVERSAL, name) )
78 return false;
79
80 // The status bar should have a themed background
81 SetThemeEnabled( true );
82
83 InitColours();
84
85 #ifdef __WXPM__
86 SetFont(*wxSMALL_FONT);
87 #endif
88
89 // Set the height according to the font and the border size
90 wxClientDC dc(this);
91 dc.SetFont(GetFont());
92
93 wxCoord y;
94 dc.GetTextExtent(_T("X"), NULL, &y );
95
96 int height = (int)( (11*y)/10 + 2*GetBorderY());
97
98 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height);
99
100 SetFieldsCount(1);
101
102 return true;
103 }
104
105
106 wxSize wxStatusBarGeneric::DoGetBestSize() const
107 {
108 int width, height;
109
110 // best width is the width of the parent
111 GetParent()->GetClientSize(&width, NULL);
112
113 // best height is as calculated above in Create
114 wxClientDC dc((wxWindow*)this);
115 dc.SetFont(GetFont());
116 wxCoord y;
117 dc.GetTextExtent(_T("X"), NULL, &y );
118 height = (int)( (11*y)/10 + 2*GetBorderY());
119
120 return wxSize(width, height);
121 }
122
123 void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths)
124 {
125 wxASSERT_MSG( number >= 0, _T("negative number of fields in wxStatusBar?") );
126
127 int i;
128 for(i = m_nFields; i < number; ++i)
129 m_statusStrings.Add( wxEmptyString );
130
131 for (i = m_nFields - 1; i >= number; --i)
132 m_statusStrings.RemoveAt(i);
133
134 // forget the old cached pixel widths
135 m_widthsAbs.Empty();
136
137 wxStatusBarBase::SetFieldsCount(number, widths);
138
139 wxASSERT_MSG( m_nFields == (int)m_statusStrings.GetCount(),
140 _T("This really should never happen, can we do away with m_nFields here?") );
141 }
142
143 void wxStatusBarGeneric::SetStatusText(const wxString& text, int number)
144 {
145 wxCHECK_RET( (number >= 0) && (number < m_nFields),
146 _T("invalid status bar field index") );
147
148 wxString oldText = m_statusStrings[number];
149 if (oldText != text)
150 {
151 m_statusStrings[number] = text;
152
153 wxRect rect;
154 GetFieldRect(number, rect);
155
156 Refresh( true, &rect );
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, y;
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(__WIN95__) || 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 = wxPen(_T("WHITE"), 1, wxSOLID);
367
368 wxColour vColour;
369
370 vColour.Set(wxString(_T("LIGHT GREY")));
371 SetBackgroundColour(vColour);
372 vColour.Set(wxString(_T("BLACK")));
373 SetForegroundColour(vColour);
374 #else
375 m_mediumShadowPen = wxPen("GREY", 1, wxSOLID);
376 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
377 #endif
378 }
379
380 // Responds to colour changes, and passes event on to children.
381 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event)
382 {
383 InitColours();
384
385 // Propagate the event to the non-top-level children
386 wxWindow::OnSysColourChanged(event);
387 }
388
389 void wxStatusBarGeneric::SetMinHeight(int height)
390 {
391 // check that this min height is not less than minimal height for the
392 // current font
393 wxClientDC dc(this);
394 wxCoord y;
395 dc.GetTextExtent( wxT("X"), NULL, &y );
396
397 if ( height > (11*y)/10 )
398 {
399 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height + 2*m_borderY);
400 }
401 }
402
403 void wxStatusBarGeneric::OnLeftDown(wxMouseEvent& event)
404 {
405 #ifdef __WXGTK20__
406 int width, height;
407 GetClientSize(&width, &height);
408
409 if (HasFlag( wxST_SIZEGRIP ) && (event.GetX() > width-height))
410 {
411 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
412
413 if (!GTK_IS_WINDOW (ancestor))
414 return;
415
416 GdkWindow *source = GTK_PIZZA(m_wxwindow)->bin_window;
417
418 int org_x = 0;
419 int org_y = 0;
420 gdk_window_get_origin( source, &org_x, &org_y );
421
422 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
423 GDK_WINDOW_EDGE_SOUTH_EAST,
424 1,
425 org_x + event.GetX(),
426 org_y + event.GetY(),
427 0);
428 }
429 else
430 {
431 event.Skip( true );
432 }
433 #else
434 event.Skip( true );
435 #endif
436 }
437
438 void wxStatusBarGeneric::OnRightDown(wxMouseEvent& event)
439 {
440 #ifdef __WXGTK20__
441 int width, height;
442 GetClientSize(&width, &height);
443
444 if (HasFlag( wxST_SIZEGRIP ) && (event.GetX() > width-height))
445 {
446 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
447
448 if (!GTK_IS_WINDOW (ancestor))
449 return;
450
451 GdkWindow *source = GTK_PIZZA(m_wxwindow)->bin_window;
452
453 int org_x = 0;
454 int org_y = 0;
455 gdk_window_get_origin( source, &org_x, &org_y );
456
457 gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
458 2,
459 org_x + event.GetX(),
460 org_y + event.GetY(),
461 0);
462 }
463 else
464 {
465 event.Skip( true );
466 }
467 #else
468 event.Skip( true );
469 #endif
470 }
471
472 #endif // wxUSE_STATUSBAR