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