]> git.saurik.com Git - wxWidgets.git/blob - src/msw/scrolbar.cpp
7eaa272adb94f424101c6cb067502f01ff156c16
[wxWidgets.git] / src / msw / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #if wxUSE_SCROLLBAR
24
25 #ifndef WX_PRECOMP
26 #include "wx/utils.h"
27 #endif
28
29 #include "wx/scrolbar.h"
30 #include "wx/msw/private.h"
31
32 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
33
34 /*
35 TODO PROPERTIES
36 value (long,0)
37 thumbsize(long,1)
38 range( long , 10 )
39 pagesize( long , 1)
40 */
41
42
43 // Scrollbar
44 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
45 const wxPoint& pos,
46 const wxSize& size, long style,
47 const wxValidator& validator,
48 const wxString& name)
49 {
50 if (!parent)
51 return FALSE;
52 parent->AddChild(this);
53 SetName(name);
54 #if wxUSE_VALIDATORS
55 SetValidator(validator);
56 #endif // wxUSE_VALIDATORS
57
58 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
59 style |= wxNO_BORDER;
60
61 SetBackgroundColour(parent->GetBackgroundColour()) ;
62 SetForegroundColour(parent->GetForegroundColour()) ;
63 m_windowStyle = style;
64
65 if ( id == -1 )
66 m_windowId = (int)NewControlId();
67 else
68 m_windowId = id;
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
90 WXDWORD exStyle = 0;
91 WXDWORD wstyle = MSWGetStyle(style, & exStyle) ;
92
93 // Now create scrollbar
94 DWORD _direction = (style & wxHORIZONTAL) ?
95 SBS_HORZ: SBS_VERT;
96 HWND scroll_bar = CreateWindowEx(exStyle, wxT("SCROLLBAR"), wxT("scrollbar"),
97 _direction | wstyle,
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
109 SetFont(parent->GetFont());
110
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
121 wxScrollBar::~wxScrollBar(void)
122 {
123 }
124
125 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
126 WXWORD pos, WXHWND WXUNUSED(control))
127 {
128 // current and max positions
129 int position,
130 maxPos, trackPos = pos;
131
132 // when we're dragging the scrollbar we can't use pos parameter because it
133 // is limited to 16 bits
134 // JACS: now always using GetScrollInfo, since there's no reason
135 // not to
136 // if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
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 }
156 #if 0
157 else
158 {
159 position = ::GetScrollPos((HWND) control, SB_CTL);
160 int minPos;
161 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
162 }
163 #endif
164
165 #if defined(__WIN95__)
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__
172
173 wxEventType scrollEvent = wxEVT_NULL;
174
175 int nScrollInc;
176 switch ( wParam )
177 {
178 case SB_BOTTOM:
179 nScrollInc = maxPos - position;
180 scrollEvent = wxEVT_SCROLL_TOP;
181 break;
182
183 case SB_TOP:
184 nScrollInc = -position;
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
208 case SB_THUMBPOSITION:
209 nScrollInc = trackPos - position;
210 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
211 break;
212
213 case SB_THUMBTRACK:
214 nScrollInc = trackPos - position;
215 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
216 break;
217
218 case SB_ENDSCROLL:
219 nScrollInc = 0;
220 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
221 break;
222
223 default:
224 nScrollInc = 0;
225 }
226
227 if ( nScrollInc )
228 {
229 position += nScrollInc;
230
231 if ( position < 0 )
232 position = 0;
233 if ( position > maxPos )
234 position = maxPos;
235
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 }
245
246 wxScrollEvent event(scrollEvent, m_windowId);
247 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
248 event.SetPosition(position);
249 event.SetEventObject( this );
250
251 return GetEventHandler()->ProcessEvent(event);
252 }
253
254 void wxScrollBar::SetThumbPosition(int viewStart)
255 {
256 #if defined(__WIN95__)
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);
265 #else
266 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
267 #endif
268 }
269
270 int wxScrollBar::GetThumbPosition(void) const
271 {
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);
283 }
284
285 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
286 bool refresh)
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 {
301 range1 += (m_pageSize - 1);
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
321 WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
322 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
323 {
324 return 0;
325 }
326
327 void wxScrollBar::Command(wxCommandEvent& event)
328 {
329 SetThumbPosition(event.m_commandInt);
330 ProcessCommand(event);
331 }
332
333 #endif // wxUSE_SCROLLBAR