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