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