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