]> git.saurik.com Git - wxWidgets.git/blob - src/generic/statusbr.cpp
don't set virtual size of the window to (0, 0) if scrollbars are removed from it
[wxWidgets.git] / src / generic / statusbr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "statusbr.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_STATUSBAR
24
25 #ifndef WX_PRECOMP
26 #include "wx/setup.h"
27 #include "wx/frame.h"
28 #include "wx/settings.h"
29 #include "wx/dcclient.h"
30 #endif
31
32 #ifdef __WXGTK20__
33 #include "wx/gtk/private.h"
34 #include "wx/gtk/win_gtk.h"
35 #endif
36
37 #include "wx/statusbr.h"
38
39 // we only have to do it here when we use wxStatusBarGeneric in addition to the
40 // standard wxStatusBar class, if wxStatusBarGeneric is the same as
41 // wxStatusBar, then the corresponding IMPLEMENT_DYNAMIC_CLASS is already in
42 // common/statbar.cpp
43 #if defined(__WXMAC__) || \
44 (defined(wxUSE_NATIVE_STATUSBAR) && wxUSE_NATIVE_STATUSBAR)
45 #include "wx/generic/statusbr.h"
46
47 IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric, wxWindow)
48 #endif // wxUSE_NATIVE_STATUSBAR
49
50 BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow)
51 EVT_PAINT(wxStatusBarGeneric::OnPaint)
52 EVT_LEFT_DOWN(wxStatusBarGeneric::OnLeftDown)
53 EVT_RIGHT_DOWN(wxStatusBarGeneric::OnRightDown)
54 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged)
55 END_EVENT_TABLE()
56
57 // Default status border dimensions
58 #define wxTHICK_LINE_BORDER 2
59 #define wxTHICK_LINE_WIDTH 1
60
61 void wxStatusBarGeneric::Init()
62 {
63 m_borderX = wxTHICK_LINE_BORDER;
64 m_borderY = wxTHICK_LINE_BORDER;
65 }
66
67 wxStatusBarGeneric::~wxStatusBarGeneric()
68 {
69 }
70
71 bool wxStatusBarGeneric::Create(wxWindow *parent,
72 wxWindowID id,
73 long style,
74 const wxString& name)
75 {
76 if ( !wxWindow::Create(parent, id,
77 wxDefaultPosition, wxDefaultSize,
78 style | wxTAB_TRAVERSAL, name) )
79 return false;
80
81 // The status bar should have a themed background
82 SetThemeEnabled( true );
83
84 InitColours();
85
86 #ifdef __WXPM__
87 SetFont(*wxSMALL_FONT);
88 #endif
89
90 // Set the height according to the font and the border size
91 wxClientDC dc(this);
92 dc.SetFont(GetFont());
93
94 wxCoord y;
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
107 wxSize wxStatusBarGeneric::DoGetBestSize() const
108 {
109 int width, height;
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 wxCoord y;
118 dc.GetTextExtent(_T("X"), NULL, &y );
119 height = (int)( (11*y)/10 + 2*GetBorderY());
120
121 return wxSize(width, height);
122 }
123
124 void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths)
125 {
126 wxASSERT_MSG( number >= 0, _T("negative number of fields in wxStatusBar?") );
127
128 int i;
129 for(i = m_nFields; i < number; ++i)
130 m_statusStrings.Add( wxEmptyString );
131
132 for (i = m_nFields - 1; i >= number; --i)
133 m_statusStrings.RemoveAt(i);
134
135 // forget the old cached pixel widths
136 m_widthsAbs.Empty();
137
138 wxStatusBarBase::SetFieldsCount(number, widths);
139
140 wxASSERT_MSG( m_nFields == (int)m_statusStrings.GetCount(),
141 _T("This really should never happen, can we do away with m_nFields here?") );
142 }
143
144 void wxStatusBarGeneric::SetStatusText(const wxString& text, int number)
145 {
146 wxCHECK_RET( (number >= 0) && (number < m_nFields),
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 }
160
161 wxString wxStatusBarGeneric::GetStatusText(int n) const
162 {
163 wxCHECK_MSG( (n >= 0) && (n < m_nFields), wxEmptyString,
164 _T("invalid status bar field index") );
165
166 return m_statusStrings[n];
167 }
168
169 void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[])
170 {
171 // only set status widths, when n == number of statuswindows
172 wxCHECK_RET( n == m_nFields, _T("status bar field count mismatch") );
173
174 // delete the old widths in any case - this function may be used to reset
175 // the widths to the default (all equal)
176 // MBN: this is incompatible with at least wxMSW and wxMAC and not
177 // documented, but let's keep it for now
178 ReinitWidths();
179
180 // forget the old cached pixel widths
181 m_widthsAbs.Empty();
182
183 if ( !widths_field )
184 {
185 // not an error, see the comment above
186 Refresh();
187 return;
188 }
189
190 wxStatusBarBase::SetStatusWidths(n, widths_field);
191 }
192
193 void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
194 {
195 wxPaintDC dc(this);
196
197 #ifdef __WXGTK20__
198 // Draw grip first
199 if (HasFlag( wxST_SIZEGRIP ))
200 {
201 int width, height;
202 GetClientSize(&width, &height);
203
204 gtk_paint_resize_grip( m_widget->style,
205 GTK_PIZZA(m_wxwindow)->bin_window,
206 (GtkStateType) GTK_WIDGET_STATE (m_widget),
207 NULL,
208 m_widget,
209 "statusbar",
210 GDK_WINDOW_EDGE_SOUTH_EAST,
211 width-height-2, 1, height-2, height-3 );
212
213 }
214 #endif
215
216 if (GetFont().Ok())
217 dc.SetFont(GetFont());
218
219 dc.SetBackgroundMode(wxTRANSPARENT);
220
221 #ifdef __WXPM__
222 wxColour vColor;
223
224 vColor = wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR);
225 ::WinFillRect(dc.m_hPS, &dc.m_vRclPaint, vColor.GetPixel());
226 #endif
227
228 for (int i = 0; i < m_nFields; i ++)
229 DrawField(dc, i);
230 }
231
232 void wxStatusBarGeneric::DrawFieldText(wxDC& dc, int i)
233 {
234 int leftMargin = 2;
235
236 wxRect rect;
237 GetFieldRect(i, rect);
238
239 wxString text(GetStatusText(i));
240
241 long x, y;
242
243 dc.GetTextExtent(text, &x, &y);
244
245 int xpos = rect.x + leftMargin;
246 int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ;
247
248 #if defined( __WXGTK__ ) || defined(__WXMAC__)
249 xpos++;
250 ypos++;
251 #endif
252
253 dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height);
254
255 dc.DrawText(text, xpos, ypos);
256
257 dc.DestroyClippingRegion();
258 }
259
260 void wxStatusBarGeneric::DrawField(wxDC& dc, int i)
261 {
262 wxRect rect;
263 GetFieldRect(i, rect);
264
265 int style = wxSB_NORMAL;
266 if (m_statusStyles)
267 style = m_statusStyles[i];
268
269 if (style != wxSB_FLAT)
270 {
271 // Draw border
272 // For wxSB_NORMAL:
273 // Have grey background, plus 3-d border -
274 // One black rectangle.
275 // Inside this, left and top sides - dark grey. Bottom and right -
276 // white.
277 // Reverse it for wxSB_RAISED
278
279 dc.SetPen((style == wxSB_RAISED) ? m_mediumShadowPen : m_hilightPen);
280
281 #ifndef __WXPM__
282
283 // Right and bottom lines
284 dc.DrawLine(rect.x + rect.width, rect.y,
285 rect.x + rect.width, rect.y + rect.height);
286 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
287 rect.x, rect.y + rect.height);
288
289 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
290
291 // Left and top lines
292 dc.DrawLine(rect.x, rect.y + rect.height,
293 rect.x, rect.y);
294 dc.DrawLine(rect.x, rect.y,
295 rect.x + rect.width, rect.y);
296 #else
297
298 dc.DrawLine(rect.x + rect.width, rect.height + 2,
299 rect.x, rect.height + 2);
300 dc.DrawLine(rect.x + rect.width, rect.y,
301 rect.x + rect.width, rect.y + rect.height);
302
303 dc.SetPen((style == wxSB_RAISED) ? m_hilightPen : m_mediumShadowPen);
304 dc.DrawLine(rect.x, rect.y,
305 rect.x + rect.width, rect.y);
306 dc.DrawLine(rect.x, rect.y + rect.height,
307 rect.x, rect.y);
308
309 #endif
310 }
311
312 DrawFieldText(dc, i);
313 }
314
315 // Get the position and size of the field's internal bounding rectangle
316 bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
317 {
318 wxCHECK_MSG( (n >= 0) && (n < m_nFields), false,
319 _T("invalid status bar field index") );
320
321 // FIXME: workarounds for OS/2 bugs have nothing to do here (VZ)
322 int width, height;
323 #ifdef __WXPM__
324 GetSize(&width, &height);
325 #else
326 GetClientSize(&width, &height);
327 #endif
328
329 // we cache m_widthsAbs between calls and recompute it if client
330 // width has changed (or when it is initially empty)
331 if ( m_widthsAbs.IsEmpty() || (m_lastClientWidth != width) )
332 {
333 wxConstCast(this, wxStatusBarGeneric)->
334 m_widthsAbs = CalculateAbsWidths(width);
335 // remember last width for which we have recomputed the widths in pixels
336 wxConstCast(this, wxStatusBarGeneric)->
337 m_lastClientWidth = width;
338 }
339
340 rect.x = 0;
341 for ( int i = 0; i < n; i++ )
342 {
343 rect.x += m_widthsAbs[i];
344 }
345
346 rect.x += m_borderX;
347 rect.y = m_borderY;
348
349 rect.width = m_widthsAbs[n] - 2*m_borderX;
350 rect.height = height - 2*m_borderY;
351
352 return true;
353 }
354
355 // Initialize colours
356 void wxStatusBarGeneric::InitColours()
357 {
358 // Shadow colours
359 #if defined(__WIN95__) || defined(__WXMAC__)
360 wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
361 m_mediumShadowPen = wxPen(mediumShadowColour, 1, wxSOLID);
362
363 wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
364 m_hilightPen = wxPen(hilightColour, 1, wxSOLID);
365 #elif defined(__WXPM__)
366 m_mediumShadowPen = wxPen(wxColour(127, 127, 127), 1, wxSOLID);
367 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
368
369 wxColour vColour;
370
371 vColour.Set(wxString("LIGHT GREY"));
372 SetBackgroundColour(vColour);
373 vColour.Set(wxString("BLACK"));
374 SetForegroundColour(vColour);
375 #else
376 m_mediumShadowPen = wxPen("GREY", 1, wxSOLID);
377 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
378 #endif
379 }
380
381 // Responds to colour changes, and passes event on to children.
382 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event)
383 {
384 InitColours();
385
386 // Propagate the event to the non-top-level children
387 wxWindow::OnSysColourChanged(event);
388 }
389
390 void wxStatusBarGeneric::SetMinHeight(int height)
391 {
392 // check that this min height is not less than minimal height for the
393 // current font
394 wxClientDC dc(this);
395 wxCoord y;
396 dc.GetTextExtent( wxT("X"), NULL, &y );
397
398 if ( height > (11*y)/10 )
399 {
400 SetSize(wxDefaultCoord, wxDefaultCoord, wxDefaultCoord, height + 2*m_borderY);
401 }
402 }
403
404 void wxStatusBarGeneric::OnLeftDown(wxMouseEvent& event)
405 {
406 #ifdef __WXGTK20__
407 int width, height;
408 GetClientSize(&width, &height);
409
410 if (HasFlag( wxST_SIZEGRIP ) && (event.GetX() > width-height))
411 {
412 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
413
414 if (!GTK_IS_WINDOW (ancestor))
415 return;
416
417 GdkWindow *source = GTK_PIZZA(m_wxwindow)->bin_window;
418
419 int org_x = 0;
420 int org_y = 0;
421 gdk_window_get_origin( source, &org_x, &org_y );
422
423 gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
424 GDK_WINDOW_EDGE_SOUTH_EAST,
425 1,
426 org_x + event.GetX(),
427 org_y + event.GetY(),
428 0);
429 }
430 else
431 {
432 event.Skip( true );
433 }
434 #else
435 event.Skip( true );
436 #endif
437 }
438
439 void wxStatusBarGeneric::OnRightDown(wxMouseEvent& event)
440 {
441 #ifdef __WXGTK20__
442 int width, height;
443 GetClientSize(&width, &height);
444
445 if (HasFlag( wxST_SIZEGRIP ) && (event.GetX() > width-height))
446 {
447 GtkWidget *ancestor = gtk_widget_get_toplevel( m_widget );
448
449 if (!GTK_IS_WINDOW (ancestor))
450 return;
451
452 GdkWindow *source = GTK_PIZZA(m_wxwindow)->bin_window;
453
454 int org_x = 0;
455 int org_y = 0;
456 gdk_window_get_origin( source, &org_x, &org_y );
457
458 gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
459 2,
460 org_x + event.GetX(),
461 org_y + event.GetY(),
462 0);
463 }
464 else
465 {
466 event.Skip( true );
467 }
468 #else
469 event.Skip( true );
470 #endif
471 }
472
473 #endif // wxUSE_STATUSBAR
474