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