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