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