]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrlwing.cpp
When drawing a solid page background, don't use theme painting
[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{
c376d80f 966 if ( m_targetWindow->GetAutoLayout() )
2b5f62a0 967 {
c376d80f
RR
968 wxSize size = m_targetWindow->GetBestVirtualSize();
969
970 // This will call ::Layout() and ::AdjustScrollbars()
168f82ce 971 m_win->SetVirtualSize( size );
2b5f62a0
VZ
972 }
973 else
c376d80f 974 {
2b5f62a0 975 AdjustScrollbars();
c376d80f 976 }
d80cd92a
VZ
977}
978
979// This calls OnDraw, having adjusted the origin according to the current
980// scroll position
1e6feb95 981void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
d80cd92a 982{
af4088f1
VZ
983 // don't use m_targetWindow here, this is always called for ourselves
984 wxPaintDC dc(m_win);
1e6feb95 985 DoPrepareDC(dc);
d80cd92a
VZ
986
987 OnDraw(dc);
988}
989
438e3558
VZ
990// kbd handling: notice that we use OnChar() and not OnKeyDown() for
991// compatibility here - if we used OnKeyDown(), the programs which process
992// arrows themselves in their OnChar() would never get the message and like
993// this they always have the priority
1e6feb95 994void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
d80cd92a
VZ
995{
996 int stx, sty, // view origin
997 szx, szy, // view size (total)
998 clix, cliy; // view size (on screen)
999
1e6feb95
VZ
1000 GetViewStart(&stx, &sty);
1001 GetTargetSize(&clix, &cliy);
566d84a7 1002 m_targetWindow->GetVirtualSize(&szx, &szy);
56dade3c
RL
1003
1004 if( m_xScrollPixelsPerLine )
1005 {
1006 clix /= m_xScrollPixelsPerLine;
d7da9756 1007 szx /= m_xScrollPixelsPerLine;
56dade3c
RL
1008 }
1009 else
1010 {
1011 clix = 0;
d7da9756 1012 szx = -1;
56dade3c
RL
1013 }
1014 if( m_yScrollPixelsPerLine )
1015 {
1016 cliy /= m_yScrollPixelsPerLine;
1017 szy /= m_yScrollPixelsPerLine;
1018 }
1019 else
1020 {
1021 cliy = 0;
d7da9756 1022 szy = -1;
56dade3c 1023 }
d80cd92a 1024
39c869a6
VZ
1025 int xScrollOld = m_xScrollPosition,
1026 yScrollOld = m_yScrollPosition;
1027
ecb01792 1028 int dsty;
4995ca80 1029 switch ( event.GetKeyCode() )
d80cd92a
VZ
1030 {
1031 case WXK_PAGEUP:
1032 case WXK_PRIOR:
ecb01792
RL
1033 dsty = sty - (5 * cliy / 6);
1034 Scroll(-1, (dsty == -1) ? 0 : dsty);
d80cd92a
VZ
1035 break;
1036
1037 case WXK_PAGEDOWN:
1038 case WXK_NEXT:
1039 Scroll(-1, sty + (5 * cliy / 6));
1040 break;
1041
d80cd92a
VZ
1042 case WXK_HOME:
1043 Scroll(0, event.ControlDown() ? 0 : -1);
1044 break;
1045
1046 case WXK_END:
4acd108c 1047 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
d80cd92a
VZ
1048 break;
1049
1050 case WXK_UP:
1051 Scroll(-1, sty - 1);
1052 break;
1053
1054 case WXK_DOWN:
1055 Scroll(-1, sty + 1);
1056 break;
1057
1058 case WXK_LEFT:
1059 Scroll(stx - 1, -1);
1060 break;
1061
1062 case WXK_RIGHT:
1063 Scroll(stx + 1, -1);
1064 break;
1065
1066 default:
1067 // not for us
1068 event.Skip();
1069 }
39c869a6
VZ
1070
1071 if ( m_xScrollPosition != xScrollOld )
1072 {
1073 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
1074 wxHORIZONTAL);
1075 event.SetEventObject(m_win);
1076 m_win->GetEventHandler()->ProcessEvent(event);
1077 }
1078
1079 if ( m_yScrollPosition != yScrollOld )
1080 {
1081 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
1082 wxVERTICAL);
1083 event.SetEventObject(m_win);
1084 m_win->GetEventHandler()->ProcessEvent(event);
1085 }
d80cd92a 1086}
d2c52078 1087
1e6feb95
VZ
1088// ----------------------------------------------------------------------------
1089// autoscroll stuff: these functions deal with sending fake scroll events when
1090// a captured mouse is being held outside the window
1091// ----------------------------------------------------------------------------
1092
1093bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
1094{
1095 // only send the event if the window is scrollable in this direction
1096 wxWindow *win = (wxWindow *)event.GetEventObject();
1097 return win->HasScrollbar(event.GetOrientation());
1098}
1099
1100void wxScrollHelper::StopAutoScrolling()
1101{
4b316329 1102#if wxUSE_TIMER
1e6feb95
VZ
1103 if ( m_timerAutoScroll )
1104 {
1105 delete m_timerAutoScroll;
1106 m_timerAutoScroll = (wxTimer *)NULL;
1107 }
4b316329 1108#endif
1e6feb95 1109}
d2c52078 1110
1e6feb95 1111void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
d2c52078 1112{
1e6feb95
VZ
1113 StopAutoScrolling();
1114
1115 event.Skip();
1116}
d2c52078 1117
1e6feb95
VZ
1118void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
1119{
1120 // don't prevent the usual processing of the event from taking place
1121 event.Skip();
1122
1123 // when a captured mouse leave a scrolled window we start generate
1124 // scrolling events to allow, for example, extending selection beyond the
1125 // visible area in some controls
1126 if ( wxWindow::GetCapture() == m_targetWindow )
1127 {
1128 // where is the mouse leaving?
1129 int pos, orient;
1130 wxPoint pt = event.GetPosition();
1131 if ( pt.x < 0 )
1132 {
1133 orient = wxHORIZONTAL;
1134 pos = 0;
1135 }
1136 else if ( pt.y < 0 )
1137 {
1138 orient = wxVERTICAL;
1139 pos = 0;
1140 }
1141 else // we're lower or to the right of the window
1142 {
1143 wxSize size = m_targetWindow->GetClientSize();
1144 if ( pt.x > size.x )
1145 {
1146 orient = wxHORIZONTAL;
1147 pos = m_xScrollLines;
1148 }
1149 else if ( pt.y > size.y )
1150 {
1151 orient = wxVERTICAL;
1152 pos = m_yScrollLines;
1153 }
1154 else // this should be impossible
1155 {
1156 // but seems to happen sometimes under wxMSW - maybe it's a bug
1157 // there but for now just ignore it
1158
1159 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1160
1161 return;
1162 }
1163 }
1164
1165 // only start the auto scroll timer if the window can be scrolled in
1166 // this direction
1167 if ( !m_targetWindow->HasScrollbar(orient) )
1168 return;
1169
4b316329 1170#if wxUSE_TIMER
1e6feb95
VZ
1171 delete m_timerAutoScroll;
1172 m_timerAutoScroll = new wxAutoScrollTimer
1173 (
1174 m_targetWindow, this,
1175 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1176 : wxEVT_SCROLLWIN_LINEDOWN,
1177 pos,
1178 orient
1179 );
1180 m_timerAutoScroll->Start(50); // FIXME: make configurable
534b127c
WS
1181#else
1182 wxUnusedVar(pos);
4b316329 1183#endif
1e6feb95
VZ
1184 }
1185}
1186
e421922f
VZ
1187#if wxUSE_MOUSEWHEEL
1188
1e6feb95
VZ
1189void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
1190{
d2c52078 1191 m_wheelRotation += event.GetWheelRotation();
1e6feb95 1192 int lines = m_wheelRotation / event.GetWheelDelta();
d2c52078
RD
1193 m_wheelRotation -= lines * event.GetWheelDelta();
1194
1e6feb95
VZ
1195 if (lines != 0)
1196 {
1e6feb95 1197
b6b85bdc
JS
1198 wxScrollWinEvent newEvent;
1199
ac6f6ffc 1200 newEvent.SetPosition(0);
b6b85bdc 1201 newEvent.SetOrientation(wxVERTICAL);
687706f5 1202 newEvent.SetEventObject(m_win);
b6b85bdc 1203
9b9337da 1204 if (event.IsPageScroll())
4b056ef5
RD
1205 {
1206 if (lines > 0)
687706f5 1207 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
4b056ef5 1208 else
687706f5 1209 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
4b056ef5 1210
b6b85bdc 1211 m_win->GetEventHandler()->ProcessEvent(newEvent);
4b056ef5
RD
1212 }
1213 else
1214 {
1215 lines *= event.GetLinesPerAction();
1216 if (lines > 0)
687706f5 1217 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
4b056ef5 1218 else
687706f5 1219 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
b6b85bdc 1220
4b056ef5
RD
1221 int times = abs(lines);
1222 for (; times > 0; times--)
1223 m_win->GetEventHandler()->ProcessEvent(newEvent);
1224 }
d2c52078
RD
1225 }
1226}
1227
e421922f
VZ
1228#endif // wxUSE_MOUSEWHEEL
1229
1e6feb95
VZ
1230// ----------------------------------------------------------------------------
1231// wxGenericScrolledWindow implementation
1232// ----------------------------------------------------------------------------
1233
1234IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
1235
349efbaa
VZ
1236BEGIN_EVENT_TABLE(wxGenericScrolledWindow, wxPanel)
1237 EVT_PAINT(wxGenericScrolledWindow::OnPaint)
1238END_EVENT_TABLE()
1239
1e6feb95
VZ
1240bool wxGenericScrolledWindow::Create(wxWindow *parent,
1241 wxWindowID id,
1242 const wxPoint& pos,
1243 const wxSize& size,
1244 long style,
1245 const wxString& name)
1246{
1247 m_targetWindow = this;
1248
04b6e2f0 1249 bool ok = wxPanel::Create(parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name);
1e6feb95 1250
1e6feb95
VZ
1251 return ok;
1252}
1253
1254wxGenericScrolledWindow::~wxGenericScrolledWindow()
1255{
1256}
d2c52078 1257
30486297
RD
1258bool wxGenericScrolledWindow::Layout()
1259{
566d84a7 1260 if (GetSizer() && m_targetWindow == this)
30486297 1261 {
566d84a7
RL
1262 // If we're the scroll target, take into account the
1263 // virtual size and scrolled position of the window.
1264
30486297
RD
1265 int x, y, w, h;
1266 CalcScrolledPosition(0,0, &x,&y);
1267 GetVirtualSize(&w, &h);
1268 GetSizer()->SetDimension(x, y, w, h);
ca65c044 1269 return true;
30486297 1270 }
7152f0c6
VZ
1271
1272 // fall back to default for LayoutConstraints
1273 return wxPanel::Layout();
30486297
RD
1274}
1275
2b5f62a0
VZ
1276void wxGenericScrolledWindow::DoSetVirtualSize(int x, int y)
1277{
1278 wxPanel::DoSetVirtualSize( x, y );
1279 AdjustScrollbars();
1280
2b5f62a0
VZ
1281 if (GetAutoLayout())
1282 Layout();
2b5f62a0
VZ
1283}
1284
349efbaa
VZ
1285void wxGenericScrolledWindow::OnPaint(wxPaintEvent& event)
1286{
1287 // the user code didn't really draw the window if we got here, so set this
1288 // flag to try to call OnDraw() later
1289 m_handler->ResetDrawnFlag();
1290
1291 event.Skip();
1292}
1293
0cf5b099 1294#ifdef __WXMSW__
c140b7e7 1295WXLRESULT
0cf5b099
VZ
1296wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg,
1297 WXWPARAM wParam,
1298 WXLPARAM lParam)
1299{
c140b7e7 1300 WXLRESULT rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam);
0cf5b099 1301
4676948b 1302#ifndef __WXWINCE__
0cf5b099
VZ
1303 // we need to process arrows ourselves for scrolling
1304 if ( nMsg == WM_GETDLGCODE )
1305 {
1306 rc |= DLGC_WANTARROWS;
1307 }
4676948b 1308#endif
0cf5b099
VZ
1309
1310 return rc;
1311}
1312
1313#endif // __WXMSW__
1314
3a8c693a
VZ
1315#endif // !wxGTK
1316
566d84a7 1317// vi:sts=4:sw=4:et