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