]> git.saurik.com Git - wxWidgets.git/blame - src/msw/scrolbar.cpp
Somehow, setting a tint color makes gauge work :/.
[wxWidgets.git] / src / msw / scrolbar.cpp
CommitLineData
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
6c9a19aa 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
2bda0e17
KB
9/////////////////////////////////////////////////////////////////////////////
10
2bda0e17
KB
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
1e6feb95 15 #pragma hdrstop
2bda0e17
KB
16#endif
17
1e6feb95
VZ
18#if wxUSE_SCROLLBAR
19
851dee09
WS
20#include "wx/scrolbar.h"
21
2bda0e17 22#ifndef WX_PRECOMP
1e6feb95 23 #include "wx/utils.h"
9eddec69 24 #include "wx/settings.h"
2bda0e17
KB
25#endif
26
2bda0e17
KB
27#include "wx/msw/private.h"
28
2bda0e17 29// Scrollbar
debe6624 30bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
2bda0e17 31 const wxPoint& pos,
debe6624 32 const wxSize& size, long style,
720afa24 33 const wxValidator& validator,
2bda0e17
KB
34 const wxString& name)
35{
720afa24 36 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
57f4f925 37 return false;
33ac7e6f 38
720afa24
DS
39 if (!MSWCreateControl(wxT("ScrollBar"), wxEmptyString, pos, size))
40 return false;
2bda0e17 41
720afa24 42 SetScrollbar(0, 1, 2, 1, false);
2bda0e17 43
57f4f925 44 return true;
2bda0e17
KB
45}
46
47wxScrollBar::~wxScrollBar(void)
48{
49}
50
a23fd0e1 51bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
8ae8032c 52 WXWORD WXUNUSED(pos), WXHWND WXUNUSED(control))
2bda0e17 53{
8ae8032c
VZ
54 // don't use pos parameter because it is limited to 16 bits, get the full
55 // 32 bit position from the control itself instead
56 WinStruct<SCROLLINFO> scrollInfo;
57 scrollInfo.fMask = SIF_RANGE | SIF_POS | SIF_TRACKPOS;
2b5f62a0 58
8ae8032c 59 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
2b5f62a0 60 {
8ae8032c
VZ
61 wxLogLastError(wxT("GetScrollInfo"));
62 return false;
2b5f62a0 63 }
8ae8032c 64
8ae8032c 65 int maxPos = scrollInfo.nMax;
a23fd0e1 66
a23fd0e1
VZ
67 // A page size greater than one has the effect of reducing the effective
68 // range, therefore the range has already been boosted artificially - so
69 // reduce it again.
70 if ( m_pageSize > 1 )
71 maxPos -= (m_pageSize - 1);
2bda0e17 72
57a4b972 73 int position = scrollInfo.nPos;
7798a18e 74 wxEventType scrollEvent = wxEVT_NULL;
2bda0e17
KB
75 switch ( wParam )
76 {
97ca4fe4 77 case SB_TOP:
57a4b972 78 position = 0;
a23fd0e1
VZ
79 scrollEvent = wxEVT_SCROLL_TOP;
80 break;
81
97ca4fe4 82 case SB_BOTTOM:
57a4b972 83 position = maxPos;
a23fd0e1
VZ
84 scrollEvent = wxEVT_SCROLL_BOTTOM;
85 break;
86
87 case SB_LINEUP:
57a4b972 88 position--;
a23fd0e1
VZ
89 scrollEvent = wxEVT_SCROLL_LINEUP;
90 break;
91
92 case SB_LINEDOWN:
57a4b972 93 position++;
a23fd0e1
VZ
94 scrollEvent = wxEVT_SCROLL_LINEDOWN;
95 break;
96
97 case SB_PAGEUP:
57a4b972 98 position -= GetPageSize();
a23fd0e1
VZ
99 scrollEvent = wxEVT_SCROLL_PAGEUP;
100 break;
101
102 case SB_PAGEDOWN:
57a4b972 103 position += GetPageSize();
a23fd0e1
VZ
104 scrollEvent = wxEVT_SCROLL_PAGEDOWN;
105 break;
106
a23fd0e1 107 case SB_THUMBPOSITION:
feda3011 108 case SB_THUMBTRACK:
57a4b972 109 position = scrollInfo.nTrackPos;
8ae8032c
VZ
110 scrollEvent = wParam == SB_THUMBPOSITION ? wxEVT_SCROLL_THUMBRELEASE
111 : wxEVT_SCROLL_THUMBTRACK;
a23fd0e1
VZ
112 break;
113
e8b669d3 114 case SB_ENDSCROLL:
cbc85508 115 scrollEvent = wxEVT_SCROLL_CHANGED;
e8b669d3 116 break;
2bda0e17
KB
117 }
118
57a4b972 119 if ( position != scrollInfo.nPos )
2bda0e17 120 {
e8b669d3
VZ
121 if ( position < 0 )
122 position = 0;
123 if ( position > maxPos )
124 position = maxPos;
a23fd0e1 125
e8b669d3
VZ
126 SetThumbPosition(position);
127 }
128 else if ( scrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
cbc85508 129 scrollEvent != wxEVT_SCROLL_CHANGED )
e8b669d3
VZ
130 {
131 // don't process the event if there is no displacement,
132 // unless this is a thumb release or end scroll event.
57f4f925 133 return false;
e8b669d3 134 }
a23fd0e1 135
a23fd0e1 136 wxScrollEvent event(scrollEvent, m_windowId);
27e229f5 137 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
e8b669d3 138 event.SetPosition(position);
a23fd0e1
VZ
139 event.SetEventObject( this );
140
937013e0 141 return HandleWindowEvent(event);
2bda0e17
KB
142}
143
4fabb575 144void wxScrollBar::SetThumbPosition(int viewStart)
2bda0e17 145{
2b5f62a0
VZ
146 SCROLLINFO info;
147 info.cbSize = sizeof(SCROLLINFO);
148 info.nPage = 0;
149 info.nMin = 0;
150 info.nPos = viewStart;
151 info.fMask = SIF_POS ;
152
153 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, TRUE);
2bda0e17
KB
154}
155
4fabb575 156int wxScrollBar::GetThumbPosition(void) const
2bda0e17 157{
be85a191 158 WinStruct<SCROLLINFO> scrollInfo;
4676948b 159 scrollInfo.fMask = SIF_POS;
f0a126fe 160
4676948b
JS
161 if ( !::GetScrollInfo(GetHwnd(), SB_CTL, &scrollInfo) )
162 {
9a83f860 163 wxLogLastError(wxT("GetScrollInfo"));
4676948b
JS
164 }
165 return scrollInfo.nPos;
2bda0e17
KB
166}
167
debe6624
JS
168void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
169 bool refresh)
2bda0e17 170{
a71d815b
WS
171 m_viewSize = pageSize;
172 m_pageSize = thumbSize;
173 m_objectSize = range;
174
175 // The range (number of scroll steps) is the
176 // object length minus the page size.
177 int range1 = wxMax((m_objectSize - m_pageSize), 0) ;
178
179 // Try to adjust the range to cope with page size > 1
180 // (see comment for SetPageLength)
181 if ( m_pageSize > 1 )
182 {
183 range1 += (m_pageSize - 1);
184 }
185
186 SCROLLINFO info;
187 info.cbSize = sizeof(SCROLLINFO);
188 info.nPage = m_pageSize;
189 info.nMin = 0;
190 info.nMax = range1;
191 info.nPos = position;
192
193 info.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
194
195 ::SetScrollInfo((HWND) GetHWND(), SB_CTL, &info, refresh);
2bda0e17
KB
196}
197
2bda0e17
KB
198void wxScrollBar::Command(wxCommandEvent& event)
199{
687706f5 200 SetThumbPosition(event.GetInt());
2bda0e17
KB
201 ProcessCommand(event);
202}
203
caafd082
DS
204wxSize wxScrollBar::DoGetBestSize() const
205{
206 int w = 100;
207 int h = 100;
208
209 if ( IsVertical() )
210 {
211 w = wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
212 }
213 else
214 {
215 h = wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
216 }
217
31582e4e
RD
218 wxSize best(w, h);
219 CacheBestSize(best);
220 return best;
caafd082
DS
221}
222
720afa24
DS
223WXDWORD wxScrollBar::MSWGetStyle(long style, WXDWORD *exstyle) const
224{
225 // we never have an external border
226 WXDWORD msStyle = wxControl::MSWGetStyle
227 (
228 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
229 );
230
231 // SBS_HORZ is 0 anyhow, but do mention it explicitly for clarity
232 msStyle |= style & wxSB_HORIZONTAL ? SBS_HORZ : SBS_VERT;
233
234 return msStyle;
235}
236
0cf11995 237WXHBRUSH wxScrollBar::MSWControlColor(WXHDC pDC, WXHWND hWnd)
123865f2 238{
0cf11995
VZ
239 // unless we have an explicitly set bg colour, use default (gradient under
240 // XP) brush instead of GetBackgroundColour() one as the base class would
241 //
242 // note that fg colour isn't used for a scrollbar
243 return UseBgCol() ? wxControl::MSWControlColor(pDC, hWnd) : NULL;
123865f2
JS
244}
245
1e6feb95 246#endif // wxUSE_SCROLLBAR