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