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