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