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