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