]> git.saurik.com Git - wxWidgets.git/blame - src/msw/scrolbar.cpp
XTI extensions
[wxWidgets.git] / src / msw / scrolbar.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: 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
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
1e6feb95 13 #pragma implementation "scrolbar.h"
2bda0e17
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
1e6feb95 20 #pragma hdrstop
2bda0e17
KB
21#endif
22
1e6feb95
VZ
23#if wxUSE_SCROLLBAR
24
2bda0e17 25#ifndef WX_PRECOMP
1e6feb95 26 #include "wx/utils.h"
2bda0e17
KB
27#endif
28
29#include "wx/scrolbar.h"
30#include "wx/msw/private.h"
31
2bda0e17
KB
32IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
33
066f1b7a
SC
34/*
35 TODO PROPERTIES
36 value (long,0)
37 thumbsize(long,1)
38 range( long , 10 )
39 pagesize( long , 1)
40*/
41
2bda0e17
KB
42
43// Scrollbar
debe6624 44bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
2bda0e17 45 const wxPoint& pos,
debe6624 46 const wxSize& size, long style,
2bda0e17
KB
47 const wxValidator& validator,
48 const wxString& name)
49{
50 if (!parent)
51 return FALSE;
52 parent->AddChild(this);
53 SetName(name);
11b6a93b 54#if wxUSE_VALIDATORS
a23fd0e1 55 SetValidator(validator);
11b6a93b 56#endif // wxUSE_VALIDATORS
33ac7e6f 57
7051e6c7
JS
58 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
59 style |= wxNO_BORDER;
60
fd71308f
JS
61 SetBackgroundColour(parent->GetBackgroundColour()) ;
62 SetForegroundColour(parent->GetForegroundColour()) ;
2bda0e17
KB
63 m_windowStyle = style;
64
65 if ( id == -1 )
a23fd0e1 66 m_windowId = (int)NewControlId();
2bda0e17 67 else
a23fd0e1 68 m_windowId = id;
2bda0e17
KB
69
70 int x = pos.x;
71 int y = pos.y;
72 int width = size.x;
73 int height = size.y;
74
75 if (width == -1)
76 {
77 if (style & wxHORIZONTAL)
78 width = 140;
79 else
80 width = 14;
81 }
82 if (height == -1)
83 {
84 if (style & wxVERTICAL)
85 height = 140;
86 else
87 height = 14;
88 }
89
fe3d9123
JS
90 WXDWORD exStyle = 0;
91 WXDWORD wstyle = MSWGetStyle(style, & exStyle) ;
b0766406 92
2bda0e17
KB
93 // Now create scrollbar
94 DWORD _direction = (style & wxHORIZONTAL) ?
95 SBS_HORZ: SBS_VERT;
fe3d9123 96 HWND scroll_bar = CreateWindowEx(exStyle, wxT("SCROLLBAR"), wxT("scrollbar"),
b0766406 97 _direction | wstyle,
2bda0e17
KB
98 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
99 wxGetInstance(), NULL);
100
101 m_pageSize = 1;
102 m_viewSize = 1;
103 m_objectSize = 1;
104
105 ::SetScrollRange(scroll_bar, SB_CTL, 0, 1, FALSE);
106 ::SetScrollPos(scroll_bar, SB_CTL, 0, FALSE);
107 ShowWindow(scroll_bar, SW_SHOW);
108
c0ed460c 109 SetFont(parent->GetFont());
1c089c47 110
2bda0e17
KB
111 m_hWnd = (WXHWND)scroll_bar;
112
113 // Subclass again for purposes of dialog editing mode
114 SubclassWin((WXHWND) scroll_bar);
115
116 SetSize(x, y, width, height);
117
118 return TRUE;
119}
120
121wxScrollBar::~wxScrollBar(void)
122{
123}
124
a23fd0e1 125bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
2eb10e2a 126 WXWORD pos, WXHWND WXUNUSED(control))
2bda0e17 127{
2b5f62a0
VZ
128 // current and max positions
129 int position,
130 maxPos, trackPos = pos;
131
2b5f62a0
VZ
132 // when we're dragging the scrollbar we can't use pos parameter because it
133 // is limited to 16 bits
4676948b
JS
134 // JACS: now always using GetScrollInfo, since there's no reason
135 // not to
136// if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
2b5f62a0
VZ
137 {
138 SCROLLINFO scrollInfo;
139 wxZeroMemory(scrollInfo);
140 scrollInfo.cbSize = sizeof(SCROLLINFO);
141
142 // also get the range if we call GetScrollInfo() anyhow -- this is less
143 // expensive than call it once here and then call GetScrollRange()
144 // below
145 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS;
146
147 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
148 {
149 wxLogLastError(_T("GetScrollInfo"));
150 }
151
152 trackPos = scrollInfo.nTrackPos;
153 position = scrollInfo.nPos;
154 maxPos = scrollInfo.nMax;
155 }
4676948b 156#if 0
2b5f62a0 157 else
2b5f62a0
VZ
158 {
159 position = ::GetScrollPos((HWND) control, SB_CTL);
160 int minPos;
161 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
162 }
4676948b 163#endif
a23fd0e1 164
2bda0e17 165#if defined(__WIN95__)
a23fd0e1
VZ
166 // A page size greater than one has the effect of reducing the effective
167 // range, therefore the range has already been boosted artificially - so
168 // reduce it again.
169 if ( m_pageSize > 1 )
170 maxPos -= (m_pageSize - 1);
171#endif // __WIN95__
2bda0e17 172
7798a18e 173 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
174
175 int nScrollInc;
176 switch ( wParam )
177 {
a29ffc34 178 case SB_BOTTOM:
a23fd0e1
VZ
179 nScrollInc = maxPos - position;
180 scrollEvent = wxEVT_SCROLL_TOP;
181 break;
182
a29ffc34
VZ
183 case SB_TOP:
184 nScrollInc = -position;
a23fd0e1
VZ
185 scrollEvent = wxEVT_SCROLL_BOTTOM;
186 break;
187
188 case SB_LINEUP:
189 nScrollInc = -1;
190 scrollEvent = wxEVT_SCROLL_LINEUP;
191 break;
192
193 case SB_LINEDOWN:
194 nScrollInc = 1;
195 scrollEvent = wxEVT_SCROLL_LINEDOWN;
196 break;
197
198 case SB_PAGEUP:
199 nScrollInc = -GetPageSize();
200 scrollEvent = wxEVT_SCROLL_PAGEUP;
201 break;
202
203 case SB_PAGEDOWN:
204 nScrollInc = GetPageSize();
205 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
206 break;
207
a23fd0e1 208 case SB_THUMBPOSITION:
2b5f62a0 209 nScrollInc = trackPos - position;
7d56fb8f
GRG
210 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
211 break;
feda3011
GRG
212
213 case SB_THUMBTRACK:
2b5f62a0 214 nScrollInc = trackPos - position;
a23fd0e1
VZ
215 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
216 break;
217
e8b669d3
VZ
218 case SB_ENDSCROLL:
219 nScrollInc = 0;
220 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
221 break;
222
a23fd0e1
VZ
223 default:
224 nScrollInc = 0;
2bda0e17
KB
225 }
226
e8b669d3 227 if ( nScrollInc )
2bda0e17 228 {
e8b669d3 229 position += nScrollInc;
2bda0e17 230
e8b669d3
VZ
231 if ( position < 0 )
232 position = 0;
233 if ( position > maxPos )
234 position = maxPos;
a23fd0e1 235
e8b669d3
VZ
236 SetThumbPosition(position);
237 }
238 else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
239 scrollEvent != wxEVT_SCROLL_ENDSCROLL )
240 {
241 // don't process the event if there is no displacement,
242 // unless this is a thumb release or end scroll event.
243 return FALSE;
244 }
a23fd0e1 245
a23fd0e1 246 wxScrollEvent event(scrollEvent, m_windowId);
27e229f5 247 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
e8b669d3 248 event.SetPosition(position);
a23fd0e1
VZ
249 event.SetEventObject( this );
250
251 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
252}
253
4fabb575 254void wxScrollBar::SetThumbPosition(int viewStart)
2bda0e17
KB
255{
256#if defined(__WIN95__)
2b5f62a0
VZ
257 SCROLLINFO info;
258 info.cbSize = sizeof(SCROLLINFO);
259 info.nPage = 0;
260 info.nMin = 0;
261 info.nPos = viewStart;
262 info.fMask = SIF_POS ;
263
264 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
2bda0e17 265#else
2b5f62a0 266 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
2bda0e17
KB
267#endif
268}
269
4fabb575 270int wxScrollBar::GetThumbPosition(void) const
2bda0e17 271{
4676948b
JS
272 SCROLLINFO scrollInfo;
273 wxZeroMemory(scrollInfo);
274 scrollInfo.cbSize = sizeof(SCROLLINFO);
275 scrollInfo.fMask = SIF_POS;
276
277 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
278 {
279 wxLogLastError(_T("GetScrollInfo"));
280 }
281 return scrollInfo.nPos;
282// return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
2bda0e17
KB
283}
284
debe6624
JS
285void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
286 bool refresh)
2bda0e17
KB
287{
288 m_viewSize = pageSize;
289 m_pageSize = thumbSize;
290 m_objectSize = range;
291
292 // The range (number of scroll steps) is the
293 // object length minus the page size.
294 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
295
296#if defined(__WIN95__)
297 // Try to adjust the range to cope with page size > 1
298 // (see comment for SetPageLength)
299 if ( m_pageSize > 1 )
300 {
a23fd0e1 301 range1 += (m_pageSize - 1);
2bda0e17
KB
302 }
303
304 SCROLLINFO info;
305 info.cbSize = sizeof(SCROLLINFO);
306 info.nPage = m_pageSize;
307 info.nMin = 0;
308 info.nMax = range1;
309 info.nPos = position;
310
311 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
312
313 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
314#else
315 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE);
316 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE);
317#endif
318}
319
320
33ac7e6f
KB
321WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
322 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
2bda0e17
KB
323{
324 return 0;
325}
326
327void wxScrollBar::Command(wxCommandEvent& event)
328{
ca5e9f67 329 SetThumbPosition(event.m_commandInt);
2bda0e17
KB
330 ProcessCommand(event);
331}
332
1e6feb95 333#endif // wxUSE_SCROLLBAR