]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrlwing.cpp
Fixes #10432 (wxDataObjectComposite::GetAllFormats() does not work)
[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
c801d85f
KB
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
23
c801d85f 24#ifdef __BORLANDC__
d80cd92a 25 #pragma hdrstop
c801d85f
KB
26#endif
27
de6185e2
WS
28#include "wx/scrolwin.h"
29
30#ifndef WX_PRECOMP
31 #include "wx/utils.h"
8e609c82 32 #include "wx/panel.h"
ed4b0fdc 33 #include "wx/dcclient.h"
7d616e99 34 #include "wx/timer.h"
ed2fbeb8 35 #include "wx/sizer.h"
7d616e99 36 #include "wx/settings.h"
4b316329 37#endif
ed4b0fdc 38
b9dac1ab
JS
39#ifdef __WXMAC__
40#include "wx/scrolbar.h"
41#endif
42
2a201ef8 43#include "wx/recguard.h"
c801d85f 44
48d1144b 45#ifdef __WXMSW__
1e6feb95 46 #include <windows.h> // for DLGC_WANTARROWS
7acf6a92 47 #include "wx/msw/winundef.h"
48d1144b
JS
48#endif
49
a91b47e8
JS
50#ifdef __WXMOTIF__
51// For wxRETAINED implementation
338dd992
JJ
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
a91b47e8 56#include <Xm/Xm.h>
338dd992
JJ
57#ifdef __VMS__
58# pragma message enable nosimpint
59#endif
a91b47e8
JS
60#endif
61
066f1b7a 62/*
ca65c044
WS
63 TODO PROPERTIES
64 style wxHSCROLL | wxVSCROLL
066f1b7a
SC
65*/
66
d80cd92a 67// ----------------------------------------------------------------------------
1e6feb95
VZ
68// wxScrollHelperEvtHandler: intercept the events from the window and forward
69// them to wxScrollHelper
d80cd92a
VZ
70// ----------------------------------------------------------------------------
71
349efbaa 72class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler
1e6feb95
VZ
73{
74public:
29e1398f 75 wxScrollHelperEvtHandler(wxScrollHelperBase *scrollHelper)
1e6feb95
VZ
76 {
77 m_scrollHelper = scrollHelper;
78 }
d80cd92a 79
1e6feb95
VZ
80 virtual bool ProcessEvent(wxEvent& event);
81
ca65c044 82 void ResetDrawnFlag() { m_hasDrawnWindow = false; }
349efbaa 83
1e6feb95 84private:
29e1398f 85 wxScrollHelperBase *m_scrollHelper;
349efbaa
VZ
86
87 bool m_hasDrawnWindow;
22f3361e
VZ
88
89 DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler)
1e6feb95
VZ
90};
91
4b316329 92#if wxUSE_TIMER
1e6feb95
VZ
93// ----------------------------------------------------------------------------
94// wxAutoScrollTimer: the timer used to generate a stream of scroll events when
95// a captured mouse is held outside the window
96// ----------------------------------------------------------------------------
97
98class wxAutoScrollTimer : public wxTimer
99{
100public:
29e1398f
VZ
101 wxAutoScrollTimer(wxWindow *winToScroll,
102 wxScrollHelperBase *scroll,
1e6feb95
VZ
103 wxEventType eventTypeToSend,
104 int pos, int orient);
105
106 virtual void Notify();
107
108private:
109 wxWindow *m_win;
29e1398f 110 wxScrollHelperBase *m_scrollHelper;
1e6feb95
VZ
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 126wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll,
29e1398f 127 wxScrollHelperBase *scroll,
1e6feb95
VZ
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
e421922f
VZ
234 if ( evType == wxEVT_PAINT )
235 {
236 m_scrollHelper->HandleOnPaint((wxPaintEvent &)event);
ca65c044 237 return true;
e421922f 238 }
1e6feb95 239
06b9c2a2
VS
240 if ( evType == wxEVT_CHILD_FOCUS )
241 {
242 m_scrollHelper->HandleOnChildFocus((wxChildFocusEvent &)event);
243 return true;
244 }
245
35c71386
VZ
246 // reset the skipped flag (which might have been set to true in
247 // ProcessEvent() above) to be able to test it below
248 bool wasSkipped = event.GetSkipped();
249 if ( wasSkipped )
250 event.Skip(false);
251
e421922f
VZ
252 if ( evType == wxEVT_SCROLLWIN_TOP ||
253 evType == wxEVT_SCROLLWIN_BOTTOM ||
254 evType == wxEVT_SCROLLWIN_LINEUP ||
255 evType == wxEVT_SCROLLWIN_LINEDOWN ||
256 evType == wxEVT_SCROLLWIN_PAGEUP ||
257 evType == wxEVT_SCROLLWIN_PAGEDOWN ||
258 evType == wxEVT_SCROLLWIN_THUMBTRACK ||
259 evType == wxEVT_SCROLLWIN_THUMBRELEASE )
260 {
35c71386
VZ
261 m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event);
262 if ( !event.GetSkipped() )
263 {
264 // it makes sense to indicate that we processed the message as we
265 // did scroll the window (and also notice that wxAutoScrollTimer
266 // relies on our return value to stop scrolling when we are at top
267 // or bottom already)
268 processed = true;
269 wasSkipped = false;
270 }
e421922f 271 }
1e6feb95 272
e421922f
VZ
273 if ( evType == wxEVT_ENTER_WINDOW )
274 {
275 m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event);
276 }
277 else if ( evType == wxEVT_LEAVE_WINDOW )
278 {
279 m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event);
280 }
281#if wxUSE_MOUSEWHEEL
602e3365
RR
282 // Use GTK's own scroll wheel handling in GtkScrolledWindow
283#ifndef __WXGTK20__
e421922f
VZ
284 else if ( evType == wxEVT_MOUSEWHEEL )
285 {
286 m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event);
602e3365 287 return true;
e421922f 288 }
602e3365 289#endif
e421922f 290#endif // wxUSE_MOUSEWHEEL
e421922f
VZ
291 else if ( evType == wxEVT_CHAR )
292 {
293 m_scrollHelper->HandleOnChar((wxKeyEvent &)event);
35c71386
VZ
294 if ( !event.GetSkipped() )
295 {
296 processed = true;
297 wasSkipped = false;
298 }
1e6feb95
VZ
299 }
300
35c71386
VZ
301 if ( processed )
302 event.Skip(wasSkipped);
303
304 return processed;
1e6feb95
VZ
305}
306
29e1398f
VZ
307// ============================================================================
308// wxScrollHelperBase implementation
309// ============================================================================
310
1e6feb95 311// ----------------------------------------------------------------------------
29e1398f 312// wxScrollHelperBase construction
1e6feb95
VZ
313// ----------------------------------------------------------------------------
314
29e1398f 315wxScrollHelperBase::wxScrollHelperBase(wxWindow *win)
1e6feb95 316{
d32e78bd
VZ
317 wxASSERT_MSG( win, _T("associated window can't be NULL in wxScrollHelper") );
318
1e6feb95
VZ
319 m_xScrollPixelsPerLine =
320 m_yScrollPixelsPerLine =
321 m_xScrollPosition =
322 m_yScrollPosition =
323 m_xScrollLines =
324 m_yScrollLines =
325 m_xScrollLinesPerPage =
139adb6a 326 m_yScrollLinesPerPage = 0;
1e6feb95
VZ
327
328 m_xScrollingEnabled =
ca65c044 329 m_yScrollingEnabled = true;
1e6feb95
VZ
330
331 m_scaleX =
139adb6a 332 m_scaleY = 1.0;
24ce4c18 333#if wxUSE_MOUSEWHEEL
d2c52078 334 m_wheelRotation = 0;
24ce4c18 335#endif
a58a12e9 336
1e6feb95 337 m_win =
29e1398f 338 m_targetWindow = NULL;
139adb6a 339
29e1398f 340 m_timerAutoScroll = NULL;
d7da9756 341
349efbaa
VZ
342 m_handler = NULL;
343
d32e78bd 344 m_win = win;
29e1398f
VZ
345
346 m_win->SetScrollHelper(static_cast<wxScrollHelper *>(this));
d32e78bd
VZ
347
348 // by default, the associated window is also the target window
349 DoSetTargetWindow(win);
1e6feb95 350}
d7da9756 351
29e1398f 352wxScrollHelperBase::~wxScrollHelperBase()
d80cd92a 353{
1e6feb95
VZ
354 StopAutoScrolling();
355
349efbaa 356 DeleteEvtHandler();
d80cd92a
VZ
357}
358
359// ----------------------------------------------------------------------------
360// setting scrolling parameters
361// ----------------------------------------------------------------------------
362
29e1398f
VZ
363void wxScrollHelperBase::SetScrollbars(int pixelsPerUnitX,
364 int pixelsPerUnitY,
365 int noUnitsX,
366 int noUnitsY,
367 int xPos,
368 int yPos,
369 bool noRefresh)
c801d85f 370{
b0486e0d
SN
371 int xpos, ypos;
372
373 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
139adb6a
RR
374 bool do_refresh =
375 (
c801d85f 376 (noUnitsX != 0 && m_xScrollLines == 0) ||
566d84a7 377 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX * noUnitsX) ||
b0486e0d 378
c801d85f 379 (noUnitsY != 0 && m_yScrollLines == 0) ||
566d84a7 380 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY * noUnitsY) ||
c801d85f 381 (xPos != m_xScrollPosition) ||
b0486e0d 382 (yPos != m_yScrollPosition)
139adb6a 383 );
a58a12e9 384
139adb6a
RR
385 m_xScrollPixelsPerLine = pixelsPerUnitX;
386 m_yScrollPixelsPerLine = pixelsPerUnitY;
387 m_xScrollPosition = xPos;
388 m_yScrollPosition = yPos;
a91b47e8 389
150c8d89
RL
390 int w = noUnitsX * pixelsPerUnitX;
391 int h = noUnitsY * pixelsPerUnitY;
392
2b5f62a0
VZ
393 // For better backward compatibility we set persisting limits
394 // here not just the size. It makes SetScrollbars 'sticky'
395 // emulating the old non-autoscroll behaviour.
2b43d588 396 // m_targetWindow->SetVirtualSizeHints( w, h );
a58a12e9 397
2b5f62a0
VZ
398 // The above should arguably be deprecated, this however we still need.
399
f18f464c
VZ
400 // take care not to set 0 virtual size, 0 means that we don't have any
401 // scrollbars and hence we should use the real size instead of the virtual
402 // one which is indicated by using wxDefaultCoord
403 m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord,
404 h ? h : wxDefaultCoord);
dc429f89 405
a58a12e9 406 if (do_refresh && !noRefresh)
ca65c044 407 m_targetWindow->Refresh(true, GetScrollRect());
a58a12e9 408
1f066698
VZ
409#ifndef __WXUNIVERSAL__
410 // If the target is not the same as the window with the scrollbars,
411 // then we need to update the scrollbars here, since they won't have
412 // been updated by SetVirtualSize().
413 if ( m_targetWindow != m_win )
414#endif // !__WXUNIVERSAL__
415 {
416 AdjustScrollbars();
417 }
418#ifndef __WXUNIVERSAL__
419 else
420 {
421 // otherwise this has been done by AdjustScrollbars, above
1f066698
VZ
422 }
423#endif // !__WXUNIVERSAL__
c801d85f
KB
424}
425
d80cd92a 426// ----------------------------------------------------------------------------
af4088f1 427// [target] window handling
d80cd92a 428// ----------------------------------------------------------------------------
ecab4dba 429
29e1398f 430void wxScrollHelperBase::DeleteEvtHandler()
ecab4dba 431{
248d771c
VZ
432 // search for m_handler in the handler list
433 if ( m_win && m_handler )
349efbaa 434 {
74f6bbf9 435 if ( m_win->RemoveEventHandler(m_handler) )
248d771c 436 {
74f6bbf9 437 delete m_handler;
248d771c 438 }
74f6bbf9
VZ
439 //else: something is very wrong, so better [maybe] leak memory than
440 // risk a crash because of double deletion
248d771c 441
74f6bbf9 442 m_handler = NULL;
349efbaa
VZ
443 }
444}
445
29e1398f 446void wxScrollHelperBase::ResetDrawnFlag()
16361ec9
VS
447{
448 wxCHECK_RET( m_handler, "invalid use of ResetDrawnFlag - no handler?" );
449 m_handler->ResetDrawnFlag();
450}
451
29e1398f 452void wxScrollHelperBase::DoSetTargetWindow(wxWindow *target)
349efbaa 453{
ecab4dba 454 m_targetWindow = target;
8adc196b
SC
455#ifdef __WXMAC__
456 target->MacSetClipChildren( true ) ;
457#endif
349efbaa
VZ
458
459 // install the event handler which will intercept the events we're
af4088f1
VZ
460 // interested in (but only do it for our real window, not the target window
461 // which we scroll - we don't need to hijack its events)
462 if ( m_targetWindow == m_win )
13ff9344 463 {
248d771c
VZ
464 // if we already have a handler, delete it first
465 DeleteEvtHandler();
466
13ff9344
JS
467 m_handler = new wxScrollHelperEvtHandler(this);
468 m_targetWindow->PushEventHandler(m_handler);
469 }
349efbaa
VZ
470}
471
29e1398f 472void wxScrollHelperBase::SetTargetWindow(wxWindow *target)
349efbaa
VZ
473{
474 wxCHECK_RET( target, wxT("target window must not be NULL") );
475
476 if ( target == m_targetWindow )
477 return;
478
af4088f1 479 DoSetTargetWindow(target);
ecab4dba
RR
480}
481
29e1398f 482wxWindow *wxScrollHelperBase::GetTargetWindow() const
ecab4dba
RR
483{
484 return m_targetWindow;
485}
486
d80cd92a
VZ
487// ----------------------------------------------------------------------------
488// scrolling implementation itself
489// ----------------------------------------------------------------------------
490
29e1398f 491void wxScrollHelperBase::HandleOnScroll(wxScrollWinEvent& event)
c801d85f 492{
139adb6a 493 int nScrollInc = CalcScrollInc(event);
1e6feb95 494 if ( nScrollInc == 0 )
139adb6a 495 {
1e6feb95
VZ
496 // can't scroll further
497 event.Skip();
498
499 return;
139adb6a 500 }
c801d85f 501
ca65c044 502 bool needsRefresh = false;
1e6feb95
VZ
503 int dx = 0,
504 dy = 0;
b97dc9c8 505 int orient = event.GetOrientation();
139adb6a
RR
506 if (orient == wxHORIZONTAL)
507 {
1e6feb95
VZ
508 if ( m_xScrollingEnabled )
509 {
510 dx = -m_xScrollPixelsPerLine * nScrollInc;
511 }
139adb6a 512 else
1e6feb95 513 {
ca65c044 514 needsRefresh = true;
1e6feb95 515 }
139adb6a 516 }
c801d85f 517 else
139adb6a 518 {
1e6feb95
VZ
519 if ( m_yScrollingEnabled )
520 {
521 dy = -m_yScrollPixelsPerLine * nScrollInc;
522 }
139adb6a 523 else
1e6feb95 524 {
ca65c044 525 needsRefresh = true;
1e6feb95
VZ
526 }
527 }
528
b97dc9c8
VS
529 if ( !needsRefresh )
530 {
531 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
532 // otherwise invalidated area could be updated incorrectly later when
533 // ScrollWindow() makes sure they're repainted before scrolling them
93727870
SC
534#ifdef __WXMAC__
535 // wxWindowMac is taking care of making sure the update area is correctly
536 // set up, while not forcing an immediate redraw
537#else
b97dc9c8 538 m_targetWindow->Update();
93727870 539#endif
b97dc9c8
VS
540 }
541
542 if (orient == wxHORIZONTAL)
543 {
544 m_xScrollPosition += nScrollInc;
545 m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
546 }
547 else
548 {
549 m_yScrollPosition += nScrollInc;
550 m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
551 }
552
1e6feb95
VZ
553 if ( needsRefresh )
554 {
ca65c044 555 m_targetWindow->Refresh(true, GetScrollRect());
1e6feb95
VZ
556 }
557 else
558 {
d5d58a93 559 m_targetWindow->ScrollWindow(dx, dy, GetScrollRect());
3d2b9c20 560 }
c801d85f
KB
561}
562
29e1398f 563int wxScrollHelperBase::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 564{
ecab4dba
RR
565 int pos = event.GetPosition();
566 int orient = event.GetOrientation();
c801d85f 567
ecab4dba 568 int nScrollInc = 0;
1a8caf94 569 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
c801d85f 570 {
ecab4dba
RR
571 if (orient == wxHORIZONTAL)
572 nScrollInc = - m_xScrollPosition;
573 else
574 nScrollInc = - m_yScrollPosition;
1a8caf94
RR
575 } else
576 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
577 {
ecab4dba
RR
578 if (orient == wxHORIZONTAL)
579 nScrollInc = m_xScrollLines - m_xScrollPosition;
580 else
581 nScrollInc = m_yScrollLines - m_yScrollPosition;
1a8caf94
RR
582 } else
583 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
584 {
ecab4dba 585 nScrollInc = -1;
1a8caf94
RR
586 } else
587 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
588 {
ecab4dba 589 nScrollInc = 1;
1a8caf94
RR
590 } else
591 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
592 {
ecab4dba
RR
593 if (orient == wxHORIZONTAL)
594 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
595 else
596 nScrollInc = -GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
597 } else
598 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
599 {
ecab4dba
RR
600 if (orient == wxHORIZONTAL)
601 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
602 else
603 nScrollInc = GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
604 } else
605 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
606 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
607 {
ecab4dba
RR
608 if (orient == wxHORIZONTAL)
609 nScrollInc = pos - m_xScrollPosition;
610 else
611 nScrollInc = pos - m_yScrollPosition;
c801d85f 612 }
88150e60 613
ecab4dba
RR
614 if (orient == wxHORIZONTAL)
615 {
2e9c0c01 616 if ( m_xScrollPosition + nScrollInc < 0 )
ecab4dba 617 {
2e9c0c01
VZ
618 // As -ve as we can go
619 nScrollInc = -m_xScrollPosition;
620 }
621 else // check for the other bound
622 {
623 const int posMax = m_xScrollLines - m_xScrollLinesPerPage;
624 if ( m_xScrollPosition + nScrollInc > posMax )
878ddad5 625 {
2e9c0c01
VZ
626 // As +ve as we can go
627 nScrollInc = posMax - m_xScrollPosition;
878ddad5 628 }
ecab4dba 629 }
9d9355c6 630 }
2e9c0c01 631 else // wxVERTICAL
ecab4dba 632 {
2e9c0c01 633 if ( m_yScrollPosition + nScrollInc < 0 )
d80cd92a 634 {
2e9c0c01
VZ
635 // As -ve as we can go
636 nScrollInc = -m_yScrollPosition;
ecab4dba 637 }
2e9c0c01 638 else // check for the other bound
be9b0663 639 {
2e9c0c01
VZ
640 const int posMax = m_yScrollLines - m_yScrollLinesPerPage;
641 if ( m_yScrollPosition + nScrollInc > posMax )
642 {
643 // As +ve as we can go
644 nScrollInc = posMax - m_yScrollPosition;
645 }
be9b0663 646 }
9d9355c6 647 }
9d9355c6 648
ecab4dba 649 return nScrollInc;
c801d85f
KB
650}
651
29e1398f 652void wxScrollHelperBase::DoPrepareDC(wxDC& dc)
c801d85f 653{
1e6feb95 654 wxPoint pt = dc.GetDeviceOrigin();
807a572e
RR
655#ifdef __WXGTK__
656 // It may actually be correct to always query
69367c56 657 // the m_sign from the DC here, but I leave the
807a572e
RR
658 // #ifdef GTK for now.
659 if (m_win->GetLayoutDirection() == wxLayout_RightToLeft)
660 dc.SetDeviceOrigin( pt.x + m_xScrollPosition * m_xScrollPixelsPerLine,
661 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
662 else
663#endif
664 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
665 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
139adb6a 666 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
667}
668
29e1398f 669void wxScrollHelperBase::SetScrollRate( int xstep, int ystep )
566d84a7
RL
670{
671 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
672 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
673
674 m_xScrollPixelsPerLine = xstep;
675 m_yScrollPixelsPerLine = ystep;
676
677 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
678 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
679
680 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
681 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
682 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
683
684 AdjustScrollbars();
685}
686
29e1398f 687void wxScrollHelperBase::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
c801d85f 688{
a0bc2c1d
VZ
689 if ( x_unit )
690 *x_unit = m_xScrollPixelsPerLine;
691 if ( y_unit )
692 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
693}
694
5713b349 695
29e1398f 696int wxScrollHelperBase::GetScrollLines( int orient ) const
5713b349
RR
697{
698 if ( orient == wxHORIZONTAL )
699 return m_xScrollLines;
700 else
701 return m_yScrollLines;
702}
703
29e1398f 704int wxScrollHelperBase::GetScrollPageSize(int orient) const
c801d85f
KB
705{
706 if ( orient == wxHORIZONTAL )
707 return m_xScrollLinesPerPage;
708 else
709 return m_yScrollLinesPerPage;
710}
711
29e1398f 712void wxScrollHelperBase::SetScrollPageSize(int orient, int pageSize)
c801d85f
KB
713{
714 if ( orient == wxHORIZONTAL )
715 m_xScrollLinesPerPage = pageSize;
716 else
717 m_yScrollLinesPerPage = pageSize;
718}
719
29e1398f 720void wxScrollHelperBase::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 721{
29e1398f
VZ
722 m_xScrollingEnabled = x_scroll;
723 m_yScrollingEnabled = y_scroll;
724}
a58a12e9 725
29e1398f
VZ
726// Where the current view starts from
727void wxScrollHelperBase::DoGetViewStart (int *x, int *y) const
728{
729 if ( x )
730 *x = m_xScrollPosition;
731 if ( y )
732 *y = m_yScrollPosition;
733}
c801d85f 734
29e1398f
VZ
735void wxScrollHelperBase::DoCalcScrolledPosition(int x, int y,
736 int *xx, int *yy) const
737{
738 if ( xx )
739 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
740 if ( yy )
741 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
742}
8ea45699 743
29e1398f
VZ
744void wxScrollHelperBase::DoCalcUnscrolledPosition(int x, int y,
745 int *xx, int *yy) const
746{
747 if ( xx )
748 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
749 if ( yy )
750 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
751}
a58a12e9 752
29e1398f
VZ
753// ----------------------------------------------------------------------------
754// geometry
755// ----------------------------------------------------------------------------
139adb6a 756
29e1398f
VZ
757bool wxScrollHelperBase::ScrollLayout()
758{
759 if ( m_win->GetSizer() && m_targetWindow == m_win )
c801d85f 760 {
29e1398f
VZ
761 // If we're the scroll target, take into account the
762 // virtual size and scrolled position of the window.
139adb6a 763
29e1398f
VZ
764 int x = 0, y = 0, w = 0, h = 0;
765 CalcScrolledPosition(0,0, &x,&y);
766 m_win->GetVirtualSize(&w, &h);
767 m_win->GetSizer()->SetDimension(x, y, w, h);
768 return true;
b97dc9c8 769 }
d2c52078 770
29e1398f
VZ
771 // fall back to default for LayoutConstraints
772 return m_win->wxWindow::Layout();
773}
b97dc9c8 774
29e1398f
VZ
775void wxScrollHelperBase::ScrollDoSetVirtualSize(int x, int y)
776{
777 m_win->wxWindow::DoSetVirtualSize( x, y );
778 AdjustScrollbars();
d32e78bd
VZ
779
780 if (m_win->GetAutoLayout())
781 m_win->Layout();
782}
783
784// wxWindow's GetBestVirtualSize returns the actual window size,
785// whereas we want to return the virtual size
29e1398f 786wxSize wxScrollHelperBase::ScrollGetBestVirtualSize() const
d32e78bd
VZ
787{
788 wxSize clientSize(m_win->GetClientSize());
789 if ( m_win->GetSizer() )
790 clientSize.IncTo(m_win->GetSizer()->CalcMin());
791
792 return clientSize;
793}
794
d80cd92a
VZ
795// ----------------------------------------------------------------------------
796// event handlers
797// ----------------------------------------------------------------------------
798
799// Default OnSize resets scrollbars, if any
29e1398f 800void wxScrollHelperBase::HandleOnSize(wxSizeEvent& WXUNUSED(event))
d80cd92a 801{
c376d80f 802 if ( m_targetWindow->GetAutoLayout() )
2b5f62a0 803 {
c376d80f 804 wxSize size = m_targetWindow->GetBestVirtualSize();
d32e78bd 805
c376d80f 806 // This will call ::Layout() and ::AdjustScrollbars()
168f82ce 807 m_win->SetVirtualSize( size );
2b5f62a0
VZ
808 }
809 else
c376d80f 810 {
2b5f62a0 811 AdjustScrollbars();
c376d80f 812 }
d80cd92a
VZ
813}
814
815// This calls OnDraw, having adjusted the origin according to the current
816// scroll position
29e1398f 817void wxScrollHelperBase::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
d80cd92a 818{
af4088f1
VZ
819 // don't use m_targetWindow here, this is always called for ourselves
820 wxPaintDC dc(m_win);
1e6feb95 821 DoPrepareDC(dc);
d80cd92a
VZ
822
823 OnDraw(dc);
824}
825
438e3558
VZ
826// kbd handling: notice that we use OnChar() and not OnKeyDown() for
827// compatibility here - if we used OnKeyDown(), the programs which process
828// arrows themselves in their OnChar() would never get the message and like
829// this they always have the priority
29e1398f 830void wxScrollHelperBase::HandleOnChar(wxKeyEvent& event)
d80cd92a 831{
8d7eaf91
MW
832 int stx = 0, sty = 0, // view origin
833 szx = 0, szy = 0, // view size (total)
834 clix = 0, cliy = 0; // view size (on screen)
d80cd92a 835
1e6feb95
VZ
836 GetViewStart(&stx, &sty);
837 GetTargetSize(&clix, &cliy);
566d84a7 838 m_targetWindow->GetVirtualSize(&szx, &szy);
56dade3c
RL
839
840 if( m_xScrollPixelsPerLine )
841 {
842 clix /= m_xScrollPixelsPerLine;
d7da9756 843 szx /= m_xScrollPixelsPerLine;
56dade3c
RL
844 }
845 else
846 {
847 clix = 0;
d7da9756 848 szx = -1;
56dade3c
RL
849 }
850 if( m_yScrollPixelsPerLine )
851 {
852 cliy /= m_yScrollPixelsPerLine;
853 szy /= m_yScrollPixelsPerLine;
854 }
855 else
856 {
857 cliy = 0;
d7da9756 858 szy = -1;
56dade3c 859 }
d80cd92a 860
39c869a6
VZ
861 int xScrollOld = m_xScrollPosition,
862 yScrollOld = m_yScrollPosition;
863
ecb01792 864 int dsty;
4995ca80 865 switch ( event.GetKeyCode() )
d80cd92a
VZ
866 {
867 case WXK_PAGEUP:
ecb01792
RL
868 dsty = sty - (5 * cliy / 6);
869 Scroll(-1, (dsty == -1) ? 0 : dsty);
d80cd92a
VZ
870 break;
871
872 case WXK_PAGEDOWN:
d80cd92a
VZ
873 Scroll(-1, sty + (5 * cliy / 6));
874 break;
875
d80cd92a
VZ
876 case WXK_HOME:
877 Scroll(0, event.ControlDown() ? 0 : -1);
878 break;
879
880 case WXK_END:
4acd108c 881 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
d80cd92a
VZ
882 break;
883
884 case WXK_UP:
885 Scroll(-1, sty - 1);
886 break;
887
888 case WXK_DOWN:
889 Scroll(-1, sty + 1);
890 break;
891
892 case WXK_LEFT:
893 Scroll(stx - 1, -1);
894 break;
895
896 case WXK_RIGHT:
897 Scroll(stx + 1, -1);
898 break;
899
900 default:
901 // not for us
902 event.Skip();
903 }
39c869a6
VZ
904
905 if ( m_xScrollPosition != xScrollOld )
906 {
74ab5f5b 907 wxScrollWinEvent evt(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
39c869a6 908 wxHORIZONTAL);
74ab5f5b
VZ
909 evt.SetEventObject(m_win);
910 m_win->GetEventHandler()->ProcessEvent(evt);
39c869a6
VZ
911 }
912
913 if ( m_yScrollPosition != yScrollOld )
914 {
74ab5f5b 915 wxScrollWinEvent evt(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
39c869a6 916 wxVERTICAL);
74ab5f5b
VZ
917 evt.SetEventObject(m_win);
918 m_win->GetEventHandler()->ProcessEvent(evt);
39c869a6 919 }
d80cd92a 920}
d2c52078 921
1e6feb95
VZ
922// ----------------------------------------------------------------------------
923// autoscroll stuff: these functions deal with sending fake scroll events when
924// a captured mouse is being held outside the window
925// ----------------------------------------------------------------------------
926
29e1398f 927bool wxScrollHelperBase::SendAutoScrollEvents(wxScrollWinEvent& event) const
1e6feb95
VZ
928{
929 // only send the event if the window is scrollable in this direction
930 wxWindow *win = (wxWindow *)event.GetEventObject();
931 return win->HasScrollbar(event.GetOrientation());
932}
933
29e1398f 934void wxScrollHelperBase::StopAutoScrolling()
1e6feb95 935{
4b316329 936#if wxUSE_TIMER
1e6feb95
VZ
937 if ( m_timerAutoScroll )
938 {
939 delete m_timerAutoScroll;
d3b9f782 940 m_timerAutoScroll = NULL;
1e6feb95 941 }
4b316329 942#endif
1e6feb95 943}
d2c52078 944
29e1398f 945void wxScrollHelperBase::HandleOnMouseEnter(wxMouseEvent& event)
d2c52078 946{
1e6feb95
VZ
947 StopAutoScrolling();
948
949 event.Skip();
950}
d2c52078 951
29e1398f 952void wxScrollHelperBase::HandleOnMouseLeave(wxMouseEvent& event)
1e6feb95
VZ
953{
954 // don't prevent the usual processing of the event from taking place
955 event.Skip();
956
957 // when a captured mouse leave a scrolled window we start generate
958 // scrolling events to allow, for example, extending selection beyond the
959 // visible area in some controls
960 if ( wxWindow::GetCapture() == m_targetWindow )
961 {
962 // where is the mouse leaving?
963 int pos, orient;
964 wxPoint pt = event.GetPosition();
965 if ( pt.x < 0 )
966 {
967 orient = wxHORIZONTAL;
968 pos = 0;
969 }
970 else if ( pt.y < 0 )
971 {
972 orient = wxVERTICAL;
973 pos = 0;
974 }
975 else // we're lower or to the right of the window
976 {
977 wxSize size = m_targetWindow->GetClientSize();
978 if ( pt.x > size.x )
979 {
980 orient = wxHORIZONTAL;
981 pos = m_xScrollLines;
982 }
983 else if ( pt.y > size.y )
984 {
985 orient = wxVERTICAL;
986 pos = m_yScrollLines;
987 }
988 else // this should be impossible
989 {
990 // but seems to happen sometimes under wxMSW - maybe it's a bug
991 // there but for now just ignore it
992
993 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
994
995 return;
996 }
997 }
998
999 // only start the auto scroll timer if the window can be scrolled in
1000 // this direction
1001 if ( !m_targetWindow->HasScrollbar(orient) )
1002 return;
1003
4b316329 1004#if wxUSE_TIMER
1e6feb95
VZ
1005 delete m_timerAutoScroll;
1006 m_timerAutoScroll = new wxAutoScrollTimer
1007 (
1008 m_targetWindow, this,
1009 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1010 : wxEVT_SCROLLWIN_LINEDOWN,
1011 pos,
1012 orient
1013 );
1014 m_timerAutoScroll->Start(50); // FIXME: make configurable
534b127c
WS
1015#else
1016 wxUnusedVar(pos);
4b316329 1017#endif
1e6feb95
VZ
1018 }
1019}
1020
e421922f
VZ
1021#if wxUSE_MOUSEWHEEL
1022
29e1398f 1023void wxScrollHelperBase::HandleOnMouseWheel(wxMouseEvent& event)
1e6feb95 1024{
d2c52078 1025 m_wheelRotation += event.GetWheelRotation();
1e6feb95 1026 int lines = m_wheelRotation / event.GetWheelDelta();
d2c52078
RD
1027 m_wheelRotation -= lines * event.GetWheelDelta();
1028
1e6feb95
VZ
1029 if (lines != 0)
1030 {
1e6feb95 1031
b6b85bdc
JS
1032 wxScrollWinEvent newEvent;
1033
ac6f6ffc 1034 newEvent.SetPosition(0);
6682e732 1035 newEvent.SetOrientation( event.GetWheelAxis() == 0 ? wxVERTICAL : wxHORIZONTAL);
687706f5 1036 newEvent.SetEventObject(m_win);
b6b85bdc 1037
9b9337da 1038 if (event.IsPageScroll())
4b056ef5
RD
1039 {
1040 if (lines > 0)
687706f5 1041 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
4b056ef5 1042 else
687706f5 1043 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
4b056ef5 1044
b6b85bdc 1045 m_win->GetEventHandler()->ProcessEvent(newEvent);
4b056ef5
RD
1046 }
1047 else
1048 {
1049 lines *= event.GetLinesPerAction();
1050 if (lines > 0)
687706f5 1051 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
4b056ef5 1052 else
687706f5 1053 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
b6b85bdc 1054
4b056ef5
RD
1055 int times = abs(lines);
1056 for (; times > 0; times--)
1057 m_win->GetEventHandler()->ProcessEvent(newEvent);
1058 }
d2c52078
RD
1059 }
1060}
1061
e421922f
VZ
1062#endif // wxUSE_MOUSEWHEEL
1063
29e1398f 1064void wxScrollHelperBase::HandleOnChildFocus(wxChildFocusEvent& event)
06b9c2a2
VS
1065{
1066 // this event should be processed by all windows in parenthood chain,
1067 // e.g. so that nested wxScrolledWindows work correctly
1068 event.Skip();
1069
1070 // find the immediate child under which the window receiving focus is:
1071 wxWindow *win = event.GetWindow();
02a33891
VS
1072
1073 if ( win == m_targetWindow )
1074 return; // nothing to do
1075
380e1c0c
JS
1076#ifdef __WXMAC__
1077 if (wxDynamicCast(win, wxScrollBar))
1078 return;
1079#endif
1080
3ec704b4
RD
1081 // Fixing ticket: http://trac.wxwidgets.org/ticket/9563
1082 // When a child inside a wxControlContainer receives a focus, the
1083 // wxControlContainer generates an artificial wxChildFocusEvent for
1084 // itself, telling its parent that 'it' received the focus. The effect is
1085 // that this->HandleOnChildFocus is called twice, first with the
1086 // artificial wxChildFocusEvent and then with the original event. We need
1087 // to ignore the artificial event here or otherwise HandleOnChildFocus
1088 // would first scroll the target window to make the entire
1089 // wxControlContainer visible and immediately afterwards scroll the target
1090 // window again to make the child widget visible. This leads to ugly
1091 // flickering when using nested wxPanels/wxScrolledWindows.
1092 //
1093 // Ignore this event if 'win' is derived from wxControlContainer AND its
1094 // parent is the m_targetWindow AND 'win' is not actually reciving the
1095 // focus (win != FindFocus). TODO: This affects all wxControlContainer
1096 // objects, but wxControlContainer is not part of the wxWidgets RTTI and
1097 // so wxDynamicCast(win, wxControlContainer) does not compile. Find a way
1098 // to determine if 'win' derives from wxControlContainer. Until then,
1099 // testing if 'win' derives from wxPanel will probably get >90% of all
1100 // cases.
1101
1102 wxWindow *actual_focus=wxWindow::FindFocus();
1103 if (win != actual_focus &&
1104 wxDynamicCast(win, wxPanel) != 0 &&
1105 win->GetParent() == m_targetWindow)
1106 // if win is a wxPanel and receives the focus, it should not be
1107 // scrolled into view
29e1398f 1108 return;
3ec704b4 1109
888e9638 1110 const wxRect viewRect(m_targetWindow->GetClientRect());
3ec704b4
RD
1111
1112 // For composite controls such as wxComboCtrl we should try to fit the
1113 // entire control inside the visible area of the target window, not just
1114 // the focused child of the control. Otherwise we'd make only the textctrl
1115 // part of a wxComboCtrl visible and the button would still be outside the
1116 // scrolled area. But do so only if the parent fits *entirely* inside the
1117 // scrolled window. In other situations, such as nested wxPanel or
1118 // wxScrolledWindows, the parent might be way to big to fit inside the
1119 // scrolled window. If that is the case, then make only the focused window
1120 // visible
1121 if ( win->GetParent() != m_targetWindow)
06b9c2a2 1122 {
3ec704b4
RD
1123 wxWindow *parent=win->GetParent();
1124 wxSize parent_size=parent->GetSize();
888e9638
VZ
1125 if (parent_size.GetWidth() <= viewRect.GetWidth() &&
1126 parent_size.GetHeight() <= viewRect.GetHeight())
3ec704b4 1127 // make the immediate parent visible instead of the focused control
29e1398f 1128 win=parent;
06b9c2a2
VS
1129 }
1130
888e9638
VZ
1131 // make win position relative to the m_targetWindow viewing area instead of
1132 // its parent
1133 const wxRect
1134 winRect(m_targetWindow->ScreenToClient(win->GetScreenPosition()),
1135 win->GetSize());
1136
1137 // check if it's fully visible
1138 if ( viewRect.Contains(winRect) )
1139 {
1140 // it is, nothing to do
1141 return;
1142 }
1143
1144 // check if we can make it fully visible: this is only possible if it's not
1145 // larger than our view area
1146 if ( winRect.GetWidth() > viewRect.GetWidth() ||
1147 winRect.GetHeight() > viewRect.GetHeight() )
1148 {
1149 // we can't make it fit so avoid scrolling it at all, this is only
1150 // going to be confusing and not helpful
1151 return;
1152 }
1153
1154
1155 // do make the window fit inside the view area by scrolling to it
06b9c2a2
VS
1156 int stepx, stepy;
1157 GetScrollPixelsPerUnit(&stepx, &stepy);
1158
06b9c2a2
VS
1159 int startx, starty;
1160 GetViewStart(&startx, &starty);
1161
1162 // first in vertical direction:
1163 if ( stepy > 0 )
1164 {
1165 int diff = 0;
1166
888e9638 1167 if ( winRect.GetTop() < 0 )
06b9c2a2 1168 {
888e9638 1169 diff = winRect.GetTop();
06b9c2a2 1170 }
888e9638 1171 else if ( winRect.GetBottom() > viewRect.GetHeight() )
06b9c2a2 1172 {
888e9638 1173 diff = winRect.GetBottom() - viewRect.GetHeight() + 1;
06b9c2a2
VS
1174 // round up to next scroll step if we can't get exact position,
1175 // so that the window is fully visible:
1176 diff += stepy - 1;
1177 }
1178
1179 starty = (starty * stepy + diff) / stepy;
1180 }
1181
1182 // then horizontal:
1183 if ( stepx > 0 )
1184 {
1185 int diff = 0;
1186
888e9638 1187 if ( winRect.GetLeft() < 0 )
06b9c2a2 1188 {
888e9638 1189 diff = winRect.GetLeft();
06b9c2a2 1190 }
888e9638 1191 else if ( winRect.GetRight() > viewRect.GetWidth() )
06b9c2a2 1192 {
888e9638 1193 diff = winRect.GetRight() - viewRect.GetWidth() + 1;
06b9c2a2
VS
1194 // round up to next scroll step if we can't get exact position,
1195 // so that the window is fully visible:
1196 diff += stepx - 1;
1197 }
1198
1199 startx = (startx * stepx + diff) / stepx;
1200 }
1201
1202 Scroll(startx, starty);
1203}
1204
29e1398f
VZ
1205
1206#ifdef wxHAS_GENERIC_SCROLLWIN
1207
1208// ----------------------------------------------------------------------------
1209// wxScrollHelper implementation
1210// ----------------------------------------------------------------------------
1211
1212wxScrollHelper::wxScrollHelper(wxWindow *winToScroll)
1213 : wxScrollHelperBase(winToScroll)
1214{
1215 m_xVisibility =
1216 m_yVisibility = wxSHOW_SB_DEFAULT;
1217}
1218
1219void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz,
1220 wxScrollbarVisibility vert)
1221{
1222 if ( horz != m_xVisibility || vert != m_yVisibility )
1223 {
1224 m_xVisibility = horz;
1225 m_yVisibility = vert;
1226
1227 AdjustScrollbars();
1228 }
1229}
1230
1231void
1232wxScrollHelper::DoAdjustScrollbar(int orient,
1233 int clientSize,
1234 int virtSize,
1ddb6d28 1235 int pixelsPerUnit,
29e1398f
VZ
1236 int& scrollUnits,
1237 int& scrollPosition,
1ddb6d28 1238 int& scrollLinesPerPage,
29e1398f
VZ
1239 wxScrollbarVisibility visibility)
1240{
29e1398f 1241 // scroll lines per page: if 0, no scrolling is needed
29e1398f 1242 // check if we need scrollbar in this direction at all
1ddb6d28 1243 if ( pixelsPerUnit == 0 || clientSize >= virtSize )
29e1398f
VZ
1244 {
1245 // scrolling is disabled or unnecessary
1246 scrollUnits =
1247 scrollPosition = 0;
1ddb6d28 1248 scrollLinesPerPage = 0;
29e1398f
VZ
1249 }
1250 else // might need scrolling
1251 {
1252 // Round up integer division to catch any "leftover" client space.
1253 scrollUnits = (virtSize + pixelsPerUnit - 1) / pixelsPerUnit;
1254
1255 // Calculate the number of fully scroll units
1ddb6d28 1256 scrollLinesPerPage = clientSize / pixelsPerUnit;
29e1398f 1257
1ddb6d28 1258 if ( scrollLinesPerPage >= scrollUnits )
29e1398f
VZ
1259 {
1260 // we're big enough to not need scrolling
1261 scrollUnits =
1262 scrollPosition = 0;
1ddb6d28 1263 scrollLinesPerPage = 0;
29e1398f
VZ
1264 }
1265 else // we do need a scrollbar
1266 {
1ddb6d28
VZ
1267 if ( scrollLinesPerPage < 1 )
1268 scrollLinesPerPage = 1;
29e1398f
VZ
1269
1270 // Correct position if greater than extent of canvas minus
1271 // the visible portion of it or if below zero
1ddb6d28 1272 const int posMax = scrollUnits - scrollLinesPerPage;
29e1398f
VZ
1273 if ( scrollPosition > posMax )
1274 scrollPosition = posMax;
1275 else if ( scrollPosition < 0 )
1276 scrollPosition = 0;
1277 }
1278 }
1279
1ddb6d28
VZ
1280 // in wxSHOW_SB_NEVER case don't show the scrollbar even if it's needed, in
1281 // wxSHOW_SB_ALWAYS case show the scrollbar even if it's not needed by
1282 // passing a special range value to SetScrollbar()
1283 int range wxDUMMY_INITIALIZE(0);
1284 switch ( visibility )
1285 {
1286 case wxSHOW_SB_NEVER:
1287 range = 0;
1288 break;
1289
1290 case wxSHOW_SB_DEFAULT:
1291 range = scrollUnits;
1292 break;
1293
1294 case wxSHOW_SB_ALWAYS:
1295 range = scrollUnits ? scrollUnits : -1;
1296 break;
1297 }
29e1398f 1298
1ddb6d28 1299 m_win->SetScrollbar(orient, scrollPosition, scrollLinesPerPage, range);
29e1398f
VZ
1300}
1301
1302void wxScrollHelper::AdjustScrollbars()
1303{
1304 static wxRecursionGuardFlag s_flagReentrancy;
1305 wxRecursionGuard guard(s_flagReentrancy);
1306 if ( guard.IsInside() )
1307 {
1308 // don't reenter AdjustScrollbars() while another call to
1309 // AdjustScrollbars() is in progress because this may lead to calling
1310 // ScrollWindow() twice and this can really happen under MSW if
1311 // SetScrollbar() call below adds or removes the scrollbar which
1312 // changes the window size and hence results in another
1313 // AdjustScrollbars() call
1314 return;
1315 }
1316
1317 int oldXScroll = m_xScrollPosition;
1318 int oldYScroll = m_yScrollPosition;
1319
1320 // we may need to readjust the scrollbars several times as enabling one of
1321 // them reduces the area available for the window contents and so can make
1322 // the other scrollbar necessary now although it wasn't necessary before
1323 //
1324 // VZ: normally this loop should be over in at most 2 iterations, I don't
1325 // know why do we need 5 of them
1326 for ( int iterationCount = 0; iterationCount < 5; iterationCount++ )
1327 {
1328 wxSize clientSize = GetTargetSize();
1329 const wxSize virtSize = m_targetWindow->GetVirtualSize();
1330
1331 // this block of code tries to work around the following problem: the
1332 // window could have been just resized to have enough space to show its
1333 // full contents without the scrollbars, but its client size could be
1334 // not big enough because it does have the scrollbars right now and so
1335 // the scrollbars would remain even though we don't need them any more
1336 //
1337 // to prevent this from happening, check if we have enough space for
1338 // everything without the scrollbars and explicitly disable them then
1339 const wxSize availSize = GetSizeAvailableForScrollTarget(
1340 m_win->GetSize() - m_win->GetWindowBorderSize());
1341 if ( availSize != clientSize )
1342 {
1343 if ( availSize.x >= virtSize.x && availSize.y >= virtSize.y )
1344 {
1345 // this will be enough to make the scrollbars disappear below
1346 // and then the client size will indeed become equal to the
1347 // full available size
1348 clientSize = availSize;
1349 }
1350 }
1351
1352
1353 DoAdjustScrollbar(wxHORIZONTAL,
1354 clientSize.x,
1355 virtSize.x,
1356 m_xScrollPixelsPerLine,
1357 m_xScrollLines,
1358 m_xScrollPosition,
1ddb6d28 1359 m_xScrollLinesPerPage,
29e1398f
VZ
1360 m_xVisibility);
1361
1362 DoAdjustScrollbar(wxVERTICAL,
1363 clientSize.y,
1364 virtSize.y,
1365 m_yScrollPixelsPerLine,
1366 m_yScrollLines,
1367 m_yScrollPosition,
1ddb6d28 1368 m_yScrollLinesPerPage,
29e1398f
VZ
1369 m_yVisibility);
1370
1371
1372 // If a scrollbar (dis)appeared as a result of this, we need to adjust
1373 // them again but if the client size didn't change, then we're done
1374 if ( GetTargetSize() == clientSize )
1375 break;
1376 }
1377
1378#ifdef __WXMOTIF__
1379 // Sorry, some Motif-specific code to implement a backing pixmap
1380 // for the wxRETAINED style. Implementing a backing store can't
1381 // be entirely generic because it relies on the wxWindowDC implementation
1382 // to duplicate X drawing calls for the backing pixmap.
1383
1384 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
1385 {
1386 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
1387
1388 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
1389 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
1390 if (m_targetWindow->GetBackingPixmap() &&
1391 !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
1392 (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
1393 {
1394 XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
1395 m_targetWindow->SetBackingPixmap((WXPixmap) 0);
1396 }
1397
1398 if (!m_targetWindow->GetBackingPixmap() &&
1399 (m_xScrollLines != 0) && (m_yScrollLines != 0))
1400 {
1401 int depth = wxDisplayDepth();
1402 m_targetWindow->SetPixmapWidth(totalPixelWidth);
1403 m_targetWindow->SetPixmapHeight(totalPixelHeight);
1404 m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
1405 m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth));
1406 }
1407
1408 }
1409#endif // Motif
1410
1411 if (oldXScroll != m_xScrollPosition)
1412 {
1413 if (m_xScrollingEnabled)
1414 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0,
1415 GetScrollRect() );
1416 else
1417 m_targetWindow->Refresh(true, GetScrollRect());
1418 }
1419
1420 if (oldYScroll != m_yScrollPosition)
1421 {
1422 if (m_yScrollingEnabled)
1423 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
1424 GetScrollRect() );
1425 else
1426 m_targetWindow->Refresh(true, GetScrollRect());
1427 }
1428}
1429
1430void wxScrollHelper::DoScroll( int x_pos, int y_pos )
1431{
1432 if (!m_targetWindow)
1433 return;
1434
1435 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
1436 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
1437
1438 int w = 0, h = 0;
1439 GetTargetSize(&w, &h);
1440
1441 // compute new position:
1442 int new_x = m_xScrollPosition;
1443 int new_y = m_yScrollPosition;
1444
1445 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
1446 {
1447 new_x = x_pos;
1448
1449 // Calculate page size i.e. number of scroll units you get on the
1450 // current client window
1451 int noPagePositions = w/m_xScrollPixelsPerLine;
1452 if (noPagePositions < 1) noPagePositions = 1;
1453
1454 // Correct position if greater than extent of canvas minus
1455 // the visible portion of it or if below zero
1456 new_x = wxMin( m_xScrollLines-noPagePositions, new_x );
1457 new_x = wxMax( 0, new_x );
1458 }
1459 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
1460 {
1461 new_y = y_pos;
1462
1463 // Calculate page size i.e. number of scroll units you get on the
1464 // current client window
1465 int noPagePositions = h/m_yScrollPixelsPerLine;
1466 if (noPagePositions < 1) noPagePositions = 1;
1467
1468 // Correct position if greater than extent of canvas minus
1469 // the visible portion of it or if below zero
1470 new_y = wxMin( m_yScrollLines-noPagePositions, new_y );
1471 new_y = wxMax( 0, new_y );
1472 }
1473
1474 if ( new_x == m_xScrollPosition && new_y == m_yScrollPosition )
1475 return; // nothing to do, the position didn't change
1476
1477 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
1478 // otherwise invalidated area could be updated incorrectly later when
1479 // ScrollWindow() makes sure they're repainted before scrolling them
1480 m_targetWindow->Update();
1481
1482 // update the position and scroll the window now:
1483 if (m_xScrollPosition != new_x)
1484 {
1485 int old_x = m_xScrollPosition;
1486 m_xScrollPosition = new_x;
1487 m_win->SetScrollPos( wxHORIZONTAL, new_x );
1488 m_targetWindow->ScrollWindow( (old_x-new_x)*m_xScrollPixelsPerLine, 0,
1489 GetScrollRect() );
1490 }
1491
1492 if (m_yScrollPosition != new_y)
1493 {
1494 int old_y = m_yScrollPosition;
1495 m_yScrollPosition = new_y;
1496 m_win->SetScrollPos( wxVERTICAL, new_y );
1497 m_targetWindow->ScrollWindow( 0, (old_y-new_y)*m_yScrollPixelsPerLine,
1498 GetScrollRect() );
1499 }
1500}
1501
1502#endif // wxHAS_GENERIC_SCROLLWIN
1503
1e6feb95 1504// ----------------------------------------------------------------------------
16361ec9 1505// wxScrolled<T> and wxScrolledWindow implementation
1e6feb95
VZ
1506// ----------------------------------------------------------------------------
1507
16361ec9 1508wxSize wxScrolledT_Helper::FilterBestSize(const wxWindow *win,
29e1398f 1509 const wxScrollHelper *helper,
16361ec9 1510 const wxSize& origBest)
7d616e99
VS
1511{
1512 // NB: We don't do this in WX_FORWARD_TO_SCROLL_HELPER, because not
1513 // all scrollable windows should behave like this, only those that
1514 // contain children controls within scrollable area
1515 // (i.e., wxScrolledWindow) and other some scrollable windows may
1516 // have different DoGetBestSize() implementation (e.g. wxTreeCtrl).
1517
16361ec9 1518 wxSize best = origBest;
7d616e99 1519
16361ec9 1520 if ( win->GetAutoLayout() )
7d616e99
VS
1521 {
1522 // Only use the content to set the window size in the direction
1523 // where there's no scrolling; otherwise we're going to get a huge
1524 // window in the direction in which scrolling is enabled
1525 int ppuX, ppuY;
16361ec9 1526 helper->GetScrollPixelsPerUnit(&ppuX, &ppuY);
7d616e99
VS
1527
1528 // NB: This code used to use *current* size if min size wasn't
1529 // specified, presumably to get some reasonable (i.e., larger than
1530 // minimal) size. But that's a wrong thing to do in GetBestSize(),
1531 // so we use minimal size as specified. If the app needs some
1532 // minimal size for its scrolled window, it should set it and put
1533 // the window into sizer as expandable so that it can use all space
1534 // available to it.
1535 //
1536 // See also http://svn.wxwidgets.org/viewvc/wx?view=rev&revision=45864
1537
16361ec9 1538 wxSize minSize = win->GetMinSize();
7d616e99
VS
1539
1540 if ( ppuX > 0 )
1541 best.x = minSize.x + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1542
1543 if ( ppuY > 0 )
1544 best.y = minSize.y + wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
1545 }
1546
1547 return best;
1548}
1549
0cf5b099 1550#ifdef __WXMSW__
f2a6c918 1551WXLRESULT wxScrolledT_Helper::FilterMSWWindowProc(WXUINT nMsg, WXLRESULT rc)
0cf5b099 1552{
4676948b 1553#ifndef __WXWINCE__
0cf5b099
VZ
1554 // we need to process arrows ourselves for scrolling
1555 if ( nMsg == WM_GETDLGCODE )
1556 {
1557 rc |= DLGC_WANTARROWS;
1558 }
4676948b 1559#endif
0cf5b099
VZ
1560 return rc;
1561}
0cf5b099 1562#endif // __WXMSW__
16361ec9
VS
1563
1564// NB: skipping wxScrolled<T> in wxRTTI information because being a templte,
1565// it doesn't and can't implement wxRTTI support
1566IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)