]> git.saurik.com Git - wxWidgets.git/blob - src/msw/scrolbar.cpp
removed gtk.h include
[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 #include "wx/settings.h"
32
33 #if wxUSE_EXTENDED_RTTI
34 WX_DEFINE_FLAGS( wxScrollBarStyle )
35
36 wxBEGIN_FLAGS( wxScrollBarStyle )
37 // new style border flags, we put them first to
38 // use them for streaming out
39 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
40 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
41 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
42 wxFLAGS_MEMBER(wxBORDER_RAISED)
43 wxFLAGS_MEMBER(wxBORDER_STATIC)
44 wxFLAGS_MEMBER(wxBORDER_NONE)
45
46 // old style border flags
47 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
48 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
49 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
50 wxFLAGS_MEMBER(wxRAISED_BORDER)
51 wxFLAGS_MEMBER(wxSTATIC_BORDER)
52 wxFLAGS_MEMBER(wxBORDER)
53
54 // standard window styles
55 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
56 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
57 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
58 wxFLAGS_MEMBER(wxWANTS_CHARS)
59 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
60 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
61 wxFLAGS_MEMBER(wxVSCROLL)
62 wxFLAGS_MEMBER(wxHSCROLL)
63
64 wxFLAGS_MEMBER(wxSB_HORIZONTAL)
65 wxFLAGS_MEMBER(wxSB_VERTICAL)
66
67 wxEND_FLAGS( wxScrollBarStyle )
68
69 IMPLEMENT_DYNAMIC_CLASS_XTI(wxScrollBar, wxControl,"wx/scrolbar.h")
70
71 wxBEGIN_PROPERTIES_TABLE(wxScrollBar)
72 wxEVENT_RANGE_PROPERTY( Scroll , wxEVT_SCROLL_TOP , wxEVT_SCROLL_ENDSCROLL , wxScrollEvent )
73
74 wxPROPERTY( ThumbPosition , int , SetThumbPosition, GetThumbPosition, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
75 wxPROPERTY( Range , int , SetRange, GetRange, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
76 wxPROPERTY( ThumbSize , int , SetThumbSize, GetThumbSize, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
77 wxPROPERTY( PageSize , int , SetPageSize, GetPageSize, 0 , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
78 wxPROPERTY_FLAGS( WindowStyle , wxScrollBarStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
79 wxEND_PROPERTIES_TABLE()
80
81 wxBEGIN_HANDLERS_TABLE(wxScrollBar)
82 wxEND_HANDLERS_TABLE()
83
84 wxCONSTRUCTOR_5( wxScrollBar , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
85 #else
86 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
87 #endif
88
89 // Scrollbar
90 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
91 const wxPoint& pos,
92 const wxSize& size, long style,
93 const wxValidator& wxVALIDATOR_PARAM(validator),
94 const wxString& name)
95 {
96 if (!parent)
97 return FALSE;
98 parent->AddChild(this);
99 SetName(name);
100 #if wxUSE_VALIDATORS
101 SetValidator(validator);
102 #endif // wxUSE_VALIDATORS
103
104 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
105 style |= wxNO_BORDER;
106
107 SetBackgroundColour(parent->GetBackgroundColour()) ;
108 SetForegroundColour(parent->GetForegroundColour()) ;
109 m_windowStyle = style;
110
111 if ( id == -1 )
112 m_windowId = (int)NewControlId();
113 else
114 m_windowId = id;
115
116 int x = pos.x;
117 int y = pos.y;
118 int width = size.x;
119 int height = size.y;
120
121 if (width == -1)
122 {
123 if (style & wxHORIZONTAL)
124 width = 140;
125 else
126 width = 14;
127 }
128 if (height == -1)
129 {
130 if (style & wxVERTICAL)
131 height = 140;
132 else
133 height = 14;
134 }
135
136 WXDWORD exStyle = 0;
137 WXDWORD wstyle = MSWGetStyle(style, & exStyle) ;
138
139 // Now create scrollbar
140 DWORD _direction = (style & wxHORIZONTAL) ?
141 SBS_HORZ: SBS_VERT;
142 HWND scroll_bar = CreateWindowEx(exStyle, wxT("SCROLLBAR"), wxT("scrollbar"),
143 _direction | wstyle,
144 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
145 wxGetInstance(), NULL);
146
147 m_pageSize = 1;
148 m_viewSize = 1;
149 m_objectSize = 1;
150
151 ::SetScrollRange(scroll_bar, SB_CTL, 0, 1, FALSE);
152 ::SetScrollPos(scroll_bar, SB_CTL, 0, FALSE);
153 ShowWindow(scroll_bar, SW_SHOW);
154
155 m_hWnd = (WXHWND)scroll_bar;
156
157 // Subclass again for purposes of dialog editing mode
158 SubclassWin((WXHWND) scroll_bar);
159
160 SetSize(x, y, width, height);
161
162 return TRUE;
163 }
164
165 wxScrollBar::~wxScrollBar(void)
166 {
167 }
168
169 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
170 WXWORD pos, WXHWND WXUNUSED(control))
171 {
172 // current and max positions
173 int position,
174 maxPos, trackPos = pos;
175
176 // when we're dragging the scrollbar we can't use pos parameter because it
177 // is limited to 16 bits
178 // JACS: now always using GetScrollInfo, since there's no reason
179 // not to
180 // if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
181 {
182 SCROLLINFO scrollInfo;
183 wxZeroMemory(scrollInfo);
184 scrollInfo.cbSize = sizeof(SCROLLINFO);
185
186 // also get the range if we call GetScrollInfo() anyhow -- this is less
187 // expensive than call it once here and then call GetScrollRange()
188 // below
189 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS;
190
191 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
192 {
193 wxLogLastError(_T("GetScrollInfo"));
194 }
195
196 trackPos = scrollInfo.nTrackPos;
197 position = scrollInfo.nPos;
198 maxPos = scrollInfo.nMax;
199 }
200 #if 0
201 else
202 {
203 position = ::GetScrollPos((HWND) control, SB_CTL);
204 int minPos;
205 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
206 }
207 #endif
208
209 #if defined(__WIN95__)
210 // A page size greater than one has the effect of reducing the effective
211 // range, therefore the range has already been boosted artificially - so
212 // reduce it again.
213 if ( m_pageSize > 1 )
214 maxPos -= (m_pageSize - 1);
215 #endif // __WIN95__
216
217 wxEventType scrollEvent = wxEVT_NULL;
218
219 int nScrollInc;
220 switch ( wParam )
221 {
222 case SB_BOTTOM:
223 nScrollInc = maxPos - position;
224 scrollEvent = wxEVT_SCROLL_TOP;
225 break;
226
227 case SB_TOP:
228 nScrollInc = -position;
229 scrollEvent = wxEVT_SCROLL_BOTTOM;
230 break;
231
232 case SB_LINEUP:
233 nScrollInc = -1;
234 scrollEvent = wxEVT_SCROLL_LINEUP;
235 break;
236
237 case SB_LINEDOWN:
238 nScrollInc = 1;
239 scrollEvent = wxEVT_SCROLL_LINEDOWN;
240 break;
241
242 case SB_PAGEUP:
243 nScrollInc = -GetPageSize();
244 scrollEvent = wxEVT_SCROLL_PAGEUP;
245 break;
246
247 case SB_PAGEDOWN:
248 nScrollInc = GetPageSize();
249 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
250 break;
251
252 case SB_THUMBPOSITION:
253 nScrollInc = trackPos - position;
254 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
255 break;
256
257 case SB_THUMBTRACK:
258 nScrollInc = trackPos - position;
259 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
260 break;
261
262 case SB_ENDSCROLL:
263 nScrollInc = 0;
264 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
265 break;
266
267 default:
268 nScrollInc = 0;
269 }
270
271 if ( nScrollInc )
272 {
273 position += nScrollInc;
274
275 if ( position < 0 )
276 position = 0;
277 if ( position > maxPos )
278 position = maxPos;
279
280 SetThumbPosition(position);
281 }
282 else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
283 scrollEvent != wxEVT_SCROLL_ENDSCROLL )
284 {
285 // don't process the event if there is no displacement,
286 // unless this is a thumb release or end scroll event.
287 return FALSE;
288 }
289
290 wxScrollEvent event(scrollEvent, m_windowId);
291 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
292 event.SetPosition(position);
293 event.SetEventObject( this );
294
295 return GetEventHandler()->ProcessEvent(event);
296 }
297
298 void wxScrollBar::SetThumbPosition(int viewStart)
299 {
300 #if defined(__WIN95__)
301 SCROLLINFO info;
302 info.cbSize = sizeof(SCROLLINFO);
303 info.nPage = 0;
304 info.nMin = 0;
305 info.nPos = viewStart;
306 info.fMask = SIF_POS ;
307
308 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
309 #else
310 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
311 #endif
312 }
313
314 int wxScrollBar::GetThumbPosition(void) const
315 {
316 SCROLLINFO scrollInfo;
317 wxZeroMemory(scrollInfo);
318 scrollInfo.cbSize = sizeof(SCROLLINFO);
319 scrollInfo.fMask = SIF_POS;
320
321 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
322 {
323 wxLogLastError(_T("GetScrollInfo"));
324 }
325 return scrollInfo.nPos;
326 // return ::GetScrollPos((HWND)m_hWnd, SB_CTL);
327 }
328
329 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
330 bool refresh)
331 {
332 m_viewSize = pageSize;
333 m_pageSize = thumbSize;
334 m_objectSize = range;
335
336 // The range (number of scroll steps) is the
337 // object length minus the page size.
338 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
339
340 #if defined(__WIN95__)
341 // Try to adjust the range to cope with page size > 1
342 // (see comment for SetPageLength)
343 if ( m_pageSize > 1 )
344 {
345 range1 += (m_pageSize - 1);
346 }
347
348 SCROLLINFO info;
349 info.cbSize = sizeof(SCROLLINFO);
350 info.nPage = m_pageSize;
351 info.nMin = 0;
352 info.nMax = range1;
353 info.nPos = position;
354
355 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
356
357 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
358 #else
359 ::SetScrollPos((HWND)m_hWnd, SB_CTL, position, TRUE);
360 ::SetScrollRange((HWND)m_hWnd, SB_CTL, 0, range1, TRUE);
361 #endif
362 }
363
364
365 WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
366 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
367 {
368 return 0;
369 }
370
371 void wxScrollBar::Command(wxCommandEvent& event)
372 {
373 SetThumbPosition(event.m_commandInt);
374 ProcessCommand(event);
375 }
376
377 wxSize wxScrollBar::DoGetBestSize() const
378 {
379 int w = 100;
380 int h = 100;
381
382 if ( IsVertical() )
383 {
384 w = wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
385 }
386 else
387 {
388 h = wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
389 }
390
391 return wxSize(w, h);
392 }
393
394 #endif // wxUSE_SCROLLBAR