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