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