]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: scrolbar.cpp | |
3 | // Purpose: wxScrollBar | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "scrolbar.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/defs.h" | |
25 | #include "wx/utils.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/scrolbar.h" | |
29 | #include "wx/msw/private.h" | |
30 | ||
31 | // extern wxList wxScrollBarList; | |
32 | extern void wxFindMaxSize(HWND hwnd, RECT *rect); | |
33 | ||
34 | #if !USE_SHARED_LIBRARY | |
35 | IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl) | |
36 | ||
37 | BEGIN_EVENT_TABLE(wxScrollBar, wxControl) | |
38 | #if WXWIN_COMPATIBILITY | |
39 | EVT_SCROLL(wxScrollBar::OnScroll) | |
40 | #endif | |
41 | END_EVENT_TABLE() | |
42 | ||
43 | #endif | |
44 | ||
45 | // Scrollbar | |
46 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, | |
47 | const wxPoint& pos, | |
48 | const wxSize& size, long style, | |
49 | const wxValidator& validator, | |
50 | const wxString& name) | |
51 | { | |
52 | if (!parent) | |
53 | return FALSE; | |
54 | parent->AddChild(this); | |
55 | SetName(name); | |
56 | SetValidator(validator); | |
57 | ||
58 | SetBackgroundColour(parent->GetBackgroundColour()) ; | |
59 | SetForegroundColour(parent->GetForegroundColour()) ; | |
60 | m_windowStyle = style; | |
61 | ||
62 | if ( id == -1 ) | |
63 | m_windowId = (int)NewControlId(); | |
64 | else | |
65 | m_windowId = id; | |
66 | ||
67 | int x = pos.x; | |
68 | int y = pos.y; | |
69 | int width = size.x; | |
70 | int height = size.y; | |
71 | ||
72 | if (width == -1) | |
73 | { | |
74 | if (style & wxHORIZONTAL) | |
75 | width = 140; | |
76 | else | |
77 | width = 14; | |
78 | } | |
79 | if (height == -1) | |
80 | { | |
81 | if (style & wxVERTICAL) | |
82 | height = 140; | |
83 | else | |
84 | height = 14; | |
85 | } | |
86 | ||
87 | // Now create scrollbar | |
88 | DWORD _direction = (style & wxHORIZONTAL) ? | |
89 | SBS_HORZ: SBS_VERT; | |
90 | HWND scroll_bar = CreateWindowEx(MakeExtendedStyle(style), "SCROLLBAR", "scrollbar", | |
91 | _direction | WS_CHILD | WS_VISIBLE, | |
92 | 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId, | |
93 | wxGetInstance(), NULL); | |
94 | ||
95 | m_pageSize = 1; | |
96 | m_viewSize = 1; | |
97 | m_objectSize = 1; | |
98 | ||
99 | ::SetScrollRange(scroll_bar, SB_CTL, 0, 1, FALSE); | |
100 | ::SetScrollPos(scroll_bar, SB_CTL, 0, FALSE); | |
101 | ShowWindow(scroll_bar, SW_SHOW); | |
102 | ||
103 | SetFont(parent->GetFont()); | |
104 | ||
105 | m_hWnd = (WXHWND)scroll_bar; | |
106 | ||
107 | // Subclass again for purposes of dialog editing mode | |
108 | SubclassWin((WXHWND) scroll_bar); | |
109 | ||
110 | SetSize(x, y, width, height); | |
111 | ||
112 | return TRUE; | |
113 | } | |
114 | ||
115 | wxScrollBar::~wxScrollBar(void) | |
116 | { | |
117 | } | |
118 | ||
119 | void wxScrollBar::MSWOnVScroll(WXWORD wParam, WXWORD pos, WXHWND control) | |
120 | { | |
121 | int position = ::GetScrollPos((HWND) control, SB_CTL); | |
122 | int minPos, maxPos; | |
123 | ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos); | |
124 | #if defined(__WIN95__) | |
125 | // A page size greater than one has the effect of reducing the | |
126 | // effective range, therefore the range has already been | |
127 | // boosted artificially - so reduce it again. | |
128 | if ( m_pageSize > 1 ) | |
129 | maxPos -= (m_pageSize - 1); | |
130 | #endif | |
131 | ||
132 | wxEventType scrollEvent = wxEVT_NULL; | |
133 | ||
134 | int nScrollInc; | |
135 | switch ( wParam ) | |
136 | { | |
137 | case SB_TOP: | |
138 | nScrollInc = maxPos - position; | |
139 | scrollEvent = wxEVT_SCROLL_TOP; | |
140 | break; | |
141 | ||
142 | case SB_BOTTOM: | |
143 | nScrollInc = - position; | |
144 | scrollEvent = wxEVT_SCROLL_BOTTOM; | |
145 | break; | |
146 | ||
147 | case SB_LINEUP: | |
148 | nScrollInc = -1; | |
149 | scrollEvent = wxEVT_SCROLL_LINEUP; | |
150 | break; | |
151 | ||
152 | case SB_LINEDOWN: | |
153 | nScrollInc = 1; | |
154 | scrollEvent = wxEVT_SCROLL_LINEDOWN; | |
155 | break; | |
156 | ||
157 | case SB_PAGEUP: | |
158 | nScrollInc = -GetPageSize(); | |
159 | scrollEvent = wxEVT_SCROLL_PAGEUP; | |
160 | break; | |
161 | ||
162 | case SB_PAGEDOWN: | |
163 | nScrollInc = GetPageSize(); | |
164 | scrollEvent = wxEVT_SCROLL_PAGEDOWN; | |
165 | break; | |
166 | ||
167 | case SB_THUMBTRACK: | |
168 | case SB_THUMBPOSITION: | |
169 | nScrollInc = pos - position; | |
170 | scrollEvent = wxEVT_SCROLL_THUMBTRACK; | |
171 | break; | |
172 | ||
173 | default: | |
174 | nScrollInc = 0; | |
175 | } | |
176 | ||
177 | if (nScrollInc != 0) | |
178 | { | |
179 | int new_pos = position + nScrollInc; | |
180 | ||
181 | if (new_pos < 0) | |
182 | new_pos = 0; | |
183 | if (new_pos > maxPos) | |
184 | new_pos = maxPos; | |
185 | ||
186 | SetThumbPosition(new_pos); | |
187 | wxScrollEvent event(scrollEvent, m_windowId); | |
188 | event.SetPosition(new_pos); | |
189 | event.SetEventObject( this ); | |
190 | GetEventHandler()->ProcessEvent(event); | |
191 | } | |
192 | } | |
193 | ||
194 | void wxScrollBar::MSWOnHScroll(WXWORD wParam, WXWORD pos, WXHWND control) | |
195 | { | |
196 | MSWOnVScroll(wParam, pos, control); | |
197 | } | |
198 | ||
199 | void wxScrollBar::SetThumbPosition(int viewStart) | |
200 | { | |
201 | #if defined(__WIN95__) | |
202 | SCROLLINFO info; | |
203 | info.cbSize = sizeof(SCROLLINFO); | |
204 | info.nPage = 0; | |
205 | info.nMin = 0; | |
206 | info.nPos = viewStart; | |
207 | info.fMask = SIF_POS ; | |
208 | ||
209 | ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE); | |
210 | #else | |
211 | ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE); | |
212 | #endif | |
213 | } | |
214 | ||
215 | int wxScrollBar::GetThumbPosition(void) const | |
216 | { | |
217 | return ::GetScrollPos((HWND)m_hWnd, SB_CTL); | |
218 | } | |
219 | ||
220 | void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, | |
221 | bool refresh) | |
222 | { | |
223 | m_viewSize = pageSize; | |
224 | m_pageSize = thumbSize; | |
225 | m_objectSize = range; | |
226 | ||
227 | // The range (number of scroll steps) is the | |
228 | // object length minus the page size. | |
229 | int range1 = wxMax((m_objectSize - m_pageSize), 0) ; | |
230 | ||
231 | #if defined(__WIN95__) | |
232 | // Try to adjust the range to cope with page size > 1 | |
233 | // (see comment for SetPageLength) | |
234 | if ( m_pageSize > 1 ) | |
235 | { | |
236 | range1 += (m_pageSize - 1); | |
237 | } | |
238 | ||
239 | SCROLLINFO info; | |
240 | info.cbSize = sizeof(SCROLLINFO); | |
241 | info.nPage = m_pageSize; | |
242 | info.nMin = 0; | |
243 | info.nMax = range1; | |
244 | info.nPos = position; | |
245 | ||
246 | info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS; | |
247 | ||
248 | ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh); | |
249 | #else | |
250 | ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE); | |
251 | ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE); | |
252 | #endif | |
253 | } | |
254 | ||
255 | ||
256 | /* From the WIN32 documentation: | |
257 | In version 4.0 or later, the maximum value that a scroll bar can report | |
258 | (that is, the maximum scrolling position) depends on the page size. | |
259 | If the scroll bar has a page size greater than one, the maximum scrolling position | |
260 | is less than the maximum range value. You can use the following formula to calculate | |
261 | the maximum scrolling position: | |
262 | ||
263 | MaxScrollPos = MaxRangeValue - (PageSize - 1) | |
264 | */ | |
265 | ||
266 | #if WXWIN_COMPATIBILITY | |
267 | void wxScrollBar::SetPageSize(int pageLength) | |
268 | { | |
269 | m_pageSize = pageLength; | |
270 | ||
271 | #if defined(__WIN95__) | |
272 | SCROLLINFO info; | |
273 | info.cbSize = sizeof(SCROLLINFO); | |
274 | info.nPage = pageLength; | |
275 | info.fMask = SIF_PAGE ; | |
276 | ||
277 | ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE); | |
278 | #endif | |
279 | } | |
280 | ||
281 | void wxScrollBar::SetObjectLength(int objectLength) | |
282 | { | |
283 | m_objectSize = objectLength; | |
284 | ||
285 | // The range (number of scroll steps) is the | |
286 | // object length minus the view size. | |
287 | int range = wxMax((objectLength - m_viewSize), 0) ; | |
288 | ||
289 | #if defined(__WIN95__) | |
290 | // Try to adjust the range to cope with page size > 1 | |
291 | // (see comment for SetPageLength) | |
292 | if ( m_pageSize > 1 ) | |
293 | { | |
294 | range += (m_pageSize - 1); | |
295 | } | |
296 | ||
297 | SCROLLINFO info; | |
298 | info.cbSize = sizeof(SCROLLINFO); | |
299 | info.nPage = 0; | |
300 | info.nMin = 0; | |
301 | info.nMax = range; | |
302 | info.nPos = 0; | |
303 | info.fMask = SIF_RANGE ; | |
304 | ||
305 | ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE); | |
306 | #else | |
307 | ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range, TRUE); | |
308 | #endif | |
309 | } | |
310 | ||
311 | void wxScrollBar::SetViewLength(int viewLength) | |
312 | { | |
313 | m_viewSize = viewLength; | |
314 | } | |
315 | ||
316 | void wxScrollBar::GetValues(int *viewStart, int *viewLength, int *objectLength, | |
317 | int *pageLength) const | |
318 | { | |
319 | *viewStart = ::GetScrollPos((HWND)m_hWnd, SB_CTL); | |
320 | *viewLength = m_viewSize; | |
321 | *objectLength = m_objectSize; | |
322 | *pageLength = m_pageSize; | |
323 | } | |
324 | #endif | |
325 | ||
326 | WXHBRUSH wxScrollBar::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, | |
327 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) | |
328 | { | |
329 | return 0; | |
330 | } | |
331 | ||
332 | void wxScrollBar::Command(wxCommandEvent& event) | |
333 | { | |
334 | SetThumbPosition(event.m_commandInt); | |
335 | ProcessCommand(event); | |
336 | } | |
337 | ||
338 | #if WXWIN_COMPATIBILITY | |
339 | // Backward compatibility | |
340 | void wxScrollBar::OnScroll(wxScrollEvent& event) | |
341 | { | |
342 | wxEventType oldEvent = event.GetEventType(); | |
343 | event.SetEventType( wxEVT_COMMAND_SCROLLBAR_UPDATED ); | |
344 | if ( !GetEventHandler()->ProcessEvent(event) ) | |
345 | { | |
346 | event.SetEventType( oldEvent ); | |
347 | if (!GetParent()->GetEventHandler()->ProcessEvent(event)) | |
348 | event.Skip(); | |
349 | } | |
350 | } | |
351 | #endif |