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