]> git.saurik.com Git - wxWidgets.git/blob - src/generic/statusbr.cpp
691cd520e11e5b0826ee5a3dd5391fbb3e11d8ab
[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 int width;
185 GetClientSize(&width, &m_lastClientHeight);
186 m_widthsAbs = CalculateAbsWidths(width);
187 }
188
189 bool wxStatusBarGeneric::ShowsSizeGrip() const
190 {
191 if ( !HasFlag(wxSTB_SIZEGRIP) )
192 return false;
193
194 wxTopLevelWindow * const
195 tlw = wxDynamicCast(wxGetTopLevelParent(GetParent()), wxTopLevelWindow);
196 return tlw && !tlw->IsMaximized() && tlw->HasFlag(wxRESIZE_BORDER);
197 }
198
199 void wxStatusBarGeneric::DrawFieldText(wxDC& dc, const wxRect& rect, int i, int textHeight)
200 {
201 wxString text(GetStatusText(i));
202 if (text.empty())
203 return; // optimization
204
205 int xpos = rect.x + wxFIELD_TEXT_MARGIN,
206 maxWidth = rect.width - 2*wxFIELD_TEXT_MARGIN,
207 ypos = (int) (((rect.height - textHeight) / 2) + rect.y + 0.5);
208
209 if (ShowsSizeGrip())
210 {
211 // don't write text over the size grip:
212 // NOTE: overloading DoGetClientSize() and GetClientAreaOrigin() wouldn't
213 // work because the adjustment needs to be done only when drawing
214 // the field text and not also when drawing the background, the
215 // size grip itself, etc
216 if ((GetLayoutDirection() == wxLayout_RightToLeft && i == 0) ||
217 (GetLayoutDirection() != wxLayout_RightToLeft &&
218 i == (int)m_panes.GetCount()-1))
219 {
220 const wxRect& gripRc = GetSizeGripRect();
221
222 // NOTE: we don't need any special treatment wrt to the layout direction
223 // since DrawText() will automatically adjust the origin of the
224 // text accordingly to the layout in use
225
226 maxWidth -= gripRc.width;
227 }
228 }
229
230 // eventually ellipsize the text so that it fits the field width
231
232 wxEllipsizeMode ellmode = (wxEllipsizeMode)-1;
233 if (HasFlag(wxSTB_ELLIPSIZE_START)) ellmode = wxELLIPSIZE_START;
234 else if (HasFlag(wxSTB_ELLIPSIZE_MIDDLE)) ellmode = wxELLIPSIZE_MIDDLE;
235 else if (HasFlag(wxSTB_ELLIPSIZE_END)) ellmode = wxELLIPSIZE_END;
236
237 if (ellmode == (wxEllipsizeMode)-1)
238 {
239 // if we have the wxSTB_SHOW_TIPS we must set the ellipsized flag even if
240 // we don't ellipsize the text but just truncate it
241 if (HasFlag(wxSTB_SHOW_TIPS))
242 SetEllipsizedFlag(i, dc.GetTextExtent(text).GetWidth() > maxWidth);
243
244 dc.SetClippingRegion(rect);
245 }
246 else
247 {
248 text = wxControl::Ellipsize(text, dc,
249 ellmode,
250 maxWidth,
251 wxELLIPSIZE_FLAGS_EXPAND_TABS);
252 // Ellipsize() will do something only if necessary
253
254 // update the ellipsization status for this pane; this is used later to
255 // decide whether a tooltip should be shown or not for this pane
256 // (if we have wxSTB_SHOW_TIPS)
257 SetEllipsizedFlag(i, text != GetStatusText(i));
258 }
259
260 #if defined( __WXGTK__ ) || defined(__WXMAC__)
261 xpos++;
262 ypos++;
263 #endif
264
265 // draw the text
266 dc.DrawText(text, xpos, ypos);
267
268 if (ellmode == (wxEllipsizeMode)-1)
269 dc.DestroyClippingRegion();
270 }
271
272 void wxStatusBarGeneric::DrawField(wxDC& dc, int i, int textHeight)
273 {
274 wxRect rect;
275 GetFieldRect(i, rect);
276
277 if (rect.GetWidth() <= 0)
278 return; // happens when the status bar is shrinked in a very small area!
279
280 int style = m_panes[i].GetStyle();
281 if (style != wxSB_FLAT)
282 {
283 // Draw border
284 // For wxSB_NORMAL: paint a grey background, plus 3-d border (one black rectangle)
285 // Inside this, left and top sides (dark grey). Bottom and right (white).
286 // Reverse it for wxSB_RAISED
287
288 dc.SetPen((style == wxSB_RAISED) ? m_mediumShadowPen : m_hilightPen);
289
290 #ifndef __WXPM__
291
292 // Right and bottom lines
293 dc.DrawLine(rect.x + rect.width, rect.y,
294 rect.x + rect.width, rect.y + rect.height);
295 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
296 rect.x, rect.y + rect.height);
297
298 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
299
300 // Left and top lines
301 dc.DrawLine(rect.x, rect.y + rect.height,
302 rect.x, rect.y);
303 dc.DrawLine(rect.x, rect.y,
304 rect.x + rect.width, rect.y);
305 #else
306
307 dc.DrawLine(rect.x + rect.width, rect.height + 2,
308 rect.x, rect.height + 2);
309 dc.DrawLine(rect.x + rect.width, rect.y,
310 rect.x + rect.width, rect.y + rect.height);
311
312 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
313 dc.DrawLine(rect.x, rect.y,
314 rect.x + rect.width, rect.y);
315 dc.DrawLine(rect.x, rect.y + rect.height,
316 rect.x, rect.y);
317 #endif
318 }
319
320 DrawFieldText(dc, rect, i, textHeight);
321 }
322
323 bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
324 {
325 wxCHECK_MSG( (n >= 0) && ((size_t)n < m_panes.GetCount()), false,
326 wxT("invalid status bar field index") );
327
328 if (m_widthsAbs.IsEmpty())
329 return false;
330
331 rect.x = 0;
332 for ( int i = 0; i < n; i++ )
333 rect.x += m_widthsAbs[i];
334 rect.x += m_borderX;
335
336 rect.y = m_borderY;
337 rect.width = m_widthsAbs[n] - 2*m_borderX;
338 rect.height = m_lastClientHeight - 2*m_borderY;
339
340 return true;
341 }
342
343 int wxStatusBarGeneric::GetFieldFromPoint(const wxPoint& pt) const
344 {
345 if (m_widthsAbs.IsEmpty())
346 return wxNOT_FOUND;
347
348 // NOTE: we explicitly don't take in count the borders since they are only
349 // useful when rendering the status text, not for hit-test computations
350
351 if (pt.y <= 0 || pt.y >= m_lastClientHeight)
352 return wxNOT_FOUND;
353
354 int x = 0;
355 for ( size_t i = 0; i < m_panes.GetCount(); i++ )
356 {
357 if (pt.x > x && pt.x < x+m_widthsAbs[i])
358 return i;
359
360 x += m_widthsAbs[i];
361 }
362
363 return wxNOT_FOUND;
364 }
365
366 void wxStatusBarGeneric::InitColours()
367 {
368 #if defined(__WXPM__)
369 m_mediumShadowPen = wxPen(wxColour(127, 127, 127));
370 m_hilightPen = *wxWHITE_PEN;
371
372 SetBackgroundColour(*wxLIGHT_GREY);
373 SetForegroundColour(*wxBLACK);
374 #else // !__WXPM__
375 m_mediumShadowPen = wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
376 m_hilightPen = wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
377 #endif // __WXPM__/!__WXPM__
378 }
379
380 void wxStatusBarGeneric::SetMinHeight(int height)
381 {
382 // check that this min height is not less than minimal height for the
383 // current font (min height is as calculated above in Create() except for border)
384 int minHeight = (int)((11*GetCharHeight())/10);
385
386 if ( height > minHeight )
387 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height + 2*m_borderY);
388 }
389
390 wxRect wxStatusBarGeneric::GetSizeGripRect() const
391 {
392 int width, height;
393 wxWindow::DoGetClientSize(&width, &height);
394
395 if (GetLayoutDirection() == wxLayout_RightToLeft)
396 return wxRect(2, 2, height-2, height-4);
397 else
398 return wxRect(width-height-2, 2, height-2, height-4);
399 }
400
401 // ----------------------------------------------------------------------------
402 // wxStatusBarGeneric - event handlers
403 // ----------------------------------------------------------------------------
404
405 void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
406 {
407 wxPaintDC dc(this);
408
409 #ifdef __WXGTK20__
410 // Draw grip first
411 if ( ShowsSizeGrip() )
412 {
413 const wxRect& rc = GetSizeGripRect();
414 GdkWindowEdge edge =
415 GetLayoutDirection() == wxLayout_RightToLeft ? GDK_WINDOW_EDGE_SOUTH_WEST :
416 GDK_WINDOW_EDGE_SOUTH_EAST;
417 gtk_paint_resize_grip( m_widget->style,
418 GTKGetDrawingWindow(),
419 (GtkStateType) GTK_WIDGET_STATE (m_widget),
420 NULL,
421 m_widget,
422 "statusbar",
423 edge,
424 rc.x, rc.y, rc.width, rc.height );
425 }
426 #endif // __WXGTK20__
427
428 if (GetFont().IsOk())
429 dc.SetFont(GetFont());
430
431 // compute char height only once for all panes:
432 int textHeight = dc.GetCharHeight();
433
434 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
435 for (size_t i = 0; i < m_panes.GetCount(); i ++)
436 DrawField(dc, i, textHeight);
437 }
438
439 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event)
440 {
441 InitColours();
442
443 // Propagate the event to the non-top-level children
444 wxWindow::OnSysColourChanged(event);
445 }
446
447 void wxStatusBarGeneric::OnLeftDown(wxMouseEvent& event)
448 {
449 #ifdef __WXGTK20__
450 int width, height;
451 GetClientSize(&width, &height);
452
453 if ( ShowsSizeGrip() && (event.GetX() > width-height) )
454 {
455 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
456
457 if (!GTK_IS_WINDOW (ancestor))
458 return;
459
460 GdkWindow *source = GTKGetDrawingWindow();
461
462 int org_x = 0;
463 int org_y = 0;
464 gdk_window_get_origin( source, &org_x, &org_y );
465
466 if (GetLayoutDirection() == wxLayout_RightToLeft)
467 {
468 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
469 GDK_WINDOW_EDGE_SOUTH_WEST,
470 1,
471 org_x - event.GetX() + GetSize().x ,
472 org_y + event.GetY(),
473 0);
474 }
475 else
476 {
477 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
478 GDK_WINDOW_EDGE_SOUTH_EAST,
479 1,
480 org_x + event.GetX(),
481 org_y + event.GetY(),
482 0);
483 }
484 }
485 else
486 {
487 event.Skip( true );
488 }
489 #else
490 event.Skip( true );
491 #endif
492 }
493
494 void wxStatusBarGeneric::OnRightDown(wxMouseEvent& event)
495 {
496 #ifdef __WXGTK20__
497 int width, height;
498 GetClientSize(&width, &height);
499
500 if ( ShowsSizeGrip() && (event.GetX() > width-height) )
501 {
502 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
503
504 if (!GTK_IS_WINDOW (ancestor))
505 return;
506
507 GdkWindow *source = GTKGetDrawingWindow();
508
509 int org_x = 0;
510 int org_y = 0;
511 gdk_window_get_origin( source, &org_x, &org_y );
512
513 gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
514 2,
515 org_x + event.GetX(),
516 org_y + event.GetY(),
517 0);
518 }
519 else
520 {
521 event.Skip( true );
522 }
523 #else
524 event.Skip( true );
525 #endif
526 }
527
528 void wxStatusBarGeneric::OnSize(wxSizeEvent& WXUNUSED(event))
529 {
530 // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
531 int width;
532 #ifdef __WXPM__
533 GetSize(&width, &m_lastClientHeight);
534 #else
535 GetClientSize(&width, &m_lastClientHeight);
536 #endif
537
538 // recompute the cache of the field widths if the status bar width has changed
539 m_widthsAbs = CalculateAbsWidths(width);
540 }
541
542 #endif // wxUSE_STATUSBAR