]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/scrlwing.cpp | |
3 | // Purpose: wxScrolledWindow implementation | |
4 | // Author: Julian Smart | |
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. | |
7 | // Created: 01/02/97 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) wxWidgets team | |
10 | // Licence: wxWindows licence | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | // ============================================================================ | |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
21 | // For compilers that support precompilation, includes "wx.h". | |
22 | #include "wx/wxprec.h" | |
23 | ||
24 | #ifdef __BORLANDC__ | |
25 | #pragma hdrstop | |
26 | #endif | |
27 | ||
28 | #ifdef __VMS | |
29 | #define XtDisplay XTDISPLAY | |
30 | #endif | |
31 | ||
32 | #include "wx/scrolwin.h" | |
33 | ||
34 | #ifndef WX_PRECOMP | |
35 | #include "wx/utils.h" | |
36 | #include "wx/panel.h" | |
37 | #include "wx/dcclient.h" | |
38 | #if wxUSE_TIMER | |
39 | #include "wx/timer.h" | |
40 | #endif | |
41 | #include "wx/sizer.h" | |
42 | #endif | |
43 | ||
44 | #ifdef __WXMAC__ | |
45 | #include "wx/scrolbar.h" | |
46 | #endif | |
47 | ||
48 | #include "wx/recguard.h" | |
49 | ||
50 | #ifdef __WXMSW__ | |
51 | #include <windows.h> // for DLGC_WANTARROWS | |
52 | #include "wx/msw/winundef.h" | |
53 | #endif | |
54 | ||
55 | #ifdef __WXMOTIF__ | |
56 | // For wxRETAINED implementation | |
57 | #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++ | |
58 | //This code switches off the compiler warnings | |
59 | # pragma message disable nosimpint | |
60 | #endif | |
61 | #include <Xm/Xm.h> | |
62 | #ifdef __VMS__ | |
63 | # pragma message enable nosimpint | |
64 | #endif | |
65 | #endif | |
66 | ||
67 | /* | |
68 | TODO PROPERTIES | |
69 | style wxHSCROLL | wxVSCROLL | |
70 | */ | |
71 | ||
72 | // ---------------------------------------------------------------------------- | |
73 | // wxScrollHelperEvtHandler: intercept the events from the window and forward | |
74 | // them to wxScrollHelper | |
75 | // ---------------------------------------------------------------------------- | |
76 | ||
77 | class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler | |
78 | { | |
79 | public: | |
80 | wxScrollHelperEvtHandler(wxScrollHelper *scrollHelper) | |
81 | { | |
82 | m_scrollHelper = scrollHelper; | |
83 | } | |
84 | ||
85 | virtual bool ProcessEvent(wxEvent& event); | |
86 | ||
87 | void ResetDrawnFlag() { m_hasDrawnWindow = false; } | |
88 | ||
89 | private: | |
90 | wxScrollHelper *m_scrollHelper; | |
91 | ||
92 | bool m_hasDrawnWindow; | |
93 | ||
94 | DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler) | |
95 | }; | |
96 | ||
97 | #if wxUSE_TIMER | |
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; | |
118 | ||
119 | DECLARE_NO_COPY_CLASS(wxAutoScrollTimer) | |
120 | }; | |
121 | ||
122 | // ============================================================================ | |
123 | // implementation | |
124 | // ============================================================================ | |
125 | ||
126 | // ---------------------------------------------------------------------------- | |
127 | // wxAutoScrollTimer | |
128 | // ---------------------------------------------------------------------------- | |
129 | ||
130 | wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll, | |
131 | wxScrollHelper *scroll, | |
132 | wxEventType eventTypeToSend, | |
133 | int pos, int orient) | |
134 | { | |
135 | m_win = winToScroll; | |
136 | m_scrollHelper = scroll; | |
137 | m_eventType = eventTypeToSend; | |
138 | m_pos = pos; | |
139 | m_orient = orient; | |
140 | } | |
141 | ||
142 | void wxAutoScrollTimer::Notify() | |
143 | { | |
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 | } | |
182 | #endif | |
183 | ||
184 | // ---------------------------------------------------------------------------- | |
185 | // wxScrollHelperEvtHandler | |
186 | // ---------------------------------------------------------------------------- | |
187 | ||
188 | bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event) | |
189 | { | |
190 | wxEventType evType = event.GetEventType(); | |
191 | ||
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 | ||
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 ) | |
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) | |
232 | if ( m_hasDrawnWindow || event.IsCommandEvent() ) | |
233 | { | |
234 | return true; | |
235 | } | |
236 | } | |
237 | ||
238 | if ( evType == wxEVT_PAINT ) | |
239 | { | |
240 | m_scrollHelper->HandleOnPaint((wxPaintEvent &)event); | |
241 | return true; | |
242 | } | |
243 | ||
244 | if ( evType == wxEVT_CHILD_FOCUS ) | |
245 | { | |
246 | m_scrollHelper->HandleOnChildFocus((wxChildFocusEvent &)event); | |
247 | return true; | |
248 | } | |
249 | ||
250 | // reset the skipped flag (which might have been set to true in | |
251 | // ProcessEvent() above) to be able to test it below | |
252 | bool wasSkipped = event.GetSkipped(); | |
253 | if ( wasSkipped ) | |
254 | event.Skip(false); | |
255 | ||
256 | if ( evType == wxEVT_SCROLLWIN_TOP || | |
257 | evType == wxEVT_SCROLLWIN_BOTTOM || | |
258 | evType == wxEVT_SCROLLWIN_LINEUP || | |
259 | evType == wxEVT_SCROLLWIN_LINEDOWN || | |
260 | evType == wxEVT_SCROLLWIN_PAGEUP || | |
261 | evType == wxEVT_SCROLLWIN_PAGEDOWN || | |
262 | evType == wxEVT_SCROLLWIN_THUMBTRACK || | |
263 | evType == wxEVT_SCROLLWIN_THUMBRELEASE ) | |
264 | { | |
265 | m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event); | |
266 | if ( !event.GetSkipped() ) | |
267 | { | |
268 | // it makes sense to indicate that we processed the message as we | |
269 | // did scroll the window (and also notice that wxAutoScrollTimer | |
270 | // relies on our return value to stop scrolling when we are at top | |
271 | // or bottom already) | |
272 | processed = true; | |
273 | wasSkipped = false; | |
274 | } | |
275 | } | |
276 | ||
277 | if ( evType == wxEVT_ENTER_WINDOW ) | |
278 | { | |
279 | m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event); | |
280 | } | |
281 | else if ( evType == wxEVT_LEAVE_WINDOW ) | |
282 | { | |
283 | m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event); | |
284 | } | |
285 | #if wxUSE_MOUSEWHEEL | |
286 | // Use GTK's own scroll wheel handling in GtkScrolledWindow | |
287 | #ifndef __WXGTK20__ | |
288 | else if ( evType == wxEVT_MOUSEWHEEL ) | |
289 | { | |
290 | m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event); | |
291 | return true; | |
292 | } | |
293 | #endif | |
294 | #endif // wxUSE_MOUSEWHEEL | |
295 | else if ( evType == wxEVT_CHAR ) | |
296 | { | |
297 | m_scrollHelper->HandleOnChar((wxKeyEvent &)event); | |
298 | if ( !event.GetSkipped() ) | |
299 | { | |
300 | processed = true; | |
301 | wasSkipped = false; | |
302 | } | |
303 | } | |
304 | ||
305 | if ( processed ) | |
306 | event.Skip(wasSkipped); | |
307 | ||
308 | return processed; | |
309 | } | |
310 | ||
311 | // ---------------------------------------------------------------------------- | |
312 | // wxScrollHelper construction | |
313 | // ---------------------------------------------------------------------------- | |
314 | ||
315 | wxScrollHelper::wxScrollHelper(wxWindow *win) | |
316 | { | |
317 | wxASSERT_MSG( win, _T("associated window can't be NULL in wxScrollHelper") ); | |
318 | ||
319 | m_xScrollPixelsPerLine = | |
320 | m_yScrollPixelsPerLine = | |
321 | m_xScrollPosition = | |
322 | m_yScrollPosition = | |
323 | m_xScrollLines = | |
324 | m_yScrollLines = | |
325 | m_xScrollLinesPerPage = | |
326 | m_yScrollLinesPerPage = 0; | |
327 | ||
328 | m_xScrollingEnabled = | |
329 | m_yScrollingEnabled = true; | |
330 | ||
331 | m_scaleX = | |
332 | m_scaleY = 1.0; | |
333 | #if wxUSE_MOUSEWHEEL | |
334 | m_wheelRotation = 0; | |
335 | #endif | |
336 | ||
337 | m_win = | |
338 | m_targetWindow = (wxWindow *)NULL; | |
339 | ||
340 | m_timerAutoScroll = (wxTimer *)NULL; | |
341 | ||
342 | m_handler = NULL; | |
343 | ||
344 | m_win = win; | |
345 | ||
346 | m_win->SetScrollHelper( this ); | |
347 | ||
348 | // by default, the associated window is also the target window | |
349 | DoSetTargetWindow(win); | |
350 | } | |
351 | ||
352 | wxScrollHelper::~wxScrollHelper() | |
353 | { | |
354 | StopAutoScrolling(); | |
355 | ||
356 | DeleteEvtHandler(); | |
357 | } | |
358 | ||
359 | // ---------------------------------------------------------------------------- | |
360 | // setting scrolling parameters | |
361 | // ---------------------------------------------------------------------------- | |
362 | ||
363 | void wxScrollHelper::SetScrollbars(int pixelsPerUnitX, | |
364 | int pixelsPerUnitY, | |
365 | int noUnitsX, | |
366 | int noUnitsY, | |
367 | int xPos, | |
368 | int yPos, | |
369 | bool noRefresh) | |
370 | { | |
371 | int xpos, ypos; | |
372 | ||
373 | CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos); | |
374 | bool do_refresh = | |
375 | ( | |
376 | (noUnitsX != 0 && m_xScrollLines == 0) || | |
377 | (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX * noUnitsX) || | |
378 | ||
379 | (noUnitsY != 0 && m_yScrollLines == 0) || | |
380 | (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY * noUnitsY) || | |
381 | (xPos != m_xScrollPosition) || | |
382 | (yPos != m_yScrollPosition) | |
383 | ); | |
384 | ||
385 | m_xScrollPixelsPerLine = pixelsPerUnitX; | |
386 | m_yScrollPixelsPerLine = pixelsPerUnitY; | |
387 | m_xScrollPosition = xPos; | |
388 | m_yScrollPosition = yPos; | |
389 | ||
390 | int w = noUnitsX * pixelsPerUnitX; | |
391 | int h = noUnitsY * pixelsPerUnitY; | |
392 | ||
393 | // For better backward compatibility we set persisting limits | |
394 | // here not just the size. It makes SetScrollbars 'sticky' | |
395 | // emulating the old non-autoscroll behaviour. | |
396 | // m_targetWindow->SetVirtualSizeHints( w, h ); | |
397 | ||
398 | // The above should arguably be deprecated, this however we still need. | |
399 | ||
400 | // take care not to set 0 virtual size, 0 means that we don't have any | |
401 | // scrollbars and hence we should use the real size instead of the virtual | |
402 | // one which is indicated by using wxDefaultCoord | |
403 | m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord, | |
404 | h ? h : wxDefaultCoord); | |
405 | ||
406 | if (do_refresh && !noRefresh) | |
407 | m_targetWindow->Refresh(true, GetScrollRect()); | |
408 | ||
409 | #ifndef __WXUNIVERSAL__ | |
410 | // If the target is not the same as the window with the scrollbars, | |
411 | // then we need to update the scrollbars here, since they won't have | |
412 | // been updated by SetVirtualSize(). | |
413 | if ( m_targetWindow != m_win ) | |
414 | #endif // !__WXUNIVERSAL__ | |
415 | { | |
416 | AdjustScrollbars(); | |
417 | } | |
418 | #ifndef __WXUNIVERSAL__ | |
419 | else | |
420 | { | |
421 | // otherwise this has been done by AdjustScrollbars, above | |
422 | } | |
423 | #endif // !__WXUNIVERSAL__ | |
424 | } | |
425 | ||
426 | // ---------------------------------------------------------------------------- | |
427 | // [target] window handling | |
428 | // ---------------------------------------------------------------------------- | |
429 | ||
430 | void wxScrollHelper::DeleteEvtHandler() | |
431 | { | |
432 | // search for m_handler in the handler list | |
433 | if ( m_win && m_handler ) | |
434 | { | |
435 | if ( m_win->RemoveEventHandler(m_handler) ) | |
436 | { | |
437 | delete m_handler; | |
438 | } | |
439 | //else: something is very wrong, so better [maybe] leak memory than | |
440 | // risk a crash because of double deletion | |
441 | ||
442 | m_handler = NULL; | |
443 | } | |
444 | } | |
445 | ||
446 | void wxScrollHelper::DoSetTargetWindow(wxWindow *target) | |
447 | { | |
448 | m_targetWindow = target; | |
449 | #ifdef __WXMAC__ | |
450 | target->MacSetClipChildren( true ) ; | |
451 | #endif | |
452 | ||
453 | // install the event handler which will intercept the events we're | |
454 | // interested in (but only do it for our real window, not the target window | |
455 | // which we scroll - we don't need to hijack its events) | |
456 | if ( m_targetWindow == m_win ) | |
457 | { | |
458 | // if we already have a handler, delete it first | |
459 | DeleteEvtHandler(); | |
460 | ||
461 | m_handler = new wxScrollHelperEvtHandler(this); | |
462 | m_targetWindow->PushEventHandler(m_handler); | |
463 | } | |
464 | } | |
465 | ||
466 | void wxScrollHelper::SetTargetWindow(wxWindow *target) | |
467 | { | |
468 | wxCHECK_RET( target, wxT("target window must not be NULL") ); | |
469 | ||
470 | if ( target == m_targetWindow ) | |
471 | return; | |
472 | ||
473 | DoSetTargetWindow(target); | |
474 | } | |
475 | ||
476 | wxWindow *wxScrollHelper::GetTargetWindow() const | |
477 | { | |
478 | return m_targetWindow; | |
479 | } | |
480 | ||
481 | // ---------------------------------------------------------------------------- | |
482 | // scrolling implementation itself | |
483 | // ---------------------------------------------------------------------------- | |
484 | ||
485 | void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event) | |
486 | { | |
487 | int nScrollInc = CalcScrollInc(event); | |
488 | if ( nScrollInc == 0 ) | |
489 | { | |
490 | // can't scroll further | |
491 | event.Skip(); | |
492 | ||
493 | return; | |
494 | } | |
495 | ||
496 | bool needsRefresh = false; | |
497 | int dx = 0, | |
498 | dy = 0; | |
499 | int orient = event.GetOrientation(); | |
500 | if (orient == wxHORIZONTAL) | |
501 | { | |
502 | if ( m_xScrollingEnabled ) | |
503 | { | |
504 | dx = -m_xScrollPixelsPerLine * nScrollInc; | |
505 | } | |
506 | else | |
507 | { | |
508 | needsRefresh = true; | |
509 | } | |
510 | } | |
511 | else | |
512 | { | |
513 | if ( m_yScrollingEnabled ) | |
514 | { | |
515 | dy = -m_yScrollPixelsPerLine * nScrollInc; | |
516 | } | |
517 | else | |
518 | { | |
519 | needsRefresh = true; | |
520 | } | |
521 | } | |
522 | ||
523 | if ( !needsRefresh ) | |
524 | { | |
525 | // flush all pending repaints before we change m_{x,y}ScrollPosition, as | |
526 | // otherwise invalidated area could be updated incorrectly later when | |
527 | // ScrollWindow() makes sure they're repainted before scrolling them | |
528 | #ifdef __WXMAC__ | |
529 | // wxWindowMac is taking care of making sure the update area is correctly | |
530 | // set up, while not forcing an immediate redraw | |
531 | #else | |
532 | m_targetWindow->Update(); | |
533 | #endif | |
534 | } | |
535 | ||
536 | if (orient == wxHORIZONTAL) | |
537 | { | |
538 | m_xScrollPosition += nScrollInc; | |
539 | m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition); | |
540 | } | |
541 | else | |
542 | { | |
543 | m_yScrollPosition += nScrollInc; | |
544 | m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition); | |
545 | } | |
546 | ||
547 | if ( needsRefresh ) | |
548 | { | |
549 | m_targetWindow->Refresh(true, GetScrollRect()); | |
550 | } | |
551 | else | |
552 | { | |
553 | m_targetWindow->ScrollWindow(dx, dy, GetScrollRect()); | |
554 | } | |
555 | } | |
556 | ||
557 | int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event) | |
558 | { | |
559 | int pos = event.GetPosition(); | |
560 | int orient = event.GetOrientation(); | |
561 | ||
562 | int nScrollInc = 0; | |
563 | if (event.GetEventType() == wxEVT_SCROLLWIN_TOP) | |
564 | { | |
565 | if (orient == wxHORIZONTAL) | |
566 | nScrollInc = - m_xScrollPosition; | |
567 | else | |
568 | nScrollInc = - m_yScrollPosition; | |
569 | } else | |
570 | if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM) | |
571 | { | |
572 | if (orient == wxHORIZONTAL) | |
573 | nScrollInc = m_xScrollLines - m_xScrollPosition; | |
574 | else | |
575 | nScrollInc = m_yScrollLines - m_yScrollPosition; | |
576 | } else | |
577 | if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP) | |
578 | { | |
579 | nScrollInc = -1; | |
580 | } else | |
581 | if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN) | |
582 | { | |
583 | nScrollInc = 1; | |
584 | } else | |
585 | if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP) | |
586 | { | |
587 | if (orient == wxHORIZONTAL) | |
588 | nScrollInc = -GetScrollPageSize(wxHORIZONTAL); | |
589 | else | |
590 | nScrollInc = -GetScrollPageSize(wxVERTICAL); | |
591 | } else | |
592 | if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN) | |
593 | { | |
594 | if (orient == wxHORIZONTAL) | |
595 | nScrollInc = GetScrollPageSize(wxHORIZONTAL); | |
596 | else | |
597 | nScrollInc = GetScrollPageSize(wxVERTICAL); | |
598 | } else | |
599 | if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) || | |
600 | (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE)) | |
601 | { | |
602 | if (orient == wxHORIZONTAL) | |
603 | nScrollInc = pos - m_xScrollPosition; | |
604 | else | |
605 | nScrollInc = pos - m_yScrollPosition; | |
606 | } | |
607 | ||
608 | if (orient == wxHORIZONTAL) | |
609 | { | |
610 | if ( m_xScrollPosition + nScrollInc < 0 ) | |
611 | { | |
612 | // As -ve as we can go | |
613 | nScrollInc = -m_xScrollPosition; | |
614 | } | |
615 | else // check for the other bound | |
616 | { | |
617 | const int posMax = m_xScrollLines - m_xScrollLinesPerPage; | |
618 | if ( m_xScrollPosition + nScrollInc > posMax ) | |
619 | { | |
620 | // As +ve as we can go | |
621 | nScrollInc = posMax - m_xScrollPosition; | |
622 | } | |
623 | } | |
624 | } | |
625 | else // wxVERTICAL | |
626 | { | |
627 | if ( m_yScrollPosition + nScrollInc < 0 ) | |
628 | { | |
629 | // As -ve as we can go | |
630 | nScrollInc = -m_yScrollPosition; | |
631 | } | |
632 | else // check for the other bound | |
633 | { | |
634 | const int posMax = m_yScrollLines - m_yScrollLinesPerPage; | |
635 | if ( m_yScrollPosition + nScrollInc > posMax ) | |
636 | { | |
637 | // As +ve as we can go | |
638 | nScrollInc = posMax - m_yScrollPosition; | |
639 | } | |
640 | } | |
641 | } | |
642 | ||
643 | return nScrollInc; | |
644 | } | |
645 | ||
646 | // Adjust the scrollbars - new version. | |
647 | void wxScrollHelper::AdjustScrollbars() | |
648 | { | |
649 | static wxRecursionGuardFlag s_flagReentrancy; | |
650 | wxRecursionGuard guard(s_flagReentrancy); | |
651 | if ( guard.IsInside() ) | |
652 | { | |
653 | // don't reenter AdjustScrollbars() while another call to | |
654 | // AdjustScrollbars() is in progress because this may lead to calling | |
655 | // ScrollWindow() twice and this can really happen under MSW if | |
656 | // SetScrollbar() call below adds or removes the scrollbar which | |
657 | // changes the window size and hence results in another | |
658 | // AdjustScrollbars() call | |
659 | return; | |
660 | } | |
661 | ||
662 | int w = 0, h = 0; | |
663 | int oldw, oldh; | |
664 | ||
665 | int oldXScroll = m_xScrollPosition; | |
666 | int oldYScroll = m_yScrollPosition; | |
667 | ||
668 | // VZ: at least under Windows this loop is useless because when scrollbars | |
669 | // [dis]appear we get a WM_SIZE resulting in another call to | |
670 | // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave | |
671 | // it here for now but it would be better to ensure that all ports | |
672 | // generate EVT_SIZE when scrollbars [dis]appear, emulating it if | |
673 | // necessary, and remove it later | |
674 | // JACS: Stop potential infinite loop by limiting number of iterations | |
675 | int iterationCount = 0; | |
676 | const int iterationMax = 5; | |
677 | do | |
678 | { | |
679 | iterationCount ++; | |
680 | ||
681 | GetTargetSize(&w, 0); | |
682 | ||
683 | // scroll lines per page: if 0, no scrolling is needed | |
684 | int linesPerPage; | |
685 | ||
686 | if ( m_xScrollPixelsPerLine == 0 ) | |
687 | { | |
688 | // scrolling is disabled | |
689 | m_xScrollLines = 0; | |
690 | m_xScrollPosition = 0; | |
691 | linesPerPage = 0; | |
692 | } | |
693 | else // might need scrolling | |
694 | { | |
695 | // Round up integer division to catch any "leftover" client space. | |
696 | const int wVirt = m_targetWindow->GetVirtualSize().GetWidth(); | |
697 | m_xScrollLines = (wVirt + m_xScrollPixelsPerLine - 1) / m_xScrollPixelsPerLine; | |
698 | ||
699 | // Calculate page size i.e. number of scroll units you get on the | |
700 | // current client window. | |
701 | linesPerPage = w / m_xScrollPixelsPerLine; | |
702 | ||
703 | // Special case. When client and virtual size are very close but | |
704 | // the client is big enough, kill scrollbar. | |
705 | if ((linesPerPage < m_xScrollLines) && (w >= wVirt)) ++linesPerPage; | |
706 | ||
707 | if (linesPerPage >= m_xScrollLines) | |
708 | { | |
709 | // we're big enough to not need scrolling | |
710 | linesPerPage = | |
711 | m_xScrollLines = | |
712 | m_xScrollPosition = 0; | |
713 | } | |
714 | else // we do need a scrollbar | |
715 | { | |
716 | if ( linesPerPage < 1 ) | |
717 | linesPerPage = 1; | |
718 | ||
719 | // Correct position if greater than extent of canvas minus | |
720 | // the visible portion of it or if below zero | |
721 | const int posMax = m_xScrollLines - linesPerPage; | |
722 | if ( m_xScrollPosition > posMax ) | |
723 | m_xScrollPosition = posMax; | |
724 | else if ( m_xScrollPosition < 0 ) | |
725 | m_xScrollPosition = 0; | |
726 | } | |
727 | } | |
728 | ||
729 | m_win->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, | |
730 | linesPerPage, m_xScrollLines); | |
731 | ||
732 | // The amount by which we scroll when paging | |
733 | SetScrollPageSize(wxHORIZONTAL, linesPerPage); | |
734 | ||
735 | GetTargetSize(0, &h); | |
736 | ||
737 | if ( m_yScrollPixelsPerLine == 0 ) | |
738 | { | |
739 | // scrolling is disabled | |
740 | m_yScrollLines = 0; | |
741 | m_yScrollPosition = 0; | |
742 | linesPerPage = 0; | |
743 | } | |
744 | else // might need scrolling | |
745 | { | |
746 | // Round up integer division to catch any "leftover" client space. | |
747 | const int hVirt = m_targetWindow->GetVirtualSize().GetHeight(); | |
748 | m_yScrollLines = ( hVirt + m_yScrollPixelsPerLine - 1 ) / m_yScrollPixelsPerLine; | |
749 | ||
750 | // Calculate page size i.e. number of scroll units you get on the | |
751 | // current client window. | |
752 | linesPerPage = h / m_yScrollPixelsPerLine; | |
753 | ||
754 | // Special case. When client and virtual size are very close but | |
755 | // the client is big enough, kill scrollbar. | |
756 | if ((linesPerPage < m_yScrollLines) && (h >= hVirt)) ++linesPerPage; | |
757 | ||
758 | if (linesPerPage >= m_yScrollLines) | |
759 | { | |
760 | // we're big enough to not need scrolling | |
761 | linesPerPage = | |
762 | m_yScrollLines = | |
763 | m_yScrollPosition = 0; | |
764 | } | |
765 | else // we do need a scrollbar | |
766 | { | |
767 | if ( linesPerPage < 1 ) | |
768 | linesPerPage = 1; | |
769 | ||
770 | // Correct position if greater than extent of canvas minus | |
771 | // the visible portion of it or if below zero | |
772 | const int posMax = m_yScrollLines - linesPerPage; | |
773 | if ( m_yScrollPosition > posMax ) | |
774 | m_yScrollPosition = posMax; | |
775 | else if ( m_yScrollPosition < 0 ) | |
776 | m_yScrollPosition = 0; | |
777 | } | |
778 | } | |
779 | ||
780 | m_win->SetScrollbar(wxVERTICAL, m_yScrollPosition, | |
781 | linesPerPage, m_yScrollLines); | |
782 | ||
783 | // The amount by which we scroll when paging | |
784 | SetScrollPageSize(wxVERTICAL, linesPerPage); | |
785 | ||
786 | ||
787 | // If a scrollbar (dis)appeared as a result of this, adjust them again. | |
788 | oldw = w; | |
789 | oldh = h; | |
790 | ||
791 | GetTargetSize( &w, &h ); | |
792 | } while ( (w != oldw || h != oldh) && (iterationCount < iterationMax) ); | |
793 | ||
794 | #ifdef __WXMOTIF__ | |
795 | // Sorry, some Motif-specific code to implement a backing pixmap | |
796 | // for the wxRETAINED style. Implementing a backing store can't | |
797 | // be entirely generic because it relies on the wxWindowDC implementation | |
798 | // to duplicate X drawing calls for the backing pixmap. | |
799 | ||
800 | if ( m_targetWindow->GetWindowStyle() & wxRETAINED ) | |
801 | { | |
802 | Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget()); | |
803 | ||
804 | int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine; | |
805 | int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine; | |
806 | if (m_targetWindow->GetBackingPixmap() && | |
807 | !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) && | |
808 | (m_targetWindow->GetPixmapHeight() == totalPixelHeight))) | |
809 | { | |
810 | XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap()); | |
811 | m_targetWindow->SetBackingPixmap((WXPixmap) 0); | |
812 | } | |
813 | ||
814 | if (!m_targetWindow->GetBackingPixmap() && | |
815 | (m_xScrollLines != 0) && (m_yScrollLines != 0)) | |
816 | { | |
817 | int depth = wxDisplayDepth(); | |
818 | m_targetWindow->SetPixmapWidth(totalPixelWidth); | |
819 | m_targetWindow->SetPixmapHeight(totalPixelHeight); | |
820 | m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)), | |
821 | m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth)); | |
822 | } | |
823 | ||
824 | } | |
825 | #endif // Motif | |
826 | ||
827 | if (oldXScroll != m_xScrollPosition) | |
828 | { | |
829 | if (m_xScrollingEnabled) | |
830 | m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0, | |
831 | GetScrollRect() ); | |
832 | else | |
833 | m_targetWindow->Refresh(true, GetScrollRect()); | |
834 | } | |
835 | ||
836 | if (oldYScroll != m_yScrollPosition) | |
837 | { | |
838 | if (m_yScrollingEnabled) | |
839 | m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), | |
840 | GetScrollRect() ); | |
841 | else | |
842 | m_targetWindow->Refresh(true, GetScrollRect()); | |
843 | } | |
844 | } | |
845 | ||
846 | void wxScrollHelper::DoPrepareDC(wxDC& dc) | |
847 | { | |
848 | wxPoint pt = dc.GetDeviceOrigin(); | |
849 | #ifdef __WXGTK__ | |
850 | // It may actually be correct to always query | |
851 | // the m_sign from the DC here, but I leve the | |
852 | // #ifdef GTK for now. | |
853 | if (m_win->GetLayoutDirection() == wxLayout_RightToLeft) | |
854 | dc.SetDeviceOrigin( pt.x + m_xScrollPosition * m_xScrollPixelsPerLine, | |
855 | pt.y - m_yScrollPosition * m_yScrollPixelsPerLine ); | |
856 | else | |
857 | #endif | |
858 | dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine, | |
859 | pt.y - m_yScrollPosition * m_yScrollPixelsPerLine ); | |
860 | dc.SetUserScale( m_scaleX, m_scaleY ); | |
861 | } | |
862 | ||
863 | void wxScrollHelper::SetScrollRate( int xstep, int ystep ) | |
864 | { | |
865 | int old_x = m_xScrollPixelsPerLine * m_xScrollPosition; | |
866 | int old_y = m_yScrollPixelsPerLine * m_yScrollPosition; | |
867 | ||
868 | m_xScrollPixelsPerLine = xstep; | |
869 | m_yScrollPixelsPerLine = ystep; | |
870 | ||
871 | int new_x = m_xScrollPixelsPerLine * m_xScrollPosition; | |
872 | int new_y = m_yScrollPixelsPerLine * m_yScrollPosition; | |
873 | ||
874 | m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition ); | |
875 | m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition ); | |
876 | m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y ); | |
877 | ||
878 | AdjustScrollbars(); | |
879 | } | |
880 | ||
881 | void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const | |
882 | { | |
883 | if ( x_unit ) | |
884 | *x_unit = m_xScrollPixelsPerLine; | |
885 | if ( y_unit ) | |
886 | *y_unit = m_yScrollPixelsPerLine; | |
887 | } | |
888 | ||
889 | ||
890 | int wxScrollHelper::GetScrollLines( int orient ) const | |
891 | { | |
892 | if ( orient == wxHORIZONTAL ) | |
893 | return m_xScrollLines; | |
894 | else | |
895 | return m_yScrollLines; | |
896 | } | |
897 | ||
898 | int wxScrollHelper::GetScrollPageSize(int orient) const | |
899 | { | |
900 | if ( orient == wxHORIZONTAL ) | |
901 | return m_xScrollLinesPerPage; | |
902 | else | |
903 | return m_yScrollLinesPerPage; | |
904 | } | |
905 | ||
906 | void wxScrollHelper::SetScrollPageSize(int orient, int pageSize) | |
907 | { | |
908 | if ( orient == wxHORIZONTAL ) | |
909 | m_xScrollLinesPerPage = pageSize; | |
910 | else | |
911 | m_yScrollLinesPerPage = pageSize; | |
912 | } | |
913 | ||
914 | /* | |
915 | * Scroll to given position (scroll position, not pixel position) | |
916 | */ | |
917 | void wxScrollHelper::Scroll( int x_pos, int y_pos ) | |
918 | { | |
919 | if (!m_targetWindow) | |
920 | return; | |
921 | ||
922 | if (((x_pos == -1) || (x_pos == m_xScrollPosition)) && | |
923 | ((y_pos == -1) || (y_pos == m_yScrollPosition))) return; | |
924 | ||
925 | int w = 0, h = 0; | |
926 | GetTargetSize(&w, &h); | |
927 | ||
928 | // compute new position: | |
929 | int new_x = m_xScrollPosition; | |
930 | int new_y = m_yScrollPosition; | |
931 | ||
932 | if ((x_pos != -1) && (m_xScrollPixelsPerLine)) | |
933 | { | |
934 | new_x = x_pos; | |
935 | ||
936 | // Calculate page size i.e. number of scroll units you get on the | |
937 | // current client window | |
938 | int noPagePositions = w/m_xScrollPixelsPerLine; | |
939 | if (noPagePositions < 1) noPagePositions = 1; | |
940 | ||
941 | // Correct position if greater than extent of canvas minus | |
942 | // the visible portion of it or if below zero | |
943 | new_x = wxMin( m_xScrollLines-noPagePositions, new_x ); | |
944 | new_x = wxMax( 0, new_x ); | |
945 | } | |
946 | if ((y_pos != -1) && (m_yScrollPixelsPerLine)) | |
947 | { | |
948 | new_y = y_pos; | |
949 | ||
950 | // Calculate page size i.e. number of scroll units you get on the | |
951 | // current client window | |
952 | int noPagePositions = h/m_yScrollPixelsPerLine; | |
953 | if (noPagePositions < 1) noPagePositions = 1; | |
954 | ||
955 | // Correct position if greater than extent of canvas minus | |
956 | // the visible portion of it or if below zero | |
957 | new_y = wxMin( m_yScrollLines-noPagePositions, new_y ); | |
958 | new_y = wxMax( 0, new_y ); | |
959 | } | |
960 | ||
961 | if ( new_x == m_xScrollPosition && new_y == m_yScrollPosition ) | |
962 | return; // nothing to do, the position didn't change | |
963 | ||
964 | // flush all pending repaints before we change m_{x,y}ScrollPosition, as | |
965 | // otherwise invalidated area could be updated incorrectly later when | |
966 | // ScrollWindow() makes sure they're repainted before scrolling them | |
967 | m_targetWindow->Update(); | |
968 | ||
969 | // update the position and scroll the window now: | |
970 | if (m_xScrollPosition != new_x) | |
971 | { | |
972 | int old_x = m_xScrollPosition; | |
973 | m_xScrollPosition = new_x; | |
974 | m_win->SetScrollPos( wxHORIZONTAL, new_x ); | |
975 | m_targetWindow->ScrollWindow( (old_x-new_x)*m_xScrollPixelsPerLine, 0, | |
976 | GetScrollRect() ); | |
977 | } | |
978 | ||
979 | if (m_yScrollPosition != new_y) | |
980 | { | |
981 | int old_y = m_yScrollPosition; | |
982 | m_yScrollPosition = new_y; | |
983 | m_win->SetScrollPos( wxVERTICAL, new_y ); | |
984 | m_targetWindow->ScrollWindow( 0, (old_y-new_y)*m_yScrollPixelsPerLine, | |
985 | GetScrollRect() ); | |
986 | } | |
987 | } | |
988 | ||
989 | void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll) | |
990 | { | |
991 | m_xScrollingEnabled = x_scroll; | |
992 | m_yScrollingEnabled = y_scroll; | |
993 | } | |
994 | ||
995 | // Where the current view starts from | |
996 | void wxScrollHelper::GetViewStart (int *x, int *y) const | |
997 | { | |
998 | if ( x ) | |
999 | *x = m_xScrollPosition; | |
1000 | if ( y ) | |
1001 | *y = m_yScrollPosition; | |
1002 | } | |
1003 | ||
1004 | void wxScrollHelper::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const | |
1005 | { | |
1006 | if ( xx ) | |
1007 | *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine; | |
1008 | if ( yy ) | |
1009 | *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine; | |
1010 | } | |
1011 | ||
1012 | void wxScrollHelper::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const | |
1013 | { | |
1014 | if ( xx ) | |
1015 | *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine; | |
1016 | if ( yy ) | |
1017 | *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine; | |
1018 | } | |
1019 | ||
1020 | // ---------------------------------------------------------------------------- | |
1021 | // geometry | |
1022 | // ---------------------------------------------------------------------------- | |
1023 | ||
1024 | bool wxScrollHelper::ScrollLayout() | |
1025 | { | |
1026 | if ( m_win->GetSizer() && m_targetWindow == m_win ) | |
1027 | { | |
1028 | // If we're the scroll target, take into account the | |
1029 | // virtual size and scrolled position of the window. | |
1030 | ||
1031 | int x = 0, y = 0, w = 0, h = 0; | |
1032 | CalcScrolledPosition(0,0, &x,&y); | |
1033 | m_win->GetVirtualSize(&w, &h); | |
1034 | m_win->GetSizer()->SetDimension(x, y, w, h); | |
1035 | return true; | |
1036 | } | |
1037 | ||
1038 | // fall back to default for LayoutConstraints | |
1039 | return m_win->wxWindow::Layout(); | |
1040 | } | |
1041 | ||
1042 | void wxScrollHelper::ScrollDoSetVirtualSize(int x, int y) | |
1043 | { | |
1044 | m_win->wxWindow::DoSetVirtualSize( x, y ); | |
1045 | AdjustScrollbars(); | |
1046 | ||
1047 | if (m_win->GetAutoLayout()) | |
1048 | m_win->Layout(); | |
1049 | } | |
1050 | ||
1051 | // wxWindow's GetBestVirtualSize returns the actual window size, | |
1052 | // whereas we want to return the virtual size | |
1053 | wxSize wxScrollHelper::ScrollGetBestVirtualSize() const | |
1054 | { | |
1055 | wxSize clientSize(m_win->GetClientSize()); | |
1056 | if ( m_win->GetSizer() ) | |
1057 | clientSize.IncTo(m_win->GetSizer()->CalcMin()); | |
1058 | ||
1059 | return clientSize; | |
1060 | } | |
1061 | ||
1062 | // return the window best size from the given best virtual size | |
1063 | wxSize | |
1064 | wxScrollHelper::ScrollGetWindowSizeForVirtualSize(const wxSize& size) const | |
1065 | { | |
1066 | // Only use the content to set the window size in the direction | |
1067 | // where there's no scrolling; otherwise we're going to get a huge | |
1068 | // window in the direction in which scrolling is enabled | |
1069 | int ppuX, ppuY; | |
1070 | GetScrollPixelsPerUnit(&ppuX, &ppuY); | |
1071 | ||
1072 | wxSize minSize = m_win->GetMinSize(); | |
1073 | if ( !minSize.IsFullySpecified() ) | |
1074 | minSize = m_win->GetSize(); | |
1075 | ||
1076 | wxSize best(size); | |
1077 | if (ppuX > 0) | |
1078 | best.x = minSize.x; | |
1079 | if (ppuY > 0) | |
1080 | best.y = minSize.y; | |
1081 | ||
1082 | return best; | |
1083 | } | |
1084 | ||
1085 | // ---------------------------------------------------------------------------- | |
1086 | // event handlers | |
1087 | // ---------------------------------------------------------------------------- | |
1088 | ||
1089 | // Default OnSize resets scrollbars, if any | |
1090 | void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event)) | |
1091 | { | |
1092 | if ( m_targetWindow->GetAutoLayout() ) | |
1093 | { | |
1094 | wxSize size = m_targetWindow->GetBestVirtualSize(); | |
1095 | ||
1096 | // This will call ::Layout() and ::AdjustScrollbars() | |
1097 | m_win->SetVirtualSize( size ); | |
1098 | } | |
1099 | else | |
1100 | { | |
1101 | AdjustScrollbars(); | |
1102 | } | |
1103 | } | |
1104 | ||
1105 | // This calls OnDraw, having adjusted the origin according to the current | |
1106 | // scroll position | |
1107 | void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event)) | |
1108 | { | |
1109 | // don't use m_targetWindow here, this is always called for ourselves | |
1110 | wxPaintDC dc(m_win); | |
1111 | DoPrepareDC(dc); | |
1112 | ||
1113 | OnDraw(dc); | |
1114 | } | |
1115 | ||
1116 | // kbd handling: notice that we use OnChar() and not OnKeyDown() for | |
1117 | // compatibility here - if we used OnKeyDown(), the programs which process | |
1118 | // arrows themselves in their OnChar() would never get the message and like | |
1119 | // this they always have the priority | |
1120 | void wxScrollHelper::HandleOnChar(wxKeyEvent& event) | |
1121 | { | |
1122 | int stx = 0, sty = 0, // view origin | |
1123 | szx = 0, szy = 0, // view size (total) | |
1124 | clix = 0, cliy = 0; // view size (on screen) | |
1125 | ||
1126 | GetViewStart(&stx, &sty); | |
1127 | GetTargetSize(&clix, &cliy); | |
1128 | m_targetWindow->GetVirtualSize(&szx, &szy); | |
1129 | ||
1130 | if( m_xScrollPixelsPerLine ) | |
1131 | { | |
1132 | clix /= m_xScrollPixelsPerLine; | |
1133 | szx /= m_xScrollPixelsPerLine; | |
1134 | } | |
1135 | else | |
1136 | { | |
1137 | clix = 0; | |
1138 | szx = -1; | |
1139 | } | |
1140 | if( m_yScrollPixelsPerLine ) | |
1141 | { | |
1142 | cliy /= m_yScrollPixelsPerLine; | |
1143 | szy /= m_yScrollPixelsPerLine; | |
1144 | } | |
1145 | else | |
1146 | { | |
1147 | cliy = 0; | |
1148 | szy = -1; | |
1149 | } | |
1150 | ||
1151 | int xScrollOld = m_xScrollPosition, | |
1152 | yScrollOld = m_yScrollPosition; | |
1153 | ||
1154 | int dsty; | |
1155 | switch ( event.GetKeyCode() ) | |
1156 | { | |
1157 | case WXK_PAGEUP: | |
1158 | dsty = sty - (5 * cliy / 6); | |
1159 | Scroll(-1, (dsty == -1) ? 0 : dsty); | |
1160 | break; | |
1161 | ||
1162 | case WXK_PAGEDOWN: | |
1163 | Scroll(-1, sty + (5 * cliy / 6)); | |
1164 | break; | |
1165 | ||
1166 | case WXK_HOME: | |
1167 | Scroll(0, event.ControlDown() ? 0 : -1); | |
1168 | break; | |
1169 | ||
1170 | case WXK_END: | |
1171 | Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1); | |
1172 | break; | |
1173 | ||
1174 | case WXK_UP: | |
1175 | Scroll(-1, sty - 1); | |
1176 | break; | |
1177 | ||
1178 | case WXK_DOWN: | |
1179 | Scroll(-1, sty + 1); | |
1180 | break; | |
1181 | ||
1182 | case WXK_LEFT: | |
1183 | Scroll(stx - 1, -1); | |
1184 | break; | |
1185 | ||
1186 | case WXK_RIGHT: | |
1187 | Scroll(stx + 1, -1); | |
1188 | break; | |
1189 | ||
1190 | default: | |
1191 | // not for us | |
1192 | event.Skip(); | |
1193 | } | |
1194 | ||
1195 | if ( m_xScrollPosition != xScrollOld ) | |
1196 | { | |
1197 | wxScrollWinEvent evt(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition, | |
1198 | wxHORIZONTAL); | |
1199 | evt.SetEventObject(m_win); | |
1200 | m_win->GetEventHandler()->ProcessEvent(evt); | |
1201 | } | |
1202 | ||
1203 | if ( m_yScrollPosition != yScrollOld ) | |
1204 | { | |
1205 | wxScrollWinEvent evt(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition, | |
1206 | wxVERTICAL); | |
1207 | evt.SetEventObject(m_win); | |
1208 | m_win->GetEventHandler()->ProcessEvent(evt); | |
1209 | } | |
1210 | } | |
1211 | ||
1212 | // ---------------------------------------------------------------------------- | |
1213 | // autoscroll stuff: these functions deal with sending fake scroll events when | |
1214 | // a captured mouse is being held outside the window | |
1215 | // ---------------------------------------------------------------------------- | |
1216 | ||
1217 | bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const | |
1218 | { | |
1219 | // only send the event if the window is scrollable in this direction | |
1220 | wxWindow *win = (wxWindow *)event.GetEventObject(); | |
1221 | return win->HasScrollbar(event.GetOrientation()); | |
1222 | } | |
1223 | ||
1224 | void wxScrollHelper::StopAutoScrolling() | |
1225 | { | |
1226 | #if wxUSE_TIMER | |
1227 | if ( m_timerAutoScroll ) | |
1228 | { | |
1229 | delete m_timerAutoScroll; | |
1230 | m_timerAutoScroll = (wxTimer *)NULL; | |
1231 | } | |
1232 | #endif | |
1233 | } | |
1234 | ||
1235 | void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event) | |
1236 | { | |
1237 | StopAutoScrolling(); | |
1238 | ||
1239 | event.Skip(); | |
1240 | } | |
1241 | ||
1242 | void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event) | |
1243 | { | |
1244 | // don't prevent the usual processing of the event from taking place | |
1245 | event.Skip(); | |
1246 | ||
1247 | // when a captured mouse leave a scrolled window we start generate | |
1248 | // scrolling events to allow, for example, extending selection beyond the | |
1249 | // visible area in some controls | |
1250 | if ( wxWindow::GetCapture() == m_targetWindow ) | |
1251 | { | |
1252 | // where is the mouse leaving? | |
1253 | int pos, orient; | |
1254 | wxPoint pt = event.GetPosition(); | |
1255 | if ( pt.x < 0 ) | |
1256 | { | |
1257 | orient = wxHORIZONTAL; | |
1258 | pos = 0; | |
1259 | } | |
1260 | else if ( pt.y < 0 ) | |
1261 | { | |
1262 | orient = wxVERTICAL; | |
1263 | pos = 0; | |
1264 | } | |
1265 | else // we're lower or to the right of the window | |
1266 | { | |
1267 | wxSize size = m_targetWindow->GetClientSize(); | |
1268 | if ( pt.x > size.x ) | |
1269 | { | |
1270 | orient = wxHORIZONTAL; | |
1271 | pos = m_xScrollLines; | |
1272 | } | |
1273 | else if ( pt.y > size.y ) | |
1274 | { | |
1275 | orient = wxVERTICAL; | |
1276 | pos = m_yScrollLines; | |
1277 | } | |
1278 | else // this should be impossible | |
1279 | { | |
1280 | // but seems to happen sometimes under wxMSW - maybe it's a bug | |
1281 | // there but for now just ignore it | |
1282 | ||
1283 | //wxFAIL_MSG( _T("can't understand where has mouse gone") ); | |
1284 | ||
1285 | return; | |
1286 | } | |
1287 | } | |
1288 | ||
1289 | // only start the auto scroll timer if the window can be scrolled in | |
1290 | // this direction | |
1291 | if ( !m_targetWindow->HasScrollbar(orient) ) | |
1292 | return; | |
1293 | ||
1294 | #if wxUSE_TIMER | |
1295 | delete m_timerAutoScroll; | |
1296 | m_timerAutoScroll = new wxAutoScrollTimer | |
1297 | ( | |
1298 | m_targetWindow, this, | |
1299 | pos == 0 ? wxEVT_SCROLLWIN_LINEUP | |
1300 | : wxEVT_SCROLLWIN_LINEDOWN, | |
1301 | pos, | |
1302 | orient | |
1303 | ); | |
1304 | m_timerAutoScroll->Start(50); // FIXME: make configurable | |
1305 | #else | |
1306 | wxUnusedVar(pos); | |
1307 | #endif | |
1308 | } | |
1309 | } | |
1310 | ||
1311 | #if wxUSE_MOUSEWHEEL | |
1312 | ||
1313 | void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event) | |
1314 | { | |
1315 | m_wheelRotation += event.GetWheelRotation(); | |
1316 | int lines = m_wheelRotation / event.GetWheelDelta(); | |
1317 | m_wheelRotation -= lines * event.GetWheelDelta(); | |
1318 | ||
1319 | if (lines != 0) | |
1320 | { | |
1321 | ||
1322 | wxScrollWinEvent newEvent; | |
1323 | ||
1324 | newEvent.SetPosition(0); | |
1325 | newEvent.SetOrientation( event.GetWheelAxis() == 0 ? wxVERTICAL : wxHORIZONTAL); | |
1326 | newEvent.SetEventObject(m_win); | |
1327 | ||
1328 | if (event.IsPageScroll()) | |
1329 | { | |
1330 | if (lines > 0) | |
1331 | newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP); | |
1332 | else | |
1333 | newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN); | |
1334 | ||
1335 | m_win->GetEventHandler()->ProcessEvent(newEvent); | |
1336 | } | |
1337 | else | |
1338 | { | |
1339 | lines *= event.GetLinesPerAction(); | |
1340 | if (lines > 0) | |
1341 | newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP); | |
1342 | else | |
1343 | newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN); | |
1344 | ||
1345 | int times = abs(lines); | |
1346 | for (; times > 0; times--) | |
1347 | m_win->GetEventHandler()->ProcessEvent(newEvent); | |
1348 | } | |
1349 | } | |
1350 | } | |
1351 | ||
1352 | #endif // wxUSE_MOUSEWHEEL | |
1353 | ||
1354 | void wxScrollHelper::HandleOnChildFocus(wxChildFocusEvent& event) | |
1355 | { | |
1356 | // this event should be processed by all windows in parenthood chain, | |
1357 | // e.g. so that nested wxScrolledWindows work correctly | |
1358 | event.Skip(); | |
1359 | ||
1360 | // find the immediate child under which the window receiving focus is: | |
1361 | wxWindow *win = event.GetWindow(); | |
1362 | while ( win->GetParent() != m_targetWindow ) | |
1363 | { | |
1364 | win = win->GetParent(); | |
1365 | wxCHECK_RET( win, "incorrectly sent wxChildFocusEvent - not our child" ); | |
1366 | } | |
1367 | ||
1368 | // if the child is not fully visible, try to scroll it into view: | |
1369 | int stepx, stepy; | |
1370 | GetScrollPixelsPerUnit(&stepx, &stepy); | |
1371 | ||
1372 | // NB: we don't call CalcScrolledPosition() on win->GetPosition() here, | |
1373 | // because children' positions are already scrolled | |
1374 | wxRect winrect(win->GetPosition(), win->GetSize()); | |
1375 | wxSize view(m_targetWindow->GetClientSize()); | |
1376 | ||
1377 | int startx, starty; | |
1378 | GetViewStart(&startx, &starty); | |
1379 | ||
1380 | // first in vertical direction: | |
1381 | if ( stepy > 0 ) | |
1382 | { | |
1383 | int diff = 0; | |
1384 | ||
1385 | if ( winrect.GetTop() < 0 ) | |
1386 | { | |
1387 | diff = winrect.GetTop(); | |
1388 | } | |
1389 | else if ( winrect.GetBottom() > view.y ) | |
1390 | { | |
1391 | diff = winrect.GetBottom() - view.y + 1; | |
1392 | // round up to next scroll step if we can't get exact position, | |
1393 | // so that the window is fully visible: | |
1394 | diff += stepy - 1; | |
1395 | } | |
1396 | ||
1397 | starty = (starty * stepy + diff) / stepy; | |
1398 | } | |
1399 | ||
1400 | // then horizontal: | |
1401 | if ( stepx > 0 ) | |
1402 | { | |
1403 | int diff = 0; | |
1404 | ||
1405 | if ( winrect.GetLeft() < 0 ) | |
1406 | { | |
1407 | diff = winrect.GetLeft(); | |
1408 | } | |
1409 | else if ( winrect.GetRight() > view.x ) | |
1410 | { | |
1411 | diff = winrect.GetRight() - view.x + 1; | |
1412 | // round up to next scroll step if we can't get exact position, | |
1413 | // so that the window is fully visible: | |
1414 | diff += stepx - 1; | |
1415 | } | |
1416 | ||
1417 | startx = (startx * stepx + diff) / stepx; | |
1418 | } | |
1419 | ||
1420 | Scroll(startx, starty); | |
1421 | } | |
1422 | ||
1423 | // ---------------------------------------------------------------------------- | |
1424 | // wxScrolledWindow implementation | |
1425 | // ---------------------------------------------------------------------------- | |
1426 | ||
1427 | IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel) | |
1428 | ||
1429 | BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel) | |
1430 | EVT_PAINT(wxScrolledWindow::OnPaint) | |
1431 | END_EVENT_TABLE() | |
1432 | ||
1433 | bool wxScrolledWindow::Create(wxWindow *parent, | |
1434 | wxWindowID id, | |
1435 | const wxPoint& pos, | |
1436 | const wxSize& size, | |
1437 | long style, | |
1438 | const wxString& name) | |
1439 | { | |
1440 | m_targetWindow = this; | |
1441 | #ifdef __WXMAC__ | |
1442 | MacSetClipChildren( true ) ; | |
1443 | #endif | |
1444 | ||
1445 | // by default, we're scrollable in both directions (but if one of the | |
1446 | // styles is specified explicitly, we shouldn't add the other one | |
1447 | // automatically) | |
1448 | if ( !(style & (wxHSCROLL | wxVSCROLL)) ) | |
1449 | style |= wxHSCROLL | wxVSCROLL; | |
1450 | ||
1451 | bool ok = wxPanel::Create(parent, id, pos, size, style, name); | |
1452 | ||
1453 | return ok; | |
1454 | } | |
1455 | ||
1456 | wxScrolledWindow::~wxScrolledWindow() | |
1457 | { | |
1458 | } | |
1459 | ||
1460 | void wxScrolledWindow::OnPaint(wxPaintEvent& event) | |
1461 | { | |
1462 | // the user code didn't really draw the window if we got here, so set this | |
1463 | // flag to try to call OnDraw() later | |
1464 | m_handler->ResetDrawnFlag(); | |
1465 | ||
1466 | event.Skip(); | |
1467 | } | |
1468 | ||
1469 | #ifdef __WXMSW__ | |
1470 | WXLRESULT wxScrolledWindow::MSWWindowProc(WXUINT nMsg, | |
1471 | WXWPARAM wParam, | |
1472 | WXLPARAM lParam) | |
1473 | { | |
1474 | WXLRESULT rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam); | |
1475 | ||
1476 | #ifndef __WXWINCE__ | |
1477 | // we need to process arrows ourselves for scrolling | |
1478 | if ( nMsg == WM_GETDLGCODE ) | |
1479 | { | |
1480 | rc |= DLGC_WANTARROWS; | |
1481 | } | |
1482 | #endif | |
1483 | ||
1484 | return rc; | |
1485 | } | |
1486 | ||
1487 | #endif // __WXMSW__ |