]> git.saurik.com Git - wxWidgets.git/blob - src/generic/scrlwing.cpp
Use ProcessEventLocally() instead of ProcessEventHere() in docview code.
[wxWidgets.git] / src / generic / scrlwing.cpp
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 #include "wx/scrolwin.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/utils.h"
32 #include "wx/panel.h"
33 #include "wx/dcclient.h"
34 #include "wx/timer.h"
35 #include "wx/sizer.h"
36 #include "wx/settings.h"
37 #endif
38
39 #ifdef __WXMAC__
40 #include "wx/scrolbar.h"
41 #endif
42
43 #include "wx/recguard.h"
44
45 #ifdef __WXMSW__
46 #include <windows.h> // for DLGC_WANTARROWS
47 #include "wx/msw/winundef.h"
48 #endif
49
50 #ifdef __WXMOTIF__
51 // For wxRETAINED implementation
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
56 #include <Xm/Xm.h>
57 #ifdef __VMS__
58 # pragma message enable nosimpint
59 #endif
60 #endif
61
62 /*
63 TODO PROPERTIES
64 style wxHSCROLL | wxVSCROLL
65 */
66
67 // ----------------------------------------------------------------------------
68 // wxScrollHelperEvtHandler: intercept the events from the window and forward
69 // them to wxScrollHelper
70 // ----------------------------------------------------------------------------
71
72 class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler
73 {
74 public:
75 wxScrollHelperEvtHandler(wxScrollHelperBase *scrollHelper)
76 {
77 m_scrollHelper = scrollHelper;
78 }
79
80 virtual bool ProcessEvent(wxEvent& event);
81
82 void ResetDrawnFlag() { m_hasDrawnWindow = false; }
83
84 private:
85 wxScrollHelperBase *m_scrollHelper;
86
87 bool m_hasDrawnWindow;
88
89 wxDECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler);
90 };
91
92 #if wxUSE_TIMER
93 // ----------------------------------------------------------------------------
94 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
95 // a captured mouse is held outside the window
96 // ----------------------------------------------------------------------------
97
98 class wxAutoScrollTimer : public wxTimer
99 {
100 public:
101 wxAutoScrollTimer(wxWindow *winToScroll,
102 wxScrollHelperBase *scroll,
103 wxEventType eventTypeToSend,
104 int pos, int orient);
105
106 virtual void Notify();
107
108 private:
109 wxWindow *m_win;
110 wxScrollHelperBase *m_scrollHelper;
111 wxEventType m_eventType;
112 int m_pos,
113 m_orient;
114
115 wxDECLARE_NO_COPY_CLASS(wxAutoScrollTimer);
116 };
117
118 // ============================================================================
119 // implementation
120 // ============================================================================
121
122 // ----------------------------------------------------------------------------
123 // wxAutoScrollTimer
124 // ----------------------------------------------------------------------------
125
126 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll,
127 wxScrollHelperBase *scroll,
128 wxEventType eventTypeToSend,
129 int pos, int orient)
130 {
131 m_win = winToScroll;
132 m_scrollHelper = scroll;
133 m_eventType = eventTypeToSend;
134 m_pos = pos;
135 m_orient = orient;
136 }
137
138 void wxAutoScrollTimer::Notify()
139 {
140 // only do all this as long as the window is capturing the mouse
141 if ( wxWindow::GetCapture() != m_win )
142 {
143 Stop();
144 }
145 else // we still capture the mouse, continue generating events
146 {
147 // first scroll the window if we are allowed to do it
148 wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
149 event1.SetEventObject(m_win);
150 if ( m_scrollHelper->SendAutoScrollEvents(event1) &&
151 m_win->GetEventHandler()->ProcessEvent(event1) )
152 {
153 // and then send a pseudo mouse-move event to refresh the selection
154 wxMouseEvent event2(wxEVT_MOTION);
155 event2.SetPosition(wxGetMousePosition());
156
157 // the mouse event coordinates should be client, not screen as
158 // returned by wxGetMousePosition
159 wxWindow *parentTop = m_win;
160 while ( parentTop->GetParent() )
161 parentTop = parentTop->GetParent();
162 wxPoint ptOrig = parentTop->GetPosition();
163 event2.m_x -= ptOrig.x;
164 event2.m_y -= ptOrig.y;
165
166 event2.SetEventObject(m_win);
167
168 // FIXME: we don't fill in the other members - ok?
169
170 m_win->GetEventHandler()->ProcessEvent(event2);
171 }
172 else // can't scroll further, stop
173 {
174 Stop();
175 }
176 }
177 }
178 #endif
179
180 // ----------------------------------------------------------------------------
181 // wxScrollHelperEvtHandler
182 // ----------------------------------------------------------------------------
183
184 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event)
185 {
186 wxEventType evType = event.GetEventType();
187
188 // the explanation of wxEVT_PAINT processing hack: for historic reasons
189 // there are 2 ways to process this event in classes deriving from
190 // wxScrolledWindow. The user code may
191 //
192 // 1. override wxScrolledWindow::OnDraw(dc)
193 // 2. define its own OnPaint() handler
194 //
195 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
196 // always processes the draw event, so we can't just try the window
197 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
198 // (the latter would never be called in wxUniversal).
199 //
200 // So the solution is to have a flag telling us whether the user code drew
201 // anything in the window. We set it to true here but reset it to false in
202 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
203 // user code defined OnPaint() in the derived class)
204 m_hasDrawnWindow = true;
205
206 // pass it on to the real handler
207 bool processed = wxEvtHandler::ProcessEvent(event);
208
209 // always process the size events ourselves, even if the user code handles
210 // them as well, as we need to AdjustScrollbars()
211 //
212 // NB: it is important to do it after processing the event in the normal
213 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
214 // scrollbar[s] (dis)appear and it should be seen by the user code
215 // after this one
216 if ( evType == wxEVT_SIZE )
217 {
218 m_scrollHelper->HandleOnSize((wxSizeEvent &)event);
219
220 return true;
221 }
222
223 if ( processed )
224 {
225 // normally, nothing more to do here - except if it was a paint event
226 // which wasn't really processed, then we'll try to call our
227 // OnDraw() below (from HandleOnPaint)
228 if ( m_hasDrawnWindow || event.IsCommandEvent() )
229 {
230 return true;
231 }
232 }
233
234 if ( evType == wxEVT_PAINT )
235 {
236 m_scrollHelper->HandleOnPaint((wxPaintEvent &)event);
237 return true;
238 }
239
240 if ( evType == wxEVT_CHILD_FOCUS )
241 {
242 m_scrollHelper->HandleOnChildFocus((wxChildFocusEvent &)event);
243 return true;
244 }
245
246 // reset the skipped flag (which might have been set to true in
247 // ProcessEvent() above) to be able to test it below
248 bool wasSkipped = event.GetSkipped();
249 if ( wasSkipped )
250 event.Skip(false);
251
252 if ( evType == wxEVT_SCROLLWIN_TOP ||
253 evType == wxEVT_SCROLLWIN_BOTTOM ||
254 evType == wxEVT_SCROLLWIN_LINEUP ||
255 evType == wxEVT_SCROLLWIN_LINEDOWN ||
256 evType == wxEVT_SCROLLWIN_PAGEUP ||
257 evType == wxEVT_SCROLLWIN_PAGEDOWN ||
258 evType == wxEVT_SCROLLWIN_THUMBTRACK ||
259 evType == wxEVT_SCROLLWIN_THUMBRELEASE )
260 {
261 m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event);
262 if ( !event.GetSkipped() )
263 {
264 // it makes sense to indicate that we processed the message as we
265 // did scroll the window (and also notice that wxAutoScrollTimer
266 // relies on our return value to stop scrolling when we are at top
267 // or bottom already)
268 processed = true;
269 wasSkipped = false;
270 }
271 }
272
273 if ( evType == wxEVT_ENTER_WINDOW )
274 {
275 m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event);
276 }
277 else if ( evType == wxEVT_LEAVE_WINDOW )
278 {
279 m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event);
280 }
281 #if wxUSE_MOUSEWHEEL
282 // Use GTK's own scroll wheel handling in GtkScrolledWindow
283 #ifndef __WXGTK20__
284 else if ( evType == wxEVT_MOUSEWHEEL )
285 {
286 m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event);
287 return true;
288 }
289 #endif
290 #endif // wxUSE_MOUSEWHEEL
291 else if ( evType == wxEVT_CHAR )
292 {
293 m_scrollHelper->HandleOnChar((wxKeyEvent &)event);
294 if ( !event.GetSkipped() )
295 {
296 processed = true;
297 wasSkipped = false;
298 }
299 }
300
301 event.Skip(wasSkipped);
302
303 return processed;
304 }
305
306 // ============================================================================
307 // wxScrollHelperBase implementation
308 // ============================================================================
309
310 // ----------------------------------------------------------------------------
311 // wxScrollHelperBase construction
312 // ----------------------------------------------------------------------------
313
314 wxScrollHelperBase::wxScrollHelperBase(wxWindow *win)
315 {
316 wxASSERT_MSG( win, wxT("associated window can't be NULL in wxScrollHelper") );
317
318 m_xScrollPixelsPerLine =
319 m_yScrollPixelsPerLine =
320 m_xScrollPosition =
321 m_yScrollPosition =
322 m_xScrollLines =
323 m_yScrollLines =
324 m_xScrollLinesPerPage =
325 m_yScrollLinesPerPage = 0;
326
327 m_xScrollingEnabled =
328 m_yScrollingEnabled = true;
329
330 m_scaleX =
331 m_scaleY = 1.0;
332 #if wxUSE_MOUSEWHEEL
333 m_wheelRotation = 0;
334 #endif
335
336 m_win =
337 m_targetWindow = NULL;
338
339 m_timerAutoScroll = NULL;
340
341 m_handler = NULL;
342
343 m_win = win;
344
345 m_win->SetScrollHelper(static_cast<wxScrollHelper *>(this));
346
347 // by default, the associated window is also the target window
348 DoSetTargetWindow(win);
349 }
350
351 wxScrollHelperBase::~wxScrollHelperBase()
352 {
353 StopAutoScrolling();
354
355 DeleteEvtHandler();
356 }
357
358 // ----------------------------------------------------------------------------
359 // setting scrolling parameters
360 // ----------------------------------------------------------------------------
361
362 void wxScrollHelperBase::SetScrollbars(int pixelsPerUnitX,
363 int pixelsPerUnitY,
364 int noUnitsX,
365 int noUnitsY,
366 int xPos,
367 int yPos,
368 bool noRefresh)
369 {
370 int xpos, ypos;
371
372 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
373 bool do_refresh =
374 (
375 (noUnitsX != 0 && m_xScrollLines == 0) ||
376 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX * noUnitsX) ||
377
378 (noUnitsY != 0 && m_yScrollLines == 0) ||
379 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY * noUnitsY) ||
380 (xPos != m_xScrollPosition) ||
381 (yPos != m_yScrollPosition)
382 );
383
384 m_xScrollPixelsPerLine = pixelsPerUnitX;
385 m_yScrollPixelsPerLine = pixelsPerUnitY;
386 m_xScrollPosition = xPos;
387 m_yScrollPosition = yPos;
388
389 int w = noUnitsX * pixelsPerUnitX;
390 int h = noUnitsY * pixelsPerUnitY;
391
392 // For better backward compatibility we set persisting limits
393 // here not just the size. It makes SetScrollbars 'sticky'
394 // emulating the old non-autoscroll behaviour.
395 // m_targetWindow->SetVirtualSizeHints( w, h );
396
397 // The above should arguably be deprecated, this however we still need.
398
399 // take care not to set 0 virtual size, 0 means that we don't have any
400 // scrollbars and hence we should use the real size instead of the virtual
401 // one which is indicated by using wxDefaultCoord
402 m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord,
403 h ? h : wxDefaultCoord);
404
405 if (do_refresh && !noRefresh)
406 m_targetWindow->Refresh(true, GetScrollRect());
407
408 #ifndef __WXUNIVERSAL__
409 // If the target is not the same as the window with the scrollbars,
410 // then we need to update the scrollbars here, since they won't have
411 // been updated by SetVirtualSize().
412 if ( m_targetWindow != m_win )
413 #endif // !__WXUNIVERSAL__
414 {
415 AdjustScrollbars();
416 }
417 #ifndef __WXUNIVERSAL__
418 else
419 {
420 // otherwise this has been done by AdjustScrollbars, above
421 }
422 #endif // !__WXUNIVERSAL__
423 }
424
425 // ----------------------------------------------------------------------------
426 // [target] window handling
427 // ----------------------------------------------------------------------------
428
429 void wxScrollHelperBase::DeleteEvtHandler()
430 {
431 // search for m_handler in the handler list
432 if ( m_win && m_handler )
433 {
434 if ( m_win->RemoveEventHandler(m_handler) )
435 {
436 delete m_handler;
437 }
438 //else: something is very wrong, so better [maybe] leak memory than
439 // risk a crash because of double deletion
440
441 m_handler = NULL;
442 }
443 }
444
445 void wxScrollHelperBase::ResetDrawnFlag()
446 {
447 wxCHECK_RET( m_handler, "invalid use of ResetDrawnFlag - no handler?" );
448 m_handler->ResetDrawnFlag();
449 }
450
451 void wxScrollHelperBase::DoSetTargetWindow(wxWindow *target)
452 {
453 m_targetWindow = target;
454 #ifdef __WXMAC__
455 target->MacSetClipChildren( true ) ;
456 #endif
457
458 // install the event handler which will intercept the events we're
459 // interested in (but only do it for our real window, not the target window
460 // which we scroll - we don't need to hijack its events)
461 if ( m_targetWindow == m_win )
462 {
463 // if we already have a handler, delete it first
464 DeleteEvtHandler();
465
466 m_handler = new wxScrollHelperEvtHandler(this);
467 m_targetWindow->PushEventHandler(m_handler);
468 }
469 }
470
471 void wxScrollHelperBase::SetTargetWindow(wxWindow *target)
472 {
473 wxCHECK_RET( target, wxT("target window must not be NULL") );
474
475 if ( target == m_targetWindow )
476 return;
477
478 DoSetTargetWindow(target);
479 }
480
481 wxWindow *wxScrollHelperBase::GetTargetWindow() const
482 {
483 return m_targetWindow;
484 }
485
486 // ----------------------------------------------------------------------------
487 // scrolling implementation itself
488 // ----------------------------------------------------------------------------
489
490 void wxScrollHelperBase::HandleOnScroll(wxScrollWinEvent& event)
491 {
492 int nScrollInc = CalcScrollInc(event);
493 if ( nScrollInc == 0 )
494 {
495 // can't scroll further
496 event.Skip();
497
498 return;
499 }
500
501 bool needsRefresh = false;
502 int dx = 0,
503 dy = 0;
504 int orient = event.GetOrientation();
505 if (orient == wxHORIZONTAL)
506 {
507 if ( m_xScrollingEnabled )
508 {
509 dx = -m_xScrollPixelsPerLine * nScrollInc;
510 }
511 else
512 {
513 needsRefresh = true;
514 }
515 }
516 else
517 {
518 if ( m_yScrollingEnabled )
519 {
520 dy = -m_yScrollPixelsPerLine * nScrollInc;
521 }
522 else
523 {
524 needsRefresh = true;
525 }
526 }
527
528 if ( !needsRefresh )
529 {
530 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
531 // otherwise invalidated area could be updated incorrectly later when
532 // ScrollWindow() makes sure they're repainted before scrolling them
533 #ifdef __WXMAC__
534 // wxWindowMac is taking care of making sure the update area is correctly
535 // set up, while not forcing an immediate redraw
536 #else
537 m_targetWindow->Update();
538 #endif
539 }
540
541 if (orient == wxHORIZONTAL)
542 {
543 m_xScrollPosition += nScrollInc;
544 m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
545 }
546 else
547 {
548 m_yScrollPosition += nScrollInc;
549 m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
550 }
551
552 if ( needsRefresh )
553 {
554 m_targetWindow->Refresh(true, GetScrollRect());
555 }
556 else
557 {
558 m_targetWindow->ScrollWindow(dx, dy, GetScrollRect());
559 }
560 }
561
562 int wxScrollHelperBase::CalcScrollInc(wxScrollWinEvent& event)
563 {
564 int pos = event.GetPosition();
565 int orient = event.GetOrientation();
566
567 int nScrollInc = 0;
568 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
569 {
570 if (orient == wxHORIZONTAL)
571 nScrollInc = - m_xScrollPosition;
572 else
573 nScrollInc = - m_yScrollPosition;
574 } else
575 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
576 {
577 if (orient == wxHORIZONTAL)
578 nScrollInc = m_xScrollLines - m_xScrollPosition;
579 else
580 nScrollInc = m_yScrollLines - m_yScrollPosition;
581 } else
582 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
583 {
584 nScrollInc = -1;
585 } else
586 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
587 {
588 nScrollInc = 1;
589 } else
590 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
591 {
592 if (orient == wxHORIZONTAL)
593 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
594 else
595 nScrollInc = -GetScrollPageSize(wxVERTICAL);
596 } else
597 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
598 {
599 if (orient == wxHORIZONTAL)
600 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
601 else
602 nScrollInc = GetScrollPageSize(wxVERTICAL);
603 } else
604 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
605 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
606 {
607 if (orient == wxHORIZONTAL)
608 nScrollInc = pos - m_xScrollPosition;
609 else
610 nScrollInc = pos - m_yScrollPosition;
611 }
612
613 if (orient == wxHORIZONTAL)
614 {
615 if ( m_xScrollPosition + nScrollInc < 0 )
616 {
617 // As -ve as we can go
618 nScrollInc = -m_xScrollPosition;
619 }
620 else // check for the other bound
621 {
622 const int posMax = m_xScrollLines - m_xScrollLinesPerPage;
623 if ( m_xScrollPosition + nScrollInc > posMax )
624 {
625 // As +ve as we can go
626 nScrollInc = posMax - m_xScrollPosition;
627 }
628 }
629 }
630 else // wxVERTICAL
631 {
632 if ( m_yScrollPosition + nScrollInc < 0 )
633 {
634 // As -ve as we can go
635 nScrollInc = -m_yScrollPosition;
636 }
637 else // check for the other bound
638 {
639 const int posMax = m_yScrollLines - m_yScrollLinesPerPage;
640 if ( m_yScrollPosition + nScrollInc > posMax )
641 {
642 // As +ve as we can go
643 nScrollInc = posMax - m_yScrollPosition;
644 }
645 }
646 }
647
648 return nScrollInc;
649 }
650
651 void wxScrollHelperBase::DoPrepareDC(wxDC& dc)
652 {
653 wxPoint pt = dc.GetDeviceOrigin();
654 #ifdef __WXGTK__
655 // It may actually be correct to always query
656 // the m_sign from the DC here, but I leave the
657 // #ifdef GTK for now.
658 if (m_win->GetLayoutDirection() == wxLayout_RightToLeft)
659 dc.SetDeviceOrigin( pt.x + m_xScrollPosition * m_xScrollPixelsPerLine,
660 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
661 else
662 #endif
663 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
664 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
665 dc.SetUserScale( m_scaleX, m_scaleY );
666 }
667
668 void wxScrollHelperBase::SetScrollRate( int xstep, int ystep )
669 {
670 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
671 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
672
673 m_xScrollPixelsPerLine = xstep;
674 m_yScrollPixelsPerLine = ystep;
675
676 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
677 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
678
679 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
680 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
681 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
682
683 AdjustScrollbars();
684 }
685
686 void wxScrollHelperBase::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
687 {
688 if ( x_unit )
689 *x_unit = m_xScrollPixelsPerLine;
690 if ( y_unit )
691 *y_unit = m_yScrollPixelsPerLine;
692 }
693
694
695 int wxScrollHelperBase::GetScrollLines( int orient ) const
696 {
697 if ( orient == wxHORIZONTAL )
698 return m_xScrollLines;
699 else
700 return m_yScrollLines;
701 }
702
703 int wxScrollHelperBase::GetScrollPageSize(int orient) const
704 {
705 if ( orient == wxHORIZONTAL )
706 return m_xScrollLinesPerPage;
707 else
708 return m_yScrollLinesPerPage;
709 }
710
711 void wxScrollHelperBase::SetScrollPageSize(int orient, int pageSize)
712 {
713 if ( orient == wxHORIZONTAL )
714 m_xScrollLinesPerPage = pageSize;
715 else
716 m_yScrollLinesPerPage = pageSize;
717 }
718
719 void wxScrollHelperBase::EnableScrolling (bool x_scroll, bool y_scroll)
720 {
721 m_xScrollingEnabled = x_scroll;
722 m_yScrollingEnabled = y_scroll;
723 }
724
725 // Where the current view starts from
726 void wxScrollHelperBase::DoGetViewStart (int *x, int *y) const
727 {
728 if ( x )
729 *x = m_xScrollPosition;
730 if ( y )
731 *y = m_yScrollPosition;
732 }
733
734 void wxScrollHelperBase::DoCalcScrolledPosition(int x, int y,
735 int *xx, int *yy) const
736 {
737 if ( xx )
738 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
739 if ( yy )
740 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
741 }
742
743 void wxScrollHelperBase::DoCalcUnscrolledPosition(int x, int y,
744 int *xx, int *yy) const
745 {
746 if ( xx )
747 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
748 if ( yy )
749 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
750 }
751
752 // ----------------------------------------------------------------------------
753 // geometry
754 // ----------------------------------------------------------------------------
755
756 bool wxScrollHelperBase::ScrollLayout()
757 {
758 if ( m_win->GetSizer() && m_targetWindow == m_win )
759 {
760 // If we're the scroll target, take into account the
761 // virtual size and scrolled position of the window.
762
763 int x = 0, y = 0, w = 0, h = 0;
764 CalcScrolledPosition(0,0, &x,&y);
765 m_win->GetVirtualSize(&w, &h);
766 m_win->GetSizer()->SetDimension(x, y, w, h);
767 return true;
768 }
769
770 // fall back to default for LayoutConstraints
771 return m_win->wxWindow::Layout();
772 }
773
774 void wxScrollHelperBase::ScrollDoSetVirtualSize(int x, int y)
775 {
776 m_win->wxWindow::DoSetVirtualSize( x, y );
777 AdjustScrollbars();
778
779 if (m_win->GetAutoLayout())
780 m_win->Layout();
781 }
782
783 // wxWindow's GetBestVirtualSize returns the actual window size,
784 // whereas we want to return the virtual size
785 wxSize wxScrollHelperBase::ScrollGetBestVirtualSize() const
786 {
787 wxSize clientSize(m_win->GetClientSize());
788 if ( m_win->GetSizer() )
789 clientSize.IncTo(m_win->GetSizer()->CalcMin());
790
791 return clientSize;
792 }
793
794 // ----------------------------------------------------------------------------
795 // event handlers
796 // ----------------------------------------------------------------------------
797
798 // Default OnSize resets scrollbars, if any
799 void wxScrollHelperBase::HandleOnSize(wxSizeEvent& WXUNUSED(event))
800 {
801 if ( m_targetWindow->GetAutoLayout() )
802 {
803 wxSize size = m_targetWindow->GetBestVirtualSize();
804
805 // This will call ::Layout() and ::AdjustScrollbars()
806 m_win->SetVirtualSize( size );
807 }
808 else
809 {
810 AdjustScrollbars();
811 }
812 }
813
814 // This calls OnDraw, having adjusted the origin according to the current
815 // scroll position
816 void wxScrollHelperBase::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
817 {
818 // don't use m_targetWindow here, this is always called for ourselves
819 wxPaintDC dc(m_win);
820 DoPrepareDC(dc);
821
822 OnDraw(dc);
823 }
824
825 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
826 // compatibility here - if we used OnKeyDown(), the programs which process
827 // arrows themselves in their OnChar() would never get the message and like
828 // this they always have the priority
829 void wxScrollHelperBase::HandleOnChar(wxKeyEvent& event)
830 {
831 // prepare the event this key press maps to
832 wxScrollWinEvent newEvent;
833
834 newEvent.SetPosition(0);
835 newEvent.SetEventObject(m_win);
836
837 // this is the default, it's changed to wxHORIZONTAL below if needed
838 newEvent.SetOrientation(wxVERTICAL);
839
840 // some key events result in scrolling in both horizontal and vertical
841 // direction, e.g. Ctrl-{Home,End}, if this flag is true we should generate
842 // a second event in horizontal direction in addition to the primary one
843 bool sendHorizontalToo = false;
844
845 switch ( event.GetKeyCode() )
846 {
847 case WXK_PAGEUP:
848 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
849 break;
850
851 case WXK_PAGEDOWN:
852 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
853 break;
854
855 case WXK_HOME:
856 newEvent.SetEventType(wxEVT_SCROLLWIN_TOP);
857
858 sendHorizontalToo = event.ControlDown();
859 break;
860
861 case WXK_END:
862 newEvent.SetEventType(wxEVT_SCROLLWIN_BOTTOM);
863
864 sendHorizontalToo = event.ControlDown();
865 break;
866
867 case WXK_LEFT:
868 newEvent.SetOrientation(wxHORIZONTAL);
869 // fall through
870
871 case WXK_UP:
872 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
873 break;
874
875 case WXK_RIGHT:
876 newEvent.SetOrientation(wxHORIZONTAL);
877 // fall through
878
879 case WXK_DOWN:
880 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
881 break;
882
883 default:
884 // not a scrolling key
885 event.Skip();
886 return;
887 }
888
889 m_win->ProcessWindowEvent(newEvent);
890
891 if ( sendHorizontalToo )
892 {
893 newEvent.SetOrientation(wxHORIZONTAL);
894 m_win->ProcessWindowEvent(newEvent);
895 }
896 }
897
898 // ----------------------------------------------------------------------------
899 // autoscroll stuff: these functions deal with sending fake scroll events when
900 // a captured mouse is being held outside the window
901 // ----------------------------------------------------------------------------
902
903 bool wxScrollHelperBase::SendAutoScrollEvents(wxScrollWinEvent& event) const
904 {
905 // only send the event if the window is scrollable in this direction
906 wxWindow *win = (wxWindow *)event.GetEventObject();
907 return win->HasScrollbar(event.GetOrientation());
908 }
909
910 void wxScrollHelperBase::StopAutoScrolling()
911 {
912 #if wxUSE_TIMER
913 if ( m_timerAutoScroll )
914 {
915 delete m_timerAutoScroll;
916 m_timerAutoScroll = NULL;
917 }
918 #endif
919 }
920
921 void wxScrollHelperBase::HandleOnMouseEnter(wxMouseEvent& event)
922 {
923 StopAutoScrolling();
924
925 event.Skip();
926 }
927
928 void wxScrollHelperBase::HandleOnMouseLeave(wxMouseEvent& event)
929 {
930 // don't prevent the usual processing of the event from taking place
931 event.Skip();
932
933 // when a captured mouse leave a scrolled window we start generate
934 // scrolling events to allow, for example, extending selection beyond the
935 // visible area in some controls
936 if ( wxWindow::GetCapture() == m_targetWindow )
937 {
938 // where is the mouse leaving?
939 int pos, orient;
940 wxPoint pt = event.GetPosition();
941 if ( pt.x < 0 )
942 {
943 orient = wxHORIZONTAL;
944 pos = 0;
945 }
946 else if ( pt.y < 0 )
947 {
948 orient = wxVERTICAL;
949 pos = 0;
950 }
951 else // we're lower or to the right of the window
952 {
953 wxSize size = m_targetWindow->GetClientSize();
954 if ( pt.x > size.x )
955 {
956 orient = wxHORIZONTAL;
957 pos = m_xScrollLines;
958 }
959 else if ( pt.y > size.y )
960 {
961 orient = wxVERTICAL;
962 pos = m_yScrollLines;
963 }
964 else // this should be impossible
965 {
966 // but seems to happen sometimes under wxMSW - maybe it's a bug
967 // there but for now just ignore it
968
969 //wxFAIL_MSG( wxT("can't understand where has mouse gone") );
970
971 return;
972 }
973 }
974
975 // only start the auto scroll timer if the window can be scrolled in
976 // this direction
977 if ( !m_targetWindow->HasScrollbar(orient) )
978 return;
979
980 #if wxUSE_TIMER
981 delete m_timerAutoScroll;
982 m_timerAutoScroll = new wxAutoScrollTimer
983 (
984 m_targetWindow, this,
985 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
986 : wxEVT_SCROLLWIN_LINEDOWN,
987 pos,
988 orient
989 );
990 m_timerAutoScroll->Start(50); // FIXME: make configurable
991 #else
992 wxUnusedVar(pos);
993 #endif
994 }
995 }
996
997 #if wxUSE_MOUSEWHEEL
998
999 void wxScrollHelperBase::HandleOnMouseWheel(wxMouseEvent& event)
1000 {
1001 m_wheelRotation += event.GetWheelRotation();
1002 int lines = m_wheelRotation / event.GetWheelDelta();
1003 m_wheelRotation -= lines * event.GetWheelDelta();
1004
1005 if (lines != 0)
1006 {
1007
1008 wxScrollWinEvent newEvent;
1009
1010 newEvent.SetPosition(0);
1011 newEvent.SetOrientation( event.GetWheelAxis() == 0 ? wxVERTICAL : wxHORIZONTAL);
1012 newEvent.SetEventObject(m_win);
1013
1014 if (event.IsPageScroll())
1015 {
1016 if (lines > 0)
1017 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
1018 else
1019 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
1020
1021 m_win->GetEventHandler()->ProcessEvent(newEvent);
1022 }
1023 else
1024 {
1025 lines *= event.GetLinesPerAction();
1026 if (lines > 0)
1027 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
1028 else
1029 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
1030
1031 int times = abs(lines);
1032 for (; times > 0; times--)
1033 m_win->GetEventHandler()->ProcessEvent(newEvent);
1034 }
1035 }
1036 }
1037
1038 #endif // wxUSE_MOUSEWHEEL
1039
1040 void wxScrollHelperBase::HandleOnChildFocus(wxChildFocusEvent& event)
1041 {
1042 // this event should be processed by all windows in parenthood chain,
1043 // e.g. so that nested wxScrolledWindows work correctly
1044 event.Skip();
1045
1046 // find the immediate child under which the window receiving focus is:
1047 wxWindow *win = event.GetWindow();
1048
1049 if ( win == m_targetWindow )
1050 return; // nothing to do
1051
1052 #if defined( __WXOSX__ ) && wxUSE_SCROLLBAR
1053 if (wxDynamicCast(win, wxScrollBar))
1054 return;
1055 #endif
1056
1057 // Fixing ticket: http://trac.wxwidgets.org/ticket/9563
1058 // When a child inside a wxControlContainer receives a focus, the
1059 // wxControlContainer generates an artificial wxChildFocusEvent for
1060 // itself, telling its parent that 'it' received the focus. The effect is
1061 // that this->HandleOnChildFocus is called twice, first with the
1062 // artificial wxChildFocusEvent and then with the original event. We need
1063 // to ignore the artificial event here or otherwise HandleOnChildFocus
1064 // would first scroll the target window to make the entire
1065 // wxControlContainer visible and immediately afterwards scroll the target
1066 // window again to make the child widget visible. This leads to ugly
1067 // flickering when using nested wxPanels/wxScrolledWindows.
1068 //
1069 // Ignore this event if 'win' is derived from wxControlContainer AND its
1070 // parent is the m_targetWindow AND 'win' is not actually reciving the
1071 // focus (win != FindFocus). TODO: This affects all wxControlContainer
1072 // objects, but wxControlContainer is not part of the wxWidgets RTTI and
1073 // so wxDynamicCast(win, wxControlContainer) does not compile. Find a way
1074 // to determine if 'win' derives from wxControlContainer. Until then,
1075 // testing if 'win' derives from wxPanel will probably get >90% of all
1076 // cases.
1077
1078 wxWindow *actual_focus=wxWindow::FindFocus();
1079 if (win != actual_focus &&
1080 wxDynamicCast(win, wxPanel) != 0 &&
1081 win->GetParent() == m_targetWindow)
1082 // if win is a wxPanel and receives the focus, it should not be
1083 // scrolled into view
1084 return;
1085
1086 const wxRect viewRect(m_targetWindow->GetClientRect());
1087
1088 // For composite controls such as wxComboCtrl we should try to fit the
1089 // entire control inside the visible area of the target window, not just
1090 // the focused child of the control. Otherwise we'd make only the textctrl
1091 // part of a wxComboCtrl visible and the button would still be outside the
1092 // scrolled area. But do so only if the parent fits *entirely* inside the
1093 // scrolled window. In other situations, such as nested wxPanel or
1094 // wxScrolledWindows, the parent might be way to big to fit inside the
1095 // scrolled window. If that is the case, then make only the focused window
1096 // visible
1097 if ( win->GetParent() != m_targetWindow)
1098 {
1099 wxWindow *parent=win->GetParent();
1100 wxSize parent_size=parent->GetSize();
1101 if (parent_size.GetWidth() <= viewRect.GetWidth() &&
1102 parent_size.GetHeight() <= viewRect.GetHeight())
1103 // make the immediate parent visible instead of the focused control
1104 win=parent;
1105 }
1106
1107 // make win position relative to the m_targetWindow viewing area instead of
1108 // its parent
1109 const wxRect
1110 winRect(m_targetWindow->ScreenToClient(win->GetScreenPosition()),
1111 win->GetSize());
1112
1113 // check if it's fully visible
1114 if ( viewRect.Contains(winRect) )
1115 {
1116 // it is, nothing to do
1117 return;
1118 }
1119
1120 // check if we can make it fully visible: this is only possible if it's not
1121 // larger than our view area
1122 if ( winRect.GetWidth() > viewRect.GetWidth() ||
1123 winRect.GetHeight() > viewRect.GetHeight() )
1124 {
1125 // we can't make it fit so avoid scrolling it at all, this is only
1126 // going to be confusing and not helpful
1127 return;
1128 }
1129
1130
1131 // do make the window fit inside the view area by scrolling to it
1132 int stepx, stepy;
1133 GetScrollPixelsPerUnit(&stepx, &stepy);
1134
1135 int startx, starty;
1136 GetViewStart(&startx, &starty);
1137
1138 // first in vertical direction:
1139 if ( stepy > 0 )
1140 {
1141 int diff = 0;
1142
1143 if ( winRect.GetTop() < 0 )
1144 {
1145 diff = winRect.GetTop();
1146 }
1147 else if ( winRect.GetBottom() > viewRect.GetHeight() )
1148 {
1149 diff = winRect.GetBottom() - viewRect.GetHeight() + 1;
1150 // round up to next scroll step if we can't get exact position,
1151 // so that the window is fully visible:
1152 diff += stepy - 1;
1153 }
1154
1155 starty = (starty * stepy + diff) / stepy;
1156 }
1157
1158 // then horizontal:
1159 if ( stepx > 0 )
1160 {
1161 int diff = 0;
1162
1163 if ( winRect.GetLeft() < 0 )
1164 {
1165 diff = winRect.GetLeft();
1166 }
1167 else if ( winRect.GetRight() > viewRect.GetWidth() )
1168 {
1169 diff = winRect.GetRight() - viewRect.GetWidth() + 1;
1170 // round up to next scroll step if we can't get exact position,
1171 // so that the window is fully visible:
1172 diff += stepx - 1;
1173 }
1174
1175 startx = (startx * stepx + diff) / stepx;
1176 }
1177
1178 Scroll(startx, starty);
1179 }
1180
1181
1182 #ifdef wxHAS_GENERIC_SCROLLWIN
1183
1184 // ----------------------------------------------------------------------------
1185 // wxScrollHelper implementation
1186 // ----------------------------------------------------------------------------
1187
1188 wxScrollHelper::wxScrollHelper(wxWindow *winToScroll)
1189 : wxScrollHelperBase(winToScroll)
1190 {
1191 m_xVisibility =
1192 m_yVisibility = wxSHOW_SB_DEFAULT;
1193 }
1194
1195 void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz,
1196 wxScrollbarVisibility vert)
1197 {
1198 if ( horz != m_xVisibility || vert != m_yVisibility )
1199 {
1200 m_xVisibility = horz;
1201 m_yVisibility = vert;
1202
1203 AdjustScrollbars();
1204 }
1205 }
1206
1207 void
1208 wxScrollHelper::DoAdjustScrollbar(int orient,
1209 int clientSize,
1210 int virtSize,
1211 int pixelsPerUnit,
1212 int& scrollUnits,
1213 int& scrollPosition,
1214 int& scrollLinesPerPage,
1215 wxScrollbarVisibility visibility)
1216 {
1217 // scroll lines per page: if 0, no scrolling is needed
1218 // check if we need scrollbar in this direction at all
1219 if ( pixelsPerUnit == 0 || clientSize >= virtSize )
1220 {
1221 // scrolling is disabled or unnecessary
1222 scrollUnits =
1223 scrollPosition = 0;
1224 scrollLinesPerPage = 0;
1225 }
1226 else // might need scrolling
1227 {
1228 // Round up integer division to catch any "leftover" client space.
1229 scrollUnits = (virtSize + pixelsPerUnit - 1) / pixelsPerUnit;
1230
1231 // Calculate the number of fully scroll units
1232 scrollLinesPerPage = clientSize / pixelsPerUnit;
1233
1234 if ( scrollLinesPerPage >= scrollUnits )
1235 {
1236 // we're big enough to not need scrolling
1237 scrollUnits =
1238 scrollPosition = 0;
1239 scrollLinesPerPage = 0;
1240 }
1241 else // we do need a scrollbar
1242 {
1243 if ( scrollLinesPerPage < 1 )
1244 scrollLinesPerPage = 1;
1245
1246 // Correct position if greater than extent of canvas minus
1247 // the visible portion of it or if below zero
1248 const int posMax = scrollUnits - scrollLinesPerPage;
1249 if ( scrollPosition > posMax )
1250 scrollPosition = posMax;
1251 else if ( scrollPosition < 0 )
1252 scrollPosition = 0;
1253 }
1254 }
1255
1256 // in wxSHOW_SB_NEVER case don't show the scrollbar even if it's needed, in
1257 // wxSHOW_SB_ALWAYS case show the scrollbar even if it's not needed by
1258 // passing a special range value to SetScrollbar()
1259 int range;
1260 switch ( visibility )
1261 {
1262 case wxSHOW_SB_NEVER:
1263 range = 0;
1264 break;
1265
1266 case wxSHOW_SB_ALWAYS:
1267 range = scrollUnits ? scrollUnits : -1;
1268 break;
1269
1270 default:
1271 wxFAIL_MSG( wxS("unknown scrollbar visibility") );
1272 // fall through
1273
1274 case wxSHOW_SB_DEFAULT:
1275 range = scrollUnits;
1276 break;
1277
1278 }
1279
1280 m_win->SetScrollbar(orient, scrollPosition, scrollLinesPerPage, range);
1281 }
1282
1283 void wxScrollHelper::AdjustScrollbars()
1284 {
1285 static wxRecursionGuardFlag s_flagReentrancy;
1286 wxRecursionGuard guard(s_flagReentrancy);
1287 if ( guard.IsInside() )
1288 {
1289 // don't reenter AdjustScrollbars() while another call to
1290 // AdjustScrollbars() is in progress because this may lead to calling
1291 // ScrollWindow() twice and this can really happen under MSW if
1292 // SetScrollbar() call below adds or removes the scrollbar which
1293 // changes the window size and hence results in another
1294 // AdjustScrollbars() call
1295 return;
1296 }
1297
1298 int oldXScroll = m_xScrollPosition;
1299 int oldYScroll = m_yScrollPosition;
1300
1301 // we may need to readjust the scrollbars several times as enabling one of
1302 // them reduces the area available for the window contents and so can make
1303 // the other scrollbar necessary now although it wasn't necessary before
1304 //
1305 // VZ: normally this loop should be over in at most 2 iterations, I don't
1306 // know why do we need 5 of them
1307 for ( int iterationCount = 0; iterationCount < 5; iterationCount++ )
1308 {
1309 wxSize clientSize = GetTargetSize();
1310 const wxSize virtSize = m_targetWindow->GetVirtualSize();
1311
1312 // this block of code tries to work around the following problem: the
1313 // window could have been just resized to have enough space to show its
1314 // full contents without the scrollbars, but its client size could be
1315 // not big enough because it does have the scrollbars right now and so
1316 // the scrollbars would remain even though we don't need them any more
1317 //
1318 // to prevent this from happening, check if we have enough space for
1319 // everything without the scrollbars and explicitly disable them then
1320 const wxSize availSize = GetSizeAvailableForScrollTarget(
1321 m_win->GetSize() - m_win->GetWindowBorderSize());
1322 if ( availSize != clientSize )
1323 {
1324 if ( availSize.x >= virtSize.x && availSize.y >= virtSize.y )
1325 {
1326 // this will be enough to make the scrollbars disappear below
1327 // and then the client size will indeed become equal to the
1328 // full available size
1329 clientSize = availSize;
1330 }
1331 }
1332
1333
1334 DoAdjustScrollbar(wxHORIZONTAL,
1335 clientSize.x,
1336 virtSize.x,
1337 m_xScrollPixelsPerLine,
1338 m_xScrollLines,
1339 m_xScrollPosition,
1340 m_xScrollLinesPerPage,
1341 m_xVisibility);
1342
1343 DoAdjustScrollbar(wxVERTICAL,
1344 clientSize.y,
1345 virtSize.y,
1346 m_yScrollPixelsPerLine,
1347 m_yScrollLines,
1348 m_yScrollPosition,
1349 m_yScrollLinesPerPage,
1350 m_yVisibility);
1351
1352
1353 // If a scrollbar (dis)appeared as a result of this, we need to adjust
1354 // them again but if the client size didn't change, then we're done
1355 if ( GetTargetSize() == clientSize )
1356 break;
1357 }
1358
1359 #ifdef __WXMOTIF__
1360 // Sorry, some Motif-specific code to implement a backing pixmap
1361 // for the wxRETAINED style. Implementing a backing store can't
1362 // be entirely generic because it relies on the wxWindowDC implementation
1363 // to duplicate X drawing calls for the backing pixmap.
1364
1365 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
1366 {
1367 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
1368
1369 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
1370 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
1371 if (m_targetWindow->GetBackingPixmap() &&
1372 !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
1373 (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
1374 {
1375 XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
1376 m_targetWindow->SetBackingPixmap((WXPixmap) 0);
1377 }
1378
1379 if (!m_targetWindow->GetBackingPixmap() &&
1380 (m_xScrollLines != 0) && (m_yScrollLines != 0))
1381 {
1382 int depth = wxDisplayDepth();
1383 m_targetWindow->SetPixmapWidth(totalPixelWidth);
1384 m_targetWindow->SetPixmapHeight(totalPixelHeight);
1385 m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
1386 m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth));
1387 }
1388
1389 }
1390 #endif // Motif
1391
1392 if (oldXScroll != m_xScrollPosition)
1393 {
1394 if (m_xScrollingEnabled)
1395 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0,
1396 GetScrollRect() );
1397 else
1398 m_targetWindow->Refresh(true, GetScrollRect());
1399 }
1400
1401 if (oldYScroll != m_yScrollPosition)
1402 {
1403 if (m_yScrollingEnabled)
1404 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
1405 GetScrollRect() );
1406 else
1407 m_targetWindow->Refresh(true, GetScrollRect());
1408 }
1409 }
1410
1411 void wxScrollHelper::DoScroll( int x_pos, int y_pos )
1412 {
1413 if (!m_targetWindow)
1414 return;
1415
1416 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
1417 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
1418
1419 int w = 0, h = 0;
1420 GetTargetSize(&w, &h);
1421
1422 // compute new position:
1423 int new_x = m_xScrollPosition;
1424 int new_y = m_yScrollPosition;
1425
1426 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
1427 {
1428 new_x = x_pos;
1429
1430 // Calculate page size i.e. number of scroll units you get on the
1431 // current client window
1432 int noPagePositions = w/m_xScrollPixelsPerLine;
1433 if (noPagePositions < 1) noPagePositions = 1;
1434
1435 // Correct position if greater than extent of canvas minus
1436 // the visible portion of it or if below zero
1437 new_x = wxMin( m_xScrollLines-noPagePositions, new_x );
1438 new_x = wxMax( 0, new_x );
1439 }
1440 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
1441 {
1442 new_y = y_pos;
1443
1444 // Calculate page size i.e. number of scroll units you get on the
1445 // current client window
1446 int noPagePositions = h/m_yScrollPixelsPerLine;
1447 if (noPagePositions < 1) noPagePositions = 1;
1448
1449 // Correct position if greater than extent of canvas minus
1450 // the visible portion of it or if below zero
1451 new_y = wxMin( m_yScrollLines-noPagePositions, new_y );
1452 new_y = wxMax( 0, new_y );
1453 }
1454
1455 if ( new_x == m_xScrollPosition && new_y == m_yScrollPosition )
1456 return; // nothing to do, the position didn't change
1457
1458 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
1459 // otherwise invalidated area could be updated incorrectly later when
1460 // ScrollWindow() makes sure they're repainted before scrolling them
1461 m_targetWindow->Update();
1462
1463 // update the position and scroll the window now:
1464 if (m_xScrollPosition != new_x)
1465 {
1466 int old_x = m_xScrollPosition;
1467 m_xScrollPosition = new_x;
1468 m_win->SetScrollPos( wxHORIZONTAL, new_x );
1469 m_targetWindow->ScrollWindow( (old_x-new_x)*m_xScrollPixelsPerLine, 0,
1470 GetScrollRect() );
1471 }
1472
1473 if (m_yScrollPosition != new_y)
1474 {
1475 int old_y = m_yScrollPosition;
1476 m_yScrollPosition = new_y;
1477 m_win->SetScrollPos( wxVERTICAL, new_y );
1478 m_targetWindow->ScrollWindow( 0, (old_y-new_y)*m_yScrollPixelsPerLine,
1479 GetScrollRect() );
1480 }
1481 }
1482
1483 #endif // wxHAS_GENERIC_SCROLLWIN
1484
1485 // ----------------------------------------------------------------------------
1486 // wxScrolled<T> and wxScrolledWindow implementation
1487 // ----------------------------------------------------------------------------
1488
1489 wxSize wxScrolledT_Helper::FilterBestSize(const wxWindow *win,
1490 const wxScrollHelper *helper,
1491 const wxSize& origBest)
1492 {
1493 // NB: We don't do this in WX_FORWARD_TO_SCROLL_HELPER, because not
1494 // all scrollable windows should behave like this, only those that
1495 // contain children controls within scrollable area
1496 // (i.e., wxScrolledWindow) and other some scrollable windows may
1497 // have different DoGetBestSize() implementation (e.g. wxTreeCtrl).
1498
1499 wxSize best = origBest;
1500
1501 if ( win->GetAutoLayout() )
1502 {
1503 // Only use the content to set the window size in the direction
1504 // where there's no scrolling; otherwise we're going to get a huge
1505 // window in the direction in which scrolling is enabled
1506 int ppuX, ppuY;
1507 helper->GetScrollPixelsPerUnit(&ppuX, &ppuY);
1508
1509 // NB: This code used to use *current* size if min size wasn't
1510 // specified, presumably to get some reasonable (i.e., larger than
1511 // minimal) size. But that's a wrong thing to do in GetBestSize(),
1512 // so we use minimal size as specified. If the app needs some
1513 // minimal size for its scrolled window, it should set it and put
1514 // the window into sizer as expandable so that it can use all space
1515 // available to it.
1516 //
1517 // See also http://svn.wxwidgets.org/viewvc/wx?view=rev&revision=45864
1518
1519 wxSize minSize = win->GetMinSize();
1520
1521 if ( ppuX > 0 )
1522 best.x = minSize.x + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1523
1524 if ( ppuY > 0 )
1525 best.y = minSize.y + wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
1526 }
1527
1528 return best;
1529 }
1530
1531 #ifdef __WXMSW__
1532 WXLRESULT wxScrolledT_Helper::FilterMSWWindowProc(WXUINT nMsg, WXLRESULT rc)
1533 {
1534 #ifndef __WXWINCE__
1535 // we need to process arrows ourselves for scrolling
1536 if ( nMsg == WM_GETDLGCODE )
1537 {
1538 rc |= DLGC_WANTARROWS;
1539 }
1540 #endif
1541 return rc;
1542 }
1543 #endif // __WXMSW__
1544
1545 // NB: skipping wxScrolled<T> in wxRTTI information because being a templte,
1546 // it doesn't and can't implement wxRTTI support
1547 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)