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