]> git.saurik.com Git - wxWidgets.git/blob - src/generic/statusbr.cpp
da95f833d9f3469845b92229cdf03b5082f55ec4
[wxWidgets.git] / src / generic / statusbr.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: statusbr.cpp
3 // Purpose: wxStatusBar 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 #ifndef WX_PRECOMP
24 #include "wx/setup.h"
25 #include "wx/frame.h"
26 #include "wx/settings.h"
27 #include "wx/dcclient.h"
28 #endif
29
30 #include "wx/generic/statusbr.h"
31
32 #ifdef __WXMSW__
33 #include <windows.h>
34
35 #ifdef DrawText
36 #undef DrawText
37 #endif
38
39 #endif
40
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_DYNAMIC_CLASS(wxStatusBar, wxWindow)
43
44 BEGIN_EVENT_TABLE(wxStatusBar, wxWindow)
45 EVT_PAINT(wxStatusBar::OnPaint)
46 EVT_SYS_COLOUR_CHANGED(wxStatusBar::OnSysColourChanged)
47 END_EVENT_TABLE()
48 #endif
49
50 // Default status border dimensions
51 #define wxTHICK_LINE_BORDER 2
52 #define wxTHICK_LINE_WIDTH 1
53
54 wxStatusBar::wxStatusBar(void)
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 wxStatusBar::~wxStatusBar(void)
64 {
65 // #ifdef __WXMSW__
66 SetFont(wxNullFont);
67 // #endif
68
69 if ( m_statusWidths )
70 delete[] m_statusWidths;
71 if ( m_statusStrings )
72 delete[] m_statusStrings;
73 }
74
75 bool wxStatusBar::Create(wxWindow *parent, wxWindowID id,
76 const wxPoint& pos,
77 const wxSize& size,
78 long style,
79 const wxString& name)
80 {
81 m_statusWidths = (int *) NULL;
82 m_statusStrings = (wxString *) NULL;
83 m_nFields = 0;
84 m_borderX = wxTHICK_LINE_BORDER;
85 m_borderY = wxTHICK_LINE_BORDER;
86
87 bool success = wxWindow::Create(parent, id, pos, size, style, name);
88
89 // Don't wish this to be found as a child
90 parent->GetChildren()->DeleteObject(this);
91
92 InitColours();
93
94 SetFont(m_defaultStatusBarFont);
95
96 return success;
97 }
98
99 void wxStatusBar::SetFieldsCount(int number, const int widths[])
100 {
101 m_nFields = number;
102
103 if ( m_statusWidths )
104 delete[] m_statusWidths;
105
106 if ( m_statusStrings )
107 delete[] m_statusStrings;
108
109 m_statusStrings = new wxString[number];
110
111 int i;
112 for (i = 0; i < number; i++)
113 m_statusStrings[i] = "";
114
115 if ( widths )
116 SetStatusWidths(number, widths);
117 }
118
119 void wxStatusBar::SetStatusText(const wxString& text, int number)
120 {
121 if ((number < 0) || (number >= m_nFields))
122 return;
123
124 m_statusStrings[number] = text;
125
126 Refresh();
127
128 #ifdef __WXMSW__
129 // For some reason, this can cause major GDI problems - graphics
130 // all over the place. E.g. in print previewing.
131 // ::UpdateWindow((HWND) GetHWND());
132 #endif
133 }
134
135 wxString wxStatusBar::GetStatusText(int n) const
136 {
137 if ((n < 0) || (n >= m_nFields))
138 return wxString("");
139 else
140 return m_statusStrings[n];
141 }
142
143 void wxStatusBar::SetStatusWidths(int n, const int widths_field[])
144 {
145 // only set status widths, when n == number of statuswindows
146 if (n == m_nFields)
147 {
148 // only set status widths,
149 // when one window (minimum) is variable (width <= 0)
150 bool is_variable = FALSE;
151 int i;
152 for (i = 0; i < m_nFields; i++)
153 {
154 if (widths_field[i] <= 0) is_variable = TRUE;
155 }
156
157 // if there are old widths, delete them
158 if (m_statusWidths)
159 delete [] m_statusWidths;
160
161 // set widths
162 m_statusWidths = new int[n];
163 for (i = 0; i < m_nFields; i++)
164 {
165 m_statusWidths[i] = widths_field[i];
166 }
167 }
168 }
169
170 void wxStatusBar::OnPaint(wxPaintEvent& WXUNUSED(event) )
171 {
172 wxPaintDC dc(this);
173
174 int i;
175 if ( GetFont() )
176 dc.SetFont(*GetFont());
177 dc.SetBackgroundMode(wxTRANSPARENT);
178
179 for ( i = 0; i < m_nFields; i ++ )
180 DrawField(dc, i);
181
182 dc.SetFont(wxNullFont);
183 }
184
185 void wxStatusBar::DrawFieldText(wxDC& dc, int i)
186 {
187 int leftMargin = 2;
188
189 wxRectangle rect;
190 GetFieldRect(i, rect);
191
192 wxString text(GetStatusText(i));
193
194 long x, y;
195
196 dc.GetTextExtent(text, &x, &y);
197
198 int xpos = rect.x + leftMargin;
199 int ypos = (int) (((rect.height - y) / 2 ) + rect.y + 0.5) ;
200
201 dc.SetClippingRegion(rect.x, rect.y, rect.width, rect.height);
202
203 dc.DrawText(text, xpos, ypos);
204
205 dc.DestroyClippingRegion();
206 }
207
208 void wxStatusBar::DrawField(wxDC& dc, int i)
209 {
210 wxRectangle rect;
211 GetFieldRect(i, rect);
212
213 // Draw border
214 // Have grey background, plus 3-d border -
215 // One black rectangle.
216 // Inside this, left and top sides - dark grey. Bottom and right -
217 // white.
218
219 dc.SetPen(m_hilightPen);
220
221 // Right and bottom white lines
222 dc.DrawLine(rect.x + rect.width, rect.y,
223 rect.x + rect.width, rect.y + rect.height);
224 dc.DrawLine(rect.x + rect.width, rect.y + rect.height,
225 rect.x, rect.y + rect.height);
226
227 dc.SetPen(m_mediumShadowPen);
228
229 // Left and top grey lines
230 dc.DrawLine(rect.x, rect.y + rect.height,
231 rect.x, rect.y);
232 dc.DrawLine(rect.x, rect.y,
233 rect.x + rect.width, rect.y);
234
235 DrawFieldText(dc, i);
236 }
237
238 // Get the position and size of the field's internal bounding rectangle
239 bool wxStatusBar::GetFieldRect(int n, wxRectangle& rect) const
240 {
241 if ((n < 0) || (n >= m_nFields))
242 return FALSE;
243
244 int width, height;
245 GetClientSize(&width, &height);
246
247 int i;
248 int sum_of_nonvar = 0;
249 int num_of_var = 0;
250 bool do_same_width = FALSE;
251
252 int fieldWidth = 0;
253 int fieldPosition = 0;
254
255 if (m_statusWidths)
256 {
257 // if sum(not variable Windows) > c_width - (20 points per variable_window)
258 // then do_same_width = TRUE;
259 for (i = 0; i < m_nFields; i++)
260 {
261 if (m_statusWidths[i] > 0) sum_of_nonvar += m_statusWidths[i];
262 else num_of_var++;
263 }
264 if (sum_of_nonvar > (width - 20*num_of_var)) do_same_width = TRUE;
265 }
266 else do_same_width = TRUE;
267 if (do_same_width)
268 {
269 for (i = 0; i < m_nFields; i++)
270 {
271 fieldWidth = (int)(width/m_nFields);
272 fieldPosition = i*fieldWidth;
273 if ( i == n )
274 break;
275 }
276 }
277 else // no_same_width
278 {
279 int *tempwidth = new int[m_nFields];
280 int temppos = 0;
281 for (i = 0; i < m_nFields; i++)
282 {
283 if (m_statusWidths[i] > 0) tempwidth[i] = m_statusWidths[i];
284 else tempwidth[i] = (width - sum_of_nonvar) / num_of_var;
285 }
286 for (i = 0; i < m_nFields; i++)
287 {
288 fieldWidth = tempwidth[i];
289 fieldPosition = temppos;
290
291 temppos += tempwidth[i];
292
293 if ( i == n )
294 break;
295 }
296 delete [] tempwidth;
297 }
298
299 rect.x = fieldPosition + wxTHICK_LINE_BORDER;
300 rect.y = wxTHICK_LINE_BORDER;
301
302 rect.width = fieldWidth - 2 * wxTHICK_LINE_BORDER ;
303 rect.height = height - 2 * wxTHICK_LINE_BORDER ;
304
305 return TRUE;
306 }
307
308 // Initialize colours
309 void wxStatusBar::InitColours(void)
310 {
311 // Shadow colours
312 #if defined(__WIN95__)
313 wxColour mediumShadowColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DSHADOW));
314 m_mediumShadowPen = wxPen(mediumShadowColour, 1, wxSOLID);
315
316 wxColour hilightColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DHILIGHT));
317 m_hilightPen = wxPen(hilightColour, 1, wxSOLID);
318 #else
319 m_mediumShadowPen = wxPen("GREY", 1, wxSOLID);
320 m_hilightPen = wxPen("WHITE", 1, wxSOLID);
321 #endif
322
323 m_defaultStatusBarFont = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
324 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
325 }
326
327 // Responds to colour changes, and passes event on to children.
328 void wxStatusBar::OnSysColourChanged(wxSysColourChangedEvent& event)
329 {
330 InitColours();
331 Refresh();
332
333 // Propagate the event to the non-top-level children
334 wxWindow::OnSysColourChanged(event);
335 }
336