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