Bunch of GTK theme changes.
[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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
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 #include "wx/statusbr.h"
33
34 // we only have to do it here when we use wxStatusBarGeneric in addition to the
35 // standard wxStatusBar class, if wxStatusBarGeneric is the same as
36 // wxStatusBar, then the corresponding IMPLEMENT_DYNAMIC_CLASS is already in
37 // common/statbar.cpp
38 #if defined(__WXMAC__) || \
39 (defined(wxUSE_NATIVE_STATUSBAR) && wxUSE_NATIVE_STATUSBAR)
40 #include "wx/generic/statusbr.h"
41
42 IMPLEMENT_DYNAMIC_CLASS(wxStatusBarGeneric, wxWindow)
43 #endif // wxUSE_NATIVE_STATUSBAR
44
45 BEGIN_EVENT_TABLE(wxStatusBarGeneric, wxWindow)
46 EVT_PAINT(wxStatusBarGeneric::OnPaint)
47 EVT_SYS_COLOUR_CHANGED(wxStatusBarGeneric::OnSysColourChanged)
48 END_EVENT_TABLE()
49
50 // Default status border dimensions
51 #define wxTHICK_LINE_BORDER 2
52 #define wxTHICK_LINE_WIDTH 1
53
54 wxStatusBarGeneric::wxStatusBarGeneric()
55 {
56 m_statusWidths = (int *) NULL;
57 m_statusStrings = (wxString *) NULL;
58 m_nFields = 0;
59 m_borderX = wxTHICK_LINE_BORDER;
60 m_borderY = wxTHICK_LINE_BORDER;
61 }
62
63 wxStatusBarGeneric::~wxStatusBarGeneric()
64 {
65 # ifdef __WXMSW__
66 SetFont(wxNullFont);
67 # endif // MSW
68
69 if ( m_statusStrings )
70 delete[] m_statusStrings;
71 }
72
73 bool wxStatusBarGeneric::Create(wxWindow *parent,
74 wxWindowID id,
75 long style,
76 const wxString& name)
77 {
78 m_statusWidths = (int *) NULL;
79 m_statusStrings = (wxString *) NULL;
80 m_nFields = 0;
81 m_borderX = wxTHICK_LINE_BORDER;
82 m_borderY = wxTHICK_LINE_BORDER;
83
84 bool success = wxWindow::Create(parent, id,
85 wxDefaultPosition, wxDefaultSize,
86 style | wxTAB_TRAVERSAL, name);
87
88 // The status bar should have a themed background
89 SetThemeEnabled( TRUE );
90
91 // Don't wish this to be found as a child
92 #ifndef __WXMAC__
93 parent->GetChildren().DeleteObject(this);
94 #endif
95 InitColours();
96
97 SetFont(m_defaultStatusBarFont);
98
99 // Set the height according to the font and the border size
100 wxClientDC dc(this);
101 dc.SetFont(GetFont());
102
103 wxCoord y;
104 dc.GetTextExtent(_T("X"), NULL, &y );
105
106 int height = (int)( (11*y)/10 + 2*GetBorderY());
107
108 SetSize(-1, -1, -1, height);
109
110 return success;
111 }
112
113 void wxStatusBarGeneric::SetFieldsCount(int number, const int *widths)
114 {
115 if ( number != m_nFields )
116 {
117 m_nFields = number;
118
119 delete[] m_statusStrings;
120 m_statusStrings = new wxString[number];
121 }
122
123 SetStatusWidths(number, widths);
124 }
125
126 void wxStatusBarGeneric::SetStatusText(const wxString& text, int number)
127 {
128 wxCHECK_RET( (number >= 0) && (number < m_nFields),
129 _T("invalid status bar field index") );
130
131 m_statusStrings[number] = text;
132
133 wxRect rect;
134 GetFieldRect(number, rect);
135
136 Refresh( TRUE, &rect );
137 }
138
139 wxString wxStatusBarGeneric::GetStatusText(int n) const
140 {
141 wxCHECK_MSG( (n >= 0) && (n < m_nFields), wxEmptyString,
142 _T("invalid status bar field index") );
143
144 return m_statusStrings[n];
145 }
146
147 void wxStatusBarGeneric::SetStatusWidths(int n, const int widths_field[])
148 {
149 // only set status widths, when n == number of statuswindows
150 wxCHECK_RET( n == m_nFields, _T("status bar field count mismatch") );
151
152 // delete the old widths in any case - this function may be used to reset
153 // the widths to the default (all equal)
154 delete [] m_statusWidths;
155
156 if ( !widths_field )
157 {
158 // not an error, see the comment above
159 m_statusWidths = (int *)NULL;
160 Refresh();
161 return;
162 }
163
164 int i;
165
166 // VZ: this doesn't do anything as is_variable is unused later
167 #if 0
168 // when one window (minimum) is variable (width <= 0)
169 bool is_variable = FALSE;
170 for (i = 0; i < m_nFields; i++)
171 {
172 if (widths_field[i] <= 0)
173 is_variable = TRUE;
174 }
175 #endif // 0
176
177 // set widths
178 m_statusWidths = new int[n];
179 for (i = 0; i < m_nFields; i++)
180 {
181 m_statusWidths[i] = widths_field[i];
182 }
183 Refresh();
184 }
185
186 void wxStatusBarGeneric::OnPaint(wxPaintEvent& WXUNUSED(event) )
187 {
188 wxPaintDC dc(this);
189
190
191 int i;
192 if ( GetFont().Ok() )
193 dc.SetFont(GetFont());
194 dc.SetBackgroundMode(wxTRANSPARENT);
195
196 #ifdef __WXPM__
197 wxColour vColor;
198
199 vColor.InitFromName("LIGHT GREY");
200 ::WinFillRect(dc.m_hPS, &dc.m_vRclPaint, vColor.GetPixel());
201 #endif
202
203 for ( i = 0; i < m_nFields; i ++ )
204 DrawField(dc, i);
205
206 #ifdef __WXMSW__
207 dc.SetFont(wxNullFont);
208 #endif // MSW
209 }
210
211 void wxStatusBarGeneric::DrawFieldText(wxDC& dc, int i)
212 {
213 int leftMargin = 2;
214
215 wxRect rect;
216 GetFieldRect(i, rect);
217
218 wxString text(GetStatusText(i));
219
220 long x, y;
221
222 dc.GetTextExtent(text, &x, &y);
223
224 int xpos = rect.x + leftMargin;
225 int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ;
226
227 #if defined( __WXGTK__ ) || defined(__WXMAC__)
228 xpos++;
229 ypos++;
230 #endif
231
232 dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height);
233
234 dc.DrawText(text, xpos, ypos);
235
236 dc.DestroyClippingRegion();
237 }
238
239 void wxStatusBarGeneric::DrawField(wxDC& dc, int i)
240 {
241 wxRect rect;
242 GetFieldRect(i, rect);
243
244 // Draw border
245 // Have grey background, plus 3-d border -
246 // One black rectangle.
247 // Inside this, left and top sides - dark grey. Bottom and right -
248 // white.
249
250 dc.SetPen(m_hilightPen);
251
252 #ifndef __WXPM__
253
254 // Right and bottom white lines
255 dc.DrawLine(rect.x + rect.width, rect.y,
256 rect.x + rect.width, rect.y + rect.height);
257 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
258 rect.x, rect.y + rect.height);
259
260 dc.SetPen(m_mediumShadowPen);
261
262 // Left and top grey lines
263 dc.DrawLine(rect.x, rect.y + rect.height,
264 rect.x, rect.y);
265 dc.DrawLine(rect.x, rect.y,
266 rect.x + rect.width, rect.y);
267 #else
268 // Right
269 dc.DrawLine(rect.x + rect.width, rect.y,
270 rect.x + rect.width, rect.y + rect.height + 2);
271 dc.SetPen(m_mediumShadowPen);
272 dc.DrawLine(rect.x + rect.width + 1, rect.y,
273 rect.x + rect.width + 1, rect.y + rect.height + 2);
274 dc.DrawLine(rect.x + rect.width + 2, rect.y,
275 rect.x + rect.width + 2, rect.y + rect.height + 2);
276 // Top
277 dc.DrawLine(rect.x + rect.width + 2, rect.y,
278 rect.x - 2, rect.y);
279 dc.DrawLine(rect.x + rect.width + 1, rect.y - 1,
280 rect.x - 2, rect.y - 1);
281 dc.SetPen(m_hilightPen);
282 dc.DrawLine(rect.x + rect.width, rect.y - 2,
283 rect.x - 2, rect.y - 2);
284
285 #endif
286
287 DrawFieldText(dc, i);
288 }
289
290 // Get the position and size of the field's internal bounding rectangle
291 bool wxStatusBarGeneric::GetFieldRect(int n, wxRect& rect) const
292 {
293 wxCHECK_MSG( (n >= 0) && (n < m_nFields), FALSE,
294 _T("invalid status bar field index") );
295
296 int width, height;
297 #ifdef __WXPM__
298 GetSize(&width, &height);
299 #else
300 GetClientSize(&width, &height);
301 #endif
302
303 int i;
304 int sum_of_nonvar = 0;
305 int num_of_var = 0;
306 bool do_same_width = FALSE;
307
308 int fieldWidth = 0;
309 int fieldPosition = 0;
310
311 if (m_statusWidths)
312 {
313 // if sum(not variable Windows) > c_width - (20 points per variable_window)
314 // then do_same_width = TRUE;
315 for (i = 0; i < m_nFields; i++)
316 {
317 if (m_statusWidths[i] > 0) sum_of_nonvar += m_statusWidths[i];
318 else num_of_var++;
319 }
320 if (sum_of_nonvar > (width - 20*num_of_var)) do_same_width = TRUE;
321 }
322 else do_same_width = TRUE;
323 if (do_same_width)
324 {
325 for (i = 0; i < m_nFields; i++)
326 {
327 fieldWidth = (int)(width/m_nFields);
328 fieldPosition = i*fieldWidth;
329 if ( i == n )
330 break;
331 }
332 }
333 else // no_same_width
334 {
335 int *tempwidth = new int[m_nFields];
336 int temppos = 0;
337 for (i = 0; i < m_nFields; i++)
338 {
339 if (m_statusWidths[i] > 0) tempwidth[i] = m_statusWidths[i];
340 else tempwidth[i] = (width - sum_of_nonvar) / num_of_var;
341 }
342 for (i = 0; i < m_nFields; i++)
343 {
344 fieldWidth = tempwidth[i];
345 fieldPosition = temppos;
346
347 temppos += tempwidth[i];
348
349 if ( i == n )
350 break;
351 }
352 delete [] tempwidth;
353 }
354
355 rect.x = fieldPosition + wxTHICK_LINE_BORDER;
356 rect.y = wxTHICK_LINE_BORDER;
357
358 rect.width = fieldWidth - 2 * wxTHICK_LINE_BORDER ;
359 rect.height = height - 2 * wxTHICK_LINE_BORDER ;
360
361 return TRUE;
362 }
363
364 // Initialize colours
365 void wxStatusBarGeneric::InitColours()
366 {
367 // Shadow colours
368 #if defined(__WIN95__)
369 wxColour mediumShadowColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW));
370 m_mediumShadowPen = wxPen(mediumShadowColour, 1, wxSOLID);
371
372 wxColour hilightColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT));
373 m_hilightPen = wxPen(hilightColour, 1, wxSOLID);
374 #elif defined(__WXPM__)
375 m_mediumShadowPen = wxPen("DARK GREY", 1, wxSOLID);
376 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
377
378 wxColour vColour;
379
380 vColour.Set(wxString("LIGHT GREY"));
381 SetBackgroundColour(vColour);
382 vColour.Set(wxString("BLACK"));
383 SetForegroundColour(vColour);
384 m_defaultStatusBarFont = *wxSMALL_FONT;
385 #else
386 m_mediumShadowPen = wxPen("GREY", 1, wxSOLID);
387 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
388 #endif
389
390 #ifndef __WXPM__
391 m_defaultStatusBarFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
392 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
393 #endif
394 }
395
396 // Responds to colour changes, and passes event on to children.
397 void wxStatusBarGeneric::OnSysColourChanged(wxSysColourChangedEvent& event)
398 {
399 InitColours();
400 Refresh();
401
402 // Propagate the event to the non-top-level children
403 wxWindow::OnSysColourChanged(event);
404 }
405
406 void wxStatusBarGeneric::SetMinHeight(int height)
407 {
408 // check that this min height is not less than minimal height for the
409 // current font
410 wxClientDC dc(this);
411 wxCoord y;
412 dc.GetTextExtent( _T("X"), NULL, &y );
413
414 if ( height > (11*y)/10 )
415 {
416 SetSize(-1, -1, -1, height + 2*m_borderY);
417 }
418 }
419
420 #endif // wxUSE_STATUSBAR