various warning fixes for icc 9.1 compilation
[wxWidgets.git] / src / generic / scrlwing.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/scrlwing.cpp
3 // Purpose: wxScrolledWindow implementation
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin on 31.08.00: wxScrollHelper allows to implement.
6 // Ron Lee on 10.4.02: virtual size / auto scrollbars et al.
7 // Created: 01/02/97
8 // RCS-ID: $Id$
9 // Copyright: (c) wxWidgets team
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #ifdef __VMS
29 #define XtDisplay XTDISPLAY
30 #endif
31
32 #include "wx/scrolwin.h"
33
34 #ifndef WX_PRECOMP
35 #include "wx/utils.h"
36 #include "wx/panel.h"
37 #include "wx/dcclient.h"
38 #if wxUSE_TIMER
39 #include "wx/timer.h"
40 #endif
41 #include "wx/sizer.h"
42 #endif
43
44 #ifdef __WXMAC__
45 #include "wx/scrolbar.h"
46 #endif
47
48 #include "wx/recguard.h"
49
50 #ifdef __WXMSW__
51 #include <windows.h> // for DLGC_WANTARROWS
52 #include "wx/msw/winundef.h"
53 #endif
54
55 #ifdef __WXMOTIF__
56 // For wxRETAINED implementation
57 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
58 //This code switches off the compiler warnings
59 # pragma message disable nosimpint
60 #endif
61 #include <Xm/Xm.h>
62 #ifdef __VMS__
63 # pragma message enable nosimpint
64 #endif
65 #endif
66
67 /*
68 TODO PROPERTIES
69 style wxHSCROLL | wxVSCROLL
70 */
71
72 // ----------------------------------------------------------------------------
73 // wxScrollHelperEvtHandler: intercept the events from the window and forward
74 // them to wxScrollHelper
75 // ----------------------------------------------------------------------------
76
77 class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler
78 {
79 public:
80 wxScrollHelperEvtHandler(wxScrollHelper *scrollHelper)
81 {
82 m_scrollHelper = scrollHelper;
83 }
84
85 virtual bool ProcessEvent(wxEvent& event);
86
87 void ResetDrawnFlag() { m_hasDrawnWindow = false; }
88
89 private:
90 wxScrollHelper *m_scrollHelper;
91
92 bool m_hasDrawnWindow;
93
94 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler)
95 };
96
97 #if wxUSE_TIMER
98 // ----------------------------------------------------------------------------
99 // wxAutoScrollTimer: the timer used to generate a stream of scroll events when
100 // a captured mouse is held outside the window
101 // ----------------------------------------------------------------------------
102
103 class wxAutoScrollTimer : public wxTimer
104 {
105 public:
106 wxAutoScrollTimer(wxWindow *winToScroll, wxScrollHelper *scroll,
107 wxEventType eventTypeToSend,
108 int pos, int orient);
109
110 virtual void Notify();
111
112 private:
113 wxWindow *m_win;
114 wxScrollHelper *m_scrollHelper;
115 wxEventType m_eventType;
116 int m_pos,
117 m_orient;
118
119 DECLARE_NO_COPY_CLASS(wxAutoScrollTimer)
120 };
121
122 // ============================================================================
123 // implementation
124 // ============================================================================
125
126 // ----------------------------------------------------------------------------
127 // wxAutoScrollTimer
128 // ----------------------------------------------------------------------------
129
130 wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll,
131 wxScrollHelper *scroll,
132 wxEventType eventTypeToSend,
133 int pos, int orient)
134 {
135 m_win = winToScroll;
136 m_scrollHelper = scroll;
137 m_eventType = eventTypeToSend;
138 m_pos = pos;
139 m_orient = orient;
140 }
141
142 void wxAutoScrollTimer::Notify()
143 {
144 // only do all this as long as the window is capturing the mouse
145 if ( wxWindow::GetCapture() != m_win )
146 {
147 Stop();
148 }
149 else // we still capture the mouse, continue generating events
150 {
151 // first scroll the window if we are allowed to do it
152 wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
153 event1.SetEventObject(m_win);
154 if ( m_scrollHelper->SendAutoScrollEvents(event1) &&
155 m_win->GetEventHandler()->ProcessEvent(event1) )
156 {
157 // and then send a pseudo mouse-move event to refresh the selection
158 wxMouseEvent event2(wxEVT_MOTION);
159 wxGetMousePosition(&event2.m_x, &event2.m_y);
160
161 // the mouse event coordinates should be client, not screen as
162 // returned by wxGetMousePosition
163 wxWindow *parentTop = m_win;
164 while ( parentTop->GetParent() )
165 parentTop = parentTop->GetParent();
166 wxPoint ptOrig = parentTop->GetPosition();
167 event2.m_x -= ptOrig.x;
168 event2.m_y -= ptOrig.y;
169
170 event2.SetEventObject(m_win);
171
172 // FIXME: we don't fill in the other members - ok?
173
174 m_win->GetEventHandler()->ProcessEvent(event2);
175 }
176 else // can't scroll further, stop
177 {
178 Stop();
179 }
180 }
181 }
182 #endif
183
184 // ----------------------------------------------------------------------------
185 // wxScrollHelperEvtHandler
186 // ----------------------------------------------------------------------------
187
188 bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event)
189 {
190 wxEventType evType = event.GetEventType();
191
192 // the explanation of wxEVT_PAINT processing hack: for historic reasons
193 // there are 2 ways to process this event in classes deriving from
194 // wxScrolledWindow. The user code may
195 //
196 // 1. override wxScrolledWindow::OnDraw(dc)
197 // 2. define its own OnPaint() handler
198 //
199 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
200 // always processes the draw event, so we can't just try the window
201 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
202 // (the latter would never be called in wxUniversal).
203 //
204 // So the solution is to have a flag telling us whether the user code drew
205 // anything in the window. We set it to true here but reset it to false in
206 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
207 // user code defined OnPaint() in the derived class)
208 m_hasDrawnWindow = true;
209
210 // pass it on to the real handler
211 bool processed = wxEvtHandler::ProcessEvent(event);
212
213 // always process the size events ourselves, even if the user code handles
214 // them as well, as we need to AdjustScrollbars()
215 //
216 // NB: it is important to do it after processing the event in the normal
217 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
218 // scrollbar[s] (dis)appear and it should be seen by the user code
219 // after this one
220 if ( evType == wxEVT_SIZE )
221 {
222 m_scrollHelper->HandleOnSize((wxSizeEvent &)event);
223
224 return true;
225 }
226
227 if ( processed )
228 {
229 // normally, nothing more to do here - except if it was a paint event
230 // which wasn't really processed, then we'll try to call our
231 // OnDraw() below (from HandleOnPaint)
232 if ( m_hasDrawnWindow || event.IsCommandEvent() )
233 {
234 return true;
235 }
236 }
237
238 if ( evType == wxEVT_PAINT )
239 {
240 m_scrollHelper->HandleOnPaint((wxPaintEvent &)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 if ( processed )
300 event.Skip(wasSkipped);
301
302 return processed;
303 }
304
305 // ----------------------------------------------------------------------------
306 // wxScrollHelper construction
307 // ----------------------------------------------------------------------------
308
309 wxScrollHelper::wxScrollHelper(wxWindow *win)
310 {
311 wxASSERT_MSG( win, _T("associated window can't be NULL in wxScrollHelper") );
312
313 m_xScrollPixelsPerLine =
314 m_yScrollPixelsPerLine =
315 m_xScrollPosition =
316 m_yScrollPosition =
317 m_xScrollLines =
318 m_yScrollLines =
319 m_xScrollLinesPerPage =
320 m_yScrollLinesPerPage = 0;
321
322 m_xScrollingEnabled =
323 m_yScrollingEnabled = true;
324
325 m_scaleX =
326 m_scaleY = 1.0;
327 #if wxUSE_MOUSEWHEEL
328 m_wheelRotation = 0;
329 #endif
330
331 m_win =
332 m_targetWindow = (wxWindow *)NULL;
333
334 m_timerAutoScroll = (wxTimer *)NULL;
335
336 m_handler = NULL;
337
338 m_win = win;
339
340 m_win->SetScrollHelper( this );
341
342 // by default, the associated window is also the target window
343 DoSetTargetWindow(win);
344 }
345
346 wxScrollHelper::~wxScrollHelper()
347 {
348 StopAutoScrolling();
349
350 DeleteEvtHandler();
351 }
352
353 // ----------------------------------------------------------------------------
354 // setting scrolling parameters
355 // ----------------------------------------------------------------------------
356
357 void wxScrollHelper::SetScrollbars(int pixelsPerUnitX,
358 int pixelsPerUnitY,
359 int noUnitsX,
360 int noUnitsY,
361 int xPos,
362 int yPos,
363 bool noRefresh)
364 {
365 int xpos, ypos;
366
367 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
368 bool do_refresh =
369 (
370 (noUnitsX != 0 && m_xScrollLines == 0) ||
371 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX * noUnitsX) ||
372
373 (noUnitsY != 0 && m_yScrollLines == 0) ||
374 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY * noUnitsY) ||
375 (xPos != m_xScrollPosition) ||
376 (yPos != m_yScrollPosition)
377 );
378
379 m_xScrollPixelsPerLine = pixelsPerUnitX;
380 m_yScrollPixelsPerLine = pixelsPerUnitY;
381 m_xScrollPosition = xPos;
382 m_yScrollPosition = yPos;
383
384 int w = noUnitsX * pixelsPerUnitX;
385 int h = noUnitsY * pixelsPerUnitY;
386
387 // For better backward compatibility we set persisting limits
388 // here not just the size. It makes SetScrollbars 'sticky'
389 // emulating the old non-autoscroll behaviour.
390 // m_targetWindow->SetVirtualSizeHints( w, h );
391
392 // The above should arguably be deprecated, this however we still need.
393
394 // take care not to set 0 virtual size, 0 means that we don't have any
395 // scrollbars and hence we should use the real size instead of the virtual
396 // one which is indicated by using wxDefaultCoord
397 m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord,
398 h ? h : wxDefaultCoord);
399
400 if (do_refresh && !noRefresh)
401 m_targetWindow->Refresh(true, GetScrollRect());
402
403 #ifndef __WXUNIVERSAL__
404 // If the target is not the same as the window with the scrollbars,
405 // then we need to update the scrollbars here, since they won't have
406 // been updated by SetVirtualSize().
407 if ( m_targetWindow != m_win )
408 #endif // !__WXUNIVERSAL__
409 {
410 AdjustScrollbars();
411 }
412 #ifndef __WXUNIVERSAL__
413 else
414 {
415 // otherwise this has been done by AdjustScrollbars, above
416 }
417 #endif // !__WXUNIVERSAL__
418 }
419
420 // ----------------------------------------------------------------------------
421 // [target] window handling
422 // ----------------------------------------------------------------------------
423
424 void wxScrollHelper::DeleteEvtHandler()
425 {
426 // search for m_handler in the handler list
427 if ( m_win && m_handler )
428 {
429 if ( m_win->RemoveEventHandler(m_handler) )
430 {
431 delete m_handler;
432 }
433 //else: something is very wrong, so better [maybe] leak memory than
434 // risk a crash because of double deletion
435
436 m_handler = NULL;
437 }
438 }
439
440 void wxScrollHelper::DoSetTargetWindow(wxWindow *target)
441 {
442 m_targetWindow = target;
443 #ifdef __WXMAC__
444 target->MacSetClipChildren( true ) ;
445 #endif
446
447 // install the event handler which will intercept the events we're
448 // interested in (but only do it for our real window, not the target window
449 // which we scroll - we don't need to hijack its events)
450 if ( m_targetWindow == m_win )
451 {
452 // if we already have a handler, delete it first
453 DeleteEvtHandler();
454
455 m_handler = new wxScrollHelperEvtHandler(this);
456 m_targetWindow->PushEventHandler(m_handler);
457 }
458 }
459
460 void wxScrollHelper::SetTargetWindow(wxWindow *target)
461 {
462 wxCHECK_RET( target, wxT("target window must not be NULL") );
463
464 if ( target == m_targetWindow )
465 return;
466
467 DoSetTargetWindow(target);
468 }
469
470 wxWindow *wxScrollHelper::GetTargetWindow() const
471 {
472 return m_targetWindow;
473 }
474
475 // ----------------------------------------------------------------------------
476 // scrolling implementation itself
477 // ----------------------------------------------------------------------------
478
479 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event)
480 {
481 int nScrollInc = CalcScrollInc(event);
482 if ( nScrollInc == 0 )
483 {
484 // can't scroll further
485 event.Skip();
486
487 return;
488 }
489
490 bool needsRefresh = false;
491 int dx = 0,
492 dy = 0;
493 int orient = event.GetOrientation();
494 if (orient == wxHORIZONTAL)
495 {
496 if ( m_xScrollingEnabled )
497 {
498 dx = -m_xScrollPixelsPerLine * nScrollInc;
499 }
500 else
501 {
502 needsRefresh = true;
503 }
504 }
505 else
506 {
507 if ( m_yScrollingEnabled )
508 {
509 dy = -m_yScrollPixelsPerLine * nScrollInc;
510 }
511 else
512 {
513 needsRefresh = true;
514 }
515 }
516
517 if ( !needsRefresh )
518 {
519 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
520 // otherwise invalidated area could be updated incorrectly later when
521 // ScrollWindow() makes sure they're repainted before scrolling them
522 #ifdef __WXMAC__
523 // wxWindowMac is taking care of making sure the update area is correctly
524 // set up, while not forcing an immediate redraw
525 #else
526 m_targetWindow->Update();
527 #endif
528 }
529
530 if (orient == wxHORIZONTAL)
531 {
532 m_xScrollPosition += nScrollInc;
533 m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
534 }
535 else
536 {
537 m_yScrollPosition += nScrollInc;
538 m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
539 }
540
541 if ( needsRefresh )
542 {
543 m_targetWindow->Refresh(true, GetScrollRect());
544 }
545 else
546 {
547 m_targetWindow->ScrollWindow(dx, dy, GetScrollRect());
548 }
549 }
550
551 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event)
552 {
553 int pos = event.GetPosition();
554 int orient = event.GetOrientation();
555
556 int nScrollInc = 0;
557 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
558 {
559 if (orient == wxHORIZONTAL)
560 nScrollInc = - m_xScrollPosition;
561 else
562 nScrollInc = - m_yScrollPosition;
563 } else
564 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
565 {
566 if (orient == wxHORIZONTAL)
567 nScrollInc = m_xScrollLines - m_xScrollPosition;
568 else
569 nScrollInc = m_yScrollLines - m_yScrollPosition;
570 } else
571 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
572 {
573 nScrollInc = -1;
574 } else
575 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
576 {
577 nScrollInc = 1;
578 } else
579 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
580 {
581 if (orient == wxHORIZONTAL)
582 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
583 else
584 nScrollInc = -GetScrollPageSize(wxVERTICAL);
585 } else
586 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
587 {
588 if (orient == wxHORIZONTAL)
589 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
590 else
591 nScrollInc = GetScrollPageSize(wxVERTICAL);
592 } else
593 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
594 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
595 {
596 if (orient == wxHORIZONTAL)
597 nScrollInc = pos - m_xScrollPosition;
598 else
599 nScrollInc = pos - m_yScrollPosition;
600 }
601
602 if (orient == wxHORIZONTAL)
603 {
604 if ( m_xScrollPosition + nScrollInc < 0 )
605 {
606 // As -ve as we can go
607 nScrollInc = -m_xScrollPosition;
608 }
609 else // check for the other bound
610 {
611 const int posMax = m_xScrollLines - m_xScrollLinesPerPage;
612 if ( m_xScrollPosition + nScrollInc > posMax )
613 {
614 // As +ve as we can go
615 nScrollInc = posMax - m_xScrollPosition;
616 }
617 }
618 }
619 else // wxVERTICAL
620 {
621 if ( m_yScrollPosition + nScrollInc < 0 )
622 {
623 // As -ve as we can go
624 nScrollInc = -m_yScrollPosition;
625 }
626 else // check for the other bound
627 {
628 const int posMax = m_yScrollLines - m_yScrollLinesPerPage;
629 if ( m_yScrollPosition + nScrollInc > posMax )
630 {
631 // As +ve as we can go
632 nScrollInc = posMax - m_yScrollPosition;
633 }
634 }
635 }
636
637 return nScrollInc;
638 }
639
640 // Adjust the scrollbars - new version.
641 void wxScrollHelper::AdjustScrollbars()
642 {
643 static wxRecursionGuardFlag s_flagReentrancy;
644 wxRecursionGuard guard(s_flagReentrancy);
645 if ( guard.IsInside() )
646 {
647 // don't reenter AdjustScrollbars() while another call to
648 // AdjustScrollbars() is in progress because this may lead to calling
649 // ScrollWindow() twice and this can really happen under MSW if
650 // SetScrollbar() call below adds or removes the scrollbar which
651 // changes the window size and hence results in another
652 // AdjustScrollbars() call
653 return;
654 }
655
656 int w = 0, h = 0;
657 int oldw, oldh;
658
659 int oldXScroll = m_xScrollPosition;
660 int oldYScroll = m_yScrollPosition;
661
662 // VZ: at least under Windows this loop is useless because when scrollbars
663 // [dis]appear we get a WM_SIZE resulting in another call to
664 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
665 // it here for now but it would be better to ensure that all ports
666 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
667 // necessary, and remove it later
668 // JACS: Stop potential infinite loop by limiting number of iterations
669 int iterationCount = 0;
670 const int iterationMax = 5;
671 do
672 {
673 iterationCount ++;
674
675 GetTargetSize(&w, 0);
676
677 // scroll lines per page: if 0, no scrolling is needed
678 int linesPerPage;
679
680 if ( m_xScrollPixelsPerLine == 0 )
681 {
682 // scrolling is disabled
683 m_xScrollLines = 0;
684 m_xScrollPosition = 0;
685 linesPerPage = 0;
686 }
687 else // might need scrolling
688 {
689 // Round up integer division to catch any "leftover" client space.
690 const int wVirt = m_targetWindow->GetVirtualSize().GetWidth();
691 m_xScrollLines = (wVirt + m_xScrollPixelsPerLine - 1) / m_xScrollPixelsPerLine;
692
693 // Calculate page size i.e. number of scroll units you get on the
694 // current client window.
695 linesPerPage = w / m_xScrollPixelsPerLine;
696
697 // Special case. When client and virtual size are very close but
698 // the client is big enough, kill scrollbar.
699 if ((linesPerPage < m_xScrollLines) && (w >= wVirt)) ++linesPerPage;
700
701 if (linesPerPage >= m_xScrollLines)
702 {
703 // we're big enough to not need scrolling
704 linesPerPage =
705 m_xScrollLines =
706 m_xScrollPosition = 0;
707 }
708 else // we do need a scrollbar
709 {
710 if ( linesPerPage < 1 )
711 linesPerPage = 1;
712
713 // Correct position if greater than extent of canvas minus
714 // the visible portion of it or if below zero
715 const int posMax = m_xScrollLines - linesPerPage;
716 if ( m_xScrollPosition > posMax )
717 m_xScrollPosition = posMax;
718 else if ( m_xScrollPosition < 0 )
719 m_xScrollPosition = 0;
720 }
721 }
722
723 m_win->SetScrollbar(wxHORIZONTAL, m_xScrollPosition,
724 linesPerPage, m_xScrollLines);
725
726 // The amount by which we scroll when paging
727 SetScrollPageSize(wxHORIZONTAL, linesPerPage);
728
729 GetTargetSize(0, &h);
730
731 if ( m_yScrollPixelsPerLine == 0 )
732 {
733 // scrolling is disabled
734 m_yScrollLines = 0;
735 m_yScrollPosition = 0;
736 linesPerPage = 0;
737 }
738 else // might need scrolling
739 {
740 // Round up integer division to catch any "leftover" client space.
741 const int hVirt = m_targetWindow->GetVirtualSize().GetHeight();
742 m_yScrollLines = ( hVirt + m_yScrollPixelsPerLine - 1 ) / m_yScrollPixelsPerLine;
743
744 // Calculate page size i.e. number of scroll units you get on the
745 // current client window.
746 linesPerPage = h / m_yScrollPixelsPerLine;
747
748 // Special case. When client and virtual size are very close but
749 // the client is big enough, kill scrollbar.
750 if ((linesPerPage < m_yScrollLines) && (h >= hVirt)) ++linesPerPage;
751
752 if (linesPerPage >= m_yScrollLines)
753 {
754 // we're big enough to not need scrolling
755 linesPerPage =
756 m_yScrollLines =
757 m_yScrollPosition = 0;
758 }
759 else // we do need a scrollbar
760 {
761 if ( linesPerPage < 1 )
762 linesPerPage = 1;
763
764 // Correct position if greater than extent of canvas minus
765 // the visible portion of it or if below zero
766 const int posMax = m_yScrollLines - linesPerPage;
767 if ( m_yScrollPosition > posMax )
768 m_yScrollPosition = posMax;
769 else if ( m_yScrollPosition < 0 )
770 m_yScrollPosition = 0;
771 }
772 }
773
774 m_win->SetScrollbar(wxVERTICAL, m_yScrollPosition,
775 linesPerPage, m_yScrollLines);
776
777 // The amount by which we scroll when paging
778 SetScrollPageSize(wxVERTICAL, linesPerPage);
779
780
781 // If a scrollbar (dis)appeared as a result of this, adjust them again.
782 oldw = w;
783 oldh = h;
784
785 GetTargetSize( &w, &h );
786 } while ( (w != oldw || h != oldh) && (iterationCount < iterationMax) );
787
788 #ifdef __WXMOTIF__
789 // Sorry, some Motif-specific code to implement a backing pixmap
790 // for the wxRETAINED style. Implementing a backing store can't
791 // be entirely generic because it relies on the wxWindowDC implementation
792 // to duplicate X drawing calls for the backing pixmap.
793
794 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
795 {
796 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
797
798 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
799 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
800 if (m_targetWindow->GetBackingPixmap() &&
801 !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
802 (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
803 {
804 XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
805 m_targetWindow->SetBackingPixmap((WXPixmap) 0);
806 }
807
808 if (!m_targetWindow->GetBackingPixmap() &&
809 (m_xScrollLines != 0) && (m_yScrollLines != 0))
810 {
811 int depth = wxDisplayDepth();
812 m_targetWindow->SetPixmapWidth(totalPixelWidth);
813 m_targetWindow->SetPixmapHeight(totalPixelHeight);
814 m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
815 m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth));
816 }
817
818 }
819 #endif // Motif
820
821 if (oldXScroll != m_xScrollPosition)
822 {
823 if (m_xScrollingEnabled)
824 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0,
825 GetScrollRect() );
826 else
827 m_targetWindow->Refresh(true, GetScrollRect());
828 }
829
830 if (oldYScroll != m_yScrollPosition)
831 {
832 if (m_yScrollingEnabled)
833 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
834 GetScrollRect() );
835 else
836 m_targetWindow->Refresh(true, GetScrollRect());
837 }
838 }
839
840 void wxScrollHelper::DoPrepareDC(wxDC& dc)
841 {
842 wxPoint pt = dc.GetDeviceOrigin();
843 #ifdef __WXGTK__
844 // It may actually be correct to always query
845 // the m_sign from the DC here, but I leve the
846 // #ifdef GTK for now.
847 if (m_win->GetLayoutDirection() == wxLayout_RightToLeft)
848 dc.SetDeviceOrigin( pt.x + m_xScrollPosition * m_xScrollPixelsPerLine,
849 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
850 else
851 #endif
852 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
853 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
854 dc.SetUserScale( m_scaleX, m_scaleY );
855 }
856
857 void wxScrollHelper::SetScrollRate( int xstep, int ystep )
858 {
859 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
860 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
861
862 m_xScrollPixelsPerLine = xstep;
863 m_yScrollPixelsPerLine = ystep;
864
865 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
866 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
867
868 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
869 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
870 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
871
872 AdjustScrollbars();
873 }
874
875 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
876 {
877 if ( x_unit )
878 *x_unit = m_xScrollPixelsPerLine;
879 if ( y_unit )
880 *y_unit = m_yScrollPixelsPerLine;
881 }
882
883
884 int wxScrollHelper::GetScrollLines( int orient ) const
885 {
886 if ( orient == wxHORIZONTAL )
887 return m_xScrollLines;
888 else
889 return m_yScrollLines;
890 }
891
892 int wxScrollHelper::GetScrollPageSize(int orient) const
893 {
894 if ( orient == wxHORIZONTAL )
895 return m_xScrollLinesPerPage;
896 else
897 return m_yScrollLinesPerPage;
898 }
899
900 void wxScrollHelper::SetScrollPageSize(int orient, int pageSize)
901 {
902 if ( orient == wxHORIZONTAL )
903 m_xScrollLinesPerPage = pageSize;
904 else
905 m_yScrollLinesPerPage = pageSize;
906 }
907
908 /*
909 * Scroll to given position (scroll position, not pixel position)
910 */
911 void wxScrollHelper::Scroll( int x_pos, int y_pos )
912 {
913 if (!m_targetWindow)
914 return;
915
916 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
917 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
918
919 int w = 0, h = 0;
920 GetTargetSize(&w, &h);
921
922 // compute new position:
923 int new_x = m_xScrollPosition;
924 int new_y = m_yScrollPosition;
925
926 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
927 {
928 new_x = x_pos;
929
930 // Calculate page size i.e. number of scroll units you get on the
931 // current client window
932 int noPagePositions = w/m_xScrollPixelsPerLine;
933 if (noPagePositions < 1) noPagePositions = 1;
934
935 // Correct position if greater than extent of canvas minus
936 // the visible portion of it or if below zero
937 new_x = wxMin( m_xScrollLines-noPagePositions, new_x );
938 new_x = wxMax( 0, new_x );
939 }
940 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
941 {
942 new_y = y_pos;
943
944 // Calculate page size i.e. number of scroll units you get on the
945 // current client window
946 int noPagePositions = h/m_yScrollPixelsPerLine;
947 if (noPagePositions < 1) noPagePositions = 1;
948
949 // Correct position if greater than extent of canvas minus
950 // the visible portion of it or if below zero
951 new_y = wxMin( m_yScrollLines-noPagePositions, new_y );
952 new_y = wxMax( 0, new_y );
953 }
954
955 if ( new_x == m_xScrollPosition && new_y == m_yScrollPosition )
956 return; // nothing to do, the position didn't change
957
958 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
959 // otherwise invalidated area could be updated incorrectly later when
960 // ScrollWindow() makes sure they're repainted before scrolling them
961 m_targetWindow->Update();
962
963 // update the position and scroll the window now:
964 if (m_xScrollPosition != new_x)
965 {
966 int old_x = m_xScrollPosition;
967 m_xScrollPosition = new_x;
968 m_win->SetScrollPos( wxHORIZONTAL, new_x );
969 m_targetWindow->ScrollWindow( (old_x-new_x)*m_xScrollPixelsPerLine, 0,
970 GetScrollRect() );
971 }
972
973 if (m_yScrollPosition != new_y)
974 {
975 int old_y = m_yScrollPosition;
976 m_yScrollPosition = new_y;
977 m_win->SetScrollPos( wxVERTICAL, new_y );
978 m_targetWindow->ScrollWindow( 0, (old_y-new_y)*m_yScrollPixelsPerLine,
979 GetScrollRect() );
980 }
981 }
982
983 void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
984 {
985 m_xScrollingEnabled = x_scroll;
986 m_yScrollingEnabled = y_scroll;
987 }
988
989 // Where the current view starts from
990 void wxScrollHelper::GetViewStart (int *x, int *y) const
991 {
992 if ( x )
993 *x = m_xScrollPosition;
994 if ( y )
995 *y = m_yScrollPosition;
996 }
997
998 void wxScrollHelper::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
999 {
1000 if ( xx )
1001 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
1002 if ( yy )
1003 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
1004 }
1005
1006 void wxScrollHelper::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
1007 {
1008 if ( xx )
1009 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
1010 if ( yy )
1011 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
1012 }
1013
1014 // ----------------------------------------------------------------------------
1015 // geometry
1016 // ----------------------------------------------------------------------------
1017
1018 bool wxScrollHelper::ScrollLayout()
1019 {
1020 if ( m_win->GetSizer() && m_targetWindow == m_win )
1021 {
1022 // If we're the scroll target, take into account the
1023 // virtual size and scrolled position of the window.
1024
1025 int x = 0, y = 0, w = 0, h = 0;
1026 CalcScrolledPosition(0,0, &x,&y);
1027 m_win->GetVirtualSize(&w, &h);
1028 m_win->GetSizer()->SetDimension(x, y, w, h);
1029 return true;
1030 }
1031
1032 // fall back to default for LayoutConstraints
1033 return m_win->wxWindow::Layout();
1034 }
1035
1036 void wxScrollHelper::ScrollDoSetVirtualSize(int x, int y)
1037 {
1038 m_win->wxWindow::DoSetVirtualSize( x, y );
1039 AdjustScrollbars();
1040
1041 if (m_win->GetAutoLayout())
1042 m_win->Layout();
1043 }
1044
1045 // wxWindow's GetBestVirtualSize returns the actual window size,
1046 // whereas we want to return the virtual size
1047 wxSize wxScrollHelper::ScrollGetBestVirtualSize() const
1048 {
1049 wxSize clientSize(m_win->GetClientSize());
1050 if ( m_win->GetSizer() )
1051 clientSize.IncTo(m_win->GetSizer()->CalcMin());
1052
1053 return clientSize;
1054 }
1055
1056 // return the window best size from the given best virtual size
1057 wxSize
1058 wxScrollHelper::ScrollGetWindowSizeForVirtualSize(const wxSize& size) const
1059 {
1060 // Only use the content to set the window size in the direction
1061 // where there's no scrolling; otherwise we're going to get a huge
1062 // window in the direction in which scrolling is enabled
1063 int ppuX, ppuY;
1064 GetScrollPixelsPerUnit(&ppuX, &ppuY);
1065
1066 wxSize minSize = m_win->GetMinSize();
1067 if ( !minSize.IsFullySpecified() )
1068 minSize = m_win->GetSize();
1069
1070 wxSize best(size);
1071 if (ppuX > 0)
1072 best.x = minSize.x;
1073 if (ppuY > 0)
1074 best.y = minSize.y;
1075
1076 return best;
1077 }
1078
1079 // ----------------------------------------------------------------------------
1080 // event handlers
1081 // ----------------------------------------------------------------------------
1082
1083 // Default OnSize resets scrollbars, if any
1084 void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
1085 {
1086 if ( m_targetWindow->GetAutoLayout() )
1087 {
1088 wxSize size = m_targetWindow->GetBestVirtualSize();
1089
1090 // This will call ::Layout() and ::AdjustScrollbars()
1091 m_win->SetVirtualSize( size );
1092 }
1093 else
1094 {
1095 AdjustScrollbars();
1096 }
1097 }
1098
1099 // This calls OnDraw, having adjusted the origin according to the current
1100 // scroll position
1101 void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
1102 {
1103 // don't use m_targetWindow here, this is always called for ourselves
1104 wxPaintDC dc(m_win);
1105 DoPrepareDC(dc);
1106
1107 OnDraw(dc);
1108 }
1109
1110 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
1111 // compatibility here - if we used OnKeyDown(), the programs which process
1112 // arrows themselves in their OnChar() would never get the message and like
1113 // this they always have the priority
1114 void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
1115 {
1116 int stx = 0, sty = 0, // view origin
1117 szx = 0, szy = 0, // view size (total)
1118 clix = 0, cliy = 0; // view size (on screen)
1119
1120 GetViewStart(&stx, &sty);
1121 GetTargetSize(&clix, &cliy);
1122 m_targetWindow->GetVirtualSize(&szx, &szy);
1123
1124 if( m_xScrollPixelsPerLine )
1125 {
1126 clix /= m_xScrollPixelsPerLine;
1127 szx /= m_xScrollPixelsPerLine;
1128 }
1129 else
1130 {
1131 clix = 0;
1132 szx = -1;
1133 }
1134 if( m_yScrollPixelsPerLine )
1135 {
1136 cliy /= m_yScrollPixelsPerLine;
1137 szy /= m_yScrollPixelsPerLine;
1138 }
1139 else
1140 {
1141 cliy = 0;
1142 szy = -1;
1143 }
1144
1145 int xScrollOld = m_xScrollPosition,
1146 yScrollOld = m_yScrollPosition;
1147
1148 int dsty;
1149 switch ( event.GetKeyCode() )
1150 {
1151 case WXK_PAGEUP:
1152 dsty = sty - (5 * cliy / 6);
1153 Scroll(-1, (dsty == -1) ? 0 : dsty);
1154 break;
1155
1156 case WXK_PAGEDOWN:
1157 Scroll(-1, sty + (5 * cliy / 6));
1158 break;
1159
1160 case WXK_HOME:
1161 Scroll(0, event.ControlDown() ? 0 : -1);
1162 break;
1163
1164 case WXK_END:
1165 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
1166 break;
1167
1168 case WXK_UP:
1169 Scroll(-1, sty - 1);
1170 break;
1171
1172 case WXK_DOWN:
1173 Scroll(-1, sty + 1);
1174 break;
1175
1176 case WXK_LEFT:
1177 Scroll(stx - 1, -1);
1178 break;
1179
1180 case WXK_RIGHT:
1181 Scroll(stx + 1, -1);
1182 break;
1183
1184 default:
1185 // not for us
1186 event.Skip();
1187 }
1188
1189 if ( m_xScrollPosition != xScrollOld )
1190 {
1191 wxScrollWinEvent evt(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
1192 wxHORIZONTAL);
1193 evt.SetEventObject(m_win);
1194 m_win->GetEventHandler()->ProcessEvent(evt);
1195 }
1196
1197 if ( m_yScrollPosition != yScrollOld )
1198 {
1199 wxScrollWinEvent evt(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
1200 wxVERTICAL);
1201 evt.SetEventObject(m_win);
1202 m_win->GetEventHandler()->ProcessEvent(evt);
1203 }
1204 }
1205
1206 // ----------------------------------------------------------------------------
1207 // autoscroll stuff: these functions deal with sending fake scroll events when
1208 // a captured mouse is being held outside the window
1209 // ----------------------------------------------------------------------------
1210
1211 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
1212 {
1213 // only send the event if the window is scrollable in this direction
1214 wxWindow *win = (wxWindow *)event.GetEventObject();
1215 return win->HasScrollbar(event.GetOrientation());
1216 }
1217
1218 void wxScrollHelper::StopAutoScrolling()
1219 {
1220 #if wxUSE_TIMER
1221 if ( m_timerAutoScroll )
1222 {
1223 delete m_timerAutoScroll;
1224 m_timerAutoScroll = (wxTimer *)NULL;
1225 }
1226 #endif
1227 }
1228
1229 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
1230 {
1231 StopAutoScrolling();
1232
1233 event.Skip();
1234 }
1235
1236 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
1237 {
1238 // don't prevent the usual processing of the event from taking place
1239 event.Skip();
1240
1241 // when a captured mouse leave a scrolled window we start generate
1242 // scrolling events to allow, for example, extending selection beyond the
1243 // visible area in some controls
1244 if ( wxWindow::GetCapture() == m_targetWindow )
1245 {
1246 // where is the mouse leaving?
1247 int pos, orient;
1248 wxPoint pt = event.GetPosition();
1249 if ( pt.x < 0 )
1250 {
1251 orient = wxHORIZONTAL;
1252 pos = 0;
1253 }
1254 else if ( pt.y < 0 )
1255 {
1256 orient = wxVERTICAL;
1257 pos = 0;
1258 }
1259 else // we're lower or to the right of the window
1260 {
1261 wxSize size = m_targetWindow->GetClientSize();
1262 if ( pt.x > size.x )
1263 {
1264 orient = wxHORIZONTAL;
1265 pos = m_xScrollLines;
1266 }
1267 else if ( pt.y > size.y )
1268 {
1269 orient = wxVERTICAL;
1270 pos = m_yScrollLines;
1271 }
1272 else // this should be impossible
1273 {
1274 // but seems to happen sometimes under wxMSW - maybe it's a bug
1275 // there but for now just ignore it
1276
1277 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1278
1279 return;
1280 }
1281 }
1282
1283 // only start the auto scroll timer if the window can be scrolled in
1284 // this direction
1285 if ( !m_targetWindow->HasScrollbar(orient) )
1286 return;
1287
1288 #if wxUSE_TIMER
1289 delete m_timerAutoScroll;
1290 m_timerAutoScroll = new wxAutoScrollTimer
1291 (
1292 m_targetWindow, this,
1293 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1294 : wxEVT_SCROLLWIN_LINEDOWN,
1295 pos,
1296 orient
1297 );
1298 m_timerAutoScroll->Start(50); // FIXME: make configurable
1299 #else
1300 wxUnusedVar(pos);
1301 #endif
1302 }
1303 }
1304
1305 #if wxUSE_MOUSEWHEEL
1306
1307 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
1308 {
1309 m_wheelRotation += event.GetWheelRotation();
1310 int lines = m_wheelRotation / event.GetWheelDelta();
1311 m_wheelRotation -= lines * event.GetWheelDelta();
1312
1313 if (lines != 0)
1314 {
1315
1316 wxScrollWinEvent newEvent;
1317
1318 newEvent.SetPosition(0);
1319 newEvent.SetOrientation( event.GetWheelAxis() == 0 ? wxVERTICAL : wxHORIZONTAL);
1320 newEvent.SetEventObject(m_win);
1321
1322 if (event.IsPageScroll())
1323 {
1324 if (lines > 0)
1325 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
1326 else
1327 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
1328
1329 m_win->GetEventHandler()->ProcessEvent(newEvent);
1330 }
1331 else
1332 {
1333 lines *= event.GetLinesPerAction();
1334 if (lines > 0)
1335 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
1336 else
1337 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
1338
1339 int times = abs(lines);
1340 for (; times > 0; times--)
1341 m_win->GetEventHandler()->ProcessEvent(newEvent);
1342 }
1343 }
1344 }
1345
1346 #endif // wxUSE_MOUSEWHEEL
1347
1348 // ----------------------------------------------------------------------------
1349 // wxScrolledWindow implementation
1350 // ----------------------------------------------------------------------------
1351
1352 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
1353
1354 BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
1355 EVT_PAINT(wxScrolledWindow::OnPaint)
1356 END_EVENT_TABLE()
1357
1358 bool wxScrolledWindow::Create(wxWindow *parent,
1359 wxWindowID id,
1360 const wxPoint& pos,
1361 const wxSize& size,
1362 long style,
1363 const wxString& name)
1364 {
1365 m_targetWindow = this;
1366 #ifdef __WXMAC__
1367 MacSetClipChildren( true ) ;
1368 #endif
1369
1370 // by default, we're scrollable in both directions (but if one of the
1371 // styles is specified explicitly, we shouldn't add the other one
1372 // automatically)
1373 if ( !(style & (wxHSCROLL | wxVSCROLL)) )
1374 style |= wxHSCROLL | wxVSCROLL;
1375
1376 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
1377
1378 return ok;
1379 }
1380
1381 wxScrolledWindow::~wxScrolledWindow()
1382 {
1383 }
1384
1385 void wxScrolledWindow::OnPaint(wxPaintEvent& event)
1386 {
1387 // the user code didn't really draw the window if we got here, so set this
1388 // flag to try to call OnDraw() later
1389 m_handler->ResetDrawnFlag();
1390
1391 event.Skip();
1392 }
1393
1394 #ifdef __WXMSW__
1395 WXLRESULT wxScrolledWindow::MSWWindowProc(WXUINT nMsg,
1396 WXWPARAM wParam,
1397 WXLPARAM lParam)
1398 {
1399 WXLRESULT rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam);
1400
1401 #ifndef __WXWINCE__
1402 // we need to process arrows ourselves for scrolling
1403 if ( nMsg == WM_GETDLGCODE )
1404 {
1405 rc |= DLGC_WANTARROWS;
1406 }
1407 #endif
1408
1409 return rc;
1410 }
1411
1412 #endif // __WXMSW__