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