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