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