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