Build fix for 2.6 compatibility off.
[wxWidgets.git] / src / os2 / scrolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/scrolbar.cpp
3 // Purpose: wxScrollBar
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/15/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/utils.h"
17 #endif
18
19 #include "wx/scrolbar.h"
20 #include "wx/os2/private.h"
21
22 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar, wxControl)
23
24 // Scrollbar
25 bool wxScrollBar::Create(wxWindow* pParent,
26 wxWindowID vId,
27 const wxPoint& rPos,
28 const wxSize& rSize,
29 long lStyle,
30 #if wxUSE_VALIDATORS
31 const wxValidator& rValidator,
32 #endif
33 const wxString& rsName
34 )
35 {
36 int nX = rPos.x;
37 int nY = rPos.y;
38 int nWidth = rSize.x;
39 int nHeight = rSize.y;
40
41 if (!pParent)
42 return false;
43
44 pParent->AddChild(this);
45 SetName(rsName);
46 #if wxUSE_VALIDATORS
47 SetValidator(rValidator);
48 #endif
49 SetBackgroundColour(pParent->GetBackgroundColour()) ;
50 SetForegroundColour(pParent->GetForegroundColour()) ;
51
52 if (vId == wxID_ANY)
53 m_windowId = (int)NewControlId();
54 else
55 m_windowId = vId;
56
57 if (nWidth == wxDefaultCoord)
58 {
59 if (lStyle & wxHORIZONTAL)
60 nWidth = 140;
61 else
62 nWidth = 14;
63 }
64 if (nHeight == wxDefaultCoord)
65 {
66 if (lStyle & wxVERTICAL)
67 nHeight = 140;
68 else
69 nHeight = 14;
70 }
71
72 DWORD dwStyle = WS_VISIBLE;
73
74 if (GetWindowStyleFlag() & wxCLIP_SIBLINGS)
75 dwStyle |= WS_CLIPSIBLINGS;
76
77 DWORD dwDirection = (lStyle & wxHORIZONTAL) ? SBS_HORZ: SBS_VERT;
78
79 HWND hScrollBar = ::WinCreateWindow( (HWND)GetHwndOf(pParent)
80 ,WC_SCROLLBAR
81 ,(PSZ)NULL
82 ,dwDirection | dwStyle
83 ,0, 0, 0, 0
84 ,(HWND)GetHwndOf(pParent)
85 ,HWND_TOP
86 ,(HMENU)m_windowId
87 ,NULL
88 ,NULL
89 );
90
91 m_nPageSize = 1;
92 m_nViewSize = 1;
93 m_nObjectSize = 1;
94 ::WinSendMsg( hScrollBar
95 ,SBM_SETSCROLLBAR
96 ,(MPARAM)0
97 ,MPFROM2SHORT(0,1)
98 );
99 ::WinShowWindow( hScrollBar, TRUE );
100 SetFont(*wxSMALL_FONT);
101
102 m_hWnd = hScrollBar;
103
104 //
105 // Subclass again for purposes of dialog editing mode
106 //
107 SubclassWin((WXHWND)hScrollBar);
108 SetSize( nX
109 ,nY
110 ,nWidth
111 ,nHeight
112 );
113 return true;
114 } // end of wxScrollBar::Create
115
116 wxScrollBar::~wxScrollBar()
117 {
118 }
119
120 bool wxScrollBar::OS2OnScroll ( int WXUNUSED(nOrientation),
121 WXWORD wParam,
122 WXWORD wPos,
123 WXHWND WXUNUSED(hControl) )
124 {
125 int nPosition;
126 int nMaxPos;
127 int nTrackPos = wPos;
128 int nMinPos;
129 int nScrollInc;
130 wxEventType vScrollEvent = wxEVT_NULL;
131
132 MRESULT vRange;
133
134 //
135 // When we're dragging the scrollbar we can't use pos parameter because it
136 // is limited to 16 bits
137 //
138 if (wParam == SB_SLIDERPOSITION || wParam == SB_SLIDERTRACK)
139 {
140 SBCDATA vScrollInfo;
141
142 vScrollInfo.sHilite = SB_SLIDERTRACK;
143
144 ::WinSendMsg((HWND)GetHwnd(), WM_QUERYWINDOWPARAMS, (PVOID)&vScrollInfo, NULL);
145
146 nTrackPos = vScrollInfo.posThumb;
147 nPosition = vScrollInfo.posFirst;
148 nMaxPos = vScrollInfo.posLast;
149 }
150 else
151 {
152 nPosition = (int)(MRESULT)::WinSendMsg((HWND)GetHwnd(), SBM_QUERYPOS, (MPARAM)NULL, (MPARAM)NULL);
153 vRange = ::WinSendMsg((HWND)GetHwnd(), SBM_QUERYRANGE, (MPARAM)NULL, (MPARAM)NULL);
154 nMinPos = SHORT1FROMMR(vRange);
155 nMaxPos = SHORT2FROMMR(vRange);
156 }
157 //
158 // A page size greater than one has the effect of reducing the effective
159 // range, therefore the range has already been boosted artificially - so
160 // reduce it again.
161 //
162 if (m_nPageSize > 1)
163 nMaxPos -= (m_nPageSize - 1);
164 switch (wParam)
165 {
166 case SB_LINEUP:
167 nScrollInc = -1;
168 vScrollEvent = wxEVT_SCROLL_LINEUP;
169 break;
170
171 case SB_LINEDOWN:
172 nScrollInc = 1;
173 vScrollEvent = wxEVT_SCROLL_LINEDOWN;
174 break;
175
176 case SB_PAGEUP:
177 nScrollInc = -GetPageSize();
178 vScrollEvent = wxEVT_SCROLL_PAGEUP;
179 break;
180
181 case SB_PAGEDOWN:
182 nScrollInc = GetPageSize();
183 vScrollEvent = wxEVT_SCROLL_PAGEDOWN;
184 break;
185
186 case SB_SLIDERTRACK:
187 nScrollInc = nTrackPos - nPosition;
188 vScrollEvent = wxEVT_SCROLL_THUMBTRACK;
189 break;
190
191 case SB_ENDSCROLL:
192 nScrollInc = 0;
193 vScrollEvent = wxEVT_SCROLL_CHANGED;
194 break;
195
196 default:
197 nScrollInc = 0;
198 }
199 if (nScrollInc)
200 {
201 nPosition += nScrollInc;
202
203 if (nPosition < 0)
204 nPosition = 0;
205 if (nPosition > nMaxPos)
206 nPosition = nMaxPos;
207 SetThumbPosition(nPosition);
208 }
209 else if ( vScrollEvent != wxEVT_SCROLL_THUMBRELEASE &&
210 vScrollEvent != wxEVT_SCROLL_CHANGED
211 )
212 {
213 //
214 // Don't process the event if there is no displacement,
215 // unless this is a thumb release or end scroll event.
216 //
217 return false;
218 }
219
220 wxScrollEvent vEvent( vScrollEvent
221 ,m_windowId
222 );
223
224 vEvent.SetOrientation(IsVertical() ? wxVERTICAL : wxHORIZONTAL);
225 vEvent.SetPosition(nPosition);
226 vEvent.SetEventObject(this);
227 return GetEventHandler()->ProcessEvent(vEvent);
228 } // end of wxScrollBar::OS2OnScroll
229
230 void wxScrollBar::SetThumbPosition ( int nViewStart )
231 {
232 SBCDATA vInfo;
233
234 memset(&vInfo, '\0', sizeof(SBCDATA));
235 vInfo.cb = sizeof(SBCDATA);
236 vInfo.posThumb = (SHORT)nViewStart;
237
238 ::WinSendMsg((HWND)GetHwnd(), WM_SETWINDOWPARAMS, (MPARAM)&vInfo, (MPARAM)NULL);
239 ::WinSendMsg((HWND)GetHwnd(), SBM_SETPOS, (MPARAM)nViewStart, (MPARAM)NULL);
240 } // end of wxScrollBar::SetThumbPosition
241
242 int wxScrollBar::GetThumbPosition() const
243 {
244 return((int)(MRESULT)::WinSendMsg((HWND)GetHwnd(), SBM_QUERYPOS, (MPARAM)NULL, (MPARAM)NULL));
245 } // end of wxScrollBar::GetThumbPosition
246
247 void wxScrollBar::SetScrollbar ( int nPosition,
248 int nThumbSize,
249 int nRange,
250 int nPageSize,
251 bool WXUNUSED(bRefresh) )
252 {
253 SBCDATA vInfo;
254 //
255 // The lRange (number of scroll steps) is the
256 // object length minus the page size.
257 //
258 int nRange1 = wxMax((m_nObjectSize - m_nPageSize), 0);
259
260 m_nViewSize = nPageSize;
261 m_nPageSize = nThumbSize;
262 m_nObjectSize = nRange;
263
264
265 //
266 // Try to adjust the lRange to cope with page size > 1
267 // (see comment for SetPageLength)
268 //
269 if (m_nPageSize > 1 )
270 {
271 nRange1 += (m_nPageSize - 1);
272 }
273 vInfo.cb = sizeof(SBCDATA);
274 vInfo.cVisible = (SHORT)m_nPageSize;
275 vInfo.posFirst = 0;
276 vInfo.posLast = (SHORT)nRange1;
277 vInfo.posThumb = (SHORT)nPosition;
278
279 ::WinSendMsg((HWND)GetHwnd(), WM_SETWINDOWPARAMS, (MPARAM)&vInfo, (MPARAM)NULL);
280 } // end of wxScrollBar::SetScrollbar
281
282 WXHBRUSH wxScrollBar::OnCtlColor ( WXHDC WXUNUSED(hDC),
283 WXHWND WXUNUSED(hWnd),
284 WXUINT WXUNUSED(uCtlColor),
285 WXUINT WXUNUSED(uMessage),
286 WXWPARAM WXUNUSED(wParam),
287 WXLPARAM WXUNUSED(lParam) )
288 {
289 //
290 // Does nothing under OS/2
291 //
292 return 0;
293 } // end of wxScrollBar::OnCtlColor
294
295 void wxScrollBar::Command ( wxCommandEvent& rEvent )
296 {
297 SetThumbPosition(rEvent.GetInt());
298 ProcessCommand(rEvent);
299 } // end of wxScrollBar::Command