]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
a71d815b | 2 | // Name: src/msw/scrolbar.cpp |
2bda0e17 KB |
3 | // Purpose: wxScrollBar |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
2bda0e17 KB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
1e6feb95 | 16 | #pragma hdrstop |
2bda0e17 KB |
17 | #endif |
18 | ||
1e6feb95 VZ |
19 | #if wxUSE_SCROLLBAR |
20 | ||
2bda0e17 | 21 | #ifndef WX_PRECOMP |
1e6feb95 | 22 | #include "wx/utils.h" |
2bda0e17 KB |
23 | #endif |
24 | ||
25 | #include "wx/scrolbar.h" | |
26 | #include "wx/msw/private.h" | |
98773c1c | 27 | #include "wx/settings.h" |
2bda0e17 | 28 | |
f0a126fe | 29 | #if wxUSE_EXTENDED_RTTI |
bc9fb572 JS |
30 | WX_DEFINE_FLAGS( wxScrollBarStyle ) |
31 | ||
3ff066a4 | 32 | wxBEGIN_FLAGS( wxScrollBarStyle ) |
bc9fb572 JS |
33 | // new style border flags, we put them first to |
34 | // use them for streaming out | |
3ff066a4 SC |
35 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
36 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
37 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
38 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
39 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
40 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
57f4f925 | 41 | |
bc9fb572 | 42 | // old style border flags |
3ff066a4 SC |
43 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
44 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
45 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
46 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
47 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 48 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
49 | |
50 | // standard window styles | |
3ff066a4 SC |
51 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
52 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
53 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
54 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 55 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
56 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
57 | wxFLAGS_MEMBER(wxVSCROLL) | |
58 | wxFLAGS_MEMBER(wxHSCROLL) | |
bc9fb572 | 59 | |
3ff066a4 SC |
60 | wxFLAGS_MEMBER(wxSB_HORIZONTAL) |
61 | wxFLAGS_MEMBER(wxSB_VERTICAL) | |
bc9fb572 | 62 | |
3ff066a4 | 63 | wxEND_FLAGS( wxScrollBarStyle ) |
bc9fb572 | 64 | |
f0a126fe SC |
65 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxScrollBar, wxControl,"wx/scrolbar.h") |
66 | ||
3ff066a4 | 67 | wxBEGIN_PROPERTIES_TABLE(wxScrollBar) |
cbc85508 | 68 | wxEVENT_RANGE_PROPERTY( Scroll , wxEVT_SCROLL_TOP , wxEVT_SCROLL_CHANGED , wxScrollEvent ) |
c5ca409b | 69 | |
3ff066a4 | 70 | wxPROPERTY( ThumbPosition , int , SetThumbPosition, GetThumbPosition, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
57f4f925 WS |
71 | wxPROPERTY( Range , int , SetRange, GetRange, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) |
72 | wxPROPERTY( ThumbSize , int , SetThumbSize, GetThumbSize, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
73 | wxPROPERTY( PageSize , int , SetPageSize, GetPageSize, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
af498247 | 74 | wxPROPERTY_FLAGS( WindowStyle , wxScrollBarStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
3ff066a4 | 75 | wxEND_PROPERTIES_TABLE() |
2bda0e17 | 76 | |
3ff066a4 SC |
77 | wxBEGIN_HANDLERS_TABLE(wxScrollBar) |
78 | wxEND_HANDLERS_TABLE() | |
066f1b7a | 79 | |
3ff066a4 | 80 | wxCONSTRUCTOR_5( wxScrollBar , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
f0a126fe SC |
81 | #else |
82 | IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl) | |
83 | #endif | |
2bda0e17 KB |
84 | |
85 | // Scrollbar | |
debe6624 | 86 | bool wxScrollBar::Create(wxWindow *parent, wxWindowID id, |
2bda0e17 | 87 | const wxPoint& pos, |
debe6624 | 88 | const wxSize& size, long style, |
720afa24 | 89 | const wxValidator& validator, |
2bda0e17 KB |
90 | const wxString& name) |
91 | { | |
720afa24 | 92 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
57f4f925 | 93 | return false; |
33ac7e6f | 94 | |
720afa24 DS |
95 | if (!MSWCreateControl(wxT("ScrollBar"), wxEmptyString, pos, size)) |
96 | return false; | |
2bda0e17 | 97 | |
720afa24 | 98 | SetScrollbar(0, 1, 2, 1, false); |
2bda0e17 | 99 | |
57f4f925 | 100 | return true; |
2bda0e17 KB |
101 | } |
102 | ||
103 | wxScrollBar::~wxScrollBar(void) | |
104 | { | |
105 | } | |
106 | ||
a23fd0e1 | 107 | bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam, |
2eb10e2a | 108 | WXWORD pos, WXHWND WXUNUSED(control)) |
2bda0e17 | 109 | { |
2b5f62a0 VZ |
110 | // current and max positions |
111 | int position, | |
112 | maxPos, trackPos = pos; | |
113 | ||
5cb598ae WS |
114 | wxUnusedVar(trackPos); |
115 | ||
2b5f62a0 VZ |
116 | // when we're dragging the scrollbar we can't use pos parameter because it |
117 | // is limited to 16 bits | |
4676948b JS |
118 | // JACS: now always using GetScrollInfo, since there's no reason |
119 | // not to | |
120 | // if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK ) | |
2b5f62a0 VZ |
121 | { |
122 | SCROLLINFO scrollInfo; | |
123 | wxZeroMemory(scrollInfo); | |
124 | scrollInfo.cbSize = sizeof(SCROLLINFO); | |
125 | ||
126 | // also get the range if we call GetScrollInfo() anyhow -- this is less | |
127 | // expensive than call it once here and then call GetScrollRange() | |
128 | // below | |
129 | scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS; | |
130 | ||
131 | if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) ) | |
132 | { | |
133 | wxLogLastError(_T("GetScrollInfo")); | |
134 | } | |
135 | ||
136 | trackPos = scrollInfo.nTrackPos; | |
137 | position = scrollInfo.nPos; | |
138 | maxPos = scrollInfo.nMax; | |
139 | } | |
4676948b | 140 | #if 0 |
2b5f62a0 | 141 | else |
2b5f62a0 VZ |
142 | { |
143 | position = ::GetScrollPos((HWND) control, SB_CTL); | |
144 | int minPos; | |
145 | ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos); | |
146 | } | |
4676948b | 147 | #endif |
a23fd0e1 | 148 | |
a23fd0e1 VZ |
149 | // A page size greater than one has the effect of reducing the effective |
150 | // range, therefore the range has already been boosted artificially - so | |
151 | // reduce it again. | |
152 | if ( m_pageSize > 1 ) | |
153 | maxPos -= (m_pageSize - 1); | |
2bda0e17 | 154 | |
7798a18e | 155 | wxEventType scrollEvent = wxEVT_NULL; |
2bda0e17 KB |
156 | |
157 | int nScrollInc; | |
158 | switch ( wParam ) | |
159 | { | |
a29ffc34 | 160 | case SB_BOTTOM: |
a23fd0e1 VZ |
161 | nScrollInc = maxPos - position; |
162 | scrollEvent = wxEVT_SCROLL_TOP; | |
163 | break; | |
164 | ||
a29ffc34 VZ |
165 | case SB_TOP: |
166 | nScrollInc = -position; | |
a23fd0e1 VZ |
167 | scrollEvent = wxEVT_SCROLL_BOTTOM; |
168 | break; | |
169 | ||
170 | case SB_LINEUP: | |
171 | nScrollInc = -1; | |
172 | scrollEvent = wxEVT_SCROLL_LINEUP; | |
173 | break; | |
174 | ||
175 | case SB_LINEDOWN: | |
176 | nScrollInc = 1; | |
177 | scrollEvent = wxEVT_SCROLL_LINEDOWN; | |
178 | break; | |
179 | ||
180 | case SB_PAGEUP: | |
181 | nScrollInc = -GetPageSize(); | |
182 | scrollEvent = wxEVT_SCROLL_PAGEUP; | |
183 | break; | |
184 | ||
185 | case SB_PAGEDOWN: | |
186 | nScrollInc = GetPageSize(); | |
187 | scrollEvent = wxEVT_SCROLL_PAGEDOWN; | |
188 | break; | |
189 | ||
a23fd0e1 | 190 | case SB_THUMBPOSITION: |
2b5f62a0 | 191 | nScrollInc = trackPos - position; |
7d56fb8f GRG |
192 | scrollEvent = wxEVT_SCROLL_THUMBRELEASE; |
193 | break; | |
feda3011 GRG |
194 | |
195 | case SB_THUMBTRACK: | |
2b5f62a0 | 196 | nScrollInc = trackPos - position; |
a23fd0e1 VZ |
197 | scrollEvent = wxEVT_SCROLL_THUMBTRACK; |
198 | break; | |
199 | ||
e8b669d3 VZ |
200 | case SB_ENDSCROLL: |
201 | nScrollInc = 0; | |
cbc85508 | 202 | scrollEvent = wxEVT_SCROLL_CHANGED; |
e8b669d3 VZ |
203 | break; |
204 | ||
a23fd0e1 VZ |
205 | default: |
206 | nScrollInc = 0; | |
2bda0e17 KB |
207 | } |
208 | ||
e8b669d3 | 209 | if ( nScrollInc ) |
2bda0e17 | 210 | { |
e8b669d3 | 211 | position += nScrollInc; |
2bda0e17 | 212 | |
e8b669d3 VZ |
213 | if ( position < 0 ) |
214 | position = 0; | |
215 | if ( position > maxPos ) | |
216 | position = maxPos; | |
a23fd0e1 | 217 | |
e8b669d3 VZ |
218 | SetThumbPosition(position); |
219 | } | |
220 | else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE && | |
cbc85508 | 221 | scrollEvent != wxEVT_SCROLL_CHANGED ) |
e8b669d3 VZ |
222 | { |
223 | // don't process the event if there is no displacement, | |
224 | // unless this is a thumb release or end scroll event. | |
57f4f925 | 225 | return false; |
e8b669d3 | 226 | } |
a23fd0e1 | 227 | |
a23fd0e1 | 228 | wxScrollEvent event(scrollEvent, m_windowId); |
27e229f5 | 229 | event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL); |
e8b669d3 | 230 | event.SetPosition(position); |
a23fd0e1 VZ |
231 | event.SetEventObject( this ); |
232 | ||
233 | return GetEventHandler()->ProcessEvent(event); | |
2bda0e17 KB |
234 | } |
235 | ||
4fabb575 | 236 | void wxScrollBar::SetThumbPosition(int viewStart) |
2bda0e17 | 237 | { |
2b5f62a0 VZ |
238 | SCROLLINFO info; |
239 | info.cbSize = sizeof(SCROLLINFO); | |
240 | info.nPage = 0; | |
241 | info.nMin = 0; | |
242 | info.nPos = viewStart; | |
243 | info.fMask = SIF_POS ; | |
244 | ||
245 | ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE); | |
2bda0e17 KB |
246 | } |
247 | ||
4fabb575 | 248 | int wxScrollBar::GetThumbPosition(void) const |
2bda0e17 | 249 | { |
4676948b JS |
250 | SCROLLINFO scrollInfo; |
251 | wxZeroMemory(scrollInfo); | |
252 | scrollInfo.cbSize = sizeof(SCROLLINFO); | |
253 | scrollInfo.fMask = SIF_POS; | |
f0a126fe | 254 | |
4676948b JS |
255 | if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) ) |
256 | { | |
257 | wxLogLastError(_T("GetScrollInfo")); | |
258 | } | |
259 | return scrollInfo.nPos; | |
260 | // return ::GetScrollPos((HWND)m_hWnd, SB_CTL); | |
2bda0e17 KB |
261 | } |
262 | ||
debe6624 JS |
263 | void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize, |
264 | bool refresh) | |
2bda0e17 | 265 | { |
a71d815b WS |
266 | m_viewSize = pageSize; |
267 | m_pageSize = thumbSize; | |
268 | m_objectSize = range; | |
269 | ||
270 | // The range (number of scroll steps) is the | |
271 | // object length minus the page size. | |
272 | int range1 = wxMax((m_objectSize - m_pageSize), 0) ; | |
273 | ||
274 | // Try to adjust the range to cope with page size > 1 | |
275 | // (see comment for SetPageLength) | |
276 | if ( m_pageSize > 1 ) | |
277 | { | |
278 | range1 += (m_pageSize - 1); | |
279 | } | |
280 | ||
281 | SCROLLINFO info; | |
282 | info.cbSize = sizeof(SCROLLINFO); | |
283 | info.nPage = m_pageSize; | |
284 | info.nMin = 0; | |
285 | info.nMax = range1; | |
286 | info.nPos = position; | |
287 | ||
288 | info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS; | |
289 | ||
290 | ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh); | |
2bda0e17 KB |
291 | } |
292 | ||
2bda0e17 KB |
293 | void wxScrollBar::Command(wxCommandEvent& event) |
294 | { | |
687706f5 | 295 | SetThumbPosition(event.GetInt()); |
2bda0e17 KB |
296 | ProcessCommand(event); |
297 | } | |
298 | ||
caafd082 DS |
299 | wxSize wxScrollBar::DoGetBestSize() const |
300 | { | |
301 | int w = 100; | |
302 | int h = 100; | |
303 | ||
304 | if ( IsVertical() ) | |
305 | { | |
306 | w = wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
307 | } | |
308 | else | |
309 | { | |
310 | h = wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y); | |
311 | } | |
312 | ||
31582e4e RD |
313 | wxSize best(w, h); |
314 | CacheBestSize(best); | |
315 | return best; | |
caafd082 DS |
316 | } |
317 | ||
720afa24 DS |
318 | WXDWORD wxScrollBar::MSWGetStyle(long style, WXDWORD *exstyle) const |
319 | { | |
320 | // we never have an external border | |
321 | WXDWORD msStyle = wxControl::MSWGetStyle | |
322 | ( | |
323 | (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle | |
324 | ); | |
325 | ||
326 | // SBS_HORZ is 0 anyhow, but do mention it explicitly for clarity | |
327 | msStyle |= style & wxSB_HORIZONTAL ? SBS_HORZ : SBS_VERT; | |
328 | ||
329 | return msStyle; | |
330 | } | |
331 | ||
0cf11995 | 332 | WXHBRUSH wxScrollBar::MSWControlColor(WXHDC pDC, WXHWND hWnd) |
123865f2 | 333 | { |
0cf11995 VZ |
334 | // unless we have an explicitly set bg colour, use default (gradient under |
335 | // XP) brush instead of GetBackgroundColour() one as the base class would | |
336 | // | |
337 | // note that fg colour isn't used for a scrollbar | |
338 | return UseBgCol() ? wxControl::MSWControlColor(pDC, hWnd) : NULL; | |
123865f2 JS |
339 | } |
340 | ||
1e6feb95 | 341 | #endif // wxUSE_SCROLLBAR |