]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
ed4b0fdc | 2 | // Name: src/generic/scrlwing.cpp |
d32e78bd | 3 | // Purpose: wxScrolledWindow implementation |
c801d85f | 4 | // Author: Julian Smart |
566d84a7 RL |
5 | // Modified by: Vadim Zeitlin on 31.08.00: wxScrollHelper allows to implement. |
6 | // Ron Lee on 10.4.02: virtual size / auto scrollbars et al. | |
c801d85f KB |
7 | // Created: 01/02/97 |
8 | // RCS-ID: $Id$ | |
77ffb593 | 9 | // Copyright: (c) wxWidgets team |
65571936 | 10 | // Licence: wxWindows licence |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
d80cd92a VZ |
13 | // ============================================================================ |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
bcd055ae JJ |
21 | #ifdef __VMS |
22 | #define XtDisplay XTDISPLAY | |
23 | #endif | |
24 | ||
c801d85f KB |
25 | // For compilers that support precompilation, includes "wx.h". |
26 | #include "wx/wxprec.h" | |
27 | ||
c801d85f | 28 | #ifdef __BORLANDC__ |
d80cd92a | 29 | #pragma hdrstop |
c801d85f KB |
30 | #endif |
31 | ||
de6185e2 WS |
32 | #include "wx/scrolwin.h" |
33 | ||
34 | #ifndef WX_PRECOMP | |
35 | #include "wx/utils.h" | |
8e609c82 | 36 | #include "wx/panel.h" |
ed4b0fdc | 37 | #include "wx/dcclient.h" |
c0badb70 WS |
38 | #if wxUSE_TIMER |
39 | #include "wx/timer.h" | |
40 | #endif | |
ed2fbeb8 | 41 | #include "wx/sizer.h" |
4b316329 | 42 | #endif |
ed4b0fdc | 43 | |
2a201ef8 | 44 | #include "wx/recguard.h" |
c801d85f | 45 | |
48d1144b | 46 | #ifdef __WXMSW__ |
1e6feb95 | 47 | #include <windows.h> // for DLGC_WANTARROWS |
7acf6a92 | 48 | #include "wx/msw/winundef.h" |
48d1144b JS |
49 | #endif |
50 | ||
a91b47e8 JS |
51 | #ifdef __WXMOTIF__ |
52 | // For wxRETAINED implementation | |
338dd992 JJ |
53 | #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++ |
54 | //This code switches off the compiler warnings | |
55 | # pragma message disable nosimpint | |
56 | #endif | |
a91b47e8 | 57 | #include <Xm/Xm.h> |
338dd992 JJ |
58 | #ifdef __VMS__ |
59 | # pragma message enable nosimpint | |
60 | #endif | |
a91b47e8 JS |
61 | #endif |
62 | ||
066f1b7a | 63 | /* |
ca65c044 WS |
64 | TODO PROPERTIES |
65 | style wxHSCROLL | wxVSCROLL | |
066f1b7a SC |
66 | */ |
67 | ||
d80cd92a | 68 | // ---------------------------------------------------------------------------- |
1e6feb95 VZ |
69 | // wxScrollHelperEvtHandler: intercept the events from the window and forward |
70 | // them to wxScrollHelper | |
d80cd92a VZ |
71 | // ---------------------------------------------------------------------------- |
72 | ||
349efbaa | 73 | class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler |
1e6feb95 VZ |
74 | { |
75 | public: | |
76 | wxScrollHelperEvtHandler(wxScrollHelper *scrollHelper) | |
77 | { | |
78 | m_scrollHelper = scrollHelper; | |
79 | } | |
d80cd92a | 80 | |
1e6feb95 VZ |
81 | virtual bool ProcessEvent(wxEvent& event); |
82 | ||
ca65c044 | 83 | void ResetDrawnFlag() { m_hasDrawnWindow = false; } |
349efbaa | 84 | |
1e6feb95 VZ |
85 | private: |
86 | wxScrollHelper *m_scrollHelper; | |
349efbaa VZ |
87 | |
88 | bool m_hasDrawnWindow; | |
22f3361e VZ |
89 | |
90 | DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler) | |
1e6feb95 VZ |
91 | }; |
92 | ||
4b316329 | 93 | #if wxUSE_TIMER |
1e6feb95 VZ |
94 | // ---------------------------------------------------------------------------- |
95 | // wxAutoScrollTimer: the timer used to generate a stream of scroll events when | |
96 | // a captured mouse is held outside the window | |
97 | // ---------------------------------------------------------------------------- | |
98 | ||
99 | class wxAutoScrollTimer : public wxTimer | |
100 | { | |
101 | public: | |
102 | wxAutoScrollTimer(wxWindow *winToScroll, wxScrollHelper *scroll, | |
103 | wxEventType eventTypeToSend, | |
104 | int pos, int orient); | |
105 | ||
106 | virtual void Notify(); | |
107 | ||
108 | private: | |
109 | wxWindow *m_win; | |
110 | wxScrollHelper *m_scrollHelper; | |
111 | wxEventType m_eventType; | |
112 | int m_pos, | |
113 | m_orient; | |
22f3361e VZ |
114 | |
115 | DECLARE_NO_COPY_CLASS(wxAutoScrollTimer) | |
1e6feb95 | 116 | }; |
d80cd92a VZ |
117 | |
118 | // ============================================================================ | |
119 | // implementation | |
120 | // ============================================================================ | |
121 | ||
122 | // ---------------------------------------------------------------------------- | |
1e6feb95 | 123 | // wxAutoScrollTimer |
d80cd92a VZ |
124 | // ---------------------------------------------------------------------------- |
125 | ||
1e6feb95 VZ |
126 | wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll, |
127 | wxScrollHelper *scroll, | |
128 | wxEventType eventTypeToSend, | |
129 | int pos, int orient) | |
c801d85f | 130 | { |
1e6feb95 VZ |
131 | m_win = winToScroll; |
132 | m_scrollHelper = scroll; | |
133 | m_eventType = eventTypeToSend; | |
134 | m_pos = pos; | |
135 | m_orient = orient; | |
c801d85f KB |
136 | } |
137 | ||
1e6feb95 | 138 | void wxAutoScrollTimer::Notify() |
c801d85f | 139 | { |
1e6feb95 VZ |
140 | // only do all this as long as the window is capturing the mouse |
141 | if ( wxWindow::GetCapture() != m_win ) | |
142 | { | |
143 | Stop(); | |
144 | } | |
145 | else // we still capture the mouse, continue generating events | |
146 | { | |
147 | // first scroll the window if we are allowed to do it | |
148 | wxScrollWinEvent event1(m_eventType, m_pos, m_orient); | |
149 | event1.SetEventObject(m_win); | |
150 | if ( m_scrollHelper->SendAutoScrollEvents(event1) && | |
151 | m_win->GetEventHandler()->ProcessEvent(event1) ) | |
152 | { | |
153 | // and then send a pseudo mouse-move event to refresh the selection | |
154 | wxMouseEvent event2(wxEVT_MOTION); | |
155 | wxGetMousePosition(&event2.m_x, &event2.m_y); | |
156 | ||
157 | // the mouse event coordinates should be client, not screen as | |
158 | // returned by wxGetMousePosition | |
159 | wxWindow *parentTop = m_win; | |
160 | while ( parentTop->GetParent() ) | |
161 | parentTop = parentTop->GetParent(); | |
162 | wxPoint ptOrig = parentTop->GetPosition(); | |
163 | event2.m_x -= ptOrig.x; | |
164 | event2.m_y -= ptOrig.y; | |
165 | ||
166 | event2.SetEventObject(m_win); | |
167 | ||
168 | // FIXME: we don't fill in the other members - ok? | |
169 | ||
170 | m_win->GetEventHandler()->ProcessEvent(event2); | |
171 | } | |
172 | else // can't scroll further, stop | |
173 | { | |
174 | Stop(); | |
175 | } | |
176 | } | |
177 | } | |
4b316329 | 178 | #endif |
1e6feb95 VZ |
179 | |
180 | // ---------------------------------------------------------------------------- | |
181 | // wxScrollHelperEvtHandler | |
182 | // ---------------------------------------------------------------------------- | |
183 | ||
184 | bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event) | |
185 | { | |
9a268018 | 186 | wxEventType evType = event.GetEventType(); |
2feed004 | 187 | |
349efbaa VZ |
188 | // the explanation of wxEVT_PAINT processing hack: for historic reasons |
189 | // there are 2 ways to process this event in classes deriving from | |
190 | // wxScrolledWindow. The user code may | |
191 | // | |
192 | // 1. override wxScrolledWindow::OnDraw(dc) | |
193 | // 2. define its own OnPaint() handler | |
194 | // | |
195 | // In addition, in wxUniversal wxWindow defines OnPaint() itself and | |
196 | // always processes the draw event, so we can't just try the window | |
197 | // OnPaint() first and call our HandleOnPaint() if it doesn't process it | |
198 | // (the latter would never be called in wxUniversal). | |
199 | // | |
200 | // So the solution is to have a flag telling us whether the user code drew | |
201 | // anything in the window. We set it to true here but reset it to false in | |
202 | // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the | |
203 | // user code defined OnPaint() in the derived class) | |
ca65c044 | 204 | m_hasDrawnWindow = true; |
349efbaa | 205 | |
ff186591 VZ |
206 | // pass it on to the real handler |
207 | bool processed = wxEvtHandler::ProcessEvent(event); | |
208 | ||
209 | // always process the size events ourselves, even if the user code handles | |
210 | // them as well, as we need to AdjustScrollbars() | |
211 | // | |
212 | // NB: it is important to do it after processing the event in the normal | |
213 | // way as HandleOnSize() may generate a wxEVT_SIZE itself if the | |
214 | // scrollbar[s] (dis)appear and it should be seen by the user code | |
215 | // after this one | |
216 | if ( evType == wxEVT_SIZE ) | |
217 | { | |
218 | m_scrollHelper->HandleOnSize((wxSizeEvent &)event); | |
219 | ||
ca65c044 | 220 | return true; |
ff186591 VZ |
221 | } |
222 | ||
223 | if ( processed ) | |
349efbaa VZ |
224 | { |
225 | // normally, nothing more to do here - except if it was a paint event | |
226 | // which wasn't really processed, then we'll try to call our | |
227 | // OnDraw() below (from HandleOnPaint) | |
c6b81ceb | 228 | if ( m_hasDrawnWindow || event.IsCommandEvent() ) |
349efbaa | 229 | { |
ca65c044 | 230 | return true; |
349efbaa VZ |
231 | } |
232 | } | |
2feed004 | 233 | |
ca65c044 | 234 | // reset the skipped flag to false as it might have been set to true in |
1e6feb95 | 235 | // ProcessEvent() above |
ca65c044 | 236 | event.Skip(false); |
1e6feb95 | 237 | |
e421922f VZ |
238 | if ( evType == wxEVT_PAINT ) |
239 | { | |
240 | m_scrollHelper->HandleOnPaint((wxPaintEvent &)event); | |
ca65c044 | 241 | return true; |
e421922f | 242 | } |
1e6feb95 | 243 | |
e421922f VZ |
244 | if ( evType == wxEVT_SCROLLWIN_TOP || |
245 | evType == wxEVT_SCROLLWIN_BOTTOM || | |
246 | evType == wxEVT_SCROLLWIN_LINEUP || | |
247 | evType == wxEVT_SCROLLWIN_LINEDOWN || | |
248 | evType == wxEVT_SCROLLWIN_PAGEUP || | |
249 | evType == wxEVT_SCROLLWIN_PAGEDOWN || | |
250 | evType == wxEVT_SCROLLWIN_THUMBTRACK || | |
251 | evType == wxEVT_SCROLLWIN_THUMBRELEASE ) | |
252 | { | |
253 | m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event); | |
1e6feb95 | 254 | return !event.GetSkipped(); |
e421922f | 255 | } |
1e6feb95 | 256 | |
e421922f VZ |
257 | if ( evType == wxEVT_ENTER_WINDOW ) |
258 | { | |
259 | m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event); | |
260 | } | |
261 | else if ( evType == wxEVT_LEAVE_WINDOW ) | |
262 | { | |
263 | m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event); | |
264 | } | |
265 | #if wxUSE_MOUSEWHEEL | |
266 | else if ( evType == wxEVT_MOUSEWHEEL ) | |
267 | { | |
268 | m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event); | |
269 | } | |
270 | #endif // wxUSE_MOUSEWHEEL | |
e421922f VZ |
271 | else if ( evType == wxEVT_CHAR ) |
272 | { | |
273 | m_scrollHelper->HandleOnChar((wxKeyEvent &)event); | |
274 | return !event.GetSkipped(); | |
1e6feb95 VZ |
275 | } |
276 | ||
ca65c044 | 277 | return false; |
1e6feb95 VZ |
278 | } |
279 | ||
280 | // ---------------------------------------------------------------------------- | |
281 | // wxScrollHelper construction | |
282 | // ---------------------------------------------------------------------------- | |
283 | ||
284 | wxScrollHelper::wxScrollHelper(wxWindow *win) | |
285 | { | |
d32e78bd VZ |
286 | wxASSERT_MSG( win, _T("associated window can't be NULL in wxScrollHelper") ); |
287 | ||
1e6feb95 VZ |
288 | m_xScrollPixelsPerLine = |
289 | m_yScrollPixelsPerLine = | |
290 | m_xScrollPosition = | |
291 | m_yScrollPosition = | |
292 | m_xScrollLines = | |
293 | m_yScrollLines = | |
294 | m_xScrollLinesPerPage = | |
139adb6a | 295 | m_yScrollLinesPerPage = 0; |
1e6feb95 VZ |
296 | |
297 | m_xScrollingEnabled = | |
ca65c044 | 298 | m_yScrollingEnabled = true; |
1e6feb95 VZ |
299 | |
300 | m_scaleX = | |
139adb6a | 301 | m_scaleY = 1.0; |
24ce4c18 | 302 | #if wxUSE_MOUSEWHEEL |
d2c52078 | 303 | m_wheelRotation = 0; |
24ce4c18 | 304 | #endif |
a58a12e9 | 305 | |
1e6feb95 VZ |
306 | m_win = |
307 | m_targetWindow = (wxWindow *)NULL; | |
139adb6a | 308 | |
1e6feb95 | 309 | m_timerAutoScroll = (wxTimer *)NULL; |
d7da9756 | 310 | |
349efbaa VZ |
311 | m_handler = NULL; |
312 | ||
d32e78bd | 313 | m_win = win; |
f4fe2f20 RR |
314 | |
315 | m_win->SetScrollHelper( this ); | |
d32e78bd VZ |
316 | |
317 | // by default, the associated window is also the target window | |
318 | DoSetTargetWindow(win); | |
1e6feb95 | 319 | } |
d7da9756 | 320 | |
1e6feb95 | 321 | wxScrollHelper::~wxScrollHelper() |
d80cd92a | 322 | { |
1e6feb95 VZ |
323 | StopAutoScrolling(); |
324 | ||
349efbaa | 325 | DeleteEvtHandler(); |
d80cd92a VZ |
326 | } |
327 | ||
328 | // ---------------------------------------------------------------------------- | |
329 | // setting scrolling parameters | |
330 | // ---------------------------------------------------------------------------- | |
331 | ||
1e6feb95 VZ |
332 | void wxScrollHelper::SetScrollbars(int pixelsPerUnitX, |
333 | int pixelsPerUnitY, | |
334 | int noUnitsX, | |
335 | int noUnitsY, | |
336 | int xPos, | |
337 | int yPos, | |
338 | bool noRefresh) | |
c801d85f | 339 | { |
b0486e0d SN |
340 | int xpos, ypos; |
341 | ||
342 | CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos); | |
139adb6a RR |
343 | bool do_refresh = |
344 | ( | |
c801d85f | 345 | (noUnitsX != 0 && m_xScrollLines == 0) || |
566d84a7 | 346 | (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX * noUnitsX) || |
b0486e0d | 347 | |
c801d85f | 348 | (noUnitsY != 0 && m_yScrollLines == 0) || |
566d84a7 | 349 | (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY * noUnitsY) || |
c801d85f | 350 | (xPos != m_xScrollPosition) || |
b0486e0d | 351 | (yPos != m_yScrollPosition) |
139adb6a | 352 | ); |
a58a12e9 | 353 | |
139adb6a RR |
354 | m_xScrollPixelsPerLine = pixelsPerUnitX; |
355 | m_yScrollPixelsPerLine = pixelsPerUnitY; | |
356 | m_xScrollPosition = xPos; | |
357 | m_yScrollPosition = yPos; | |
a91b47e8 | 358 | |
150c8d89 RL |
359 | int w = noUnitsX * pixelsPerUnitX; |
360 | int h = noUnitsY * pixelsPerUnitY; | |
361 | ||
2b5f62a0 VZ |
362 | // For better backward compatibility we set persisting limits |
363 | // here not just the size. It makes SetScrollbars 'sticky' | |
364 | // emulating the old non-autoscroll behaviour. | |
2b43d588 | 365 | // m_targetWindow->SetVirtualSizeHints( w, h ); |
a58a12e9 | 366 | |
2b5f62a0 VZ |
367 | // The above should arguably be deprecated, this however we still need. |
368 | ||
f18f464c VZ |
369 | // take care not to set 0 virtual size, 0 means that we don't have any |
370 | // scrollbars and hence we should use the real size instead of the virtual | |
371 | // one which is indicated by using wxDefaultCoord | |
372 | m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord, | |
373 | h ? h : wxDefaultCoord); | |
dc429f89 | 374 | |
a58a12e9 | 375 | if (do_refresh && !noRefresh) |
ca65c044 | 376 | m_targetWindow->Refresh(true, GetScrollRect()); |
a58a12e9 | 377 | |
1f066698 VZ |
378 | #ifndef __WXUNIVERSAL__ |
379 | // If the target is not the same as the window with the scrollbars, | |
380 | // then we need to update the scrollbars here, since they won't have | |
381 | // been updated by SetVirtualSize(). | |
382 | if ( m_targetWindow != m_win ) | |
383 | #endif // !__WXUNIVERSAL__ | |
384 | { | |
385 | AdjustScrollbars(); | |
386 | } | |
387 | #ifndef __WXUNIVERSAL__ | |
388 | else | |
389 | { | |
390 | // otherwise this has been done by AdjustScrollbars, above | |
1f066698 VZ |
391 | } |
392 | #endif // !__WXUNIVERSAL__ | |
c801d85f KB |
393 | } |
394 | ||
d80cd92a | 395 | // ---------------------------------------------------------------------------- |
af4088f1 | 396 | // [target] window handling |
d80cd92a | 397 | // ---------------------------------------------------------------------------- |
ecab4dba | 398 | |
349efbaa | 399 | void wxScrollHelper::DeleteEvtHandler() |
ecab4dba | 400 | { |
248d771c VZ |
401 | // search for m_handler in the handler list |
402 | if ( m_win && m_handler ) | |
349efbaa | 403 | { |
74f6bbf9 | 404 | if ( m_win->RemoveEventHandler(m_handler) ) |
248d771c | 405 | { |
74f6bbf9 | 406 | delete m_handler; |
248d771c | 407 | } |
74f6bbf9 VZ |
408 | //else: something is very wrong, so better [maybe] leak memory than |
409 | // risk a crash because of double deletion | |
248d771c | 410 | |
74f6bbf9 | 411 | m_handler = NULL; |
349efbaa VZ |
412 | } |
413 | } | |
414 | ||
af4088f1 | 415 | void wxScrollHelper::DoSetTargetWindow(wxWindow *target) |
349efbaa | 416 | { |
ecab4dba | 417 | m_targetWindow = target; |
8adc196b SC |
418 | #ifdef __WXMAC__ |
419 | target->MacSetClipChildren( true ) ; | |
420 | #endif | |
349efbaa VZ |
421 | |
422 | // install the event handler which will intercept the events we're | |
af4088f1 VZ |
423 | // interested in (but only do it for our real window, not the target window |
424 | // which we scroll - we don't need to hijack its events) | |
425 | if ( m_targetWindow == m_win ) | |
13ff9344 | 426 | { |
248d771c VZ |
427 | // if we already have a handler, delete it first |
428 | DeleteEvtHandler(); | |
429 | ||
13ff9344 JS |
430 | m_handler = new wxScrollHelperEvtHandler(this); |
431 | m_targetWindow->PushEventHandler(m_handler); | |
432 | } | |
349efbaa VZ |
433 | } |
434 | ||
af4088f1 | 435 | void wxScrollHelper::SetTargetWindow(wxWindow *target) |
349efbaa VZ |
436 | { |
437 | wxCHECK_RET( target, wxT("target window must not be NULL") ); | |
438 | ||
439 | if ( target == m_targetWindow ) | |
440 | return; | |
441 | ||
af4088f1 | 442 | DoSetTargetWindow(target); |
ecab4dba RR |
443 | } |
444 | ||
1e6feb95 | 445 | wxWindow *wxScrollHelper::GetTargetWindow() const |
ecab4dba RR |
446 | { |
447 | return m_targetWindow; | |
448 | } | |
449 | ||
d80cd92a VZ |
450 | // ---------------------------------------------------------------------------- |
451 | // scrolling implementation itself | |
452 | // ---------------------------------------------------------------------------- | |
453 | ||
1e6feb95 | 454 | void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event) |
c801d85f | 455 | { |
139adb6a | 456 | int nScrollInc = CalcScrollInc(event); |
1e6feb95 | 457 | if ( nScrollInc == 0 ) |
139adb6a | 458 | { |
1e6feb95 VZ |
459 | // can't scroll further |
460 | event.Skip(); | |
461 | ||
462 | return; | |
139adb6a | 463 | } |
c801d85f | 464 | |
ca65c044 | 465 | bool needsRefresh = false; |
6682e732 SC |
466 | #ifdef __WXMAC__ |
467 | // OS X blocks on immediate redraws, so make this a refresh | |
468 | needsRefresh = true; | |
469 | #endif | |
1e6feb95 VZ |
470 | int dx = 0, |
471 | dy = 0; | |
b97dc9c8 | 472 | int orient = event.GetOrientation(); |
139adb6a RR |
473 | if (orient == wxHORIZONTAL) |
474 | { | |
1e6feb95 VZ |
475 | if ( m_xScrollingEnabled ) |
476 | { | |
477 | dx = -m_xScrollPixelsPerLine * nScrollInc; | |
478 | } | |
139adb6a | 479 | else |
1e6feb95 | 480 | { |
ca65c044 | 481 | needsRefresh = true; |
1e6feb95 | 482 | } |
139adb6a | 483 | } |
c801d85f | 484 | else |
139adb6a | 485 | { |
1e6feb95 VZ |
486 | if ( m_yScrollingEnabled ) |
487 | { | |
488 | dy = -m_yScrollPixelsPerLine * nScrollInc; | |
489 | } | |
139adb6a | 490 | else |
1e6feb95 | 491 | { |
ca65c044 | 492 | needsRefresh = true; |
1e6feb95 VZ |
493 | } |
494 | } | |
495 | ||
b97dc9c8 VS |
496 | if ( !needsRefresh ) |
497 | { | |
498 | // flush all pending repaints before we change m_{x,y}ScrollPosition, as | |
499 | // otherwise invalidated area could be updated incorrectly later when | |
500 | // ScrollWindow() makes sure they're repainted before scrolling them | |
501 | m_targetWindow->Update(); | |
502 | } | |
503 | ||
504 | if (orient == wxHORIZONTAL) | |
505 | { | |
506 | m_xScrollPosition += nScrollInc; | |
507 | m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition); | |
508 | } | |
509 | else | |
510 | { | |
511 | m_yScrollPosition += nScrollInc; | |
512 | m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition); | |
513 | } | |
514 | ||
1e6feb95 VZ |
515 | if ( needsRefresh ) |
516 | { | |
ca65c044 | 517 | m_targetWindow->Refresh(true, GetScrollRect()); |
1e6feb95 VZ |
518 | } |
519 | else | |
520 | { | |
d5d58a93 | 521 | m_targetWindow->ScrollWindow(dx, dy, GetScrollRect()); |
3d2b9c20 | 522 | } |
c801d85f KB |
523 | } |
524 | ||
1e6feb95 | 525 | int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event) |
c801d85f | 526 | { |
ecab4dba RR |
527 | int pos = event.GetPosition(); |
528 | int orient = event.GetOrientation(); | |
c801d85f | 529 | |
ecab4dba | 530 | int nScrollInc = 0; |
1a8caf94 | 531 | if (event.GetEventType() == wxEVT_SCROLLWIN_TOP) |
c801d85f | 532 | { |
ecab4dba RR |
533 | if (orient == wxHORIZONTAL) |
534 | nScrollInc = - m_xScrollPosition; | |
535 | else | |
536 | nScrollInc = - m_yScrollPosition; | |
1a8caf94 RR |
537 | } else |
538 | if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM) | |
539 | { | |
ecab4dba RR |
540 | if (orient == wxHORIZONTAL) |
541 | nScrollInc = m_xScrollLines - m_xScrollPosition; | |
542 | else | |
543 | nScrollInc = m_yScrollLines - m_yScrollPosition; | |
1a8caf94 RR |
544 | } else |
545 | if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP) | |
546 | { | |
ecab4dba | 547 | nScrollInc = -1; |
1a8caf94 RR |
548 | } else |
549 | if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN) | |
550 | { | |
ecab4dba | 551 | nScrollInc = 1; |
1a8caf94 RR |
552 | } else |
553 | if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP) | |
554 | { | |
ecab4dba RR |
555 | if (orient == wxHORIZONTAL) |
556 | nScrollInc = -GetScrollPageSize(wxHORIZONTAL); | |
557 | else | |
558 | nScrollInc = -GetScrollPageSize(wxVERTICAL); | |
1a8caf94 RR |
559 | } else |
560 | if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN) | |
561 | { | |
ecab4dba RR |
562 | if (orient == wxHORIZONTAL) |
563 | nScrollInc = GetScrollPageSize(wxHORIZONTAL); | |
564 | else | |
565 | nScrollInc = GetScrollPageSize(wxVERTICAL); | |
1a8caf94 RR |
566 | } else |
567 | if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) || | |
568 | (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE)) | |
569 | { | |
ecab4dba RR |
570 | if (orient == wxHORIZONTAL) |
571 | nScrollInc = pos - m_xScrollPosition; | |
572 | else | |
573 | nScrollInc = pos - m_yScrollPosition; | |
c801d85f | 574 | } |
88150e60 | 575 | |
ecab4dba RR |
576 | if (orient == wxHORIZONTAL) |
577 | { | |
a58a12e9 | 578 | if (m_xScrollPixelsPerLine > 0) |
ecab4dba | 579 | { |
878ddad5 RR |
580 | if ( m_xScrollPosition + nScrollInc < 0 ) |
581 | { | |
582 | // As -ve as we can go | |
583 | nScrollInc = -m_xScrollPosition; | |
584 | } | |
585 | else // check for the other bound | |
586 | { | |
587 | const int posMax = m_xScrollLines - m_xScrollLinesPerPage; | |
588 | if ( m_xScrollPosition + nScrollInc > posMax ) | |
589 | { | |
590 | // As +ve as we can go | |
591 | nScrollInc = posMax - m_xScrollPosition; | |
592 | } | |
593 | } | |
ecab4dba RR |
594 | } |
595 | else | |
ca65c044 | 596 | m_targetWindow->Refresh(true, GetScrollRect()); |
9d9355c6 VZ |
597 | } |
598 | else | |
ecab4dba | 599 | { |
be9b0663 | 600 | if ( m_yScrollPixelsPerLine > 0 ) |
d80cd92a | 601 | { |
be9b0663 VZ |
602 | if ( m_yScrollPosition + nScrollInc < 0 ) |
603 | { | |
604 | // As -ve as we can go | |
605 | nScrollInc = -m_yScrollPosition; | |
606 | } | |
607 | else // check for the other bound | |
608 | { | |
609 | const int posMax = m_yScrollLines - m_yScrollLinesPerPage; | |
610 | if ( m_yScrollPosition + nScrollInc > posMax ) | |
611 | { | |
612 | // As +ve as we can go | |
613 | nScrollInc = posMax - m_yScrollPosition; | |
614 | } | |
615 | } | |
ecab4dba RR |
616 | } |
617 | else | |
be9b0663 VZ |
618 | { |
619 | // VZ: why do we do this? (FIXME) | |
ca65c044 | 620 | m_targetWindow->Refresh(true, GetScrollRect()); |
be9b0663 | 621 | } |
9d9355c6 | 622 | } |
9d9355c6 | 623 | |
ecab4dba | 624 | return nScrollInc; |
c801d85f KB |
625 | } |
626 | ||
627 | // Adjust the scrollbars - new version. | |
1e6feb95 | 628 | void wxScrollHelper::AdjustScrollbars() |
c801d85f | 629 | { |
2a201ef8 VZ |
630 | static wxRecursionGuardFlag s_flagReentrancy; |
631 | wxRecursionGuard guard(s_flagReentrancy); | |
632 | if ( guard.IsInside() ) | |
633 | { | |
634 | // don't reenter AdjustScrollbars() while another call to | |
635 | // AdjustScrollbars() is in progress because this may lead to calling | |
636 | // ScrollWindow() twice and this can really happen under MSW if | |
637 | // SetScrollbar() call below adds or removes the scrollbar which | |
638 | // changes the window size and hence results in another | |
639 | // AdjustScrollbars() call | |
640 | return; | |
641 | } | |
642 | ||
566d84a7 RL |
643 | int w = 0, h = 0; |
644 | int oldw, oldh; | |
a58a12e9 | 645 | |
27d029c7 RR |
646 | int oldXScroll = m_xScrollPosition; |
647 | int oldYScroll = m_yScrollPosition; | |
c801d85f | 648 | |
be9b0663 VZ |
649 | // VZ: at least under Windows this loop is useless because when scrollbars |
650 | // [dis]appear we get a WM_SIZE resulting in another call to | |
651 | // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave | |
652 | // it here for now but it would be better to ensure that all ports | |
653 | // generate EVT_SIZE when scrollbars [dis]appear, emulating it if | |
654 | // necessary, and remove it later | |
870c86bc JS |
655 | // JACS: Stop potential infinite loop by limiting number of iterations |
656 | int iterationCount = 0; | |
657 | const int iterationMax = 5; | |
be9b0663 VZ |
658 | do |
659 | { | |
870c86bc | 660 | iterationCount ++; |
d32e78bd | 661 | |
566d84a7 | 662 | GetTargetSize(&w, 0); |
c801d85f | 663 | |
878ddad5 RR |
664 | // scroll lines per page: if 0, no scrolling is needed |
665 | int linesPerPage; | |
666 | ||
667 | if ( m_xScrollPixelsPerLine == 0 ) | |
566d84a7 | 668 | { |
878ddad5 | 669 | // scrolling is disabled |
566d84a7 RL |
670 | m_xScrollLines = 0; |
671 | m_xScrollPosition = 0; | |
878ddad5 | 672 | linesPerPage = 0; |
566d84a7 | 673 | } |
878ddad5 | 674 | else // might need scrolling |
566d84a7 | 675 | { |
878ddad5 RR |
676 | // Round up integer division to catch any "leftover" client space. |
677 | const int wVirt = m_targetWindow->GetVirtualSize().GetWidth(); | |
678 | m_xScrollLines = (wVirt + m_xScrollPixelsPerLine - 1) / m_xScrollPixelsPerLine; | |
566d84a7 RL |
679 | |
680 | // Calculate page size i.e. number of scroll units you get on the | |
878ddad5 RR |
681 | // current client window. |
682 | linesPerPage = w / m_xScrollPixelsPerLine; | |
683 | ||
684 | // Special case. When client and virtual size are very close but | |
685 | // the client is big enough, kill scrollbar. | |
686 | if ((linesPerPage < m_xScrollLines) && (w >= wVirt)) ++linesPerPage; | |
687 | ||
688 | if (linesPerPage >= m_xScrollLines) | |
689 | { | |
690 | // we're big enough to not need scrolling | |
691 | linesPerPage = | |
692 | m_xScrollLines = | |
693 | m_xScrollPosition = 0; | |
694 | } | |
695 | else // we do need a scrollbar | |
696 | { | |
697 | if ( linesPerPage < 1 ) | |
698 | linesPerPage = 1; | |
699 | ||
700 | // Correct position if greater than extent of canvas minus | |
701 | // the visible portion of it or if below zero | |
702 | const int posMax = m_xScrollLines - linesPerPage; | |
703 | if ( m_xScrollPosition > posMax ) | |
704 | m_xScrollPosition = posMax; | |
705 | else if ( m_xScrollPosition < 0 ) | |
706 | m_xScrollPosition = 0; | |
707 | } | |
566d84a7 | 708 | } |
c801d85f | 709 | |
878ddad5 RR |
710 | m_win->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, |
711 | linesPerPage, m_xScrollLines); | |
a58a12e9 | 712 | |
878ddad5 RR |
713 | // The amount by which we scroll when paging |
714 | SetScrollPageSize(wxHORIZONTAL, linesPerPage); | |
715 | ||
716 | GetTargetSize(0, &h); | |
be9b0663 VZ |
717 | |
718 | if ( m_yScrollPixelsPerLine == 0 ) | |
566d84a7 | 719 | { |
be9b0663 | 720 | // scrolling is disabled |
566d84a7 RL |
721 | m_yScrollLines = 0; |
722 | m_yScrollPosition = 0; | |
be9b0663 | 723 | linesPerPage = 0; |
566d84a7 | 724 | } |
be9b0663 | 725 | else // might need scrolling |
566d84a7 | 726 | { |
878ddad5 RR |
727 | // Round up integer division to catch any "leftover" client space. |
728 | const int hVirt = m_targetWindow->GetVirtualSize().GetHeight(); | |
729 | m_yScrollLines = ( hVirt + m_yScrollPixelsPerLine - 1 ) / m_yScrollPixelsPerLine; | |
566d84a7 RL |
730 | |
731 | // Calculate page size i.e. number of scroll units you get on the | |
878ddad5 | 732 | // current client window. |
be9b0663 | 733 | linesPerPage = h / m_yScrollPixelsPerLine; |
878ddad5 RR |
734 | |
735 | // Special case. When client and virtual size are very close but | |
736 | // the client is big enough, kill scrollbar. | |
737 | if ((linesPerPage < m_yScrollLines) && (h >= hVirt)) ++linesPerPage; | |
738 | ||
739 | if (linesPerPage >= m_yScrollLines) | |
be9b0663 VZ |
740 | { |
741 | // we're big enough to not need scrolling | |
742 | linesPerPage = | |
743 | m_yScrollLines = | |
744 | m_yScrollPosition = 0; | |
745 | } | |
746 | else // we do need a scrollbar | |
747 | { | |
748 | if ( linesPerPage < 1 ) | |
749 | linesPerPage = 1; | |
750 | ||
751 | // Correct position if greater than extent of canvas minus | |
752 | // the visible portion of it or if below zero | |
753 | const int posMax = m_yScrollLines - linesPerPage; | |
754 | if ( m_yScrollPosition > posMax ) | |
755 | m_yScrollPosition = posMax; | |
756 | else if ( m_yScrollPosition < 0 ) | |
757 | m_yScrollPosition = 0; | |
758 | } | |
759 | } | |
566d84a7 | 760 | |
be9b0663 VZ |
761 | m_win->SetScrollbar(wxVERTICAL, m_yScrollPosition, |
762 | linesPerPage, m_yScrollLines); | |
566d84a7 | 763 | |
be9b0663 VZ |
764 | // The amount by which we scroll when paging |
765 | SetScrollPageSize(wxVERTICAL, linesPerPage); | |
c801d85f | 766 | |
139adb6a | 767 | |
be9b0663 | 768 | // If a scrollbar (dis)appeared as a result of this, adjust them again. |
566d84a7 RL |
769 | oldw = w; |
770 | oldh = h; | |
771 | ||
772 | GetTargetSize( &w, &h ); | |
870c86bc | 773 | } while ( (w != oldw || h != oldh) && (iterationCount < iterationMax) ); |
566d84a7 RL |
774 | |
775 | #ifdef __WXMOTIF__ | |
776 | // Sorry, some Motif-specific code to implement a backing pixmap | |
777 | // for the wxRETAINED style. Implementing a backing store can't | |
778 | // be entirely generic because it relies on the wxWindowDC implementation | |
779 | // to duplicate X drawing calls for the backing pixmap. | |
780 | ||
781 | if ( m_targetWindow->GetWindowStyle() & wxRETAINED ) | |
139adb6a | 782 | { |
566d84a7 RL |
783 | Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget()); |
784 | ||
785 | int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine; | |
786 | int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine; | |
787 | if (m_targetWindow->GetBackingPixmap() && | |
788 | !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) && | |
789 | (m_targetWindow->GetPixmapHeight() == totalPixelHeight))) | |
790 | { | |
791 | XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap()); | |
792 | m_targetWindow->SetBackingPixmap((WXPixmap) 0); | |
793 | } | |
794 | ||
795 | if (!m_targetWindow->GetBackingPixmap() && | |
9b66c26a | 796 | (m_xScrollLines != 0) && (m_yScrollLines != 0)) |
566d84a7 RL |
797 | { |
798 | int depth = wxDisplayDepth(); | |
799 | m_targetWindow->SetPixmapWidth(totalPixelWidth); | |
800 | m_targetWindow->SetPixmapHeight(totalPixelHeight); | |
801 | m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)), | |
802 | m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth)); | |
803 | } | |
804 | ||
139adb6a | 805 | } |
566d84a7 | 806 | #endif // Motif |
a58a12e9 | 807 | |
27d029c7 RR |
808 | if (oldXScroll != m_xScrollPosition) |
809 | { | |
810 | if (m_xScrollingEnabled) | |
566d84a7 | 811 | m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0, |
d5d58a93 | 812 | GetScrollRect() ); |
27d029c7 | 813 | else |
ca65c044 | 814 | m_targetWindow->Refresh(true, GetScrollRect()); |
27d029c7 | 815 | } |
a58a12e9 | 816 | |
27d029c7 RR |
817 | if (oldYScroll != m_yScrollPosition) |
818 | { | |
819 | if (m_yScrollingEnabled) | |
1e6feb95 | 820 | m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), |
d5d58a93 | 821 | GetScrollRect() ); |
27d029c7 | 822 | else |
ca65c044 | 823 | m_targetWindow->Refresh(true, GetScrollRect()); |
27d029c7 | 824 | } |
c801d85f KB |
825 | } |
826 | ||
1e6feb95 | 827 | void wxScrollHelper::DoPrepareDC(wxDC& dc) |
c801d85f | 828 | { |
1e6feb95 | 829 | wxPoint pt = dc.GetDeviceOrigin(); |
807a572e RR |
830 | #ifdef __WXGTK__ |
831 | // It may actually be correct to always query | |
832 | // the m_sign from the DC here, but I leve the | |
833 | // #ifdef GTK for now. | |
834 | if (m_win->GetLayoutDirection() == wxLayout_RightToLeft) | |
835 | dc.SetDeviceOrigin( pt.x + m_xScrollPosition * m_xScrollPixelsPerLine, | |
836 | pt.y - m_yScrollPosition * m_yScrollPixelsPerLine ); | |
837 | else | |
838 | #endif | |
839 | dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine, | |
840 | pt.y - m_yScrollPosition * m_yScrollPixelsPerLine ); | |
139adb6a | 841 | dc.SetUserScale( m_scaleX, m_scaleY ); |
c801d85f KB |
842 | } |
843 | ||
566d84a7 RL |
844 | void wxScrollHelper::SetScrollRate( int xstep, int ystep ) |
845 | { | |
846 | int old_x = m_xScrollPixelsPerLine * m_xScrollPosition; | |
847 | int old_y = m_yScrollPixelsPerLine * m_yScrollPosition; | |
848 | ||
849 | m_xScrollPixelsPerLine = xstep; | |
850 | m_yScrollPixelsPerLine = ystep; | |
851 | ||
852 | int new_x = m_xScrollPixelsPerLine * m_xScrollPosition; | |
853 | int new_y = m_yScrollPixelsPerLine * m_yScrollPosition; | |
854 | ||
855 | m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition ); | |
856 | m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition ); | |
857 | m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y ); | |
858 | ||
859 | AdjustScrollbars(); | |
860 | } | |
861 | ||
1e6feb95 | 862 | void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const |
c801d85f | 863 | { |
a0bc2c1d VZ |
864 | if ( x_unit ) |
865 | *x_unit = m_xScrollPixelsPerLine; | |
866 | if ( y_unit ) | |
867 | *y_unit = m_yScrollPixelsPerLine; | |
c801d85f KB |
868 | } |
869 | ||
5713b349 RR |
870 | |
871 | int wxScrollHelper::GetScrollLines( int orient ) const | |
872 | { | |
873 | if ( orient == wxHORIZONTAL ) | |
874 | return m_xScrollLines; | |
875 | else | |
876 | return m_yScrollLines; | |
877 | } | |
878 | ||
1e6feb95 | 879 | int wxScrollHelper::GetScrollPageSize(int orient) const |
c801d85f KB |
880 | { |
881 | if ( orient == wxHORIZONTAL ) | |
882 | return m_xScrollLinesPerPage; | |
883 | else | |
884 | return m_yScrollLinesPerPage; | |
885 | } | |
886 | ||
1e6feb95 | 887 | void wxScrollHelper::SetScrollPageSize(int orient, int pageSize) |
c801d85f KB |
888 | { |
889 | if ( orient == wxHORIZONTAL ) | |
890 | m_xScrollLinesPerPage = pageSize; | |
891 | else | |
892 | m_yScrollLinesPerPage = pageSize; | |
893 | } | |
894 | ||
895 | /* | |
896 | * Scroll to given position (scroll position, not pixel position) | |
897 | */ | |
1e6feb95 | 898 | void wxScrollHelper::Scroll( int x_pos, int y_pos ) |
c801d85f | 899 | { |
8b089c5e JS |
900 | if (!m_targetWindow) |
901 | return; | |
902 | ||
a58a12e9 | 903 | if (((x_pos == -1) || (x_pos == m_xScrollPosition)) && |
139adb6a | 904 | ((y_pos == -1) || (y_pos == m_yScrollPosition))) return; |
a58a12e9 | 905 | |
8d7eaf91 | 906 | int w = 0, h = 0; |
1e6feb95 | 907 | GetTargetSize(&w, &h); |
c801d85f | 908 | |
b97dc9c8 VS |
909 | // compute new position: |
910 | int new_x = m_xScrollPosition; | |
911 | int new_y = m_yScrollPosition; | |
8ea45699 | 912 | |
8775b357 | 913 | if ((x_pos != -1) && (m_xScrollPixelsPerLine)) |
c801d85f | 914 | { |
b97dc9c8 | 915 | new_x = x_pos; |
a58a12e9 | 916 | |
3d2b9c20 RR |
917 | // Calculate page size i.e. number of scroll units you get on the |
918 | // current client window | |
5713b349 | 919 | int noPagePositions = w/m_xScrollPixelsPerLine; |
139adb6a RR |
920 | if (noPagePositions < 1) noPagePositions = 1; |
921 | ||
922 | // Correct position if greater than extent of canvas minus | |
3d2b9c20 | 923 | // the visible portion of it or if below zero |
b97dc9c8 VS |
924 | new_x = wxMin( m_xScrollLines-noPagePositions, new_x ); |
925 | new_x = wxMax( 0, new_x ); | |
c801d85f | 926 | } |
8775b357 | 927 | if ((y_pos != -1) && (m_yScrollPixelsPerLine)) |
c801d85f | 928 | { |
b97dc9c8 | 929 | new_y = y_pos; |
a58a12e9 | 930 | |
3d2b9c20 RR |
931 | // Calculate page size i.e. number of scroll units you get on the |
932 | // current client window | |
5713b349 | 933 | int noPagePositions = h/m_yScrollPixelsPerLine; |
139adb6a RR |
934 | if (noPagePositions < 1) noPagePositions = 1; |
935 | ||
936 | // Correct position if greater than extent of canvas minus | |
3d2b9c20 | 937 | // the visible portion of it or if below zero |
b97dc9c8 VS |
938 | new_y = wxMin( m_yScrollLines-noPagePositions, new_y ); |
939 | new_y = wxMax( 0, new_y ); | |
940 | } | |
d2c52078 | 941 | |
b97dc9c8 VS |
942 | if ( new_x == m_xScrollPosition && new_y == m_yScrollPosition ) |
943 | return; // nothing to do, the position didn't change | |
944 | ||
945 | // flush all pending repaints before we change m_{x,y}ScrollPosition, as | |
946 | // otherwise invalidated area could be updated incorrectly later when | |
947 | // ScrollWindow() makes sure they're repainted before scrolling them | |
948 | m_targetWindow->Update(); | |
949 | ||
950 | // update the position and scroll the window now: | |
951 | if (m_xScrollPosition != new_x) | |
952 | { | |
953 | int old_x = m_xScrollPosition; | |
954 | m_xScrollPosition = new_x; | |
955 | m_win->SetScrollPos( wxHORIZONTAL, new_x ); | |
956 | m_targetWindow->ScrollWindow( (old_x-new_x)*m_xScrollPixelsPerLine, 0, | |
957 | GetScrollRect() ); | |
958 | } | |
959 | ||
960 | if (m_yScrollPosition != new_y) | |
961 | { | |
962 | int old_y = m_yScrollPosition; | |
963 | m_yScrollPosition = new_y; | |
964 | m_win->SetScrollPos( wxVERTICAL, new_y ); | |
965 | m_targetWindow->ScrollWindow( 0, (old_y-new_y)*m_yScrollPixelsPerLine, | |
966 | GetScrollRect() ); | |
c801d85f | 967 | } |
c801d85f KB |
968 | } |
969 | ||
1e6feb95 | 970 | void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll) |
c801d85f | 971 | { |
139adb6a RR |
972 | m_xScrollingEnabled = x_scroll; |
973 | m_yScrollingEnabled = y_scroll; | |
c801d85f KB |
974 | } |
975 | ||
c801d85f | 976 | // Where the current view starts from |
1e6feb95 | 977 | void wxScrollHelper::GetViewStart (int *x, int *y) const |
c801d85f | 978 | { |
a0bc2c1d VZ |
979 | if ( x ) |
980 | *x = m_xScrollPosition; | |
981 | if ( y ) | |
982 | *y = m_yScrollPosition; | |
c801d85f KB |
983 | } |
984 | ||
8c2f3797 | 985 | void wxScrollHelper::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const |
c801d85f | 986 | { |
a0bc2c1d | 987 | if ( xx ) |
807a572e | 988 | *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine; |
a0bc2c1d VZ |
989 | if ( yy ) |
990 | *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine; | |
c801d85f KB |
991 | } |
992 | ||
8c2f3797 | 993 | void wxScrollHelper::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const |
c801d85f | 994 | { |
a0bc2c1d | 995 | if ( xx ) |
807a572e | 996 | *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine; |
a0bc2c1d VZ |
997 | if ( yy ) |
998 | *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine; | |
c801d85f | 999 | } |
d80cd92a | 1000 | |
d32e78bd VZ |
1001 | // ---------------------------------------------------------------------------- |
1002 | // geometry | |
1003 | // ---------------------------------------------------------------------------- | |
1004 | ||
1005 | bool wxScrollHelper::ScrollLayout() | |
1006 | { | |
1007 | if ( m_win->GetSizer() && m_targetWindow == m_win ) | |
1008 | { | |
1009 | // If we're the scroll target, take into account the | |
1010 | // virtual size and scrolled position of the window. | |
1011 | ||
8d7eaf91 | 1012 | int x = 0, y = 0, w = 0, h = 0; |
d32e78bd VZ |
1013 | CalcScrolledPosition(0,0, &x,&y); |
1014 | m_win->GetVirtualSize(&w, &h); | |
1015 | m_win->GetSizer()->SetDimension(x, y, w, h); | |
1016 | return true; | |
1017 | } | |
1018 | ||
1019 | // fall back to default for LayoutConstraints | |
1020 | return m_win->wxWindow::Layout(); | |
1021 | } | |
1022 | ||
1023 | void wxScrollHelper::ScrollDoSetVirtualSize(int x, int y) | |
1024 | { | |
1025 | m_win->wxWindow::DoSetVirtualSize( x, y ); | |
1026 | AdjustScrollbars(); | |
1027 | ||
1028 | if (m_win->GetAutoLayout()) | |
1029 | m_win->Layout(); | |
1030 | } | |
1031 | ||
1032 | // wxWindow's GetBestVirtualSize returns the actual window size, | |
1033 | // whereas we want to return the virtual size | |
1034 | wxSize wxScrollHelper::ScrollGetBestVirtualSize() const | |
1035 | { | |
1036 | wxSize clientSize(m_win->GetClientSize()); | |
1037 | if ( m_win->GetSizer() ) | |
1038 | clientSize.IncTo(m_win->GetSizer()->CalcMin()); | |
1039 | ||
1040 | return clientSize; | |
1041 | } | |
1042 | ||
1043 | // return the window best size from the given best virtual size | |
1044 | wxSize | |
1045 | wxScrollHelper::ScrollGetWindowSizeForVirtualSize(const wxSize& size) const | |
1046 | { | |
1047 | // Only use the content to set the window size in the direction | |
1048 | // where there's no scrolling; otherwise we're going to get a huge | |
1049 | // window in the direction in which scrolling is enabled | |
1050 | int ppuX, ppuY; | |
1051 | GetScrollPixelsPerUnit(&ppuX, &ppuY); | |
1052 | ||
1053 | wxSize minSize = m_win->GetMinSize(); | |
1054 | if ( !minSize.IsFullySpecified() ) | |
1055 | minSize = m_win->GetSize(); | |
1056 | ||
1057 | wxSize best(size); | |
1058 | if (ppuX > 0) | |
1059 | best.x = minSize.x; | |
1060 | if (ppuY > 0) | |
1061 | best.y = minSize.y; | |
1062 | ||
1063 | return best; | |
1064 | } | |
1065 | ||
d80cd92a VZ |
1066 | // ---------------------------------------------------------------------------- |
1067 | // event handlers | |
1068 | // ---------------------------------------------------------------------------- | |
1069 | ||
1070 | // Default OnSize resets scrollbars, if any | |
1e6feb95 | 1071 | void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event)) |
d80cd92a | 1072 | { |
c376d80f | 1073 | if ( m_targetWindow->GetAutoLayout() ) |
2b5f62a0 | 1074 | { |
c376d80f | 1075 | wxSize size = m_targetWindow->GetBestVirtualSize(); |
d32e78bd | 1076 | |
c376d80f | 1077 | // This will call ::Layout() and ::AdjustScrollbars() |
168f82ce | 1078 | m_win->SetVirtualSize( size ); |
2b5f62a0 VZ |
1079 | } |
1080 | else | |
c376d80f | 1081 | { |
2b5f62a0 | 1082 | AdjustScrollbars(); |
c376d80f | 1083 | } |
d80cd92a VZ |
1084 | } |
1085 | ||
1086 | // This calls OnDraw, having adjusted the origin according to the current | |
1087 | // scroll position | |
1e6feb95 | 1088 | void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event)) |
d80cd92a | 1089 | { |
af4088f1 VZ |
1090 | // don't use m_targetWindow here, this is always called for ourselves |
1091 | wxPaintDC dc(m_win); | |
1e6feb95 | 1092 | DoPrepareDC(dc); |
d80cd92a VZ |
1093 | |
1094 | OnDraw(dc); | |
1095 | } | |
1096 | ||
438e3558 VZ |
1097 | // kbd handling: notice that we use OnChar() and not OnKeyDown() for |
1098 | // compatibility here - if we used OnKeyDown(), the programs which process | |
1099 | // arrows themselves in their OnChar() would never get the message and like | |
1100 | // this they always have the priority | |
1e6feb95 | 1101 | void wxScrollHelper::HandleOnChar(wxKeyEvent& event) |
d80cd92a | 1102 | { |
8d7eaf91 MW |
1103 | int stx = 0, sty = 0, // view origin |
1104 | szx = 0, szy = 0, // view size (total) | |
1105 | clix = 0, cliy = 0; // view size (on screen) | |
d80cd92a | 1106 | |
1e6feb95 VZ |
1107 | GetViewStart(&stx, &sty); |
1108 | GetTargetSize(&clix, &cliy); | |
566d84a7 | 1109 | m_targetWindow->GetVirtualSize(&szx, &szy); |
56dade3c RL |
1110 | |
1111 | if( m_xScrollPixelsPerLine ) | |
1112 | { | |
1113 | clix /= m_xScrollPixelsPerLine; | |
d7da9756 | 1114 | szx /= m_xScrollPixelsPerLine; |
56dade3c RL |
1115 | } |
1116 | else | |
1117 | { | |
1118 | clix = 0; | |
d7da9756 | 1119 | szx = -1; |
56dade3c RL |
1120 | } |
1121 | if( m_yScrollPixelsPerLine ) | |
1122 | { | |
1123 | cliy /= m_yScrollPixelsPerLine; | |
1124 | szy /= m_yScrollPixelsPerLine; | |
1125 | } | |
1126 | else | |
1127 | { | |
1128 | cliy = 0; | |
d7da9756 | 1129 | szy = -1; |
56dade3c | 1130 | } |
d80cd92a | 1131 | |
39c869a6 VZ |
1132 | int xScrollOld = m_xScrollPosition, |
1133 | yScrollOld = m_yScrollPosition; | |
1134 | ||
ecb01792 | 1135 | int dsty; |
4995ca80 | 1136 | switch ( event.GetKeyCode() ) |
d80cd92a VZ |
1137 | { |
1138 | case WXK_PAGEUP: | |
ecb01792 RL |
1139 | dsty = sty - (5 * cliy / 6); |
1140 | Scroll(-1, (dsty == -1) ? 0 : dsty); | |
d80cd92a VZ |
1141 | break; |
1142 | ||
1143 | case WXK_PAGEDOWN: | |
d80cd92a VZ |
1144 | Scroll(-1, sty + (5 * cliy / 6)); |
1145 | break; | |
1146 | ||
d80cd92a VZ |
1147 | case WXK_HOME: |
1148 | Scroll(0, event.ControlDown() ? 0 : -1); | |
1149 | break; | |
1150 | ||
1151 | case WXK_END: | |
4acd108c | 1152 | Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1); |
d80cd92a VZ |
1153 | break; |
1154 | ||
1155 | case WXK_UP: | |
1156 | Scroll(-1, sty - 1); | |
1157 | break; | |
1158 | ||
1159 | case WXK_DOWN: | |
1160 | Scroll(-1, sty + 1); | |
1161 | break; | |
1162 | ||
1163 | case WXK_LEFT: | |
1164 | Scroll(stx - 1, -1); | |
1165 | break; | |
1166 | ||
1167 | case WXK_RIGHT: | |
1168 | Scroll(stx + 1, -1); | |
1169 | break; | |
1170 | ||
1171 | default: | |
1172 | // not for us | |
1173 | event.Skip(); | |
1174 | } | |
39c869a6 VZ |
1175 | |
1176 | if ( m_xScrollPosition != xScrollOld ) | |
1177 | { | |
1178 | wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition, | |
1179 | wxHORIZONTAL); | |
1180 | event.SetEventObject(m_win); | |
1181 | m_win->GetEventHandler()->ProcessEvent(event); | |
1182 | } | |
1183 | ||
1184 | if ( m_yScrollPosition != yScrollOld ) | |
1185 | { | |
1186 | wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition, | |
1187 | wxVERTICAL); | |
1188 | event.SetEventObject(m_win); | |
1189 | m_win->GetEventHandler()->ProcessEvent(event); | |
1190 | } | |
d80cd92a | 1191 | } |
d2c52078 | 1192 | |
1e6feb95 VZ |
1193 | // ---------------------------------------------------------------------------- |
1194 | // autoscroll stuff: these functions deal with sending fake scroll events when | |
1195 | // a captured mouse is being held outside the window | |
1196 | // ---------------------------------------------------------------------------- | |
1197 | ||
1198 | bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const | |
1199 | { | |
1200 | // only send the event if the window is scrollable in this direction | |
1201 | wxWindow *win = (wxWindow *)event.GetEventObject(); | |
1202 | return win->HasScrollbar(event.GetOrientation()); | |
1203 | } | |
1204 | ||
1205 | void wxScrollHelper::StopAutoScrolling() | |
1206 | { | |
4b316329 | 1207 | #if wxUSE_TIMER |
1e6feb95 VZ |
1208 | if ( m_timerAutoScroll ) |
1209 | { | |
1210 | delete m_timerAutoScroll; | |
1211 | m_timerAutoScroll = (wxTimer *)NULL; | |
1212 | } | |
4b316329 | 1213 | #endif |
1e6feb95 | 1214 | } |
d2c52078 | 1215 | |
1e6feb95 | 1216 | void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event) |
d2c52078 | 1217 | { |
1e6feb95 VZ |
1218 | StopAutoScrolling(); |
1219 | ||
1220 | event.Skip(); | |
1221 | } | |
d2c52078 | 1222 | |
1e6feb95 VZ |
1223 | void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event) |
1224 | { | |
1225 | // don't prevent the usual processing of the event from taking place | |
1226 | event.Skip(); | |
1227 | ||
1228 | // when a captured mouse leave a scrolled window we start generate | |
1229 | // scrolling events to allow, for example, extending selection beyond the | |
1230 | // visible area in some controls | |
1231 | if ( wxWindow::GetCapture() == m_targetWindow ) | |
1232 | { | |
1233 | // where is the mouse leaving? | |
1234 | int pos, orient; | |
1235 | wxPoint pt = event.GetPosition(); | |
1236 | if ( pt.x < 0 ) | |
1237 | { | |
1238 | orient = wxHORIZONTAL; | |
1239 | pos = 0; | |
1240 | } | |
1241 | else if ( pt.y < 0 ) | |
1242 | { | |
1243 | orient = wxVERTICAL; | |
1244 | pos = 0; | |
1245 | } | |
1246 | else // we're lower or to the right of the window | |
1247 | { | |
1248 | wxSize size = m_targetWindow->GetClientSize(); | |
1249 | if ( pt.x > size.x ) | |
1250 | { | |
1251 | orient = wxHORIZONTAL; | |
1252 | pos = m_xScrollLines; | |
1253 | } | |
1254 | else if ( pt.y > size.y ) | |
1255 | { | |
1256 | orient = wxVERTICAL; | |
1257 | pos = m_yScrollLines; | |
1258 | } | |
1259 | else // this should be impossible | |
1260 | { | |
1261 | // but seems to happen sometimes under wxMSW - maybe it's a bug | |
1262 | // there but for now just ignore it | |
1263 | ||
1264 | //wxFAIL_MSG( _T("can't understand where has mouse gone") ); | |
1265 | ||
1266 | return; | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | // only start the auto scroll timer if the window can be scrolled in | |
1271 | // this direction | |
1272 | if ( !m_targetWindow->HasScrollbar(orient) ) | |
1273 | return; | |
1274 | ||
4b316329 | 1275 | #if wxUSE_TIMER |
1e6feb95 VZ |
1276 | delete m_timerAutoScroll; |
1277 | m_timerAutoScroll = new wxAutoScrollTimer | |
1278 | ( | |
1279 | m_targetWindow, this, | |
1280 | pos == 0 ? wxEVT_SCROLLWIN_LINEUP | |
1281 | : wxEVT_SCROLLWIN_LINEDOWN, | |
1282 | pos, | |
1283 | orient | |
1284 | ); | |
1285 | m_timerAutoScroll->Start(50); // FIXME: make configurable | |
534b127c WS |
1286 | #else |
1287 | wxUnusedVar(pos); | |
4b316329 | 1288 | #endif |
1e6feb95 VZ |
1289 | } |
1290 | } | |
1291 | ||
e421922f VZ |
1292 | #if wxUSE_MOUSEWHEEL |
1293 | ||
1e6feb95 VZ |
1294 | void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event) |
1295 | { | |
d2c52078 | 1296 | m_wheelRotation += event.GetWheelRotation(); |
1e6feb95 | 1297 | int lines = m_wheelRotation / event.GetWheelDelta(); |
d2c52078 RD |
1298 | m_wheelRotation -= lines * event.GetWheelDelta(); |
1299 | ||
1e6feb95 VZ |
1300 | if (lines != 0) |
1301 | { | |
1e6feb95 | 1302 | |
b6b85bdc JS |
1303 | wxScrollWinEvent newEvent; |
1304 | ||
ac6f6ffc | 1305 | newEvent.SetPosition(0); |
6682e732 | 1306 | newEvent.SetOrientation( event.GetWheelAxis() == 0 ? wxVERTICAL : wxHORIZONTAL); |
687706f5 | 1307 | newEvent.SetEventObject(m_win); |
b6b85bdc | 1308 | |
9b9337da | 1309 | if (event.IsPageScroll()) |
4b056ef5 RD |
1310 | { |
1311 | if (lines > 0) | |
687706f5 | 1312 | newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP); |
4b056ef5 | 1313 | else |
687706f5 | 1314 | newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN); |
4b056ef5 | 1315 | |
b6b85bdc | 1316 | m_win->GetEventHandler()->ProcessEvent(newEvent); |
4b056ef5 RD |
1317 | } |
1318 | else | |
1319 | { | |
1320 | lines *= event.GetLinesPerAction(); | |
1321 | if (lines > 0) | |
687706f5 | 1322 | newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP); |
4b056ef5 | 1323 | else |
687706f5 | 1324 | newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN); |
b6b85bdc | 1325 | |
4b056ef5 RD |
1326 | int times = abs(lines); |
1327 | for (; times > 0; times--) | |
1328 | m_win->GetEventHandler()->ProcessEvent(newEvent); | |
1329 | } | |
d2c52078 RD |
1330 | } |
1331 | } | |
1332 | ||
e421922f VZ |
1333 | #endif // wxUSE_MOUSEWHEEL |
1334 | ||
1e6feb95 | 1335 | // ---------------------------------------------------------------------------- |
d32e78bd | 1336 | // wxScrolledWindow implementation |
1e6feb95 VZ |
1337 | // ---------------------------------------------------------------------------- |
1338 | ||
d32e78bd | 1339 | IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel) |
1e6feb95 | 1340 | |
d32e78bd VZ |
1341 | BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel) |
1342 | EVT_PAINT(wxScrolledWindow::OnPaint) | |
349efbaa VZ |
1343 | END_EVENT_TABLE() |
1344 | ||
d32e78bd | 1345 | bool wxScrolledWindow::Create(wxWindow *parent, |
1e6feb95 VZ |
1346 | wxWindowID id, |
1347 | const wxPoint& pos, | |
1348 | const wxSize& size, | |
1349 | long style, | |
1350 | const wxString& name) | |
1351 | { | |
1352 | m_targetWindow = this; | |
8adc196b SC |
1353 | #ifdef __WXMAC__ |
1354 | MacSetClipChildren( true ) ; | |
1355 | #endif | |
1e6feb95 | 1356 | |
04b6e2f0 | 1357 | bool ok = wxPanel::Create(parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name); |
1e6feb95 | 1358 | |
1e6feb95 VZ |
1359 | return ok; |
1360 | } | |
1361 | ||
d32e78bd | 1362 | wxScrolledWindow::~wxScrolledWindow() |
1e6feb95 VZ |
1363 | { |
1364 | } | |
d2c52078 | 1365 | |
d32e78bd | 1366 | void wxScrolledWindow::OnPaint(wxPaintEvent& event) |
349efbaa VZ |
1367 | { |
1368 | // the user code didn't really draw the window if we got here, so set this | |
1369 | // flag to try to call OnDraw() later | |
1370 | m_handler->ResetDrawnFlag(); | |
1371 | ||
1372 | event.Skip(); | |
1373 | } | |
1374 | ||
0cf5b099 | 1375 | #ifdef __WXMSW__ |
d32e78bd | 1376 | WXLRESULT wxScrolledWindow::MSWWindowProc(WXUINT nMsg, |
0cf5b099 VZ |
1377 | WXWPARAM wParam, |
1378 | WXLPARAM lParam) | |
1379 | { | |
c140b7e7 | 1380 | WXLRESULT rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam); |
0cf5b099 | 1381 | |
4676948b | 1382 | #ifndef __WXWINCE__ |
0cf5b099 VZ |
1383 | // we need to process arrows ourselves for scrolling |
1384 | if ( nMsg == WM_GETDLGCODE ) | |
1385 | { | |
1386 | rc |= DLGC_WANTARROWS; | |
1387 | } | |
4676948b | 1388 | #endif |
0cf5b099 VZ |
1389 | |
1390 | return rc; | |
1391 | } | |
1392 | ||
1393 | #endif // __WXMSW__ |