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