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