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