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