]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrlwing.cpp
Syntax fix.
[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
14f355c2 21#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
3a8c693a 22 #pragma implementation "genscrolwin.h"
c801d85f
KB
23#endif
24
bcd055ae
JJ
25#ifdef __VMS
26#define XtDisplay XTDISPLAY
27#endif
28
c801d85f
KB
29// For compilers that support precompilation, includes "wx.h".
30#include "wx/wxprec.h"
31
c801d85f 32#ifdef __BORLANDC__
d80cd92a 33 #pragma hdrstop
c801d85f
KB
34#endif
35
3a8c693a
VZ
36#if !defined(__WXGTK__) || defined(__WXUNIVERSAL__)
37
d80cd92a
VZ
38#include "wx/utils.h"
39#include "wx/dcclient.h"
40
1e6feb95 41#include "wx/scrolwin.h"
053f9cc1 42#include "wx/panel.h"
4b316329 43#if wxUSE_TIMER
1e6feb95 44#include "wx/timer.h"
4b316329 45#endif
95d60b24 46#include "wx/sizer.h"
2a201ef8 47#include "wx/recguard.h"
c801d85f 48
48d1144b 49#ifdef __WXMSW__
1e6feb95 50 #include <windows.h> // for DLGC_WANTARROWS
48d1144b
JS
51#endif
52
a91b47e8
JS
53#ifdef __WXMOTIF__
54// For wxRETAINED implementation
338dd992
JJ
55#ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
56 //This code switches off the compiler warnings
57# pragma message disable nosimpint
58#endif
a91b47e8 59#include <Xm/Xm.h>
338dd992
JJ
60#ifdef __VMS__
61# pragma message enable nosimpint
62#endif
a91b47e8
JS
63#endif
64
fa3541bd 65IMPLEMENT_CLASS(wxScrolledWindow, wxGenericScrolledWindow)
fa3541bd 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)
993cfa87 232 if ( m_hasDrawnWindow )
349efbaa 233 {
ca65c044 234 return true;
349efbaa
VZ
235 }
236 }
2feed004 237
ca65c044 238 // reset the skipped flag to false as it might have been set to true in
1e6feb95 239 // ProcessEvent() above
ca65c044 240 event.Skip(false);
1e6feb95 241
e421922f
VZ
242 if ( evType == wxEVT_PAINT )
243 {
244 m_scrollHelper->HandleOnPaint((wxPaintEvent &)event);
ca65c044 245 return true;
e421922f 246 }
1e6feb95 247
e421922f
VZ
248 if ( evType == wxEVT_SCROLLWIN_TOP ||
249 evType == wxEVT_SCROLLWIN_BOTTOM ||
250 evType == wxEVT_SCROLLWIN_LINEUP ||
251 evType == wxEVT_SCROLLWIN_LINEDOWN ||
252 evType == wxEVT_SCROLLWIN_PAGEUP ||
253 evType == wxEVT_SCROLLWIN_PAGEDOWN ||
254 evType == wxEVT_SCROLLWIN_THUMBTRACK ||
255 evType == wxEVT_SCROLLWIN_THUMBRELEASE )
256 {
257 m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event);
1e6feb95 258 return !event.GetSkipped();
e421922f 259 }
1e6feb95 260
e421922f
VZ
261 if ( evType == wxEVT_ENTER_WINDOW )
262 {
263 m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event);
264 }
265 else if ( evType == wxEVT_LEAVE_WINDOW )
266 {
267 m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event);
268 }
269#if wxUSE_MOUSEWHEEL
270 else if ( evType == wxEVT_MOUSEWHEEL )
271 {
272 m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event);
273 }
274#endif // wxUSE_MOUSEWHEEL
e421922f
VZ
275 else if ( evType == wxEVT_CHAR )
276 {
277 m_scrollHelper->HandleOnChar((wxKeyEvent &)event);
278 return !event.GetSkipped();
1e6feb95
VZ
279 }
280
ca65c044 281 return false;
1e6feb95
VZ
282}
283
284// ----------------------------------------------------------------------------
285// wxScrollHelper construction
286// ----------------------------------------------------------------------------
287
288wxScrollHelper::wxScrollHelper(wxWindow *win)
289{
290 m_xScrollPixelsPerLine =
291 m_yScrollPixelsPerLine =
292 m_xScrollPosition =
293 m_yScrollPosition =
294 m_xScrollLines =
295 m_yScrollLines =
296 m_xScrollLinesPerPage =
139adb6a 297 m_yScrollLinesPerPage = 0;
1e6feb95
VZ
298
299 m_xScrollingEnabled =
ca65c044 300 m_yScrollingEnabled = true;
1e6feb95
VZ
301
302 m_scaleX =
139adb6a 303 m_scaleY = 1.0;
24ce4c18 304#if wxUSE_MOUSEWHEEL
d2c52078 305 m_wheelRotation = 0;
24ce4c18 306#endif
a58a12e9 307
1e6feb95
VZ
308 m_win =
309 m_targetWindow = (wxWindow *)NULL;
139adb6a 310
1e6feb95 311 m_timerAutoScroll = (wxTimer *)NULL;
d7da9756 312
349efbaa
VZ
313 m_handler = NULL;
314
1e6feb95
VZ
315 if ( win )
316 SetWindow(win);
317}
d7da9756 318
1e6feb95 319wxScrollHelper::~wxScrollHelper()
d80cd92a 320{
1e6feb95
VZ
321 StopAutoScrolling();
322
349efbaa 323 DeleteEvtHandler();
d80cd92a
VZ
324}
325
326// ----------------------------------------------------------------------------
327// setting scrolling parameters
328// ----------------------------------------------------------------------------
329
1e6feb95
VZ
330void wxScrollHelper::SetScrollbars(int pixelsPerUnitX,
331 int pixelsPerUnitY,
332 int noUnitsX,
333 int noUnitsY,
334 int xPos,
335 int yPos,
336 bool noRefresh)
c801d85f 337{
b0486e0d
SN
338 int xpos, ypos;
339
340 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
139adb6a
RR
341 bool do_refresh =
342 (
c801d85f 343 (noUnitsX != 0 && m_xScrollLines == 0) ||
566d84a7 344 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX * noUnitsX) ||
b0486e0d 345
c801d85f 346 (noUnitsY != 0 && m_yScrollLines == 0) ||
566d84a7 347 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY * noUnitsY) ||
c801d85f 348 (xPos != m_xScrollPosition) ||
b0486e0d 349 (yPos != m_yScrollPosition)
139adb6a 350 );
a58a12e9 351
139adb6a
RR
352 m_xScrollPixelsPerLine = pixelsPerUnitX;
353 m_yScrollPixelsPerLine = pixelsPerUnitY;
354 m_xScrollPosition = xPos;
355 m_yScrollPosition = yPos;
a91b47e8 356
150c8d89
RL
357 int w = noUnitsX * pixelsPerUnitX;
358 int h = noUnitsY * pixelsPerUnitY;
359
2b5f62a0
VZ
360 // For better backward compatibility we set persisting limits
361 // here not just the size. It makes SetScrollbars 'sticky'
362 // emulating the old non-autoscroll behaviour.
363
150c8d89 364 m_targetWindow->SetVirtualSizeHints( w, h );
a58a12e9 365
2b5f62a0
VZ
366 // The above should arguably be deprecated, this however we still need.
367
150c8d89 368 m_targetWindow->SetVirtualSize( w, h );
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;
349efbaa
VZ
423
424 // install the event handler which will intercept the events we're
af4088f1
VZ
425 // interested in (but only do it for our real window, not the target window
426 // which we scroll - we don't need to hijack its events)
427 if ( m_targetWindow == m_win )
13ff9344 428 {
248d771c
VZ
429 // if we already have a handler, delete it first
430 DeleteEvtHandler();
431
13ff9344
JS
432 m_handler = new wxScrollHelperEvtHandler(this);
433 m_targetWindow->PushEventHandler(m_handler);
434 }
349efbaa
VZ
435}
436
af4088f1 437void wxScrollHelper::SetTargetWindow(wxWindow *target)
349efbaa
VZ
438{
439 wxCHECK_RET( target, wxT("target window must not be NULL") );
440
441 if ( target == m_targetWindow )
442 return;
443
af4088f1 444 DoSetTargetWindow(target);
ecab4dba
RR
445}
446
1e6feb95 447wxWindow *wxScrollHelper::GetTargetWindow() const
ecab4dba
RR
448{
449 return m_targetWindow;
450}
451
d80cd92a
VZ
452// ----------------------------------------------------------------------------
453// scrolling implementation itself
454// ----------------------------------------------------------------------------
455
1e6feb95 456void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event)
c801d85f 457{
139adb6a 458 int nScrollInc = CalcScrollInc(event);
1e6feb95 459 if ( nScrollInc == 0 )
139adb6a 460 {
1e6feb95
VZ
461 // can't scroll further
462 event.Skip();
463
464 return;
139adb6a 465 }
c801d85f 466
1e6feb95 467 int orient = event.GetOrientation();
139adb6a
RR
468 if (orient == wxHORIZONTAL)
469 {
470 m_xScrollPosition += nScrollInc;
5f3286d1 471 m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
139adb6a 472 }
c801d85f 473 else
139adb6a
RR
474 {
475 m_yScrollPosition += nScrollInc;
5f3286d1 476 m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
139adb6a 477 }
a58a12e9 478
ca65c044 479 bool needsRefresh = false;
1e6feb95
VZ
480 int dx = 0,
481 dy = 0;
139adb6a
RR
482 if (orient == wxHORIZONTAL)
483 {
1e6feb95
VZ
484 if ( m_xScrollingEnabled )
485 {
486 dx = -m_xScrollPixelsPerLine * nScrollInc;
487 }
139adb6a 488 else
1e6feb95 489 {
ca65c044 490 needsRefresh = true;
1e6feb95 491 }
139adb6a 492 }
c801d85f 493 else
139adb6a 494 {
1e6feb95
VZ
495 if ( m_yScrollingEnabled )
496 {
497 dy = -m_yScrollPixelsPerLine * nScrollInc;
498 }
139adb6a 499 else
1e6feb95 500 {
ca65c044 501 needsRefresh = true;
1e6feb95
VZ
502 }
503 }
504
505 if ( needsRefresh )
506 {
ca65c044 507 m_targetWindow->Refresh(true, GetScrollRect());
1e6feb95
VZ
508 }
509 else
510 {
d5d58a93 511 m_targetWindow->ScrollWindow(dx, dy, GetScrollRect());
3d2b9c20 512 }
c801d85f
KB
513}
514
1e6feb95 515int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 516{
ecab4dba
RR
517 int pos = event.GetPosition();
518 int orient = event.GetOrientation();
c801d85f 519
ecab4dba 520 int nScrollInc = 0;
1a8caf94 521 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
c801d85f 522 {
ecab4dba
RR
523 if (orient == wxHORIZONTAL)
524 nScrollInc = - m_xScrollPosition;
525 else
526 nScrollInc = - m_yScrollPosition;
1a8caf94
RR
527 } else
528 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
529 {
ecab4dba
RR
530 if (orient == wxHORIZONTAL)
531 nScrollInc = m_xScrollLines - m_xScrollPosition;
532 else
533 nScrollInc = m_yScrollLines - m_yScrollPosition;
1a8caf94
RR
534 } else
535 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
536 {
ecab4dba 537 nScrollInc = -1;
1a8caf94
RR
538 } else
539 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
540 {
ecab4dba 541 nScrollInc = 1;
1a8caf94
RR
542 } else
543 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
544 {
ecab4dba
RR
545 if (orient == wxHORIZONTAL)
546 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
547 else
548 nScrollInc = -GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
549 } else
550 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
551 {
ecab4dba
RR
552 if (orient == wxHORIZONTAL)
553 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
554 else
555 nScrollInc = GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
556 } else
557 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
558 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
559 {
ecab4dba
RR
560 if (orient == wxHORIZONTAL)
561 nScrollInc = pos - m_xScrollPosition;
562 else
563 nScrollInc = pos - m_yScrollPosition;
c801d85f 564 }
88150e60 565
ecab4dba
RR
566 if (orient == wxHORIZONTAL)
567 {
a58a12e9 568 if (m_xScrollPixelsPerLine > 0)
ecab4dba
RR
569 {
570 int w, h;
1e6feb95 571 GetTargetSize(&w, &h);
ecab4dba
RR
572
573 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
574 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
575 if (noPositions < 0)
d80cd92a 576 noPositions = 0;
ecab4dba
RR
577
578 if ( (m_xScrollPosition + nScrollInc) < 0 )
d80cd92a 579 nScrollInc = -m_xScrollPosition; // As -ve as we can go
ecab4dba 580 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
d80cd92a 581 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
ecab4dba
RR
582 }
583 else
ca65c044 584 m_targetWindow->Refresh(true, GetScrollRect());
9d9355c6
VZ
585 }
586 else
ecab4dba 587 {
be9b0663 588 if ( m_yScrollPixelsPerLine > 0 )
d80cd92a 589 {
be9b0663
VZ
590 if ( m_yScrollPosition + nScrollInc < 0 )
591 {
592 // As -ve as we can go
593 nScrollInc = -m_yScrollPosition;
594 }
595 else // check for the other bound
596 {
597 const int posMax = m_yScrollLines - m_yScrollLinesPerPage;
598 if ( m_yScrollPosition + nScrollInc > posMax )
599 {
600 // As +ve as we can go
601 nScrollInc = posMax - m_yScrollPosition;
602 }
603 }
ecab4dba
RR
604 }
605 else
be9b0663
VZ
606 {
607 // VZ: why do we do this? (FIXME)
ca65c044 608 m_targetWindow->Refresh(true, GetScrollRect());
be9b0663 609 }
9d9355c6 610 }
9d9355c6 611
ecab4dba 612 return nScrollInc;
c801d85f
KB
613}
614
615// Adjust the scrollbars - new version.
1e6feb95 616void wxScrollHelper::AdjustScrollbars()
c801d85f 617{
2a201ef8
VZ
618 static wxRecursionGuardFlag s_flagReentrancy;
619 wxRecursionGuard guard(s_flagReentrancy);
620 if ( guard.IsInside() )
621 {
622 // don't reenter AdjustScrollbars() while another call to
623 // AdjustScrollbars() is in progress because this may lead to calling
624 // ScrollWindow() twice and this can really happen under MSW if
625 // SetScrollbar() call below adds or removes the scrollbar which
626 // changes the window size and hence results in another
627 // AdjustScrollbars() call
628 return;
629 }
630
566d84a7
RL
631 int w = 0, h = 0;
632 int oldw, oldh;
a58a12e9 633
27d029c7
RR
634 int oldXScroll = m_xScrollPosition;
635 int oldYScroll = m_yScrollPosition;
c801d85f 636
be9b0663
VZ
637 // VZ: at least under Windows this loop is useless because when scrollbars
638 // [dis]appear we get a WM_SIZE resulting in another call to
639 // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave
640 // it here for now but it would be better to ensure that all ports
641 // generate EVT_SIZE when scrollbars [dis]appear, emulating it if
642 // necessary, and remove it later
643 do
644 {
566d84a7 645 GetTargetSize(&w, 0);
c801d85f 646
566d84a7
RL
647 if (m_xScrollPixelsPerLine == 0)
648 {
649 m_xScrollLines = 0;
650 m_xScrollPosition = 0;
ca65c044 651 m_win->SetScrollbar (wxHORIZONTAL, 0, 0, 0, false);
566d84a7
RL
652 }
653 else
654 {
655 m_xScrollLines = m_targetWindow->GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine;
656
657 // Calculate page size i.e. number of scroll units you get on the
658 // current client window
659 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
660 if (noPagePositions < 1) noPagePositions = 1;
661 if ( noPagePositions > m_xScrollLines )
662 noPagePositions = m_xScrollLines;
663
664 // Correct position if greater than extent of canvas minus
665 // the visible portion of it or if below zero
666 m_xScrollPosition = wxMin( m_xScrollLines - noPagePositions, m_xScrollPosition);
667 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
668
669 m_win->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
670 // The amount by which we scroll when paging
671 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
672 }
c801d85f 673
566d84a7 674 GetTargetSize(0, &h);
a58a12e9 675
be9b0663
VZ
676 // scroll lines per page: if 0, no scrolling is needed
677 int linesPerPage;
678
679 if ( m_yScrollPixelsPerLine == 0 )
566d84a7 680 {
be9b0663 681 // scrolling is disabled
566d84a7
RL
682 m_yScrollLines = 0;
683 m_yScrollPosition = 0;
be9b0663 684 linesPerPage = 0;
566d84a7 685 }
be9b0663 686 else // might need scrolling
566d84a7 687 {
be9b0663
VZ
688 int hVirt = m_targetWindow->GetVirtualSize().GetHeight();
689 m_yScrollLines = hVirt / m_yScrollPixelsPerLine;
566d84a7
RL
690
691 // Calculate page size i.e. number of scroll units you get on the
692 // current client window
be9b0663
VZ
693 linesPerPage = h / m_yScrollPixelsPerLine;
694 if ( linesPerPage >= m_yScrollLines )
695 {
696 // we're big enough to not need scrolling
697 linesPerPage =
698 m_yScrollLines =
699 m_yScrollPosition = 0;
700 }
701 else // we do need a scrollbar
702 {
703 if ( linesPerPage < 1 )
704 linesPerPage = 1;
705
706 // Correct position if greater than extent of canvas minus
707 // the visible portion of it or if below zero
708 const int posMax = m_yScrollLines - linesPerPage;
709 if ( m_yScrollPosition > posMax )
710 m_yScrollPosition = posMax;
711 else if ( m_yScrollPosition < 0 )
712 m_yScrollPosition = 0;
713 }
714 }
566d84a7 715
be9b0663
VZ
716 m_win->SetScrollbar(wxVERTICAL, m_yScrollPosition,
717 linesPerPage, m_yScrollLines);
566d84a7 718
be9b0663
VZ
719 // The amount by which we scroll when paging
720 SetScrollPageSize(wxVERTICAL, linesPerPage);
c801d85f 721
139adb6a 722
be9b0663 723 // If a scrollbar (dis)appeared as a result of this, adjust them again.
566d84a7
RL
724 oldw = w;
725 oldh = h;
726
727 GetTargetSize( &w, &h );
c4513b23 728 } while ( w != oldw || h != oldh );
566d84a7
RL
729
730#ifdef __WXMOTIF__
731 // Sorry, some Motif-specific code to implement a backing pixmap
732 // for the wxRETAINED style. Implementing a backing store can't
733 // be entirely generic because it relies on the wxWindowDC implementation
734 // to duplicate X drawing calls for the backing pixmap.
735
736 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
139adb6a 737 {
566d84a7
RL
738 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
739
740 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
741 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
742 if (m_targetWindow->GetBackingPixmap() &&
743 !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
744 (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
745 {
746 XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
747 m_targetWindow->SetBackingPixmap((WXPixmap) 0);
748 }
749
750 if (!m_targetWindow->GetBackingPixmap() &&
9b66c26a 751 (m_xScrollLines != 0) && (m_yScrollLines != 0))
566d84a7
RL
752 {
753 int depth = wxDisplayDepth();
754 m_targetWindow->SetPixmapWidth(totalPixelWidth);
755 m_targetWindow->SetPixmapHeight(totalPixelHeight);
756 m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
757 m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth));
758 }
759
139adb6a 760 }
566d84a7 761#endif // Motif
a58a12e9 762
27d029c7
RR
763 if (oldXScroll != m_xScrollPosition)
764 {
765 if (m_xScrollingEnabled)
566d84a7 766 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0,
d5d58a93 767 GetScrollRect() );
27d029c7 768 else
ca65c044 769 m_targetWindow->Refresh(true, GetScrollRect());
27d029c7 770 }
a58a12e9 771
27d029c7
RR
772 if (oldYScroll != m_yScrollPosition)
773 {
774 if (m_yScrollingEnabled)
1e6feb95 775 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
d5d58a93 776 GetScrollRect() );
27d029c7 777 else
ca65c044 778 m_targetWindow->Refresh(true, GetScrollRect());
27d029c7 779 }
c801d85f
KB
780}
781
1e6feb95 782void wxScrollHelper::DoPrepareDC(wxDC& dc)
c801d85f 783{
1e6feb95
VZ
784 wxPoint pt = dc.GetDeviceOrigin();
785 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
786 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
139adb6a 787 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
788}
789
566d84a7
RL
790void wxScrollHelper::SetScrollRate( int xstep, int ystep )
791{
792 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
793 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
794
795 m_xScrollPixelsPerLine = xstep;
796 m_yScrollPixelsPerLine = ystep;
797
798 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
799 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
800
801 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
802 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
803 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
804
805 AdjustScrollbars();
806}
807
1e6feb95 808void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
c801d85f 809{
a0bc2c1d
VZ
810 if ( x_unit )
811 *x_unit = m_xScrollPixelsPerLine;
812 if ( y_unit )
813 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
814}
815
1e6feb95 816int wxScrollHelper::GetScrollPageSize(int orient) const
c801d85f
KB
817{
818 if ( orient == wxHORIZONTAL )
819 return m_xScrollLinesPerPage;
820 else
821 return m_yScrollLinesPerPage;
822}
823
1e6feb95 824void wxScrollHelper::SetScrollPageSize(int orient, int pageSize)
c801d85f
KB
825{
826 if ( orient == wxHORIZONTAL )
827 m_xScrollLinesPerPage = pageSize;
828 else
829 m_yScrollLinesPerPage = pageSize;
830}
831
832/*
833 * Scroll to given position (scroll position, not pixel position)
834 */
1e6feb95 835void wxScrollHelper::Scroll( int x_pos, int y_pos )
c801d85f 836{
8b089c5e
JS
837 if (!m_targetWindow)
838 return;
839
a58a12e9 840 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
139adb6a 841 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
a58a12e9 842
139adb6a 843 int w, h;
1e6feb95 844 GetTargetSize(&w, &h);
c801d85f 845
8775b357 846 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
c801d85f 847 {
ed673c6a 848 int old_x = m_xScrollPosition;
139adb6a 849 m_xScrollPosition = x_pos;
a58a12e9 850
3d2b9c20
RR
851 // Calculate page size i.e. number of scroll units you get on the
852 // current client window
ecab4dba 853 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a
RR
854 if (noPagePositions < 1) noPagePositions = 1;
855
856 // Correct position if greater than extent of canvas minus
3d2b9c20 857 // the visible portion of it or if below zero
139adb6a
RR
858 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
859 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
a58a12e9 860
f6bcfd97 861 if (old_x != m_xScrollPosition) {
5f3286d1 862 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
1e6feb95 863 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0,
d5d58a93 864 GetScrollRect() );
f6bcfd97 865 }
c801d85f 866 }
8775b357 867 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
c801d85f 868 {
ed673c6a 869 int old_y = m_yScrollPosition;
139adb6a 870 m_yScrollPosition = y_pos;
a58a12e9 871
3d2b9c20
RR
872 // Calculate page size i.e. number of scroll units you get on the
873 // current client window
ecab4dba 874 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a
RR
875 if (noPagePositions < 1) noPagePositions = 1;
876
877 // Correct position if greater than extent of canvas minus
3d2b9c20 878 // the visible portion of it or if below zero
139adb6a
RR
879 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
880 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
d2c52078 881
f6bcfd97 882 if (old_y != m_yScrollPosition) {
5f3286d1 883 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
1e6feb95 884 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine,
d5d58a93 885 GetScrollRect() );
f6bcfd97 886 }
c801d85f 887 }
c801d85f
KB
888}
889
1e6feb95 890void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 891{
139adb6a
RR
892 m_xScrollingEnabled = x_scroll;
893 m_yScrollingEnabled = y_scroll;
c801d85f
KB
894}
895
c801d85f 896// Where the current view starts from
1e6feb95 897void wxScrollHelper::GetViewStart (int *x, int *y) const
c801d85f 898{
a0bc2c1d
VZ
899 if ( x )
900 *x = m_xScrollPosition;
901 if ( y )
902 *y = m_yScrollPosition;
c801d85f
KB
903}
904
2d67974d
WS
905#if WXWIN_COMPATIBILITY_2_2
906
907void wxScrollHelper::ViewStart(int *x, int *y) const
908{
909 GetViewStart( x, y );
910}
911
912#endif // WXWIN_COMPATIBILITY_2_2
913
8c2f3797 914void wxScrollHelper::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 915{
a0bc2c1d
VZ
916 if ( xx )
917 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
918 if ( yy )
919 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f
KB
920}
921
8c2f3797 922void wxScrollHelper::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 923{
a0bc2c1d
VZ
924 if ( xx )
925 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
926 if ( yy )
927 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f 928}
d80cd92a
VZ
929
930// ----------------------------------------------------------------------------
931// event handlers
932// ----------------------------------------------------------------------------
933
934// Default OnSize resets scrollbars, if any
1e6feb95 935void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
d80cd92a 936{
150c8d89 937 if( m_win->GetAutoLayout() || m_targetWindow->GetAutoLayout() )
2b5f62a0
VZ
938 {
939 if ( m_targetWindow != m_win )
940 m_targetWindow->FitInside();
566d84a7 941
2b5f62a0 942 m_win->FitInside();
dc429f89 943
150c8d89
RL
944 // FIXME: Something is really weird here... This should be
945 // called by FitInside above (and apparently is), yet the
946 // scrollsub sample will get the scrollbar wrong if resized
947 // quickly. This masks the bug, but is surely not the right
948 // answer at all.
949 AdjustScrollbars();
2b5f62a0
VZ
950 }
951 else
952 AdjustScrollbars();
d80cd92a
VZ
953}
954
955// This calls OnDraw, having adjusted the origin according to the current
956// scroll position
1e6feb95 957void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
d80cd92a 958{
af4088f1
VZ
959 // don't use m_targetWindow here, this is always called for ourselves
960 wxPaintDC dc(m_win);
1e6feb95 961 DoPrepareDC(dc);
d80cd92a
VZ
962
963 OnDraw(dc);
964}
965
438e3558
VZ
966// kbd handling: notice that we use OnChar() and not OnKeyDown() for
967// compatibility here - if we used OnKeyDown(), the programs which process
968// arrows themselves in their OnChar() would never get the message and like
969// this they always have the priority
1e6feb95 970void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
d80cd92a
VZ
971{
972 int stx, sty, // view origin
973 szx, szy, // view size (total)
974 clix, cliy; // view size (on screen)
975
1e6feb95
VZ
976 GetViewStart(&stx, &sty);
977 GetTargetSize(&clix, &cliy);
566d84a7 978 m_targetWindow->GetVirtualSize(&szx, &szy);
56dade3c
RL
979
980 if( m_xScrollPixelsPerLine )
981 {
982 clix /= m_xScrollPixelsPerLine;
d7da9756 983 szx /= m_xScrollPixelsPerLine;
56dade3c
RL
984 }
985 else
986 {
987 clix = 0;
d7da9756 988 szx = -1;
56dade3c
RL
989 }
990 if( m_yScrollPixelsPerLine )
991 {
992 cliy /= m_yScrollPixelsPerLine;
993 szy /= m_yScrollPixelsPerLine;
994 }
995 else
996 {
997 cliy = 0;
d7da9756 998 szy = -1;
56dade3c 999 }
d80cd92a 1000
39c869a6
VZ
1001 int xScrollOld = m_xScrollPosition,
1002 yScrollOld = m_yScrollPosition;
1003
ecb01792 1004 int dsty;
4995ca80 1005 switch ( event.GetKeyCode() )
d80cd92a
VZ
1006 {
1007 case WXK_PAGEUP:
1008 case WXK_PRIOR:
ecb01792
RL
1009 dsty = sty - (5 * cliy / 6);
1010 Scroll(-1, (dsty == -1) ? 0 : dsty);
d80cd92a
VZ
1011 break;
1012
1013 case WXK_PAGEDOWN:
1014 case WXK_NEXT:
1015 Scroll(-1, sty + (5 * cliy / 6));
1016 break;
1017
d80cd92a
VZ
1018 case WXK_HOME:
1019 Scroll(0, event.ControlDown() ? 0 : -1);
1020 break;
1021
1022 case WXK_END:
4acd108c 1023 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
d80cd92a
VZ
1024 break;
1025
1026 case WXK_UP:
1027 Scroll(-1, sty - 1);
1028 break;
1029
1030 case WXK_DOWN:
1031 Scroll(-1, sty + 1);
1032 break;
1033
1034 case WXK_LEFT:
1035 Scroll(stx - 1, -1);
1036 break;
1037
1038 case WXK_RIGHT:
1039 Scroll(stx + 1, -1);
1040 break;
1041
1042 default:
1043 // not for us
1044 event.Skip();
1045 }
39c869a6
VZ
1046
1047 if ( m_xScrollPosition != xScrollOld )
1048 {
1049 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
1050 wxHORIZONTAL);
1051 event.SetEventObject(m_win);
1052 m_win->GetEventHandler()->ProcessEvent(event);
1053 }
1054
1055 if ( m_yScrollPosition != yScrollOld )
1056 {
1057 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
1058 wxVERTICAL);
1059 event.SetEventObject(m_win);
1060 m_win->GetEventHandler()->ProcessEvent(event);
1061 }
d80cd92a 1062}
d2c52078 1063
1e6feb95
VZ
1064// ----------------------------------------------------------------------------
1065// autoscroll stuff: these functions deal with sending fake scroll events when
1066// a captured mouse is being held outside the window
1067// ----------------------------------------------------------------------------
1068
1069bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
1070{
1071 // only send the event if the window is scrollable in this direction
1072 wxWindow *win = (wxWindow *)event.GetEventObject();
1073 return win->HasScrollbar(event.GetOrientation());
1074}
1075
1076void wxScrollHelper::StopAutoScrolling()
1077{
4b316329 1078#if wxUSE_TIMER
1e6feb95
VZ
1079 if ( m_timerAutoScroll )
1080 {
1081 delete m_timerAutoScroll;
1082 m_timerAutoScroll = (wxTimer *)NULL;
1083 }
4b316329 1084#endif
1e6feb95 1085}
d2c52078 1086
1e6feb95 1087void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
d2c52078 1088{
1e6feb95
VZ
1089 StopAutoScrolling();
1090
1091 event.Skip();
1092}
d2c52078 1093
1e6feb95
VZ
1094void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
1095{
1096 // don't prevent the usual processing of the event from taking place
1097 event.Skip();
1098
1099 // when a captured mouse leave a scrolled window we start generate
1100 // scrolling events to allow, for example, extending selection beyond the
1101 // visible area in some controls
1102 if ( wxWindow::GetCapture() == m_targetWindow )
1103 {
1104 // where is the mouse leaving?
1105 int pos, orient;
1106 wxPoint pt = event.GetPosition();
1107 if ( pt.x < 0 )
1108 {
1109 orient = wxHORIZONTAL;
1110 pos = 0;
1111 }
1112 else if ( pt.y < 0 )
1113 {
1114 orient = wxVERTICAL;
1115 pos = 0;
1116 }
1117 else // we're lower or to the right of the window
1118 {
1119 wxSize size = m_targetWindow->GetClientSize();
1120 if ( pt.x > size.x )
1121 {
1122 orient = wxHORIZONTAL;
1123 pos = m_xScrollLines;
1124 }
1125 else if ( pt.y > size.y )
1126 {
1127 orient = wxVERTICAL;
1128 pos = m_yScrollLines;
1129 }
1130 else // this should be impossible
1131 {
1132 // but seems to happen sometimes under wxMSW - maybe it's a bug
1133 // there but for now just ignore it
1134
1135 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1136
1137 return;
1138 }
1139 }
1140
1141 // only start the auto scroll timer if the window can be scrolled in
1142 // this direction
1143 if ( !m_targetWindow->HasScrollbar(orient) )
1144 return;
1145
4b316329 1146#if wxUSE_TIMER
1e6feb95
VZ
1147 delete m_timerAutoScroll;
1148 m_timerAutoScroll = new wxAutoScrollTimer
1149 (
1150 m_targetWindow, this,
1151 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1152 : wxEVT_SCROLLWIN_LINEDOWN,
1153 pos,
1154 orient
1155 );
1156 m_timerAutoScroll->Start(50); // FIXME: make configurable
534b127c
WS
1157#else
1158 wxUnusedVar(pos);
4b316329 1159#endif
1e6feb95
VZ
1160 }
1161}
1162
e421922f
VZ
1163#if wxUSE_MOUSEWHEEL
1164
1e6feb95
VZ
1165void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
1166{
d2c52078 1167 m_wheelRotation += event.GetWheelRotation();
1e6feb95 1168 int lines = m_wheelRotation / event.GetWheelDelta();
d2c52078
RD
1169 m_wheelRotation -= lines * event.GetWheelDelta();
1170
1e6feb95
VZ
1171 if (lines != 0)
1172 {
1e6feb95 1173
b6b85bdc
JS
1174 wxScrollWinEvent newEvent;
1175
ac6f6ffc 1176 newEvent.SetPosition(0);
b6b85bdc 1177 newEvent.SetOrientation(wxVERTICAL);
687706f5 1178 newEvent.SetEventObject(m_win);
b6b85bdc 1179
9b9337da 1180 if (event.IsPageScroll())
4b056ef5
RD
1181 {
1182 if (lines > 0)
687706f5 1183 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
4b056ef5 1184 else
687706f5 1185 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
4b056ef5 1186
b6b85bdc 1187 m_win->GetEventHandler()->ProcessEvent(newEvent);
4b056ef5
RD
1188 }
1189 else
1190 {
1191 lines *= event.GetLinesPerAction();
1192 if (lines > 0)
687706f5 1193 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
4b056ef5 1194 else
687706f5 1195 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
b6b85bdc 1196
4b056ef5
RD
1197 int times = abs(lines);
1198 for (; times > 0; times--)
1199 m_win->GetEventHandler()->ProcessEvent(newEvent);
1200 }
d2c52078
RD
1201 }
1202}
1203
e421922f
VZ
1204#endif // wxUSE_MOUSEWHEEL
1205
1e6feb95
VZ
1206// ----------------------------------------------------------------------------
1207// wxGenericScrolledWindow implementation
1208// ----------------------------------------------------------------------------
1209
1210IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
1211
349efbaa
VZ
1212BEGIN_EVENT_TABLE(wxGenericScrolledWindow, wxPanel)
1213 EVT_PAINT(wxGenericScrolledWindow::OnPaint)
1214END_EVENT_TABLE()
1215
1e6feb95
VZ
1216bool wxGenericScrolledWindow::Create(wxWindow *parent,
1217 wxWindowID id,
1218 const wxPoint& pos,
1219 const wxSize& size,
1220 long style,
1221 const wxString& name)
1222{
1223 m_targetWindow = this;
1224
04b6e2f0 1225 bool ok = wxPanel::Create(parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name);
1e6feb95 1226
1e6feb95
VZ
1227 return ok;
1228}
1229
1230wxGenericScrolledWindow::~wxGenericScrolledWindow()
1231{
1232}
d2c52078 1233
30486297
RD
1234bool wxGenericScrolledWindow::Layout()
1235{
566d84a7 1236 if (GetSizer() && m_targetWindow == this)
30486297 1237 {
566d84a7
RL
1238 // If we're the scroll target, take into account the
1239 // virtual size and scrolled position of the window.
1240
30486297
RD
1241 int x, y, w, h;
1242 CalcScrolledPosition(0,0, &x,&y);
1243 GetVirtualSize(&w, &h);
1244 GetSizer()->SetDimension(x, y, w, h);
ca65c044 1245 return true;
30486297 1246 }
7152f0c6
VZ
1247
1248 // fall back to default for LayoutConstraints
1249 return wxPanel::Layout();
30486297
RD
1250}
1251
2b5f62a0
VZ
1252void wxGenericScrolledWindow::DoSetVirtualSize(int x, int y)
1253{
1254 wxPanel::DoSetVirtualSize( x, y );
1255 AdjustScrollbars();
1256
2b5f62a0
VZ
1257 if (GetAutoLayout())
1258 Layout();
2b5f62a0
VZ
1259}
1260
349efbaa
VZ
1261void wxGenericScrolledWindow::OnPaint(wxPaintEvent& event)
1262{
1263 // the user code didn't really draw the window if we got here, so set this
1264 // flag to try to call OnDraw() later
1265 m_handler->ResetDrawnFlag();
1266
1267 event.Skip();
1268}
1269
0cf5b099 1270#ifdef __WXMSW__
c140b7e7 1271WXLRESULT
0cf5b099
VZ
1272wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg,
1273 WXWPARAM wParam,
1274 WXLPARAM lParam)
1275{
c140b7e7 1276 WXLRESULT rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam);
0cf5b099 1277
4676948b 1278#ifndef __WXWINCE__
0cf5b099
VZ
1279 // we need to process arrows ourselves for scrolling
1280 if ( nMsg == WM_GETDLGCODE )
1281 {
1282 rc |= DLGC_WANTARROWS;
1283 }
4676948b 1284#endif
0cf5b099
VZ
1285
1286 return rc;
1287}
1288
1289#endif // __WXMSW__
1290
3a8c693a
VZ
1291#endif // !wxGTK
1292
566d84a7 1293// vi:sts=4:sw=4:et