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