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