Update the field widths on demand in wxStatusBarGeneric.
[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: Francesco Montorsi
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 #include "wx/control.h"
28 #endif
29
30 #ifdef __WXGTK20__
31 #include <gtk/gtk.h>
32 #include "wx/gtk/private.h"
33 #endif
34
35 // we only have to do it here when we use wxStatusBarGeneric in addition to the
36 // standard wxStatusBar class, if wxStatusBarGeneric is the same as
37 // wxStatusBar, then the corresponding IMPLEMENT_DYNAMIC_CLASS is already in
38 // common/statbar.cpp
39 #if defined(__WXMAC__) || \
40 (defined(wxUSE_NATIVE_STATUSBAR) && wxUSE_NATIVE_STATUSBAR)
41 #include "wx/generic/statusbr.h"
42
43 IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric, wxWindow)
44 #endif // wxUSE_NATIVE_STATUSBAR
45
46 // Default status border dimensions
47 #define wxTHICK_LINE_BORDER 2
48
49 // Margin between the field text and the field rect
50 #define wxFIELD_TEXT_MARGIN 2
51
52 // ----------------------------------------------------------------------------
53 // GTK+ signal handler
54 // ----------------------------------------------------------------------------
55
56 #if defined( __WXGTK20__ )
57 #if GTK_CHECK_VERSION(2,12,0)
58 extern "C" {
59 static
60 gboolean statusbar_query_tooltip(GtkWidget* WXUNUSED(widget),
61 gint x,
62 gint y,
63 gboolean WXUNUSED(keyboard_mode),
64 GtkTooltip *tooltip,
65 wxStatusBar* statbar)
66 {
67 int n = statbar->GetFieldFromPoint(wxPoint(x,y));
68 if (n == wxNOT_FOUND)
69 return FALSE;
70
71 // should we show the tooltip for the n-th pane of the statusbar?
72 if (!statbar->GetField(n).IsEllipsized())
73 return FALSE; // no, it's not useful
74
75 const wxString& str = statbar->GetStatusText(n);
76 if (str.empty())
77 return FALSE;
78
79 gtk_tooltip_set_text(tooltip, wxGTK_CONV_SYS(str));
80 return TRUE;
81 }
82 }
83 #endif
84 #endif
85
86 // ----------------------------------------------------------------------------
87 // wxStatusBarGeneric
88 // ----------------------------------------------------------------------------
89
90 BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow)
91 EVT_PAINT(wxStatusBarGeneric::OnPaint)
92 EVT_SIZE(wxStatusBarGeneric::OnSize)
93 EVT_LEFT_DOWN(wxStatusBarGeneric::OnLeftDown)
94 EVT_RIGHT_DOWN(wxStatusBarGeneric::OnRightDown)
95 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged)
96 END_EVENT_TABLE()
97
98 void wxStatusBarGeneric::Init()
99 {
100 m_borderX = wxTHICK_LINE_BORDER;
101 m_borderY = wxTHICK_LINE_BORDER;
102 }
103
104 wxStatusBarGeneric::~wxStatusBarGeneric()
105 {
106 }
107
108 bool wxStatusBarGeneric::Create(wxWindow *parent,
109 wxWindowID id,
110 long style,
111 const wxString& name)
112 {
113 style |= wxTAB_TRAVERSAL | wxFULL_REPAINT_ON_RESIZE;
114 if ( !wxWindow::Create(parent, id,
115 wxDefaultPosition, wxDefaultSize,
116 style, name) )
117 return false;
118
119 // The status bar should have a themed background
120 SetThemeEnabled( true );
121
122 InitColours();
123
124 #ifdef __WXPM__
125 SetFont(*wxSMALL_FONT);
126 #endif
127
128 int height = (int)((11*GetCharHeight())/10 + 2*GetBorderY());
129 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height);
130
131 SetFieldsCount(1);
132
133 #if defined( __WXGTK20__ )
134 #if GTK_CHECK_VERSION(2,12,0)
135 if (HasFlag(wxSTB_SHOW_TIPS) && !gtk_check_version(2,12,0))
136 {
137 g_object_set(m_widget, "has-tooltip", TRUE, NULL);
138 g_signal_connect(m_widget, "query-tooltip",
139 G_CALLBACK(statusbar_query_tooltip), this);
140 }
141 #endif
142 #endif
143
144 return true;
145 }
146
147 wxSize wxStatusBarGeneric::DoGetBestSize() const
148 {
149 int width, height;
150
151 // best width is the width of the parent
152 if (GetParent())
153 GetParent()->GetClientSize(&width, NULL);
154 else
155 width = 80; // a dummy value
156
157 // best height is as calculated above in Create()
158 height = (int)((11*GetCharHeight())/10 + 2*GetBorderY());
159
160 return wxSize(width, height);
161 }
162
163 void wxStatusBarGeneric::DoUpdateStatusText(int number)
164 {
165 wxRect rect;
166 GetFieldRect(number, rect);
167
168 Refresh(true, &rect);
169
170 // it's common to show some text in the status bar before starting a
171 // relatively lengthy operation, ensure that the text is shown to the
172 // user immediately and not after the lengthy operation end
173 Update();
174 }
175
176 void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[])
177 {
178 // only set status widths when n == number of statuswindows
179 wxCHECK_RET( (size_t)n == m_panes.GetCount(), wxT("status bar field count mismatch") );
180
181 wxStatusBarBase::SetStatusWidths(n, widths_field);
182
183 // update cache
184 DoUpdateFieldWidths();
185 }
186
187 void wxStatusBarGeneric::DoUpdateFieldWidths()
188 {
189 m_lastClientSize = GetClientSize();
190
191 // recompute the cache of the field widths if the status bar width has changed
192 m_widthsAbs = CalculateAbsWidths(m_lastClientSize.x);
193 }
194
195 bool wxStatusBarGeneric::ShowsSizeGrip() const
196 {
197 if ( !HasFlag(wxSTB_SIZEGRIP) )
198 return false;
199
200 wxTopLevelWindow * const
201 tlw = wxDynamicCast(wxGetTopLevelParent(GetParent()), wxTopLevelWindow);
202 return tlw && !tlw->IsMaximized() && tlw->HasFlag(wxRESIZE_BORDER);
203 }
204
205 void wxStatusBarGeneric::DrawFieldText(wxDC& dc, const wxRect& rect, int i, int textHeight)
206 {
207 wxString text(GetStatusText(i));
208 if (text.empty())
209 return; // optimization
210
211 int xpos = rect.x + wxFIELD_TEXT_MARGIN,
212 maxWidth = rect.width - 2*wxFIELD_TEXT_MARGIN,
213 ypos = (int) (((rect.height - textHeight) / 2) + rect.y + 0.5);
214
215 if (ShowsSizeGrip())
216 {
217 // don't write text over the size grip:
218 // NOTE: overloading DoGetClientSize() and GetClientAreaOrigin() wouldn't
219 // work because the adjustment needs to be done only when drawing
220 // the field text and not also when drawing the background, the
221 // size grip itself, etc
222 if ((GetLayoutDirection() == wxLayout_RightToLeft && i == 0) ||
223 (GetLayoutDirection() != wxLayout_RightToLeft &&
224 i == (int)m_panes.GetCount()-1))
225 {
226 const wxRect& gripRc = GetSizeGripRect();
227
228 // NOTE: we don't need any special treatment wrt to the layout direction
229 // since DrawText() will automatically adjust the origin of the
230 // text accordingly to the layout in use
231
232 maxWidth -= gripRc.width;
233 }
234 }
235
236 // eventually ellipsize the text so that it fits the field width
237
238 wxEllipsizeMode ellmode = (wxEllipsizeMode)-1;
239 if (HasFlag(wxSTB_ELLIPSIZE_START)) ellmode = wxELLIPSIZE_START;
240 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE)) ellmode = wxELLIPSIZE_MIDDLE;
241 else if (HasFlag(wxSTB_ELLIPSIZE_END)) ellmode = wxELLIPSIZE_END;
242
243 if (ellmode == (wxEllipsizeMode)-1)
244 {
245 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
246 // we don't ellipsize the text but just truncate it
247 if (HasFlag(wxSTB_SHOW_TIPS))
248 SetEllipsizedFlag(i, dc.GetTextExtent(text).GetWidth() > maxWidth);
249
250 dc.SetClippingRegion(rect);
251 }
252 else
253 {
254 text = wxControl::Ellipsize(text, dc,
255 ellmode,
256 maxWidth,
257 wxELLIPSIZE_FLAGS_EXPAND_TABS);
258 // Ellipsize() will do something only if necessary
259
260 // update the ellipsization status for this pane; this is used later to
261 // decide whether a tooltip should be shown or not for this pane
262 // (if we have wxSTB_SHOW_TIPS)
263 SetEllipsizedFlag(i, text != GetStatusText(i));
264 }
265
266 #if defined( __WXGTK__ ) || defined(__WXMAC__)
267 xpos++;
268 ypos++;
269 #endif
270
271 // draw the text
272 dc.DrawText(text, xpos, ypos);
273
274 if (ellmode == (wxEllipsizeMode)-1)
275 dc.DestroyClippingRegion();
276 }
277
278 void wxStatusBarGeneric::DrawField(wxDC& dc, int i, int textHeight)
279 {
280 wxRect rect;
281 GetFieldRect(i, rect);
282
283 if (rect.GetWidth() <= 0)
284 return; // happens when the status bar is shrunk in a very small area!
285
286 int style = m_panes[i].GetStyle();
287 if (style != wxSB_FLAT)
288 {
289 // Draw border
290 // For wxSB_NORMAL: paint a grey background, plus 3-d border (one black rectangle)
291 // Inside this, left and top sides (dark grey). Bottom and right (white).
292 // Reverse it for wxSB_RAISED
293
294 dc.SetPen((style == wxSB_RAISED) ? m_mediumShadowPen : m_hilightPen);
295
296 #ifndef __WXPM__
297
298 // Right and bottom lines
299 dc.DrawLine(rect.x + rect.width, rect.y,
300 rect.x + rect.width, rect.y + rect.height);
301 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
302 rect.x, rect.y + rect.height);
303
304 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
305
306 // Left and top lines
307 dc.DrawLine(rect.x, rect.y + rect.height,
308 rect.x, rect.y);
309 dc.DrawLine(rect.x, rect.y,
310 rect.x + rect.width, rect.y);
311 #else
312
313 dc.DrawLine(rect.x + rect.width, rect.height + 2,
314 rect.x, rect.height + 2);
315 dc.DrawLine(rect.x + rect.width, rect.y,
316 rect.x + rect.width, rect.y + rect.height);
317
318 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
319 dc.DrawLine(rect.x, rect.y,
320 rect.x + rect.width, rect.y);
321 dc.DrawLine(rect.x, rect.y + rect.height,
322 rect.x, rect.y);
323 #endif
324 }
325
326 DrawFieldText(dc, rect, i, textHeight);
327 }
328
329 bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
330 {
331 wxCHECK_MSG( (n >= 0) && ((size_t)n < m_panes.GetCount()), false,
332 wxT("invalid status bar field index") );
333
334 // We can be called from the user-defined EVT_SIZE handler in which case
335 // the widths haven't been updated yet and we need to do it now. This is
336 // not very efficient as we keep testing the size but there is no other way
337 // to make the code needing the up-to-date fields sizes in its EVT_SIZE to
338 // work.
339 if ( GetClientSize().x != m_lastClientSize.x )
340 {
341 const_cast<wxStatusBarGeneric*>(this)->DoUpdateFieldWidths();
342 }
343
344 if (m_widthsAbs.IsEmpty())
345 return false;
346
347 rect.x = 0;
348 for ( int i = 0; i < n; i++ )
349 rect.x += m_widthsAbs[i];
350 rect.x += m_borderX;
351
352 rect.y = m_borderY;
353 rect.width = m_widthsAbs[n] - 2*m_borderX;
354 rect.height = m_lastClientSize.y - 2*m_borderY;
355
356 return true;
357 }
358
359 int wxStatusBarGeneric::GetFieldFromPoint(const wxPoint& pt) const
360 {
361 if (m_widthsAbs.IsEmpty())
362 return wxNOT_FOUND;
363
364 // NOTE: we explicitly don't take in count the borders since they are only
365 // useful when rendering the status text, not for hit-test computations
366
367 if (pt.y <= 0 || pt.y >= m_lastClientSize.y)
368 return wxNOT_FOUND;
369
370 int x = 0;
371 for ( size_t i = 0; i < m_panes.GetCount(); i++ )
372 {
373 if (pt.x > x && pt.x < x+m_widthsAbs[i])
374 return i;
375
376 x += m_widthsAbs[i];
377 }
378
379 return wxNOT_FOUND;
380 }
381
382 void wxStatusBarGeneric::InitColours()
383 {
384 #if defined(__WXPM__)
385 m_mediumShadowPen = wxPen(wxColour(127, 127, 127));
386 m_hilightPen = *wxWHITE_PEN;
387
388 SetBackgroundColour(*wxLIGHT_GREY);
389 SetForegroundColour(*wxBLACK);
390 #else // !__WXPM__
391 m_mediumShadowPen = wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
392 m_hilightPen = wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
393 #endif // __WXPM__/!__WXPM__
394 }
395
396 void wxStatusBarGeneric::SetMinHeight(int height)
397 {
398 // check that this min height is not less than minimal height for the
399 // current font (min height is as calculated above in Create() except for border)
400 int minHeight = (int)((11*GetCharHeight())/10);
401
402 if ( height > minHeight )
403 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height + 2*m_borderY);
404 }
405
406 wxRect wxStatusBarGeneric::GetSizeGripRect() const
407 {
408 int width, height;
409 wxWindow::DoGetClientSize(&width, &height);
410
411 if (GetLayoutDirection() == wxLayout_RightToLeft)
412 return wxRect(2, 2, height-2, height-4);
413 else
414 return wxRect(width-height-2, 2, height-2, height-4);
415 }
416
417 // ----------------------------------------------------------------------------
418 // wxStatusBarGeneric - event handlers
419 // ----------------------------------------------------------------------------
420
421 void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
422 {
423 wxPaintDC dc(this);
424
425 #ifdef __WXGTK20__
426 // Draw grip first
427 if ( ShowsSizeGrip() )
428 {
429 const wxRect& rc = GetSizeGripRect();
430 GdkWindowEdge edge =
431 GetLayoutDirection() == wxLayout_RightToLeft ? GDK_WINDOW_EDGE_SOUTH_WEST :
432 GDK_WINDOW_EDGE_SOUTH_EAST;
433 gtk_paint_resize_grip(gtk_widget_get_style(m_widget),
434 GTKGetDrawingWindow(),
435 gtk_widget_get_state(m_widget),
436 NULL,
437 m_widget,
438 "statusbar",
439 edge,
440 rc.x, rc.y, rc.width, rc.height );
441 }
442 #endif // __WXGTK20__
443
444 if (GetFont().IsOk())
445 dc.SetFont(GetFont());
446
447 // compute char height only once for all panes:
448 int textHeight = dc.GetCharHeight();
449
450 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
451 for (size_t i = 0; i < m_panes.GetCount(); i ++)
452 DrawField(dc, i, textHeight);
453 }
454
455 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event)
456 {
457 InitColours();
458
459 // Propagate the event to the non-top-level children
460 wxWindow::OnSysColourChanged(event);
461 }
462
463 void wxStatusBarGeneric::OnLeftDown(wxMouseEvent& event)
464 {
465 #ifdef __WXGTK20__
466 int width, height;
467 GetClientSize(&width, &height);
468
469 if ( ShowsSizeGrip() && (event.GetX() > width-height) )
470 {
471 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
472
473 if (!GTK_IS_WINDOW (ancestor))
474 return;
475
476 GdkWindow *source = GTKGetDrawingWindow();
477
478 int org_x = 0;
479 int org_y = 0;
480 gdk_window_get_origin( source, &org_x, &org_y );
481
482 if (GetLayoutDirection() == wxLayout_RightToLeft)
483 {
484 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
485 GDK_WINDOW_EDGE_SOUTH_WEST,
486 1,
487 org_x - event.GetX() + GetSize().x ,
488 org_y + event.GetY(),
489 0);
490 }
491 else
492 {
493 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
494 GDK_WINDOW_EDGE_SOUTH_EAST,
495 1,
496 org_x + event.GetX(),
497 org_y + event.GetY(),
498 0);
499 }
500 }
501 else
502 {
503 event.Skip( true );
504 }
505 #else
506 event.Skip( true );
507 #endif
508 }
509
510 void wxStatusBarGeneric::OnRightDown(wxMouseEvent& event)
511 {
512 #ifdef __WXGTK20__
513 int width, height;
514 GetClientSize(&width, &height);
515
516 if ( ShowsSizeGrip() && (event.GetX() > width-height) )
517 {
518 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
519
520 if (!GTK_IS_WINDOW (ancestor))
521 return;
522
523 GdkWindow *source = GTKGetDrawingWindow();
524
525 int org_x = 0;
526 int org_y = 0;
527 gdk_window_get_origin( source, &org_x, &org_y );
528
529 gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
530 2,
531 org_x + event.GetX(),
532 org_y + event.GetY(),
533 0);
534 }
535 else
536 {
537 event.Skip( true );
538 }
539 #else
540 event.Skip( true );
541 #endif
542 }
543
544 void wxStatusBarGeneric::OnSize(wxSizeEvent& event)
545 {
546 DoUpdateFieldWidths();
547
548 event.Skip();
549 }
550
551 #endif // wxUSE_STATUSBAR