use Ellipsize() in wxStatusBarGeneric so that the text is correctly ellipsized when...
[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 #include "wx/toplevel.h"
27 #endif
28
29 #ifdef __WXGTK20__
30 #include <gtk/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 // Default status border dimensions
45 #define wxTHICK_LINE_BORDER 2
46
47 // Margin between the field text and the field rect
48 #define wxFIELD_TEXT_MARGIN 2
49
50
51 // ----------------------------------------------------------------------------
52 // wxStatusBarGeneric
53 // ----------------------------------------------------------------------------
54
55 BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow)
56 EVT_PAINT(wxStatusBarGeneric::OnPaint)
57 EVT_LEFT_DOWN(wxStatusBarGeneric::OnLeftDown)
58 EVT_RIGHT_DOWN(wxStatusBarGeneric::OnRightDown)
59 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged)
60 END_EVENT_TABLE()
61
62 void wxStatusBarGeneric::Init()
63 {
64 m_borderX = wxTHICK_LINE_BORDER;
65 m_borderY = wxTHICK_LINE_BORDER;
66 }
67
68 wxStatusBarGeneric::~wxStatusBarGeneric()
69 {
70 }
71
72 bool wxStatusBarGeneric::Create(wxWindow *parent,
73 wxWindowID id,
74 long style,
75 const wxString& name)
76 {
77 style |= wxTAB_TRAVERSAL | wxFULL_REPAINT_ON_RESIZE;
78 if ( !wxWindow::Create(parent, id,
79 wxDefaultPosition, wxDefaultSize,
80 style, name) )
81 return false;
82
83 // The status bar should have a themed background
84 SetThemeEnabled( true );
85
86 InitColours();
87
88 #ifdef __WXPM__
89 SetFont(*wxSMALL_FONT);
90 #endif
91
92 wxCoord y;
93 {
94 // Set the height according to the font and the border size
95 wxClientDC dc(this);
96 dc.SetFont(GetFont());
97
98 dc.GetTextExtent(_T("X"), NULL, &y );
99 }
100 int height = (int)( (11*y)/10 + 2*GetBorderY());
101
102 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height);
103
104 SetFieldsCount(1);
105
106 return true;
107 }
108
109 wxSize wxStatusBarGeneric::DoGetBestSize() const
110 {
111 int width, height;
112 wxCoord y;
113
114 // best width is the width of the parent
115 GetParent()->GetClientSize(&width, NULL);
116
117 // best height is as calculated above in Create()
118 wxClientDC dc((wxWindow*)this);
119 dc.SetFont(GetFont());
120 dc.GetTextExtent(_T("X"), NULL, &y);
121 height = (int)((11*y)/10 + 2*GetBorderY());
122
123 return wxSize(width, height);
124 }
125
126 void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths)
127 {
128 wxASSERT_MSG( number >= 0, _T("negative number of fields in wxStatusBar?") );
129
130 // enlarge the m_statusStrings array if needed:
131 for (size_t i = m_panes.GetCount(); i < (size_t)number; ++i)
132 m_statusStrings.Add( wxEmptyString );
133
134 // shrink the m_statusStrings array if needed:
135 for (int j = (int)m_panes.GetCount() - 1; j >= number; --j)
136 m_statusStrings.RemoveAt(j);
137
138 // forget the old cached pixel widths
139 m_widthsAbs.Empty();
140
141 wxStatusBarBase::SetFieldsCount(number, widths);
142
143 wxASSERT_MSG( m_panes.GetCount() == m_statusStrings.GetCount(),
144 _T("This really should never happen, can we do away with m_panes.GetCount() here?") );
145 }
146
147 void wxStatusBarGeneric::SetStatusText(const wxString& text, int number)
148 {
149 wxCHECK_RET( (number >= 0) && ((size_t)number < m_panes.GetCount()),
150 _T("invalid status bar field index") );
151
152 wxString oldText = m_statusStrings[number];
153 if (oldText != text)
154 {
155 m_statusStrings[number] = text;
156
157 wxRect rect;
158 GetFieldRect(number, rect);
159
160 Refresh(true, &rect);
161
162 // it's common to show some text in the status bar before starting a
163 // relatively lengthy operation, ensure that the text is shown to the
164 // user immediately and not after the lengthy operation end
165 Update();
166 }
167 }
168
169 wxString wxStatusBarGeneric::GetStatusText(int n) const
170 {
171 wxCHECK_MSG( (n >= 0) && ((size_t)n < m_panes.GetCount()), wxEmptyString,
172 _T("invalid status bar field index") );
173
174 return m_statusStrings[n];
175 }
176
177 void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[])
178 {
179 // only set status widths when n == number of statuswindows
180 wxCHECK_RET( (size_t)n == m_panes.GetCount(), _T("status bar field count mismatch") );
181
182 // forget the old cached pixel widths
183 m_widthsAbs.Empty();
184
185 wxStatusBarBase::SetStatusWidths(n, widths_field);
186 }
187
188 bool wxStatusBarGeneric::ShowsSizeGrip() const
189 {
190 if ( !HasFlag(wxST_SIZEGRIP) )
191 return false;
192
193 wxTopLevelWindow * const
194 tlw = wxDynamicCast(wxGetTopLevelParent(GetParent()), wxTopLevelWindow);
195 return tlw && !tlw->IsMaximized() && tlw->HasFlag(wxRESIZE_BORDER);
196 }
197
198 void wxStatusBarGeneric::DrawFieldText(wxDC& dc, int i)
199 {
200 wxRect rect;
201 GetFieldRect(i, rect);
202
203 if (rect.GetWidth() <= 0)
204 return; // happens when the status bar is shrinked in a very small area!
205
206 if (ShowsSizeGrip())
207 {
208 // don't write text over the size grip:
209 // NOTE: overloading DoGetClientSize() and GetClientAreaOrigin() wouldn't
210 // work because the adjustment needs to be done only when drawing
211 // the field text and not also when drawing the background, the
212 // size grip itself, etc
213 if (GetLayoutDirection() == wxLayout_RightToLeft && i == 0)
214 {
215 const wxRect& rc = GetSizeGripRect();
216
217 rect.x += rc.GetRight();
218 rect.width -= rc.GetRight();
219 }
220 else if (GetLayoutDirection() != wxLayout_RightToLeft && i == m_panes.GetCount()-1)
221 {
222 const wxRect& rc = GetSizeGripRect();
223
224 rect.width = rc.x - rect.x;
225 }
226 }
227
228 // eventually ellipsize the text so that it fits the field width
229 wxString text(GetStatusText(i));
230
231 text = wxControl::Ellipsize(
232 text, dc,
233 GetLayoutDirection() == wxLayout_RightToLeft ? wxELLIPSIZE_START : wxELLIPSIZE_END,
234 rect.GetWidth() - 2*wxFIELD_TEXT_MARGIN,
235 wxELLIPSIZE_EXPAND_TAB);
236 // Ellipsize() will do something only if necessary
237
238 // center the text in its field
239 wxSize sz = dc.GetTextExtent(text);
240 int xpos = rect.x + wxFIELD_TEXT_MARGIN;
241 int ypos = (int) (((rect.height - sz.GetHeight()) / 2) + rect.y + 0.5);
242
243 #if defined( __WXGTK__ ) || defined(__WXMAC__)
244 xpos++;
245 ypos++;
246 #endif
247
248 // draw the text
249 dc.DrawText(text, xpos, ypos);
250 }
251
252 void wxStatusBarGeneric::DrawField(wxDC& dc, int i)
253 {
254 wxRect rect;
255 GetFieldRect(i, rect);
256
257 int style = m_panes[i].nStyle;
258 if (style != wxSB_FLAT)
259 {
260 // Draw border
261 // For wxSB_NORMAL:
262 // Have grey background, plus 3-d border -
263 // One black rectangle.
264 // Inside this, left and top sides - dark grey. Bottom and right -
265 // white.
266 // Reverse it for wxSB_RAISED
267
268 dc.SetPen((style == wxSB_RAISED) ? m_mediumShadowPen : m_hilightPen);
269
270 #ifndef __WXPM__
271
272 // Right and bottom lines
273 dc.DrawLine(rect.x + rect.width, rect.y,
274 rect.x + rect.width, rect.y + rect.height);
275 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
276 rect.x, rect.y + rect.height);
277
278 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
279
280 // Left and top lines
281 dc.DrawLine(rect.x, rect.y + rect.height,
282 rect.x, rect.y);
283 dc.DrawLine(rect.x, rect.y,
284 rect.x + rect.width, rect.y);
285 #else
286
287 dc.DrawLine(rect.x + rect.width, rect.height + 2,
288 rect.x, rect.height + 2);
289 dc.DrawLine(rect.x + rect.width, rect.y,
290 rect.x + rect.width, rect.y + rect.height);
291
292 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
293 dc.DrawLine(rect.x, rect.y,
294 rect.x + rect.width, rect.y);
295 dc.DrawLine(rect.x, rect.y + rect.height,
296 rect.x, rect.y);
297
298 #endif
299 }
300
301 DrawFieldText(dc, i);
302 }
303
304 // Get the position and size of the field's internal bounding rectangle
305 bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
306 {
307 wxCHECK_MSG( (n >= 0) && ((size_t)n < m_panes.GetCount()), false,
308 _T("invalid status bar field index") );
309
310 // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
311 int width, height;
312 #ifdef __WXPM__
313 GetSize(&width, &height);
314 #else
315 GetClientSize(&width, &height);
316 #endif
317
318 // we cache m_widthsAbs between calls and recompute it if client
319 // width has changed (or when it is initially empty)
320 if ( m_widthsAbs.IsEmpty() || m_lastClientWidth != width )
321 {
322 // FIXME: why don't we use an OnSize(wxSizeEvent&) event handler to
323 // update the cache? (FM)
324
325 wxConstCast(this, wxStatusBarGeneric)->m_widthsAbs = CalculateAbsWidths(width);
326
327 // remember last width for which we have recomputed the widths in pixels
328 wxConstCast(this, wxStatusBarGeneric)->m_lastClientWidth = width;
329 }
330
331 rect.x = 0;
332 for ( int i = 0; i < n; i++ )
333 rect.x += m_widthsAbs[i];
334
335 rect.x += m_borderX;
336 rect.y = m_borderY;
337
338 rect.width = m_widthsAbs[n] - 2*m_borderX;
339 rect.height = height - 2*m_borderY;
340
341 return true;
342 }
343
344 // Initialize colours
345 void wxStatusBarGeneric::InitColours()
346 {
347 #if defined(__WXPM__)
348 m_mediumShadowPen = wxPen(wxColour(127, 127, 127), 1, wxSOLID);
349 m_hilightPen = *wxWHITE_PEN;
350
351 SetBackgroundColour(*wxLIGHT_GREY);
352 SetForegroundColour(*wxBLACK);
353 #else // !__WXPM__
354 m_mediumShadowPen = wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
355 m_hilightPen = wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
356 #endif // __WXPM__/!__WXPM__
357 }
358
359 void wxStatusBarGeneric::SetMinHeight(int height)
360 {
361 // check that this min height is not less than minimal height for the
362 // current font
363 wxClientDC dc(this);
364 wxCoord y;
365 dc.GetTextExtent( wxT("X"), NULL, &y );
366
367 if ( height > (11*y)/10 )
368 {
369 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height + 2*m_borderY);
370 }
371 }
372
373 wxRect wxStatusBarGeneric::GetSizeGripRect() const
374 {
375 int width, height;
376 wxWindow::DoGetClientSize(&width, &height);
377
378 if (GetLayoutDirection() == wxLayout_RightToLeft)
379 return wxRect(2, 2, height-2, height-4);
380 else
381 return wxRect(width-height-2, 2, height-2, height-4);
382 }
383
384 // ----------------------------------------------------------------------------
385 // wxStatusBarGeneric - event handlers
386 // ----------------------------------------------------------------------------
387
388 void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
389 {
390 wxPaintDC dc(this);
391
392 #ifdef __WXGTK20__
393 // Draw grip first
394 if ( ShowsSizeGrip() )
395 {
396 const wxRect& rc = GetSizeGripRect();
397 GdkWindowEdge edge =
398 GetLayoutDirection() == wxLayout_RightToLeft ? GDK_WINDOW_EDGE_SOUTH_WEST :
399 GDK_WINDOW_EDGE_SOUTH_EAST;
400 gtk_paint_resize_grip( m_widget->style,
401 GTKGetDrawingWindow(),
402 (GtkStateType) GTK_WIDGET_STATE (m_widget),
403 NULL,
404 m_widget,
405 "statusbar",
406 edge,
407 rc.x, rc.y, rc.width, rc.height );
408 }
409 #endif // __WXGTK20__
410
411 if (GetFont().IsOk())
412 dc.SetFont(GetFont());
413
414 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
415
416 for (size_t i = 0; i < m_panes.GetCount(); i ++)
417 DrawField(dc, i);
418 }
419
420 // Responds to colour changes, and passes event on to children.
421 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event)
422 {
423 InitColours();
424
425 // Propagate the event to the non-top-level children
426 wxWindow::OnSysColourChanged(event);
427 }
428
429 void wxStatusBarGeneric::OnLeftDown(wxMouseEvent& event)
430 {
431 #ifdef __WXGTK20__
432 int width, height;
433 GetClientSize(&width, &height);
434
435 if ( ShowsSizeGrip() && (event.GetX() > width-height) )
436 {
437 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
438
439 if (!GTK_IS_WINDOW (ancestor))
440 return;
441
442 GdkWindow *source = GTKGetDrawingWindow();
443
444 int org_x = 0;
445 int org_y = 0;
446 gdk_window_get_origin( source, &org_x, &org_y );
447
448 if (GetLayoutDirection() == wxLayout_RightToLeft)
449 {
450 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
451 GDK_WINDOW_EDGE_SOUTH_WEST,
452 1,
453 org_x - event.GetX() + GetSize().x ,
454 org_y + event.GetY(),
455 0);
456 }
457 else
458 {
459 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
460 GDK_WINDOW_EDGE_SOUTH_EAST,
461 1,
462 org_x + event.GetX(),
463 org_y + event.GetY(),
464 0);
465 }
466 }
467 else
468 {
469 event.Skip( true );
470 }
471 #else
472 event.Skip( true );
473 #endif
474 }
475
476 void wxStatusBarGeneric::OnRightDown(wxMouseEvent& event)
477 {
478 #ifdef __WXGTK20__
479 int width, height;
480 GetClientSize(&width, &height);
481
482 if ( ShowsSizeGrip() && (event.GetX() > width-height) )
483 {
484 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
485
486 if (!GTK_IS_WINDOW (ancestor))
487 return;
488
489 GdkWindow *source = GTKGetDrawingWindow();
490
491 int org_x = 0;
492 int org_y = 0;
493 gdk_window_get_origin( source, &org_x, &org_y );
494
495 gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
496 2,
497 org_x + event.GetX(),
498 org_y + event.GetY(),
499 0);
500 }
501 else
502 {
503 event.Skip( true );
504 }
505 #else
506 event.Skip( true );
507 #endif
508 }
509
510 #endif // wxUSE_STATUSBAR