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