]> git.saurik.com Git - wxWidgets.git/blame - src/generic/statusbr.cpp
include missing header (wx/msw/wrapcctl.h) for PCH-less build
[wxWidgets.git] / src / generic / statusbr.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
a71d815b 2// Name: src/generic/statusbr.cpp
ed791986 3// Purpose: wxStatusBarGeneric class implementation
c801d85f
KB
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
c801d85f
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
76b49cf4 16 #pragma hdrstop
c801d85f
KB
17#endif
18
1e6feb95 19#if wxUSE_STATUSBAR
ed791986 20
3304646d
WS
21#include "wx/statusbr.h"
22
c801d85f 23#ifndef WX_PRECOMP
76b49cf4
WS
24 #include "wx/settings.h"
25 #include "wx/dcclient.h"
a0125cfc 26 #include "wx/toplevel.h"
c801d85f
KB
27#endif
28
2b5f62a0 29#ifdef __WXGTK20__
53b6d7a2 30 #include <gtk/gtk.h>
2b5f62a0
VZ
31#endif
32
84f68c21
VZ
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)
b64a8473 39 #include "wx/generic/statusbr.h"
c801d85f 40
b64a8473
VZ
41 IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric, wxWindow)
42#endif // wxUSE_NATIVE_STATUSBAR
ed791986 43
7b6fefbe
FM
44// Default status border dimensions
45#define wxTHICK_LINE_BORDER 2
46
47
48// ----------------------------------------------------------------------------
49// wxStatusBarGeneric
50// ----------------------------------------------------------------------------
51
ed791986
VZ
52BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow)
53 EVT_PAINT(wxStatusBarGeneric::OnPaint)
2b5f62a0
VZ
54 EVT_LEFT_DOWN(wxStatusBarGeneric::OnLeftDown)
55 EVT_RIGHT_DOWN(wxStatusBarGeneric::OnRightDown)
ed791986 56 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged)
c801d85f 57END_EVENT_TABLE()
c801d85f 58
390015c0 59void wxStatusBarGeneric::Init()
c801d85f 60{
48f7ffbe
WS
61 m_borderX = wxTHICK_LINE_BORDER;
62 m_borderY = wxTHICK_LINE_BORDER;
c801d85f
KB
63}
64
ed791986 65wxStatusBarGeneric::~wxStatusBarGeneric()
c801d85f 66{
c801d85f
KB
67}
68
ed791986 69bool wxStatusBarGeneric::Create(wxWindow *parent,
76880b87
RL
70 wxWindowID id,
71 long style,
72 const wxString& name)
c801d85f 73{
53b6d7a2 74 style |= wxTAB_TRAVERSAL | wxFULL_REPAINT_ON_RESIZE;
48f7ffbe
WS
75 if ( !wxWindow::Create(parent, id,
76 wxDefaultPosition, wxDefaultSize,
53b6d7a2 77 style, name) )
48f7ffbe 78 return false;
c801d85f 79
48f7ffbe
WS
80 // The status bar should have a themed background
81 SetThemeEnabled( true );
82
83 InitColours();
f90566f5 84
1ca21594 85#ifdef __WXPM__
48f7ffbe 86 SetFont(*wxSMALL_FONT);
1ca21594 87#endif
c801d85f 88
7422d7c4
VZ
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 }
48f7ffbe 97 int height = (int)( (11*y)/10 + 2*GetBorderY());
71e03035 98
48f7ffbe 99 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height);
71e03035 100
48f7ffbe 101 SetFieldsCount(1);
8aa2cea1 102
48f7ffbe 103 return true;
c801d85f
KB
104}
105
595a9493
RD
106wxSize wxStatusBarGeneric::DoGetBestSize() const
107{
108 int width, height;
109
110 // best width is the width of the parent
111 GetParent()->GetClientSize(&width, NULL);
112
113 // best height is as calculated above in Create
114 wxClientDC dc((wxWindow*)this);
115 dc.SetFont(GetFont());
116 wxCoord y;
117 dc.GetTextExtent(_T("X"), NULL, &y );
118 height = (int)( (11*y)/10 + 2*GetBorderY());
119
120 return wxSize(width, height);
ca65c044 121}
595a9493 122
cc56206f 123void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths)
c801d85f 124{
390015c0 125 wxASSERT_MSG( number >= 0, _T("negative number of fields in wxStatusBar?") );
76880b87 126
7b6fefbe
FM
127 // enlarge the m_statusStrings array if needed:
128 for (size_t i = m_panes.GetCount(); i < (size_t)number; ++i)
76880b87
RL
129 m_statusStrings.Add( wxEmptyString );
130
7b6fefbe
FM
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);
76880b87 134
f9a961e1
DS
135 // forget the old cached pixel widths
136 m_widthsAbs.Empty();
137
d64b1c76
DS
138 wxStatusBarBase::SetFieldsCount(number, widths);
139
7b6fefbe
FM
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?") );
c801d85f
KB
142}
143
ed791986 144void wxStatusBarGeneric::SetStatusText(const wxString& text, int number)
c801d85f 145{
7b6fefbe 146 wxCHECK_RET( (number >= 0) && ((size_t)number < m_panes.GetCount()),
633d67bb 147 _T("invalid status bar field index") );
c801d85f 148
87ff599d
JS
149 wxString oldText = m_statusStrings[number];
150 if (oldText != text)
151 {
152 m_statusStrings[number] = text;
c801d85f 153
87ff599d
JS
154 wxRect rect;
155 GetFieldRect(number, rect);
e715f4e7 156
e5e0c727
VZ
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();
87ff599d 163 }
c801d85f
KB
164}
165
ed791986 166wxString wxStatusBarGeneric::GetStatusText(int n) const
c801d85f 167{
7b6fefbe 168 wxCHECK_MSG( (n >= 0) && ((size_t)n < m_panes.GetCount()), wxEmptyString,
ed791986
VZ
169 _T("invalid status bar field index") );
170
c801d85f
KB
171 return m_statusStrings[n];
172}
173
ed791986 174void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[])
c801d85f 175{
7b6fefbe
FM
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") );
633d67bb 178
390015c0
VZ
179 // forget the old cached pixel widths
180 m_widthsAbs.Empty();
181
1f361cdd 182 wxStatusBarBase::SetStatusWidths(n, widths_field);
c801d85f
KB
183}
184
7422d7c4
VZ
185bool 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
ed791986 195void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
c801d85f 196{
2b5f62a0 197 wxPaintDC dc(this);
c801d85f 198
2b5f62a0
VZ
199#ifdef __WXGTK20__
200 // Draw grip first
7422d7c4 201 if ( ShowsSizeGrip() )
2b5f62a0
VZ
202 {
203 int width, height;
204 GetClientSize(&width, &height);
7422d7c4 205
fc2d4209
RR
206 if (GetLayoutDirection() == wxLayout_RightToLeft)
207 {
208 gtk_paint_resize_grip( m_widget->style,
08f53168 209 GTKGetDrawingWindow(),
fc2d4209
RR
210 (GtkStateType) GTK_WIDGET_STATE (m_widget),
211 NULL,
212 m_widget,
213 "statusbar",
214 GDK_WINDOW_EDGE_SOUTH_WEST,
215 2, 2, height-2, height-4 );
216 }
217 else
218 {
219 gtk_paint_resize_grip( m_widget->style,
08f53168 220 GTKGetDrawingWindow(),
2b5f62a0
VZ
221 (GtkStateType) GTK_WIDGET_STATE (m_widget),
222 NULL,
223 m_widget,
224 "statusbar",
225 GDK_WINDOW_EDGE_SOUTH_EAST,
fc2d4209
RR
226 width-height-2, 2, height-2, height-4 );
227 }
2b5f62a0 228 }
7422d7c4 229#endif // __WXGTK20__
5b3ed311 230
48f7ffbe 231 if (GetFont().Ok())
2b5f62a0 232 dc.SetFont(GetFont());
8aa2cea1 233
04ee05f9 234 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
c801d85f 235
7b6fefbe 236 for (size_t i = 0; i < m_panes.GetCount(); i ++)
2b5f62a0 237 DrawField(dc, i);
c801d85f
KB
238}
239
ed791986 240void wxStatusBarGeneric::DrawFieldText(wxDC& dc, int i)
c801d85f 241{
48f7ffbe 242 int leftMargin = 2;
c801d85f 243
48f7ffbe
WS
244 wxRect rect;
245 GetFieldRect(i, rect);
c801d85f 246
48f7ffbe 247 wxString text(GetStatusText(i));
c801d85f 248
01399dcd 249 wxCoord x = 0, y = 0;
c801d85f 250
48f7ffbe 251 dc.GetTextExtent(text, &x, &y);
c801d85f 252
48f7ffbe
WS
253 int xpos = rect.x + leftMargin;
254 int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ;
ed791986 255
7c74e7fe 256#if defined( __WXGTK__ ) || defined(__WXMAC__)
48f7ffbe
WS
257 xpos++;
258 ypos++;
92976ab6 259#endif
c801d85f 260
48f7ffbe 261 dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height);
c801d85f 262
48f7ffbe 263 dc.DrawText(text, xpos, ypos);
c801d85f 264
48f7ffbe 265 dc.DestroyClippingRegion();
c801d85f
KB
266}
267
ed791986 268void wxStatusBarGeneric::DrawField(wxDC& dc, int i)
c801d85f 269{
2b5f62a0
VZ
270 wxRect rect;
271 GetFieldRect(i, rect);
c801d85f 272
7b6fefbe 273 int style = m_panes[i].nStyle;
c2919ab3
VZ
274 if (style != wxSB_FLAT)
275 {
276 // Draw border
277 // For wxSB_NORMAL:
278 // Have grey background, plus 3-d border -
279 // One black rectangle.
280 // Inside this, left and top sides - dark grey. Bottom and right -
281 // white.
282 // Reverse it for wxSB_RAISED
283
284 dc.SetPen((style == wxSB_RAISED) ? m_mediumShadowPen : m_hilightPen);
285
286 #ifndef __WXPM__
287
288 // Right and bottom lines
289 dc.DrawLine(rect.x + rect.width, rect.y,
290 rect.x + rect.width, rect.y + rect.height);
291 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
292 rect.x, rect.y + rect.height);
293
294 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
295
296 // Left and top lines
297 dc.DrawLine(rect.x, rect.y + rect.height,
298 rect.x, rect.y);
299 dc.DrawLine(rect.x, rect.y,
300 rect.x + rect.width, rect.y);
301 #else
302
303 dc.DrawLine(rect.x + rect.width, rect.height + 2,
304 rect.x, rect.height + 2);
305 dc.DrawLine(rect.x + rect.width, rect.y,
306 rect.x + rect.width, rect.y + rect.height);
307
308 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
309 dc.DrawLine(rect.x, rect.y,
310 rect.x + rect.width, rect.y);
311 dc.DrawLine(rect.x, rect.y + rect.height,
312 rect.x, rect.y);
b34590eb
DW
313
314#endif
c2919ab3 315 }
c801d85f 316
f51f94ea 317 DrawFieldText(dc, i);
c801d85f
KB
318}
319
320 // Get the position and size of the field's internal bounding rectangle
ed791986 321bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
c801d85f 322{
7b6fefbe 323 wxCHECK_MSG( (n >= 0) && ((size_t)n < m_panes.GetCount()), false,
390015c0 324 _T("invalid status bar field index") );
c801d85f 325
390015c0
VZ
326 // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
327 int width, height;
28be2e8a 328#ifdef __WXPM__
390015c0 329 GetSize(&width, &height);
28be2e8a 330#else
390015c0 331 GetClientSize(&width, &height);
28be2e8a 332#endif
c801d85f 333
2b5f62a0
VZ
334 // we cache m_widthsAbs between calls and recompute it if client
335 // width has changed (or when it is initially empty)
336 if ( m_widthsAbs.IsEmpty() || (m_lastClientWidth != width) )
c801d85f 337 {
390015c0
VZ
338 wxConstCast(this, wxStatusBarGeneric)->
339 m_widthsAbs = CalculateAbsWidths(width);
2b5f62a0
VZ
340 // remember last width for which we have recomputed the widths in pixels
341 wxConstCast(this, wxStatusBarGeneric)->
342 m_lastClientWidth = width;
c801d85f 343 }
c801d85f 344
390015c0
VZ
345 rect.x = 0;
346 for ( int i = 0; i < n; i++ )
347 {
348 rect.x += m_widthsAbs[i];
c801d85f 349 }
c801d85f 350
390015c0
VZ
351 rect.x += m_borderX;
352 rect.y = m_borderY;
c801d85f 353
390015c0
VZ
354 rect.width = m_widthsAbs[n] - 2*m_borderX;
355 rect.height = height - 2*m_borderY;
c801d85f 356
ca65c044 357 return true;
c801d85f
KB
358}
359
360// Initialize colours
ed791986 361void wxStatusBarGeneric::InitColours()
c801d85f 362{
ba959d4c 363#if defined(__WXPM__)
e715f4e7 364 m_mediumShadowPen = wxPen(wxColour(127, 127, 127), 1, wxSOLID);
b0fa2187 365 m_hilightPen = *wxWHITE_PEN;
dd7d1435 366
b0fa2187
PC
367 SetBackgroundColour(*wxLIGHT_GREY);
368 SetForegroundColour(*wxBLACK);
ba959d4c
VZ
369#else // !__WXPM__
370 m_mediumShadowPen = wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
371 m_hilightPen = wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
372#endif // __WXPM__/!__WXPM__
c801d85f
KB
373}
374
375// Responds to colour changes, and passes event on to children.
ed791986 376void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event)
c801d85f
KB
377{
378 InitColours();
c801d85f
KB
379
380 // Propagate the event to the non-top-level children
381 wxWindow::OnSysColourChanged(event);
382}
383
ed791986
VZ
384void wxStatusBarGeneric::SetMinHeight(int height)
385{
386 // check that this min height is not less than minimal height for the
387 // current font
388 wxClientDC dc(this);
389 wxCoord y;
2b5f62a0 390 dc.GetTextExtent( wxT("X"), NULL, &y );
ed791986
VZ
391
392 if ( height > (11*y)/10 )
393 {
422d0ff0 394 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height + 2*m_borderY);
ed791986
VZ
395 }
396}
397
2b5f62a0 398void wxStatusBarGeneric::OnLeftDown(wxMouseEvent& event)
390015c0 399{
2b5f62a0
VZ
400#ifdef __WXGTK20__
401 int width, height;
402 GetClientSize(&width, &height);
8aa2cea1 403
7422d7c4 404 if ( ShowsSizeGrip() && (event.GetX() > width-height) )
2b5f62a0
VZ
405 {
406 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
407
408 if (!GTK_IS_WINDOW (ancestor))
409 return;
390015c0 410
08f53168 411 GdkWindow *source = GTKGetDrawingWindow();
2b5f62a0
VZ
412
413 int org_x = 0;
414 int org_y = 0;
415 gdk_window_get_origin( source, &org_x, &org_y );
416
fc2d4209
RR
417 if (GetLayoutDirection() == wxLayout_RightToLeft)
418 {
419 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
420 GDK_WINDOW_EDGE_SOUTH_WEST,
421 1,
422 org_x - event.GetX() + GetSize().x ,
423 org_y + event.GetY(),
424 0);
425 }
426 else
427 {
428 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
2b5f62a0
VZ
429 GDK_WINDOW_EDGE_SOUTH_EAST,
430 1,
8aa2cea1
RD
431 org_x + event.GetX(),
432 org_y + event.GetY(),
2b5f62a0 433 0);
fc2d4209 434 }
2b5f62a0
VZ
435 }
436 else
437 {
ca65c044 438 event.Skip( true );
2b5f62a0
VZ
439 }
440#else
ca65c044 441 event.Skip( true );
2b5f62a0
VZ
442#endif
443}
444
445void wxStatusBarGeneric::OnRightDown(wxMouseEvent& event)
446{
447#ifdef __WXGTK20__
448 int width, height;
449 GetClientSize(&width, &height);
8aa2cea1 450
7422d7c4 451 if ( ShowsSizeGrip() && (event.GetX() > width-height) )
2b5f62a0
VZ
452 {
453 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
454
455 if (!GTK_IS_WINDOW (ancestor))
456 return;
457
08f53168 458 GdkWindow *source = GTKGetDrawingWindow();
2b5f62a0
VZ
459
460 int org_x = 0;
461 int org_y = 0;
462 gdk_window_get_origin( source, &org_x, &org_y );
8aa2cea1 463
2b5f62a0
VZ
464 gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
465 2,
8aa2cea1
RD
466 org_x + event.GetX(),
467 org_y + event.GetY(),
2b5f62a0
VZ
468 0);
469 }
470 else
471 {
ca65c044 472 event.Skip( true );
2b5f62a0
VZ
473 }
474#else
ca65c044 475 event.Skip( true );
2b5f62a0 476#endif
390015c0
VZ
477}
478
1e6feb95 479#endif // wxUSE_STATUSBAR