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