]> git.saurik.com Git - wxWidgets.git/blob - src/generic/scrlwing.cpp
compilation fix
[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 licence
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 int w = noUnitsX * pixelsPerUnitX;
348 int h = noUnitsY * pixelsPerUnitY;
349
350 // For better backward compatibility we set persisting limits
351 // here not just the size. It makes SetScrollbars 'sticky'
352 // emulating the old non-autoscroll behaviour.
353
354 m_targetWindow->SetVirtualSizeHints( w, h );
355
356 // The above should arguably be deprecated, this however we still need.
357
358 m_targetWindow->SetVirtualSize( w, h );
359
360 if (do_refresh && !noRefresh)
361 m_targetWindow->Refresh(TRUE, GetRect());
362
363 // TODO: check if we can use AdjustScrollbars always.
364 #ifdef __WXUNIVERSAL__
365 AdjustScrollbars();
366 #else
367 // This is also done by AdjustScrollbars, above
368 #ifdef __WXMAC__
369 m_targetWindow->MacUpdateImmediately() ;
370 #endif
371 #endif
372 }
373
374 // ----------------------------------------------------------------------------
375 // [target] window handling
376 // ----------------------------------------------------------------------------
377
378 void wxScrollHelper::DeleteEvtHandler()
379 {
380 // search for m_handler in the handler list
381 if ( m_win && m_handler )
382 {
383 if ( m_win->RemoveEventHandler(m_handler) )
384 {
385 delete m_handler;
386 }
387 //else: something is very wrong, so better [maybe] leak memory than
388 // risk a crash because of double deletion
389
390 m_handler = NULL;
391 }
392 }
393
394 void wxScrollHelper::SetWindow(wxWindow *win)
395 {
396 wxCHECK_RET( win, _T("wxScrollHelper needs a window to scroll") );
397
398 m_win = win;
399
400 // by default, the associated window is also the target window
401 DoSetTargetWindow(win);
402 }
403
404 void wxScrollHelper::DoSetTargetWindow(wxWindow *target)
405 {
406 m_targetWindow = target;
407
408 // install the event handler which will intercept the events we're
409 // interested in (but only do it for our real window, not the target window
410 // which we scroll - we don't need to hijack its events)
411 if ( m_targetWindow == m_win )
412 {
413 // if we already have a handler, delete it first
414 DeleteEvtHandler();
415
416 m_handler = new wxScrollHelperEvtHandler(this);
417 m_targetWindow->PushEventHandler(m_handler);
418 }
419 }
420
421 void wxScrollHelper::SetTargetWindow(wxWindow *target)
422 {
423 wxCHECK_RET( target, wxT("target window must not be NULL") );
424
425 if ( target == m_targetWindow )
426 return;
427
428 DoSetTargetWindow(target);
429 }
430
431 wxWindow *wxScrollHelper::GetTargetWindow() const
432 {
433 return m_targetWindow;
434 }
435
436 // ----------------------------------------------------------------------------
437 // scrolling implementation itself
438 // ----------------------------------------------------------------------------
439
440 void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event)
441 {
442 int nScrollInc = CalcScrollInc(event);
443 if ( nScrollInc == 0 )
444 {
445 // can't scroll further
446 event.Skip();
447
448 return;
449 }
450
451 int orient = event.GetOrientation();
452 if (orient == wxHORIZONTAL)
453 {
454 m_xScrollPosition += nScrollInc;
455 m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
456 }
457 else
458 {
459 m_yScrollPosition += nScrollInc;
460 m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
461 }
462
463 bool needsRefresh = FALSE;
464 int dx = 0,
465 dy = 0;
466 if (orient == wxHORIZONTAL)
467 {
468 if ( m_xScrollingEnabled )
469 {
470 dx = -m_xScrollPixelsPerLine * nScrollInc;
471 }
472 else
473 {
474 needsRefresh = TRUE;
475 }
476 }
477 else
478 {
479 if ( m_yScrollingEnabled )
480 {
481 dy = -m_yScrollPixelsPerLine * nScrollInc;
482 }
483 else
484 {
485 needsRefresh = TRUE;
486 }
487 }
488
489 if ( needsRefresh )
490 {
491 m_targetWindow->Refresh(TRUE, GetRect());
492 }
493 else
494 {
495 m_targetWindow->ScrollWindow(dx, dy, GetRect());
496 }
497
498 #ifdef __WXMAC__
499 m_targetWindow->MacUpdateImmediately() ;
500 #endif
501 }
502
503 int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event)
504 {
505 int pos = event.GetPosition();
506 int orient = event.GetOrientation();
507
508 int nScrollInc = 0;
509 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
510 {
511 if (orient == wxHORIZONTAL)
512 nScrollInc = - m_xScrollPosition;
513 else
514 nScrollInc = - m_yScrollPosition;
515 } else
516 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
517 {
518 if (orient == wxHORIZONTAL)
519 nScrollInc = m_xScrollLines - m_xScrollPosition;
520 else
521 nScrollInc = m_yScrollLines - m_yScrollPosition;
522 } else
523 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
524 {
525 nScrollInc = -1;
526 } else
527 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
528 {
529 nScrollInc = 1;
530 } else
531 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
532 {
533 if (orient == wxHORIZONTAL)
534 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
535 else
536 nScrollInc = -GetScrollPageSize(wxVERTICAL);
537 } else
538 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
539 {
540 if (orient == wxHORIZONTAL)
541 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
542 else
543 nScrollInc = GetScrollPageSize(wxVERTICAL);
544 } else
545 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
546 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
547 {
548 if (orient == wxHORIZONTAL)
549 nScrollInc = pos - m_xScrollPosition;
550 else
551 nScrollInc = pos - m_yScrollPosition;
552 }
553
554 if (orient == wxHORIZONTAL)
555 {
556 if (m_xScrollPixelsPerLine > 0)
557 {
558 int w, h;
559 GetTargetSize(&w, &h);
560
561 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
562 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
563 if (noPositions < 0)
564 noPositions = 0;
565
566 if ( (m_xScrollPosition + nScrollInc) < 0 )
567 nScrollInc = -m_xScrollPosition; // As -ve as we can go
568 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
569 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
570 }
571 else
572 m_targetWindow->Refresh(TRUE, GetRect());
573 }
574 else
575 {
576 if (m_yScrollPixelsPerLine > 0)
577 {
578 int w, h;
579 GetTargetSize(&w, &h);
580
581 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
582 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
583 if (noPositions < 0)
584 noPositions = 0;
585
586 if ( (m_yScrollPosition + nScrollInc) < 0 )
587 nScrollInc = -m_yScrollPosition; // As -ve as we can go
588 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
589 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
590 }
591 else
592 m_targetWindow->Refresh(TRUE, GetRect());
593 }
594
595 return nScrollInc;
596 }
597
598 // Adjust the scrollbars - new version.
599 void wxScrollHelper::AdjustScrollbars()
600 {
601 #ifdef __WXMAC__
602 m_targetWindow->MacUpdateImmediately();
603 #endif
604
605 int w = 0, h = 0;
606 int oldw, oldh;
607
608 int oldXScroll = m_xScrollPosition;
609 int oldYScroll = m_yScrollPosition;
610
611 do {
612 GetTargetSize(&w, 0);
613
614 if (m_xScrollPixelsPerLine == 0)
615 {
616 m_xScrollLines = 0;
617 m_xScrollPosition = 0;
618 m_win->SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
619 }
620 else
621 {
622 m_xScrollLines = m_targetWindow->GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine;
623
624 // Calculate page size i.e. number of scroll units you get on the
625 // current client window
626 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
627 if (noPagePositions < 1) noPagePositions = 1;
628 if ( noPagePositions > m_xScrollLines )
629 noPagePositions = m_xScrollLines;
630
631 // Correct position if greater than extent of canvas minus
632 // the visible portion of it or if below zero
633 m_xScrollPosition = wxMin( m_xScrollLines - noPagePositions, m_xScrollPosition);
634 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
635
636 m_win->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
637 // The amount by which we scroll when paging
638 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
639 }
640
641 GetTargetSize(0, &h);
642
643 if (m_yScrollPixelsPerLine == 0)
644 {
645 m_yScrollLines = 0;
646 m_yScrollPosition = 0;
647 m_win->SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
648 }
649 else
650 {
651 m_yScrollLines = m_targetWindow->GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine;
652
653 // Calculate page size i.e. number of scroll units you get on the
654 // current client window
655 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
656 if (noPagePositions < 1) noPagePositions = 1;
657 if ( noPagePositions > m_yScrollLines )
658 noPagePositions = m_yScrollLines;
659
660 // Correct position if greater than extent of canvas minus
661 // the visible portion of it or if below zero
662 m_yScrollPosition = wxMin( m_yScrollLines - noPagePositions, m_yScrollPosition );
663 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
664
665 m_win->SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
666 // The amount by which we scroll when paging
667 SetScrollPageSize(wxVERTICAL, noPagePositions);
668 }
669
670 // If a scrollbar (dis)appeared as a result of this, adjust them again.
671
672 oldw = w;
673 oldh = h;
674
675 GetTargetSize( &w, &h );
676 } while ( w != oldw && h != oldh );
677
678 #ifdef __WXMOTIF__
679 // Sorry, some Motif-specific code to implement a backing pixmap
680 // for the wxRETAINED style. Implementing a backing store can't
681 // be entirely generic because it relies on the wxWindowDC implementation
682 // to duplicate X drawing calls for the backing pixmap.
683
684 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
685 {
686 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
687
688 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
689 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
690 if (m_targetWindow->GetBackingPixmap() &&
691 !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
692 (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
693 {
694 XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
695 m_targetWindow->SetBackingPixmap((WXPixmap) 0);
696 }
697
698 if (!m_targetWindow->GetBackingPixmap() &&
699 (m_xScrollLines != 0) && (m_yScrollLines != 0))
700 {
701 int depth = wxDisplayDepth();
702 m_targetWindow->SetPixmapWidth(totalPixelWidth);
703 m_targetWindow->SetPixmapHeight(totalPixelHeight);
704 m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
705 m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth));
706 }
707
708 }
709 #endif // Motif
710
711 if (oldXScroll != m_xScrollPosition)
712 {
713 if (m_xScrollingEnabled)
714 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0,
715 GetRect() );
716 else
717 m_targetWindow->Refresh(TRUE, GetRect());
718 }
719
720 if (oldYScroll != m_yScrollPosition)
721 {
722 if (m_yScrollingEnabled)
723 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
724 GetRect() );
725 else
726 m_targetWindow->Refresh(TRUE, GetRect());
727 }
728
729 #ifdef __WXMAC__
730 m_targetWindow->MacUpdateImmediately();
731 #endif
732 }
733
734 void wxScrollHelper::DoPrepareDC(wxDC& dc)
735 {
736 wxPoint pt = dc.GetDeviceOrigin();
737 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
738 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
739 dc.SetUserScale( m_scaleX, m_scaleY );
740 }
741
742 void wxScrollHelper::SetScrollRate( int xstep, int ystep )
743 {
744 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
745 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
746
747 m_xScrollPixelsPerLine = xstep;
748 m_yScrollPixelsPerLine = ystep;
749
750 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
751 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
752
753 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
754 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
755 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
756
757 AdjustScrollbars();
758 }
759
760 void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
761 {
762 if ( x_unit )
763 *x_unit = m_xScrollPixelsPerLine;
764 if ( y_unit )
765 *y_unit = m_yScrollPixelsPerLine;
766 }
767
768 int wxScrollHelper::GetScrollPageSize(int orient) const
769 {
770 if ( orient == wxHORIZONTAL )
771 return m_xScrollLinesPerPage;
772 else
773 return m_yScrollLinesPerPage;
774 }
775
776 void wxScrollHelper::SetScrollPageSize(int orient, int pageSize)
777 {
778 if ( orient == wxHORIZONTAL )
779 m_xScrollLinesPerPage = pageSize;
780 else
781 m_yScrollLinesPerPage = pageSize;
782 }
783
784 /*
785 * Scroll to given position (scroll position, not pixel position)
786 */
787 void wxScrollHelper::Scroll( int x_pos, int y_pos )
788 {
789 if (!m_targetWindow)
790 return;
791
792 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
793 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
794
795 #ifdef __WXMAC__
796 m_targetWindow->MacUpdateImmediately();
797 #endif
798
799 int w, h;
800 GetTargetSize(&w, &h);
801
802 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
803 {
804 int old_x = m_xScrollPosition;
805 m_xScrollPosition = x_pos;
806
807 // Calculate page size i.e. number of scroll units you get on the
808 // current client window
809 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
810 if (noPagePositions < 1) noPagePositions = 1;
811
812 // Correct position if greater than extent of canvas minus
813 // the visible portion of it or if below zero
814 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
815 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
816
817 if (old_x != m_xScrollPosition) {
818 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
819 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0,
820 GetRect() );
821 }
822 }
823 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
824 {
825 int old_y = m_yScrollPosition;
826 m_yScrollPosition = y_pos;
827
828 // Calculate page size i.e. number of scroll units you get on the
829 // current client window
830 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
831 if (noPagePositions < 1) noPagePositions = 1;
832
833 // Correct position if greater than extent of canvas minus
834 // the visible portion of it or if below zero
835 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
836 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
837
838 if (old_y != m_yScrollPosition) {
839 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
840 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine,
841 GetRect() );
842 }
843 }
844
845 #ifdef __WXMAC__
846 m_targetWindow->MacUpdateImmediately();
847 #endif
848
849 }
850
851 void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
852 {
853 m_xScrollingEnabled = x_scroll;
854 m_yScrollingEnabled = y_scroll;
855 }
856
857 // Where the current view starts from
858 void wxScrollHelper::GetViewStart (int *x, int *y) const
859 {
860 if ( x )
861 *x = m_xScrollPosition;
862 if ( y )
863 *y = m_yScrollPosition;
864 }
865
866 void wxScrollHelper::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
867 {
868 if ( xx )
869 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
870 if ( yy )
871 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
872 }
873
874 void wxScrollHelper::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
875 {
876 if ( xx )
877 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
878 if ( yy )
879 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
880 }
881
882 // ----------------------------------------------------------------------------
883 // event handlers
884 // ----------------------------------------------------------------------------
885
886 // Default OnSize resets scrollbars, if any
887 void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
888 {
889 if( m_win->GetAutoLayout() || m_targetWindow->GetAutoLayout() )
890 {
891 if ( m_targetWindow != m_win )
892 m_targetWindow->FitInside();
893
894 m_win->FitInside();
895
896 // FIXME: Something is really weird here... This should be
897 // called by FitInside above (and apparently is), yet the
898 // scrollsub sample will get the scrollbar wrong if resized
899 // quickly. This masks the bug, but is surely not the right
900 // answer at all.
901 AdjustScrollbars();
902 }
903 else
904 AdjustScrollbars();
905 }
906
907 // This calls OnDraw, having adjusted the origin according to the current
908 // scroll position
909 void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
910 {
911 // don't use m_targetWindow here, this is always called for ourselves
912 wxPaintDC dc(m_win);
913 DoPrepareDC(dc);
914
915 OnDraw(dc);
916 }
917
918 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
919 // compatibility here - if we used OnKeyDown(), the programs which process
920 // arrows themselves in their OnChar() would never get the message and like
921 // this they always have the priority
922 void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
923 {
924 int stx, sty, // view origin
925 szx, szy, // view size (total)
926 clix, cliy; // view size (on screen)
927
928 GetViewStart(&stx, &sty);
929 GetTargetSize(&clix, &cliy);
930 m_targetWindow->GetVirtualSize(&szx, &szy);
931
932 if( m_xScrollPixelsPerLine )
933 {
934 clix /= m_xScrollPixelsPerLine;
935 szx /= m_xScrollPixelsPerLine;
936 }
937 else
938 {
939 clix = 0;
940 szx = -1;
941 }
942 if( m_yScrollPixelsPerLine )
943 {
944 cliy /= m_yScrollPixelsPerLine;
945 szy /= m_yScrollPixelsPerLine;
946 }
947 else
948 {
949 cliy = 0;
950 szy = -1;
951 }
952
953 int xScrollOld = m_xScrollPosition,
954 yScrollOld = m_yScrollPosition;
955
956 int dsty;
957 switch ( event.GetKeyCode() )
958 {
959 case WXK_PAGEUP:
960 case WXK_PRIOR:
961 dsty = sty - (5 * cliy / 6);
962 Scroll(-1, (dsty == -1) ? 0 : dsty);
963 break;
964
965 case WXK_PAGEDOWN:
966 case WXK_NEXT:
967 Scroll(-1, sty + (5 * cliy / 6));
968 break;
969
970 case WXK_HOME:
971 Scroll(0, event.ControlDown() ? 0 : -1);
972 break;
973
974 case WXK_END:
975 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
976 break;
977
978 case WXK_UP:
979 Scroll(-1, sty - 1);
980 break;
981
982 case WXK_DOWN:
983 Scroll(-1, sty + 1);
984 break;
985
986 case WXK_LEFT:
987 Scroll(stx - 1, -1);
988 break;
989
990 case WXK_RIGHT:
991 Scroll(stx + 1, -1);
992 break;
993
994 default:
995 // not for us
996 event.Skip();
997 }
998
999 if ( m_xScrollPosition != xScrollOld )
1000 {
1001 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
1002 wxHORIZONTAL);
1003 event.SetEventObject(m_win);
1004 m_win->GetEventHandler()->ProcessEvent(event);
1005 }
1006
1007 if ( m_yScrollPosition != yScrollOld )
1008 {
1009 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
1010 wxVERTICAL);
1011 event.SetEventObject(m_win);
1012 m_win->GetEventHandler()->ProcessEvent(event);
1013 }
1014 }
1015
1016 // ----------------------------------------------------------------------------
1017 // autoscroll stuff: these functions deal with sending fake scroll events when
1018 // a captured mouse is being held outside the window
1019 // ----------------------------------------------------------------------------
1020
1021 bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
1022 {
1023 // only send the event if the window is scrollable in this direction
1024 wxWindow *win = (wxWindow *)event.GetEventObject();
1025 return win->HasScrollbar(event.GetOrientation());
1026 }
1027
1028 void wxScrollHelper::StopAutoScrolling()
1029 {
1030 if ( m_timerAutoScroll )
1031 {
1032 delete m_timerAutoScroll;
1033 m_timerAutoScroll = (wxTimer *)NULL;
1034 }
1035 }
1036
1037 void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
1038 {
1039 StopAutoScrolling();
1040
1041 event.Skip();
1042 }
1043
1044 void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
1045 {
1046 // don't prevent the usual processing of the event from taking place
1047 event.Skip();
1048
1049 // when a captured mouse leave a scrolled window we start generate
1050 // scrolling events to allow, for example, extending selection beyond the
1051 // visible area in some controls
1052 if ( wxWindow::GetCapture() == m_targetWindow )
1053 {
1054 // where is the mouse leaving?
1055 int pos, orient;
1056 wxPoint pt = event.GetPosition();
1057 if ( pt.x < 0 )
1058 {
1059 orient = wxHORIZONTAL;
1060 pos = 0;
1061 }
1062 else if ( pt.y < 0 )
1063 {
1064 orient = wxVERTICAL;
1065 pos = 0;
1066 }
1067 else // we're lower or to the right of the window
1068 {
1069 wxSize size = m_targetWindow->GetClientSize();
1070 if ( pt.x > size.x )
1071 {
1072 orient = wxHORIZONTAL;
1073 pos = m_xScrollLines;
1074 }
1075 else if ( pt.y > size.y )
1076 {
1077 orient = wxVERTICAL;
1078 pos = m_yScrollLines;
1079 }
1080 else // this should be impossible
1081 {
1082 // but seems to happen sometimes under wxMSW - maybe it's a bug
1083 // there but for now just ignore it
1084
1085 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1086
1087 return;
1088 }
1089 }
1090
1091 // only start the auto scroll timer if the window can be scrolled in
1092 // this direction
1093 if ( !m_targetWindow->HasScrollbar(orient) )
1094 return;
1095
1096 delete m_timerAutoScroll;
1097 m_timerAutoScroll = new wxAutoScrollTimer
1098 (
1099 m_targetWindow, this,
1100 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1101 : wxEVT_SCROLLWIN_LINEDOWN,
1102 pos,
1103 orient
1104 );
1105 m_timerAutoScroll->Start(50); // FIXME: make configurable
1106 }
1107 }
1108
1109 #if wxUSE_MOUSEWHEEL
1110
1111 void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
1112 {
1113 m_wheelRotation += event.GetWheelRotation();
1114 int lines = m_wheelRotation / event.GetWheelDelta();
1115 m_wheelRotation -= lines * event.GetWheelDelta();
1116
1117 if (lines != 0)
1118 {
1119
1120 wxScrollWinEvent newEvent;
1121
1122 newEvent.SetPosition(0);
1123 newEvent.SetOrientation(wxVERTICAL);
1124 newEvent.m_eventObject = m_win;
1125
1126 if (event.IsPageScroll())
1127 {
1128 if (lines > 0)
1129 newEvent.m_eventType = wxEVT_SCROLLWIN_PAGEUP;
1130 else
1131 newEvent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN;
1132
1133 m_win->GetEventHandler()->ProcessEvent(newEvent);
1134 }
1135 else
1136 {
1137 lines *= event.GetLinesPerAction();
1138 if (lines > 0)
1139 newEvent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
1140 else
1141 newEvent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
1142
1143 int times = abs(lines);
1144 for (; times > 0; times--)
1145 m_win->GetEventHandler()->ProcessEvent(newEvent);
1146 }
1147 }
1148 }
1149
1150 #endif // wxUSE_MOUSEWHEEL
1151
1152 // ----------------------------------------------------------------------------
1153 // wxGenericScrolledWindow implementation
1154 // ----------------------------------------------------------------------------
1155
1156 IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
1157
1158 BEGIN_EVENT_TABLE(wxGenericScrolledWindow, wxPanel)
1159 EVT_PAINT(wxGenericScrolledWindow::OnPaint)
1160 END_EVENT_TABLE()
1161
1162 bool wxGenericScrolledWindow::Create(wxWindow *parent,
1163 wxWindowID id,
1164 const wxPoint& pos,
1165 const wxSize& size,
1166 long style,
1167 const wxString& name)
1168 {
1169 m_targetWindow = this;
1170
1171 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
1172
1173 return ok;
1174 }
1175
1176 wxGenericScrolledWindow::~wxGenericScrolledWindow()
1177 {
1178 }
1179
1180 bool wxGenericScrolledWindow::Layout()
1181 {
1182 if (GetSizer() && m_targetWindow == this)
1183 {
1184 // If we're the scroll target, take into account the
1185 // virtual size and scrolled position of the window.
1186
1187 int x, y, w, h;
1188 CalcScrolledPosition(0,0, &x,&y);
1189 GetVirtualSize(&w, &h);
1190 GetSizer()->SetDimension(x, y, w, h);
1191 return TRUE;
1192 }
1193
1194 // fall back to default for LayoutConstraints
1195 return wxPanel::Layout();
1196 }
1197
1198 void wxGenericScrolledWindow::DoSetVirtualSize(int x, int y)
1199 {
1200 wxPanel::DoSetVirtualSize( x, y );
1201 AdjustScrollbars();
1202
1203 #if wxUSE_CONSTRAINTS
1204 if (GetAutoLayout())
1205 Layout();
1206 #endif
1207 }
1208
1209 void wxGenericScrolledWindow::OnPaint(wxPaintEvent& event)
1210 {
1211 // the user code didn't really draw the window if we got here, so set this
1212 // flag to try to call OnDraw() later
1213 m_handler->ResetDrawnFlag();
1214
1215 event.Skip();
1216 }
1217
1218 #ifdef __WXMSW__
1219 long
1220 wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg,
1221 WXWPARAM wParam,
1222 WXLPARAM lParam)
1223 {
1224 long rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam);
1225
1226 // we need to process arrows ourselves for scrolling
1227 if ( nMsg == WM_GETDLGCODE )
1228 {
1229 rc |= DLGC_WANTARROWS;
1230 }
1231
1232 return rc;
1233 }
1234
1235 #endif // __WXMSW__
1236
1237 #if WXWIN_COMPATIBILITY
1238
1239 void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
1240 {
1241 *x_page = GetScrollPageSize(wxHORIZONTAL);
1242 *y_page = GetScrollPageSize(wxVERTICAL);
1243 }
1244
1245 void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
1246 {
1247 if ( xx )
1248 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
1249 if ( yy )
1250 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
1251 }
1252
1253 #endif // WXWIN_COMPATIBILITY
1254
1255 #endif // !wxGTK
1256
1257 // vi:sts=4:sw=4:et