]> git.saurik.com Git - wxWidgets.git/blob - src/msw/scrolbar.cpp
Added some window style metadata
[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 #if wxUSE_EXTENDED_RTTI
33 WX_DEFINE_FLAGS( wxScrollBarStyle )
34
35 WX_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
66 WX_END_FLAGS( wxScrollBarStyle )
67
68 IMPLEMENT_DYNAMIC_CLASS_XTI(wxScrollBar, wxControl,"wx/scrolbar.h")
69
70 WX_BEGIN_PROPERTIES_TABLE(wxScrollBar)
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"))
75 WX_PROPERTY_FLAGS( WindowStyle , wxScrollBarStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
76 WX_END_PROPERTIES_TABLE()
77
78 WX_BEGIN_HANDLERS_TABLE(wxScrollBar)
79 WX_END_HANDLERS_TABLE()
80
81 WX_CONSTRUCTOR_5( wxScrollBar , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
82 #else
83 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
84 #endif
85
86 // Scrollbar
87 bool wxScrollBar::Create(wxWindow *parent, wxWindowID id,
88 const wxPoint& pos,
89 const wxSize& size, long style,
90 const wxValidator& validator,
91 const wxString& name)
92 {
93 if (!parent)
94 return FALSE;
95 parent->AddChild(this);
96 SetName(name);
97 #if wxUSE_VALIDATORS
98 SetValidator(validator);
99 #endif // wxUSE_VALIDATORS
100
101 if ((style & wxBORDER_MASK) == wxBORDER_DEFAULT)
102 style |= wxNO_BORDER;
103
104 SetBackgroundColour(parent->GetBackgroundColour()) ;
105 SetForegroundColour(parent->GetForegroundColour()) ;
106 m_windowStyle = style;
107
108 if ( id == -1 )
109 m_windowId = (int)NewControlId();
110 else
111 m_windowId = id;
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
133 WXDWORD exStyle = 0;
134 WXDWORD wstyle = MSWGetStyle(style, & exStyle) ;
135
136 // Now create scrollbar
137 DWORD _direction = (style & wxHORIZONTAL) ?
138 SBS_HORZ: SBS_VERT;
139 HWND scroll_bar = CreateWindowEx(exStyle, wxT("SCROLLBAR"), wxT("scrollbar"),
140 _direction | wstyle,
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
152 SetFont(parent->GetFont());
153
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
164 wxScrollBar::~wxScrollBar(void)
165 {
166 }
167
168 bool wxScrollBar::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
169 WXWORD pos, WXHWND WXUNUSED(control))
170 {
171 // current and max positions
172 int position,
173 maxPos, trackPos = pos;
174
175 // when we're dragging the scrollbar we can't use pos parameter because it
176 // is limited to 16 bits
177 // JACS: now always using GetScrollInfo, since there's no reason
178 // not to
179 // if ( wParam == SB_THUMBPOSITION || wParam == SB_THUMBTRACK )
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 }
199 #if 0
200 else
201 {
202 position = ::GetScrollPos((HWND) control, SB_CTL);
203 int minPos;
204 ::GetScrollRange((HWND) control, SB_CTL, &minPos, &maxPos);
205 }
206 #endif
207
208 #if defined(__WIN95__)
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__
215
216 wxEventType scrollEvent = wxEVT_NULL;
217
218 int nScrollInc;
219 switch ( wParam )
220 {
221 case SB_BOTTOM:
222 nScrollInc = maxPos - position;
223 scrollEvent = wxEVT_SCROLL_TOP;
224 break;
225
226 case SB_TOP:
227 nScrollInc = -position;
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
251 case SB_THUMBPOSITION:
252 nScrollInc = trackPos - position;
253 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
254 break;
255
256 case SB_THUMBTRACK:
257 nScrollInc = trackPos - position;
258 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
259 break;
260
261 case SB_ENDSCROLL:
262 nScrollInc = 0;
263 scrollEvent = wxEVT_SCROLL_ENDSCROLL;
264 break;
265
266 default:
267 nScrollInc = 0;
268 }
269
270 if ( nScrollInc )
271 {
272 position += nScrollInc;
273
274 if ( position < 0 )
275 position = 0;
276 if ( position > maxPos )
277 position = maxPos;
278
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 }
288
289 wxScrollEvent event(scrollEvent, m_windowId);
290 event.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
291 event.SetPosition(position);
292 event.SetEventObject( this );
293
294 return GetEventHandler()->ProcessEvent(event);
295 }
296
297 void wxScrollBar::SetThumbPosition(int viewStart)
298 {
299 #if defined(__WIN95__)
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);
308 #else
309 ::SetScrollPos((HWND) GetHWND(), SB_CTL, viewStart, TRUE);
310 #endif
311 }
312
313 int wxScrollBar::GetThumbPosition(void) const
314 {
315 SCROLLINFO scrollInfo;
316 wxZeroMemory(scrollInfo);
317 scrollInfo.cbSize = sizeof(SCROLLINFO);
318 scrollInfo.fMask = SIF_POS;
319
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);
326 }
327
328 void wxScrollBar::SetScrollbar(int position, int thumbSize, int range, int pageSize,
329 bool refresh)
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 {
344 range1 += (m_pageSize - 1);
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
364 WXHBRUSH wxScrollBar::OnCtlColor(WXHDC WXUNUSED(pDC), WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
365 WXUINT WXUNUSED(message), WXWPARAM WXUNUSED(wParam), WXLPARAM WXUNUSED(lParam))
366 {
367 return 0;
368 }
369
370 void wxScrollBar::Command(wxCommandEvent& event)
371 {
372 SetThumbPosition(event.m_commandInt);
373 ProcessCommand(event);
374 }
375
376 #endif // wxUSE_SCROLLBAR