]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrlwing.cpp
compile fix for Mac
[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
VZ
313 m_win = win;
314
315 // by default, the associated window is also the target window
316 DoSetTargetWindow(win);
1e6feb95 317}
d7da9756 318
1e6feb95 319wxScrollHelper::~wxScrollHelper()
d80cd92a 320{
1e6feb95
VZ
321 StopAutoScrolling();
322
349efbaa 323 DeleteEvtHandler();
d80cd92a
VZ
324}
325
326// ----------------------------------------------------------------------------
327// setting scrolling parameters
328// ----------------------------------------------------------------------------
329
1e6feb95
VZ
330void wxScrollHelper::SetScrollbars(int pixelsPerUnitX,
331 int pixelsPerUnitY,
332 int noUnitsX,
333 int noUnitsY,
334 int xPos,
335 int yPos,
336 bool noRefresh)
c801d85f 337{
b0486e0d
SN
338 int xpos, ypos;
339
340 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
139adb6a
RR
341 bool do_refresh =
342 (
c801d85f 343 (noUnitsX != 0 && m_xScrollLines == 0) ||
566d84a7 344 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX * noUnitsX) ||
b0486e0d 345
c801d85f 346 (noUnitsY != 0 && m_yScrollLines == 0) ||
566d84a7 347 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY * noUnitsY) ||
c801d85f 348 (xPos != m_xScrollPosition) ||
b0486e0d 349 (yPos != m_yScrollPosition)
139adb6a 350 );
a58a12e9 351
139adb6a
RR
352 m_xScrollPixelsPerLine = pixelsPerUnitX;
353 m_yScrollPixelsPerLine = pixelsPerUnitY;
354 m_xScrollPosition = xPos;
355 m_yScrollPosition = yPos;
a91b47e8 356
150c8d89
RL
357 int w = noUnitsX * pixelsPerUnitX;
358 int h = noUnitsY * pixelsPerUnitY;
359
2b5f62a0
VZ
360 // For better backward compatibility we set persisting limits
361 // here not just the size. It makes SetScrollbars 'sticky'
362 // emulating the old non-autoscroll behaviour.
2b43d588 363 // m_targetWindow->SetVirtualSizeHints( w, h );
a58a12e9 364
2b5f62a0
VZ
365 // The above should arguably be deprecated, this however we still need.
366
f18f464c
VZ
367 // take care not to set 0 virtual size, 0 means that we don't have any
368 // scrollbars and hence we should use the real size instead of the virtual
369 // one which is indicated by using wxDefaultCoord
370 m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord,
371 h ? h : wxDefaultCoord);
dc429f89 372
a58a12e9 373 if (do_refresh && !noRefresh)
ca65c044 374 m_targetWindow->Refresh(true, GetScrollRect());
a58a12e9 375
1f066698
VZ
376#ifndef __WXUNIVERSAL__
377 // If the target is not the same as the window with the scrollbars,
378 // then we need to update the scrollbars here, since they won't have
379 // been updated by SetVirtualSize().
380 if ( m_targetWindow != m_win )
381#endif // !__WXUNIVERSAL__
382 {
383 AdjustScrollbars();
384 }
385#ifndef __WXUNIVERSAL__
386 else
387 {
388 // otherwise this has been done by AdjustScrollbars, above
1f066698
VZ
389 }
390#endif // !__WXUNIVERSAL__
c801d85f
KB
391}
392
d80cd92a 393// ----------------------------------------------------------------------------
af4088f1 394// [target] window handling
d80cd92a 395// ----------------------------------------------------------------------------
ecab4dba 396
349efbaa 397void wxScrollHelper::DeleteEvtHandler()
ecab4dba 398{
248d771c
VZ
399 // search for m_handler in the handler list
400 if ( m_win && m_handler )
349efbaa 401 {
74f6bbf9 402 if ( m_win->RemoveEventHandler(m_handler) )
248d771c 403 {
74f6bbf9 404 delete m_handler;
248d771c 405 }
74f6bbf9
VZ
406 //else: something is very wrong, so better [maybe] leak memory than
407 // risk a crash because of double deletion
248d771c 408
74f6bbf9 409 m_handler = NULL;
349efbaa
VZ
410 }
411}
412
af4088f1 413void wxScrollHelper::DoSetTargetWindow(wxWindow *target)
349efbaa 414{
ecab4dba 415 m_targetWindow = target;
8adc196b
SC
416#ifdef __WXMAC__
417 target->MacSetClipChildren( true ) ;
418#endif
349efbaa
VZ
419
420 // install the event handler which will intercept the events we're
af4088f1
VZ
421 // interested in (but only do it for our real window, not the target window
422 // which we scroll - we don't need to hijack its events)
423 if ( m_targetWindow == m_win )
13ff9344 424 {
248d771c
VZ
425 // if we already have a handler, delete it first
426 DeleteEvtHandler();
427
13ff9344
JS
428 m_handler = new wxScrollHelperEvtHandler(this);
429 m_targetWindow->PushEventHandler(m_handler);
430 }
349efbaa
VZ
431}
432
af4088f1 433void wxScrollHelper::SetTargetWindow(wxWindow *target)
349efbaa
VZ
434{
435 wxCHECK_RET( target, wxT("target window must not be NULL") );
436
437 if ( target == m_targetWindow )
438 return;
439
af4088f1 440 DoSetTargetWindow(target);
ecab4dba
RR
441}
442
1e6feb95 443wxWindow *wxScrollHelper::GetTargetWindow() const
ecab4dba
RR
444{
445 return m_targetWindow;
446}
447
d80cd92a
VZ
448// ----------------------------------------------------------------------------
449// scrolling implementation itself
450// ----------------------------------------------------------------------------
451
1e6feb95 452void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event)
c801d85f 453{
139adb6a 454 int nScrollInc = CalcScrollInc(event);
1e6feb95 455 if ( nScrollInc == 0 )
139adb6a 456 {
1e6feb95
VZ
457 // can't scroll further
458 event.Skip();
459
460 return;
139adb6a 461 }
c801d85f 462
1e6feb95 463 int orient = event.GetOrientation();
139adb6a
RR
464 if (orient == wxHORIZONTAL)
465 {
466 m_xScrollPosition += nScrollInc;
5f3286d1 467 m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
139adb6a 468 }
c801d85f 469 else
139adb6a
RR
470 {
471 m_yScrollPosition += nScrollInc;
5f3286d1 472 m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
139adb6a 473 }
a58a12e9 474
ca65c044 475 bool needsRefresh = false;
1e6feb95
VZ
476 int dx = 0,
477 dy = 0;
139adb6a
RR
478 if (orient == wxHORIZONTAL)
479 {
1e6feb95
VZ
480 if ( m_xScrollingEnabled )
481 {
482 dx = -m_xScrollPixelsPerLine * nScrollInc;
483 }
139adb6a 484 else
1e6feb95 485 {
ca65c044 486 needsRefresh = true;
1e6feb95 487 }
139adb6a 488 }
c801d85f 489 else
139adb6a 490 {
1e6feb95
VZ
491 if ( m_yScrollingEnabled )
492 {
493 dy = -m_yScrollPixelsPerLine * nScrollInc;
494 }
139adb6a 495 else
1e6feb95 496 {
ca65c044 497 needsRefresh = true;
1e6feb95
VZ
498 }
499 }
500
501 if ( needsRefresh )
502 {
ca65c044 503 m_targetWindow->Refresh(true, GetScrollRect());
1e6feb95
VZ
504 }
505 else
506 {
d5d58a93 507 m_targetWindow->ScrollWindow(dx, dy, GetScrollRect());
3d2b9c20 508 }
c801d85f
KB
509}
510
1e6feb95 511int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 512{
ecab4dba
RR
513 int pos = event.GetPosition();
514 int orient = event.GetOrientation();
c801d85f 515
ecab4dba 516 int nScrollInc = 0;
1a8caf94 517 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
c801d85f 518 {
ecab4dba
RR
519 if (orient == wxHORIZONTAL)
520 nScrollInc = - m_xScrollPosition;
521 else
522 nScrollInc = - m_yScrollPosition;
1a8caf94
RR
523 } else
524 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
525 {
ecab4dba
RR
526 if (orient == wxHORIZONTAL)
527 nScrollInc = m_xScrollLines - m_xScrollPosition;
528 else
529 nScrollInc = m_yScrollLines - m_yScrollPosition;
1a8caf94
RR
530 } else
531 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
532 {
ecab4dba 533 nScrollInc = -1;
1a8caf94
RR
534 } else
535 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
536 {
ecab4dba 537 nScrollInc = 1;
1a8caf94
RR
538 } else
539 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
540 {
ecab4dba
RR
541 if (orient == wxHORIZONTAL)
542 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
543 else
544 nScrollInc = -GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
545 } else
546 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
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_THUMBTRACK) ||
554 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
555 {
ecab4dba
RR
556 if (orient == wxHORIZONTAL)
557 nScrollInc = pos - m_xScrollPosition;
558 else
559 nScrollInc = pos - m_yScrollPosition;
c801d85f 560 }
88150e60 561
ecab4dba
RR
562 if (orient == wxHORIZONTAL)
563 {
a58a12e9 564 if (m_xScrollPixelsPerLine > 0)
ecab4dba 565 {
878ddad5
RR
566 if ( m_xScrollPosition + nScrollInc < 0 )
567 {
568 // As -ve as we can go
569 nScrollInc = -m_xScrollPosition;
570 }
571 else // check for the other bound
572 {
573 const int posMax = m_xScrollLines - m_xScrollLinesPerPage;
574 if ( m_xScrollPosition + nScrollInc > posMax )
575 {
576 // As +ve as we can go
577 nScrollInc = posMax - m_xScrollPosition;
578 }
579 }
ecab4dba
RR
580 }
581 else
ca65c044 582 m_targetWindow->Refresh(true, GetScrollRect());
9d9355c6
VZ
583 }
584 else
ecab4dba 585 {
be9b0663 586 if ( m_yScrollPixelsPerLine > 0 )
d80cd92a 587 {
be9b0663
VZ
588 if ( m_yScrollPosition + nScrollInc < 0 )
589 {
590 // As -ve as we can go
591 nScrollInc = -m_yScrollPosition;
592 }
593 else // check for the other bound
594 {
595 const int posMax = m_yScrollLines - m_yScrollLinesPerPage;
596 if ( m_yScrollPosition + nScrollInc > posMax )
597 {
598 // As +ve as we can go
599 nScrollInc = posMax - m_yScrollPosition;
600 }
601 }
ecab4dba
RR
602 }
603 else
be9b0663
VZ
604 {
605 // VZ: why do we do this? (FIXME)
ca65c044 606 m_targetWindow->Refresh(true, GetScrollRect());
be9b0663 607 }
9d9355c6 608 }
9d9355c6 609
ecab4dba 610 return nScrollInc;
c801d85f
KB
611}
612
613// Adjust the scrollbars - new version.
1e6feb95 614void wxScrollHelper::AdjustScrollbars()
c801d85f 615{
2a201ef8
VZ
616 static wxRecursionGuardFlag s_flagReentrancy;
617 wxRecursionGuard guard(s_flagReentrancy);
618 if ( guard.IsInside() )
619 {
620 // don't reenter AdjustScrollbars() while another call to
621 // AdjustScrollbars() is in progress because this may lead to calling
622 // ScrollWindow() twice and this can really happen under MSW if
623 // SetScrollbar() call below adds or removes the scrollbar which
624 // changes the window size and hence results in another
625 // AdjustScrollbars() call
626 return;
627 }
628
566d84a7
RL
629 int w = 0, h = 0;
630 int oldw, oldh;
a58a12e9 631
27d029c7
RR
632 int oldXScroll = m_xScrollPosition;
633 int oldYScroll = m_yScrollPosition;
c801d85f 634
be9b0663
VZ
635 // VZ: at least under Windows this loop is useless because when scrollbars
636 // [dis]appear we get a WM_SIZE resulting in another call to
637 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
638 // it here for now but it would be better to ensure that all ports
639 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
640 // necessary, and remove it later
870c86bc
JS
641 // JACS: Stop potential infinite loop by limiting number of iterations
642 int iterationCount = 0;
643 const int iterationMax = 5;
be9b0663
VZ
644 do
645 {
870c86bc 646 iterationCount ++;
d32e78bd 647
566d84a7 648 GetTargetSize(&w, 0);
c801d85f 649
878ddad5
RR
650 // scroll lines per page: if 0, no scrolling is needed
651 int linesPerPage;
652
653 if ( m_xScrollPixelsPerLine == 0 )
566d84a7 654 {
878ddad5 655 // scrolling is disabled
566d84a7
RL
656 m_xScrollLines = 0;
657 m_xScrollPosition = 0;
878ddad5 658 linesPerPage = 0;
566d84a7 659 }
878ddad5 660 else // might need scrolling
566d84a7 661 {
878ddad5
RR
662 // Round up integer division to catch any "leftover" client space.
663 const int wVirt = m_targetWindow->GetVirtualSize().GetWidth();
664 m_xScrollLines = (wVirt + m_xScrollPixelsPerLine - 1) / m_xScrollPixelsPerLine;
566d84a7
RL
665
666 // Calculate page size i.e. number of scroll units you get on the
878ddad5
RR
667 // current client window.
668 linesPerPage = w / m_xScrollPixelsPerLine;
669
670 // Special case. When client and virtual size are very close but
671 // the client is big enough, kill scrollbar.
672 if ((linesPerPage < m_xScrollLines) && (w >= wVirt)) ++linesPerPage;
673
674 if (linesPerPage >= m_xScrollLines)
675 {
676 // we're big enough to not need scrolling
677 linesPerPage =
678 m_xScrollLines =
679 m_xScrollPosition = 0;
680 }
681 else // we do need a scrollbar
682 {
683 if ( linesPerPage < 1 )
684 linesPerPage = 1;
685
686 // Correct position if greater than extent of canvas minus
687 // the visible portion of it or if below zero
688 const int posMax = m_xScrollLines - linesPerPage;
689 if ( m_xScrollPosition > posMax )
690 m_xScrollPosition = posMax;
691 else if ( m_xScrollPosition < 0 )
692 m_xScrollPosition = 0;
693 }
566d84a7 694 }
c801d85f 695
878ddad5
RR
696 m_win->SetScrollbar(wxHORIZONTAL, m_xScrollPosition,
697 linesPerPage, m_xScrollLines);
a58a12e9 698
878ddad5
RR
699 // The amount by which we scroll when paging
700 SetScrollPageSize(wxHORIZONTAL, linesPerPage);
701
702 GetTargetSize(0, &h);
be9b0663
VZ
703
704 if ( m_yScrollPixelsPerLine == 0 )
566d84a7 705 {
be9b0663 706 // scrolling is disabled
566d84a7
RL
707 m_yScrollLines = 0;
708 m_yScrollPosition = 0;
be9b0663 709 linesPerPage = 0;
566d84a7 710 }
be9b0663 711 else // might need scrolling
566d84a7 712 {
878ddad5
RR
713 // Round up integer division to catch any "leftover" client space.
714 const int hVirt = m_targetWindow->GetVirtualSize().GetHeight();
715 m_yScrollLines = ( hVirt + m_yScrollPixelsPerLine - 1 ) / m_yScrollPixelsPerLine;
566d84a7
RL
716
717 // Calculate page size i.e. number of scroll units you get on the
878ddad5 718 // current client window.
be9b0663 719 linesPerPage = h / m_yScrollPixelsPerLine;
878ddad5
RR
720
721 // Special case. When client and virtual size are very close but
722 // the client is big enough, kill scrollbar.
723 if ((linesPerPage < m_yScrollLines) && (h >= hVirt)) ++linesPerPage;
724
725 if (linesPerPage >= m_yScrollLines)
be9b0663
VZ
726 {
727 // we're big enough to not need scrolling
728 linesPerPage =
729 m_yScrollLines =
730 m_yScrollPosition = 0;
731 }
732 else // we do need a scrollbar
733 {
734 if ( linesPerPage < 1 )
735 linesPerPage = 1;
736
737 // Correct position if greater than extent of canvas minus
738 // the visible portion of it or if below zero
739 const int posMax = m_yScrollLines - linesPerPage;
740 if ( m_yScrollPosition > posMax )
741 m_yScrollPosition = posMax;
742 else if ( m_yScrollPosition < 0 )
743 m_yScrollPosition = 0;
744 }
745 }
566d84a7 746
be9b0663
VZ
747 m_win->SetScrollbar(wxVERTICAL, m_yScrollPosition,
748 linesPerPage, m_yScrollLines);
566d84a7 749
be9b0663
VZ
750 // The amount by which we scroll when paging
751 SetScrollPageSize(wxVERTICAL, linesPerPage);
c801d85f 752
139adb6a 753
be9b0663 754 // If a scrollbar (dis)appeared as a result of this, adjust them again.
566d84a7
RL
755 oldw = w;
756 oldh = h;
757
758 GetTargetSize( &w, &h );
870c86bc 759 } while ( (w != oldw || h != oldh) && (iterationCount < iterationMax) );
566d84a7
RL
760
761#ifdef __WXMOTIF__
762 // Sorry, some Motif-specific code to implement a backing pixmap
763 // for the wxRETAINED style. Implementing a backing store can't
764 // be entirely generic because it relies on the wxWindowDC implementation
765 // to duplicate X drawing calls for the backing pixmap.
766
767 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
139adb6a 768 {
566d84a7
RL
769 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
770
771 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
772 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
773 if (m_targetWindow->GetBackingPixmap() &&
774 !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
775 (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
776 {
777 XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
778 m_targetWindow->SetBackingPixmap((WXPixmap) 0);
779 }
780
781 if (!m_targetWindow->GetBackingPixmap() &&
9b66c26a 782 (m_xScrollLines != 0) && (m_yScrollLines != 0))
566d84a7
RL
783 {
784 int depth = wxDisplayDepth();
785 m_targetWindow->SetPixmapWidth(totalPixelWidth);
786 m_targetWindow->SetPixmapHeight(totalPixelHeight);
787 m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
788 m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth));
789 }
790
139adb6a 791 }
566d84a7 792#endif // Motif
a58a12e9 793
27d029c7
RR
794 if (oldXScroll != m_xScrollPosition)
795 {
796 if (m_xScrollingEnabled)
566d84a7 797 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0,
d5d58a93 798 GetScrollRect() );
27d029c7 799 else
ca65c044 800 m_targetWindow->Refresh(true, GetScrollRect());
27d029c7 801 }
a58a12e9 802
27d029c7
RR
803 if (oldYScroll != m_yScrollPosition)
804 {
805 if (m_yScrollingEnabled)
1e6feb95 806 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
d5d58a93 807 GetScrollRect() );
27d029c7 808 else
ca65c044 809 m_targetWindow->Refresh(true, GetScrollRect());
27d029c7 810 }
c801d85f
KB
811}
812
1e6feb95 813void wxScrollHelper::DoPrepareDC(wxDC& dc)
c801d85f 814{
1e6feb95
VZ
815 wxPoint pt = dc.GetDeviceOrigin();
816 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
817 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
139adb6a 818 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
819}
820
566d84a7
RL
821void wxScrollHelper::SetScrollRate( int xstep, int ystep )
822{
823 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
824 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
825
826 m_xScrollPixelsPerLine = xstep;
827 m_yScrollPixelsPerLine = ystep;
828
829 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
830 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
831
832 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
833 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
834 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
835
836 AdjustScrollbars();
837}
838
1e6feb95 839void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
c801d85f 840{
a0bc2c1d
VZ
841 if ( x_unit )
842 *x_unit = m_xScrollPixelsPerLine;
843 if ( y_unit )
844 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
845}
846
1e6feb95 847int wxScrollHelper::GetScrollPageSize(int orient) const
c801d85f
KB
848{
849 if ( orient == wxHORIZONTAL )
850 return m_xScrollLinesPerPage;
851 else
852 return m_yScrollLinesPerPage;
853}
854
1e6feb95 855void wxScrollHelper::SetScrollPageSize(int orient, int pageSize)
c801d85f
KB
856{
857 if ( orient == wxHORIZONTAL )
858 m_xScrollLinesPerPage = pageSize;
859 else
860 m_yScrollLinesPerPage = pageSize;
861}
862
863/*
864 * Scroll to given position (scroll position, not pixel position)
865 */
1e6feb95 866void wxScrollHelper::Scroll( int x_pos, int y_pos )
c801d85f 867{
8b089c5e
JS
868 if (!m_targetWindow)
869 return;
870
a58a12e9 871 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
139adb6a 872 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
a58a12e9 873
8d7eaf91 874 int w = 0, h = 0;
1e6feb95 875 GetTargetSize(&w, &h);
c801d85f 876
8775b357 877 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
c801d85f 878 {
ed673c6a 879 int old_x = m_xScrollPosition;
139adb6a 880 m_xScrollPosition = x_pos;
a58a12e9 881
3d2b9c20
RR
882 // Calculate page size i.e. number of scroll units you get on the
883 // current client window
ecab4dba 884 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a
RR
885 if (noPagePositions < 1) noPagePositions = 1;
886
887 // Correct position if greater than extent of canvas minus
3d2b9c20 888 // the visible portion of it or if below zero
139adb6a
RR
889 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
890 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
a58a12e9 891
f6bcfd97 892 if (old_x != m_xScrollPosition) {
5f3286d1 893 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
1e6feb95 894 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0,
d5d58a93 895 GetScrollRect() );
f6bcfd97 896 }
c801d85f 897 }
8775b357 898 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
c801d85f 899 {
ed673c6a 900 int old_y = m_yScrollPosition;
139adb6a 901 m_yScrollPosition = y_pos;
a58a12e9 902
3d2b9c20
RR
903 // Calculate page size i.e. number of scroll units you get on the
904 // current client window
ecab4dba 905 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a
RR
906 if (noPagePositions < 1) noPagePositions = 1;
907
908 // Correct position if greater than extent of canvas minus
3d2b9c20 909 // the visible portion of it or if below zero
139adb6a
RR
910 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
911 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
d2c52078 912
f6bcfd97 913 if (old_y != m_yScrollPosition) {
5f3286d1 914 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
1e6feb95 915 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine,
d5d58a93 916 GetScrollRect() );
f6bcfd97 917 }
c801d85f 918 }
c801d85f
KB
919}
920
1e6feb95 921void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 922{
139adb6a
RR
923 m_xScrollingEnabled = x_scroll;
924 m_yScrollingEnabled = y_scroll;
c801d85f
KB
925}
926
c801d85f 927// Where the current view starts from
1e6feb95 928void wxScrollHelper::GetViewStart (int *x, int *y) const
c801d85f 929{
a0bc2c1d
VZ
930 if ( x )
931 *x = m_xScrollPosition;
932 if ( y )
933 *y = m_yScrollPosition;
c801d85f
KB
934}
935
8c2f3797 936void wxScrollHelper::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 937{
a0bc2c1d 938 if ( xx )
4f0eda0e
RR
939 {
940 if ((m_xScrollLines == 0) || (m_xScrollPixelsPerLine == 0))
941 {
942 // nothing to do
943 *xx = x;
944 }
945 else
946 {
947#ifdef __WXGTK__
948 if (m_win->GetLayoutDirection() == wxLayout_RightToLeft)
949 {
950 int w = 0, h = 0;
951 GetTargetSize(&w, &h);
952
953 // Calculate page size i.e. number of scroll units you get on the
954 // current client window
955 int noPagePositions = w/m_xScrollPixelsPerLine;
956 if (noPagePositions < 1) noPagePositions = 1;
957 *xx = x - ((m_xScrollLines - noPagePositions - m_xScrollPosition) * m_xScrollPixelsPerLine);
958 }
959 else
960#endif
961 {
962 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
963 }
964 }
965 }
a0bc2c1d
VZ
966 if ( yy )
967 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f
KB
968}
969
8c2f3797 970void wxScrollHelper::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 971{
a0bc2c1d 972 if ( xx )
4f0eda0e
RR
973 {
974 if ((m_xScrollLines == 0) || (m_xScrollPixelsPerLine == 0))
975 {
976 // nothing to do
977 *xx = x;
978 }
979 else
980 {
981#ifdef __WXGTK__
982 if (m_win->GetLayoutDirection() == wxLayout_RightToLeft)
983 {
984 int w = 0, h = 0;
985 GetTargetSize(&w, &h);
986
987 // Calculate page size i.e. number of scroll units you get on the
988 // current client window
989 int noPagePositions = w/m_xScrollPixelsPerLine;
990 if (noPagePositions < 1) noPagePositions = 1;
991 *xx = x + ((m_xScrollLines - noPagePositions - m_xScrollPosition) * m_xScrollPixelsPerLine);
992 }
993 else
994#endif
995 {
996 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
997 }
998 }
999 }
a0bc2c1d
VZ
1000 if ( yy )
1001 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f 1002}
d80cd92a 1003
d32e78bd
VZ
1004// ----------------------------------------------------------------------------
1005// geometry
1006// ----------------------------------------------------------------------------
1007
1008bool wxScrollHelper::ScrollLayout()
1009{
1010 if ( m_win->GetSizer() && m_targetWindow == m_win )
1011 {
1012 // If we're the scroll target, take into account the
1013 // virtual size and scrolled position of the window.
1014
8d7eaf91 1015 int x = 0, y = 0, w = 0, h = 0;
d32e78bd
VZ
1016 CalcScrolledPosition(0,0, &x,&y);
1017 m_win->GetVirtualSize(&w, &h);
1018 m_win->GetSizer()->SetDimension(x, y, w, h);
1019 return true;
1020 }
1021
1022 // fall back to default for LayoutConstraints
1023 return m_win->wxWindow::Layout();
1024}
1025
1026void wxScrollHelper::ScrollDoSetVirtualSize(int x, int y)
1027{
1028 m_win->wxWindow::DoSetVirtualSize( x, y );
1029 AdjustScrollbars();
1030
1031 if (m_win->GetAutoLayout())
1032 m_win->Layout();
1033}
1034
1035// wxWindow's GetBestVirtualSize returns the actual window size,
1036// whereas we want to return the virtual size
1037wxSize wxScrollHelper::ScrollGetBestVirtualSize() const
1038{
1039 wxSize clientSize(m_win->GetClientSize());
1040 if ( m_win->GetSizer() )
1041 clientSize.IncTo(m_win->GetSizer()->CalcMin());
1042
1043 return clientSize;
1044}
1045
1046// return the window best size from the given best virtual size
1047wxSize
1048wxScrollHelper::ScrollGetWindowSizeForVirtualSize(const wxSize& size) const
1049{
1050 // Only use the content to set the window size in the direction
1051 // where there's no scrolling; otherwise we're going to get a huge
1052 // window in the direction in which scrolling is enabled
1053 int ppuX, ppuY;
1054 GetScrollPixelsPerUnit(&ppuX, &ppuY);
1055
1056 wxSize minSize = m_win->GetMinSize();
1057 if ( !minSize.IsFullySpecified() )
1058 minSize = m_win->GetSize();
1059
1060 wxSize best(size);
1061 if (ppuX > 0)
1062 best.x = minSize.x;
1063 if (ppuY > 0)
1064 best.y = minSize.y;
1065
1066 return best;
1067}
1068
d80cd92a
VZ
1069// ----------------------------------------------------------------------------
1070// event handlers
1071// ----------------------------------------------------------------------------
1072
1073// Default OnSize resets scrollbars, if any
1e6feb95 1074void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
d80cd92a 1075{
c376d80f 1076 if ( m_targetWindow->GetAutoLayout() )
2b5f62a0 1077 {
c376d80f 1078 wxSize size = m_targetWindow->GetBestVirtualSize();
d32e78bd 1079
c376d80f 1080 // This will call ::Layout() and ::AdjustScrollbars()
168f82ce 1081 m_win->SetVirtualSize( size );
2b5f62a0
VZ
1082 }
1083 else
c376d80f 1084 {
2b5f62a0 1085 AdjustScrollbars();
c376d80f 1086 }
d80cd92a
VZ
1087}
1088
1089// This calls OnDraw, having adjusted the origin according to the current
1090// scroll position
1e6feb95 1091void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
d80cd92a 1092{
af4088f1
VZ
1093 // don't use m_targetWindow here, this is always called for ourselves
1094 wxPaintDC dc(m_win);
1e6feb95 1095 DoPrepareDC(dc);
d80cd92a
VZ
1096
1097 OnDraw(dc);
1098}
1099
438e3558
VZ
1100// kbd handling: notice that we use OnChar() and not OnKeyDown() for
1101// compatibility here - if we used OnKeyDown(), the programs which process
1102// arrows themselves in their OnChar() would never get the message and like
1103// this they always have the priority
1e6feb95 1104void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
d80cd92a 1105{
8d7eaf91
MW
1106 int stx = 0, sty = 0, // view origin
1107 szx = 0, szy = 0, // view size (total)
1108 clix = 0, cliy = 0; // view size (on screen)
d80cd92a 1109
1e6feb95
VZ
1110 GetViewStart(&stx, &sty);
1111 GetTargetSize(&clix, &cliy);
566d84a7 1112 m_targetWindow->GetVirtualSize(&szx, &szy);
56dade3c
RL
1113
1114 if( m_xScrollPixelsPerLine )
1115 {
1116 clix /= m_xScrollPixelsPerLine;
d7da9756 1117 szx /= m_xScrollPixelsPerLine;
56dade3c
RL
1118 }
1119 else
1120 {
1121 clix = 0;
d7da9756 1122 szx = -1;
56dade3c
RL
1123 }
1124 if( m_yScrollPixelsPerLine )
1125 {
1126 cliy /= m_yScrollPixelsPerLine;
1127 szy /= m_yScrollPixelsPerLine;
1128 }
1129 else
1130 {
1131 cliy = 0;
d7da9756 1132 szy = -1;
56dade3c 1133 }
d80cd92a 1134
39c869a6
VZ
1135 int xScrollOld = m_xScrollPosition,
1136 yScrollOld = m_yScrollPosition;
1137
ecb01792 1138 int dsty;
4995ca80 1139 switch ( event.GetKeyCode() )
d80cd92a
VZ
1140 {
1141 case WXK_PAGEUP:
ecb01792
RL
1142 dsty = sty - (5 * cliy / 6);
1143 Scroll(-1, (dsty == -1) ? 0 : dsty);
d80cd92a
VZ
1144 break;
1145
1146 case WXK_PAGEDOWN:
d80cd92a
VZ
1147 Scroll(-1, sty + (5 * cliy / 6));
1148 break;
1149
d80cd92a
VZ
1150 case WXK_HOME:
1151 Scroll(0, event.ControlDown() ? 0 : -1);
1152 break;
1153
1154 case WXK_END:
4acd108c 1155 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
d80cd92a
VZ
1156 break;
1157
1158 case WXK_UP:
1159 Scroll(-1, sty - 1);
1160 break;
1161
1162 case WXK_DOWN:
1163 Scroll(-1, sty + 1);
1164 break;
1165
1166 case WXK_LEFT:
1167 Scroll(stx - 1, -1);
1168 break;
1169
1170 case WXK_RIGHT:
1171 Scroll(stx + 1, -1);
1172 break;
1173
1174 default:
1175 // not for us
1176 event.Skip();
1177 }
39c869a6
VZ
1178
1179 if ( m_xScrollPosition != xScrollOld )
1180 {
1181 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
1182 wxHORIZONTAL);
1183 event.SetEventObject(m_win);
1184 m_win->GetEventHandler()->ProcessEvent(event);
1185 }
1186
1187 if ( m_yScrollPosition != yScrollOld )
1188 {
1189 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
1190 wxVERTICAL);
1191 event.SetEventObject(m_win);
1192 m_win->GetEventHandler()->ProcessEvent(event);
1193 }
d80cd92a 1194}
d2c52078 1195
1e6feb95
VZ
1196// ----------------------------------------------------------------------------
1197// autoscroll stuff: these functions deal with sending fake scroll events when
1198// a captured mouse is being held outside the window
1199// ----------------------------------------------------------------------------
1200
1201bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
1202{
1203 // only send the event if the window is scrollable in this direction
1204 wxWindow *win = (wxWindow *)event.GetEventObject();
1205 return win->HasScrollbar(event.GetOrientation());
1206}
1207
1208void wxScrollHelper::StopAutoScrolling()
1209{
4b316329 1210#if wxUSE_TIMER
1e6feb95
VZ
1211 if ( m_timerAutoScroll )
1212 {
1213 delete m_timerAutoScroll;
1214 m_timerAutoScroll = (wxTimer *)NULL;
1215 }
4b316329 1216#endif
1e6feb95 1217}
d2c52078 1218
1e6feb95 1219void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
d2c52078 1220{
1e6feb95
VZ
1221 StopAutoScrolling();
1222
1223 event.Skip();
1224}
d2c52078 1225
1e6feb95
VZ
1226void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
1227{
1228 // don't prevent the usual processing of the event from taking place
1229 event.Skip();
1230
1231 // when a captured mouse leave a scrolled window we start generate
1232 // scrolling events to allow, for example, extending selection beyond the
1233 // visible area in some controls
1234 if ( wxWindow::GetCapture() == m_targetWindow )
1235 {
1236 // where is the mouse leaving?
1237 int pos, orient;
1238 wxPoint pt = event.GetPosition();
1239 if ( pt.x < 0 )
1240 {
1241 orient = wxHORIZONTAL;
1242 pos = 0;
1243 }
1244 else if ( pt.y < 0 )
1245 {
1246 orient = wxVERTICAL;
1247 pos = 0;
1248 }
1249 else // we're lower or to the right of the window
1250 {
1251 wxSize size = m_targetWindow->GetClientSize();
1252 if ( pt.x > size.x )
1253 {
1254 orient = wxHORIZONTAL;
1255 pos = m_xScrollLines;
1256 }
1257 else if ( pt.y > size.y )
1258 {
1259 orient = wxVERTICAL;
1260 pos = m_yScrollLines;
1261 }
1262 else // this should be impossible
1263 {
1264 // but seems to happen sometimes under wxMSW - maybe it's a bug
1265 // there but for now just ignore it
1266
1267 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1268
1269 return;
1270 }
1271 }
1272
1273 // only start the auto scroll timer if the window can be scrolled in
1274 // this direction
1275 if ( !m_targetWindow->HasScrollbar(orient) )
1276 return;
1277
4b316329 1278#if wxUSE_TIMER
1e6feb95
VZ
1279 delete m_timerAutoScroll;
1280 m_timerAutoScroll = new wxAutoScrollTimer
1281 (
1282 m_targetWindow, this,
1283 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1284 : wxEVT_SCROLLWIN_LINEDOWN,
1285 pos,
1286 orient
1287 );
1288 m_timerAutoScroll->Start(50); // FIXME: make configurable
534b127c
WS
1289#else
1290 wxUnusedVar(pos);
4b316329 1291#endif
1e6feb95
VZ
1292 }
1293}
1294
e421922f
VZ
1295#if wxUSE_MOUSEWHEEL
1296
1e6feb95
VZ
1297void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
1298{
d2c52078 1299 m_wheelRotation += event.GetWheelRotation();
1e6feb95 1300 int lines = m_wheelRotation / event.GetWheelDelta();
d2c52078
RD
1301 m_wheelRotation -= lines * event.GetWheelDelta();
1302
1e6feb95
VZ
1303 if (lines != 0)
1304 {
1e6feb95 1305
b6b85bdc
JS
1306 wxScrollWinEvent newEvent;
1307
ac6f6ffc 1308 newEvent.SetPosition(0);
b6b85bdc 1309 newEvent.SetOrientation(wxVERTICAL);
687706f5 1310 newEvent.SetEventObject(m_win);
b6b85bdc 1311
9b9337da 1312 if (event.IsPageScroll())
4b056ef5
RD
1313 {
1314 if (lines > 0)
687706f5 1315 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
4b056ef5 1316 else
687706f5 1317 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
4b056ef5 1318
b6b85bdc 1319 m_win->GetEventHandler()->ProcessEvent(newEvent);
4b056ef5
RD
1320 }
1321 else
1322 {
1323 lines *= event.GetLinesPerAction();
1324 if (lines > 0)
687706f5 1325 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
4b056ef5 1326 else
687706f5 1327 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
b6b85bdc 1328
4b056ef5
RD
1329 int times = abs(lines);
1330 for (; times > 0; times--)
1331 m_win->GetEventHandler()->ProcessEvent(newEvent);
1332 }
d2c52078
RD
1333 }
1334}
1335
e421922f
VZ
1336#endif // wxUSE_MOUSEWHEEL
1337
1e6feb95 1338// ----------------------------------------------------------------------------
d32e78bd 1339// wxScrolledWindow implementation
1e6feb95
VZ
1340// ----------------------------------------------------------------------------
1341
d32e78bd 1342IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
1e6feb95 1343
d32e78bd
VZ
1344BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
1345 EVT_PAINT(wxScrolledWindow::OnPaint)
349efbaa
VZ
1346END_EVENT_TABLE()
1347
d32e78bd 1348bool wxScrolledWindow::Create(wxWindow *parent,
1e6feb95
VZ
1349 wxWindowID id,
1350 const wxPoint& pos,
1351 const wxSize& size,
1352 long style,
1353 const wxString& name)
1354{
1355 m_targetWindow = this;
8adc196b
SC
1356#ifdef __WXMAC__
1357 MacSetClipChildren( true ) ;
1358#endif
1e6feb95 1359
04b6e2f0 1360 bool ok = wxPanel::Create(parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name);
1e6feb95 1361
1e6feb95
VZ
1362 return ok;
1363}
1364
d32e78bd 1365wxScrolledWindow::~wxScrolledWindow()
1e6feb95
VZ
1366{
1367}
d2c52078 1368
d32e78bd 1369void wxScrolledWindow::OnPaint(wxPaintEvent& event)
349efbaa
VZ
1370{
1371 // the user code didn't really draw the window if we got here, so set this
1372 // flag to try to call OnDraw() later
1373 m_handler->ResetDrawnFlag();
1374
1375 event.Skip();
1376}
1377
0cf5b099 1378#ifdef __WXMSW__
d32e78bd 1379WXLRESULT wxScrolledWindow::MSWWindowProc(WXUINT nMsg,
0cf5b099
VZ
1380 WXWPARAM wParam,
1381 WXLPARAM lParam)
1382{
c140b7e7 1383 WXLRESULT rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam);
0cf5b099 1384
4676948b 1385#ifndef __WXWINCE__
0cf5b099
VZ
1386 // we need to process arrows ourselves for scrolling
1387 if ( nMsg == WM_GETDLGCODE )
1388 {
1389 rc |= DLGC_WANTARROWS;
1390 }
4676948b 1391#endif
0cf5b099
VZ
1392
1393 return rc;
1394}
1395
1396#endif // __WXMSW__