]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrlwing.cpp
fix wxCHECK_MSG() return value
[wxWidgets.git] / src / generic / scrlwing.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
ed4b0fdc 2// Name: src/generic/scrlwing.cpp
d32e78bd 3// Purpose: wxScrolledWindow implementation
c801d85f 4// Author: Julian Smart
566d84a7
RL
5// Modified by: Vadim Zeitlin on 31.08.00: wxScrollHelper allows to implement.
6// Ron Lee on 10.4.02: virtual size / auto scrollbars et al.
c801d85f
KB
7// Created: 01/02/97
8// RCS-ID: $Id$
77ffb593 9// Copyright: (c) wxWidgets team
65571936 10// Licence: wxWindows licence
c801d85f
KB
11/////////////////////////////////////////////////////////////////////////////
12
d80cd92a
VZ
13// ============================================================================
14// declarations
15// ============================================================================
16
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
c801d85f
KB
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
23
c801d85f 24#ifdef __BORLANDC__
d80cd92a 25 #pragma hdrstop
c801d85f
KB
26#endif
27
de6185e2
WS
28#include "wx/scrolwin.h"
29
30#ifndef WX_PRECOMP
31 #include "wx/utils.h"
8e609c82 32 #include "wx/panel.h"
ed4b0fdc 33 #include "wx/dcclient.h"
7d616e99 34 #include "wx/timer.h"
ed2fbeb8 35 #include "wx/sizer.h"
7d616e99 36 #include "wx/settings.h"
4b316329 37#endif
ed4b0fdc 38
b9dac1ab
JS
39#ifdef __WXMAC__
40#include "wx/scrolbar.h"
41#endif
42
2a201ef8 43#include "wx/recguard.h"
c801d85f 44
48d1144b 45#ifdef __WXMSW__
1e6feb95 46 #include <windows.h> // for DLGC_WANTARROWS
7acf6a92 47 #include "wx/msw/winundef.h"
48d1144b
JS
48#endif
49
a91b47e8
JS
50#ifdef __WXMOTIF__
51// For wxRETAINED implementation
338dd992
JJ
52#ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
53 //This code switches off the compiler warnings
54# pragma message disable nosimpint
55#endif
a91b47e8 56#include <Xm/Xm.h>
338dd992
JJ
57#ifdef __VMS__
58# pragma message enable nosimpint
59#endif
a91b47e8
JS
60#endif
61
066f1b7a 62/*
ca65c044
WS
63 TODO PROPERTIES
64 style wxHSCROLL | wxVSCROLL
066f1b7a
SC
65*/
66
d80cd92a 67// ----------------------------------------------------------------------------
1e6feb95
VZ
68// wxScrollHelperEvtHandler: intercept the events from the window and forward
69// them to wxScrollHelper
d80cd92a
VZ
70// ----------------------------------------------------------------------------
71
349efbaa 72class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler
1e6feb95
VZ
73{
74public:
29e1398f 75 wxScrollHelperEvtHandler(wxScrollHelperBase *scrollHelper)
1e6feb95
VZ
76 {
77 m_scrollHelper = scrollHelper;
78 }
d80cd92a 79
1e6feb95
VZ
80 virtual bool ProcessEvent(wxEvent& event);
81
82private:
29e1398f 83 wxScrollHelperBase *m_scrollHelper;
349efbaa 84
c0c133e1 85 wxDECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler);
1e6feb95
VZ
86};
87
4b316329 88#if wxUSE_TIMER
1e6feb95
VZ
89// ----------------------------------------------------------------------------
90// wxAutoScrollTimer: the timer used to generate a stream of scroll events when
91// a captured mouse is held outside the window
92// ----------------------------------------------------------------------------
93
94class wxAutoScrollTimer : public wxTimer
95{
96public:
29e1398f
VZ
97 wxAutoScrollTimer(wxWindow *winToScroll,
98 wxScrollHelperBase *scroll,
1e6feb95
VZ
99 wxEventType eventTypeToSend,
100 int pos, int orient);
101
102 virtual void Notify();
103
104private:
105 wxWindow *m_win;
29e1398f 106 wxScrollHelperBase *m_scrollHelper;
1e6feb95
VZ
107 wxEventType m_eventType;
108 int m_pos,
109 m_orient;
22f3361e 110
c0c133e1 111 wxDECLARE_NO_COPY_CLASS(wxAutoScrollTimer);
1e6feb95 112};
d80cd92a
VZ
113
114// ============================================================================
115// implementation
116// ============================================================================
117
118// ----------------------------------------------------------------------------
1e6feb95 119// wxAutoScrollTimer
d80cd92a
VZ
120// ----------------------------------------------------------------------------
121
1e6feb95 122wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll,
29e1398f 123 wxScrollHelperBase *scroll,
1e6feb95
VZ
124 wxEventType eventTypeToSend,
125 int pos, int orient)
c801d85f 126{
1e6feb95
VZ
127 m_win = winToScroll;
128 m_scrollHelper = scroll;
129 m_eventType = eventTypeToSend;
130 m_pos = pos;
131 m_orient = orient;
c801d85f
KB
132}
133
1e6feb95 134void wxAutoScrollTimer::Notify()
c801d85f 135{
1e6feb95
VZ
136 // only do all this as long as the window is capturing the mouse
137 if ( wxWindow::GetCapture() != m_win )
138 {
139 Stop();
140 }
141 else // we still capture the mouse, continue generating events
142 {
143 // first scroll the window if we are allowed to do it
144 wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
145 event1.SetEventObject(m_win);
d148c294 146 event1.SetId(m_win->GetId());
1e6feb95
VZ
147 if ( m_scrollHelper->SendAutoScrollEvents(event1) &&
148 m_win->GetEventHandler()->ProcessEvent(event1) )
149 {
150 // and then send a pseudo mouse-move event to refresh the selection
151 wxMouseEvent event2(wxEVT_MOTION);
b19bcdeb 152 event2.SetPosition(wxGetMousePosition());
1e6feb95
VZ
153
154 // the mouse event coordinates should be client, not screen as
155 // returned by wxGetMousePosition
156 wxWindow *parentTop = m_win;
157 while ( parentTop->GetParent() )
158 parentTop = parentTop->GetParent();
159 wxPoint ptOrig = parentTop->GetPosition();
160 event2.m_x -= ptOrig.x;
161 event2.m_y -= ptOrig.y;
162
163 event2.SetEventObject(m_win);
164
52682336
JS
165 wxMouseState mouseState = wxGetMouseState();
166
18ca6665
PC
167 event2.m_leftDown = mouseState.LeftIsDown();
168 event2.m_middleDown = mouseState.MiddleIsDown();
169 event2.m_rightDown = mouseState.RightIsDown();
52682336
JS
170
171 event2.m_shiftDown = mouseState.ShiftDown();
172 event2.m_controlDown = mouseState.ControlDown();
173 event2.m_altDown = mouseState.AltDown();
174 event2.m_metaDown = mouseState.MetaDown();
1e6feb95
VZ
175
176 m_win->GetEventHandler()->ProcessEvent(event2);
177 }
178 else // can't scroll further, stop
179 {
180 Stop();
181 }
182 }
183}
4b316329 184#endif
1e6feb95
VZ
185
186// ----------------------------------------------------------------------------
187// wxScrollHelperEvtHandler
188// ----------------------------------------------------------------------------
189
190bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event)
191{
9a268018 192 wxEventType evType = event.GetEventType();
2feed004 193
52212bcb
VZ
194 // Pass it on to the real handler: notice that we must not call
195 // ProcessEvent() on this object itself as it wouldn't pass it to the next
196 // handler (i.e. the real window) if we're called from a previous handler
197 // (as indicated by "process here only" flag being set) and we do want to
198 // execute the handler defined in the window we're associated with right
199 // now, without waiting until TryAfter() is called from wxEvtHandler.
52212bcb 200 bool processed = m_nextHandler->ProcessEvent(event);
ff186591
VZ
201
202 // always process the size events ourselves, even if the user code handles
203 // them as well, as we need to AdjustScrollbars()
204 //
205 // NB: it is important to do it after processing the event in the normal
206 // way as HandleOnSize() may generate a wxEVT_SIZE itself if the
207 // scrollbar[s] (dis)appear and it should be seen by the user code
208 // after this one
209 if ( evType == wxEVT_SIZE )
210 {
211 m_scrollHelper->HandleOnSize((wxSizeEvent &)event);
ca65c044 212 return true;
ff186591
VZ
213 }
214
4ca30c7d
VZ
215 if ( processed && event.IsCommandEvent())
216 return true;
2feed004 217
4ca30c7d
VZ
218 // For wxEVT_PAINT the user code can either handle this event as usual or
219 // override virtual OnDraw(), so if the event hasn't been handled we need
220 // to call this virtual function ourselves.
221 if (
222#ifndef __WXUNIVERSAL__
223 // in wxUniversal "processed" will always be true, because
224 // all windows use the paint event to draw themselves.
225 // In this case we can't use this flag to determine if a custom
226 // paint event handler already drew our window and we just
227 // call OnDraw() anyway.
228 !processed &&
229#endif // !__WXUNIVERSAL__
230 evType == wxEVT_PAINT )
e421922f
VZ
231 {
232 m_scrollHelper->HandleOnPaint((wxPaintEvent &)event);
ca65c044 233 return true;
e421922f 234 }
1e6feb95 235
06b9c2a2
VS
236 if ( evType == wxEVT_CHILD_FOCUS )
237 {
238 m_scrollHelper->HandleOnChildFocus((wxChildFocusEvent &)event);
239 return true;
240 }
241
35c71386
VZ
242 // reset the skipped flag (which might have been set to true in
243 // ProcessEvent() above) to be able to test it below
244 bool wasSkipped = event.GetSkipped();
245 if ( wasSkipped )
246 event.Skip(false);
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 {
35c71386
VZ
257 m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event);
258 if ( !event.GetSkipped() )
259 {
260 // it makes sense to indicate that we processed the message as we
261 // did scroll the window (and also notice that wxAutoScrollTimer
262 // relies on our return value to stop scrolling when we are at top
263 // or bottom already)
264 processed = true;
265 wasSkipped = false;
266 }
e421922f 267 }
1e6feb95 268
e421922f
VZ
269 if ( evType == wxEVT_ENTER_WINDOW )
270 {
271 m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event);
272 }
273 else if ( evType == wxEVT_LEAVE_WINDOW )
274 {
275 m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event);
276 }
277#if wxUSE_MOUSEWHEEL
602e3365
RR
278 // Use GTK's own scroll wheel handling in GtkScrolledWindow
279#ifndef __WXGTK20__
e421922f
VZ
280 else if ( evType == wxEVT_MOUSEWHEEL )
281 {
282 m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event);
602e3365 283 return true;
e421922f 284 }
602e3365 285#endif
e421922f 286#endif // wxUSE_MOUSEWHEEL
e421922f
VZ
287 else if ( evType == wxEVT_CHAR )
288 {
289 m_scrollHelper->HandleOnChar((wxKeyEvent &)event);
35c71386
VZ
290 if ( !event.GetSkipped() )
291 {
292 processed = true;
293 wasSkipped = false;
294 }
1e6feb95
VZ
295 }
296
d4a58a7c 297 event.Skip(wasSkipped);
35c71386 298
bbdee10d
VZ
299 // We called ProcessEvent() on the next handler, meaning that we explicitly
300 // worked around the request to process the event in this handler only. As
301 // explained above, this is unfortunately really necessary but the trouble
302 // is that the event will continue to be post-processed by the previous
303 // handler resulting in duplicate calls to event handlers. Call the special
304 // function below to prevent this from happening, base class DoTryChain()
305 // will check for it and behave accordingly.
306 //
307 // And if we're not called from DoTryChain(), this won't do anything anyhow.
308 event.DidntHonourProcessOnlyIn();
309
35c71386 310 return processed;
1e6feb95
VZ
311}
312
29e1398f
VZ
313// ============================================================================
314// wxScrollHelperBase implementation
315// ============================================================================
316
1e6feb95 317// ----------------------------------------------------------------------------
29e1398f 318// wxScrollHelperBase construction
1e6feb95
VZ
319// ----------------------------------------------------------------------------
320
29e1398f 321wxScrollHelperBase::wxScrollHelperBase(wxWindow *win)
1e6feb95 322{
9a83f860 323 wxASSERT_MSG( win, wxT("associated window can't be NULL in wxScrollHelper") );
d32e78bd 324
1e6feb95
VZ
325 m_xScrollPixelsPerLine =
326 m_yScrollPixelsPerLine =
327 m_xScrollPosition =
328 m_yScrollPosition =
329 m_xScrollLines =
330 m_yScrollLines =
331 m_xScrollLinesPerPage =
139adb6a 332 m_yScrollLinesPerPage = 0;
1e6feb95
VZ
333
334 m_xScrollingEnabled =
ca65c044 335 m_yScrollingEnabled = true;
1e6feb95 336
d6a658ff
VZ
337 m_kbdScrollingEnabled = true;
338
1e6feb95 339 m_scaleX =
139adb6a 340 m_scaleY = 1.0;
24ce4c18 341#if wxUSE_MOUSEWHEEL
d2c52078 342 m_wheelRotation = 0;
24ce4c18 343#endif
a58a12e9 344
1e6feb95 345 m_win =
29e1398f 346 m_targetWindow = NULL;
139adb6a 347
29e1398f 348 m_timerAutoScroll = NULL;
d7da9756 349
349efbaa
VZ
350 m_handler = NULL;
351
d32e78bd 352 m_win = win;
29e1398f
VZ
353
354 m_win->SetScrollHelper(static_cast<wxScrollHelper *>(this));
d32e78bd
VZ
355
356 // by default, the associated window is also the target window
357 DoSetTargetWindow(win);
1e6feb95 358}
d7da9756 359
29e1398f 360wxScrollHelperBase::~wxScrollHelperBase()
d80cd92a 361{
1e6feb95
VZ
362 StopAutoScrolling();
363
349efbaa 364 DeleteEvtHandler();
d80cd92a
VZ
365}
366
367// ----------------------------------------------------------------------------
368// setting scrolling parameters
369// ----------------------------------------------------------------------------
370
29e1398f
VZ
371void wxScrollHelperBase::SetScrollbars(int pixelsPerUnitX,
372 int pixelsPerUnitY,
373 int noUnitsX,
374 int noUnitsY,
375 int xPos,
376 int yPos,
377 bool noRefresh)
c801d85f 378{
7fa7e46b
VZ
379 // Convert positions expressed in scroll units to positions in pixels.
380 int xPosInPixels = (xPos + m_xScrollPosition)*m_xScrollPixelsPerLine,
381 yPosInPixels = (yPos + m_yScrollPosition)*m_yScrollPixelsPerLine;
b0486e0d 382
139adb6a
RR
383 bool do_refresh =
384 (
c801d85f 385 (noUnitsX != 0 && m_xScrollLines == 0) ||
7fa7e46b 386 (noUnitsX < m_xScrollLines && xPosInPixels > pixelsPerUnitX * noUnitsX) ||
b0486e0d 387
c801d85f 388 (noUnitsY != 0 && m_yScrollLines == 0) ||
7fa7e46b 389 (noUnitsY < m_yScrollLines && yPosInPixels > pixelsPerUnitY * noUnitsY) ||
c801d85f 390 (xPos != m_xScrollPosition) ||
b0486e0d 391 (yPos != m_yScrollPosition)
139adb6a 392 );
a58a12e9 393
139adb6a
RR
394 m_xScrollPixelsPerLine = pixelsPerUnitX;
395 m_yScrollPixelsPerLine = pixelsPerUnitY;
396 m_xScrollPosition = xPos;
397 m_yScrollPosition = yPos;
a91b47e8 398
150c8d89
RL
399 int w = noUnitsX * pixelsPerUnitX;
400 int h = noUnitsY * pixelsPerUnitY;
401
2b5f62a0
VZ
402 // For better backward compatibility we set persisting limits
403 // here not just the size. It makes SetScrollbars 'sticky'
404 // emulating the old non-autoscroll behaviour.
2b43d588 405 // m_targetWindow->SetVirtualSizeHints( w, h );
a58a12e9 406
2b5f62a0
VZ
407 // The above should arguably be deprecated, this however we still need.
408
f18f464c
VZ
409 // take care not to set 0 virtual size, 0 means that we don't have any
410 // scrollbars and hence we should use the real size instead of the virtual
411 // one which is indicated by using wxDefaultCoord
412 m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord,
413 h ? h : wxDefaultCoord);
dc429f89 414
a58a12e9 415 if (do_refresh && !noRefresh)
ca65c044 416 m_targetWindow->Refresh(true, GetScrollRect());
a58a12e9 417
1f066698
VZ
418#ifndef __WXUNIVERSAL__
419 // If the target is not the same as the window with the scrollbars,
420 // then we need to update the scrollbars here, since they won't have
421 // been updated by SetVirtualSize().
422 if ( m_targetWindow != m_win )
423#endif // !__WXUNIVERSAL__
424 {
425 AdjustScrollbars();
426 }
427#ifndef __WXUNIVERSAL__
428 else
429 {
430 // otherwise this has been done by AdjustScrollbars, above
1f066698
VZ
431 }
432#endif // !__WXUNIVERSAL__
c801d85f
KB
433}
434
d80cd92a 435// ----------------------------------------------------------------------------
af4088f1 436// [target] window handling
d80cd92a 437// ----------------------------------------------------------------------------
ecab4dba 438
29e1398f 439void wxScrollHelperBase::DeleteEvtHandler()
ecab4dba 440{
248d771c
VZ
441 // search for m_handler in the handler list
442 if ( m_win && m_handler )
349efbaa 443 {
74f6bbf9 444 if ( m_win->RemoveEventHandler(m_handler) )
248d771c 445 {
74f6bbf9 446 delete m_handler;
248d771c 447 }
74f6bbf9
VZ
448 //else: something is very wrong, so better [maybe] leak memory than
449 // risk a crash because of double deletion
248d771c 450
74f6bbf9 451 m_handler = NULL;
349efbaa
VZ
452 }
453}
454
29e1398f 455void wxScrollHelperBase::DoSetTargetWindow(wxWindow *target)
349efbaa 456{
ecab4dba 457 m_targetWindow = target;
8adc196b
SC
458#ifdef __WXMAC__
459 target->MacSetClipChildren( true ) ;
460#endif
349efbaa
VZ
461
462 // install the event handler which will intercept the events we're
af4088f1
VZ
463 // interested in (but only do it for our real window, not the target window
464 // which we scroll - we don't need to hijack its events)
465 if ( m_targetWindow == m_win )
13ff9344 466 {
248d771c
VZ
467 // if we already have a handler, delete it first
468 DeleteEvtHandler();
469
13ff9344
JS
470 m_handler = new wxScrollHelperEvtHandler(this);
471 m_targetWindow->PushEventHandler(m_handler);
472 }
349efbaa
VZ
473}
474
29e1398f 475void wxScrollHelperBase::SetTargetWindow(wxWindow *target)
349efbaa
VZ
476{
477 wxCHECK_RET( target, wxT("target window must not be NULL") );
478
479 if ( target == m_targetWindow )
480 return;
481
af4088f1 482 DoSetTargetWindow(target);
ecab4dba
RR
483}
484
29e1398f 485wxWindow *wxScrollHelperBase::GetTargetWindow() const
ecab4dba
RR
486{
487 return m_targetWindow;
488}
489
d80cd92a
VZ
490// ----------------------------------------------------------------------------
491// scrolling implementation itself
492// ----------------------------------------------------------------------------
493
29e1398f 494void wxScrollHelperBase::HandleOnScroll(wxScrollWinEvent& event)
c801d85f 495{
139adb6a 496 int nScrollInc = CalcScrollInc(event);
1e6feb95 497 if ( nScrollInc == 0 )
139adb6a 498 {
1e6feb95
VZ
499 // can't scroll further
500 event.Skip();
501
502 return;
139adb6a 503 }
c801d85f 504
ca65c044 505 bool needsRefresh = false;
1e6feb95
VZ
506 int dx = 0,
507 dy = 0;
b97dc9c8 508 int orient = event.GetOrientation();
139adb6a
RR
509 if (orient == wxHORIZONTAL)
510 {
1e6feb95
VZ
511 if ( m_xScrollingEnabled )
512 {
513 dx = -m_xScrollPixelsPerLine * nScrollInc;
514 }
139adb6a 515 else
1e6feb95 516 {
ca65c044 517 needsRefresh = true;
1e6feb95 518 }
139adb6a 519 }
c801d85f 520 else
139adb6a 521 {
1e6feb95
VZ
522 if ( m_yScrollingEnabled )
523 {
524 dy = -m_yScrollPixelsPerLine * nScrollInc;
525 }
139adb6a 526 else
1e6feb95 527 {
ca65c044 528 needsRefresh = true;
1e6feb95
VZ
529 }
530 }
531
b97dc9c8
VS
532 if ( !needsRefresh )
533 {
534 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
535 // otherwise invalidated area could be updated incorrectly later when
536 // ScrollWindow() makes sure they're repainted before scrolling them
93727870
SC
537#ifdef __WXMAC__
538 // wxWindowMac is taking care of making sure the update area is correctly
539 // set up, while not forcing an immediate redraw
540#else
b97dc9c8 541 m_targetWindow->Update();
93727870 542#endif
b97dc9c8
VS
543 }
544
545 if (orient == wxHORIZONTAL)
546 {
547 m_xScrollPosition += nScrollInc;
548 m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
549 }
550 else
551 {
552 m_yScrollPosition += nScrollInc;
553 m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition);
554 }
555
1e6feb95
VZ
556 if ( needsRefresh )
557 {
ca65c044 558 m_targetWindow->Refresh(true, GetScrollRect());
1e6feb95
VZ
559 }
560 else
561 {
d5d58a93 562 m_targetWindow->ScrollWindow(dx, dy, GetScrollRect());
3d2b9c20 563 }
c801d85f
KB
564}
565
29e1398f 566int wxScrollHelperBase::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 567{
ecab4dba
RR
568 int pos = event.GetPosition();
569 int orient = event.GetOrientation();
c801d85f 570
ecab4dba 571 int nScrollInc = 0;
1a8caf94 572 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
c801d85f 573 {
ecab4dba
RR
574 if (orient == wxHORIZONTAL)
575 nScrollInc = - m_xScrollPosition;
576 else
577 nScrollInc = - m_yScrollPosition;
1a8caf94
RR
578 } else
579 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
580 {
ecab4dba
RR
581 if (orient == wxHORIZONTAL)
582 nScrollInc = m_xScrollLines - m_xScrollPosition;
583 else
584 nScrollInc = m_yScrollLines - m_yScrollPosition;
1a8caf94
RR
585 } else
586 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
587 {
ecab4dba 588 nScrollInc = -1;
1a8caf94
RR
589 } else
590 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
591 {
ecab4dba 592 nScrollInc = 1;
1a8caf94
RR
593 } else
594 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
595 {
ecab4dba
RR
596 if (orient == wxHORIZONTAL)
597 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
598 else
599 nScrollInc = -GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
600 } else
601 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
602 {
ecab4dba
RR
603 if (orient == wxHORIZONTAL)
604 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
605 else
606 nScrollInc = GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
607 } else
608 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
609 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
610 {
ecab4dba
RR
611 if (orient == wxHORIZONTAL)
612 nScrollInc = pos - m_xScrollPosition;
613 else
614 nScrollInc = pos - m_yScrollPosition;
c801d85f 615 }
88150e60 616
ecab4dba
RR
617 if (orient == wxHORIZONTAL)
618 {
2e9c0c01 619 if ( m_xScrollPosition + nScrollInc < 0 )
ecab4dba 620 {
2e9c0c01
VZ
621 // As -ve as we can go
622 nScrollInc = -m_xScrollPosition;
623 }
624 else // check for the other bound
625 {
626 const int posMax = m_xScrollLines - m_xScrollLinesPerPage;
627 if ( m_xScrollPosition + nScrollInc > posMax )
878ddad5 628 {
2e9c0c01
VZ
629 // As +ve as we can go
630 nScrollInc = posMax - m_xScrollPosition;
878ddad5 631 }
ecab4dba 632 }
9d9355c6 633 }
2e9c0c01 634 else // wxVERTICAL
ecab4dba 635 {
2e9c0c01 636 if ( m_yScrollPosition + nScrollInc < 0 )
d80cd92a 637 {
2e9c0c01
VZ
638 // As -ve as we can go
639 nScrollInc = -m_yScrollPosition;
ecab4dba 640 }
2e9c0c01 641 else // check for the other bound
be9b0663 642 {
2e9c0c01
VZ
643 const int posMax = m_yScrollLines - m_yScrollLinesPerPage;
644 if ( m_yScrollPosition + nScrollInc > posMax )
645 {
646 // As +ve as we can go
647 nScrollInc = posMax - m_yScrollPosition;
648 }
be9b0663 649 }
9d9355c6 650 }
9d9355c6 651
ecab4dba 652 return nScrollInc;
c801d85f
KB
653}
654
29e1398f 655void wxScrollHelperBase::DoPrepareDC(wxDC& dc)
c801d85f 656{
1e6feb95 657 wxPoint pt = dc.GetDeviceOrigin();
807a572e
RR
658#ifdef __WXGTK__
659 // It may actually be correct to always query
69367c56 660 // the m_sign from the DC here, but I leave the
807a572e
RR
661 // #ifdef GTK for now.
662 if (m_win->GetLayoutDirection() == wxLayout_RightToLeft)
663 dc.SetDeviceOrigin( pt.x + m_xScrollPosition * m_xScrollPixelsPerLine,
664 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
665 else
666#endif
667 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
668 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
139adb6a 669 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
670}
671
29e1398f 672void wxScrollHelperBase::SetScrollRate( int xstep, int ystep )
566d84a7
RL
673{
674 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
675 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
676
677 m_xScrollPixelsPerLine = xstep;
678 m_yScrollPixelsPerLine = ystep;
679
680 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
681 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
682
683 m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
684 m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition );
685 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
686
687 AdjustScrollbars();
688}
689
29e1398f 690void wxScrollHelperBase::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
c801d85f 691{
a0bc2c1d
VZ
692 if ( x_unit )
693 *x_unit = m_xScrollPixelsPerLine;
694 if ( y_unit )
695 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
696}
697
5713b349 698
29e1398f 699int wxScrollHelperBase::GetScrollLines( int orient ) const
5713b349
RR
700{
701 if ( orient == wxHORIZONTAL )
702 return m_xScrollLines;
703 else
704 return m_yScrollLines;
705}
706
29e1398f 707int wxScrollHelperBase::GetScrollPageSize(int orient) const
c801d85f
KB
708{
709 if ( orient == wxHORIZONTAL )
710 return m_xScrollLinesPerPage;
711 else
712 return m_yScrollLinesPerPage;
713}
714
29e1398f 715void wxScrollHelperBase::SetScrollPageSize(int orient, int pageSize)
c801d85f
KB
716{
717 if ( orient == wxHORIZONTAL )
718 m_xScrollLinesPerPage = pageSize;
719 else
720 m_yScrollLinesPerPage = pageSize;
721}
722
29e1398f 723void wxScrollHelperBase::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 724{
29e1398f
VZ
725 m_xScrollingEnabled = x_scroll;
726 m_yScrollingEnabled = y_scroll;
727}
a58a12e9 728
29e1398f
VZ
729// Where the current view starts from
730void wxScrollHelperBase::DoGetViewStart (int *x, int *y) const
731{
732 if ( x )
733 *x = m_xScrollPosition;
734 if ( y )
735 *y = m_yScrollPosition;
736}
c801d85f 737
29e1398f
VZ
738void wxScrollHelperBase::DoCalcScrolledPosition(int x, int y,
739 int *xx, int *yy) const
740{
741 if ( xx )
742 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
743 if ( yy )
744 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
745}
8ea45699 746
29e1398f
VZ
747void wxScrollHelperBase::DoCalcUnscrolledPosition(int x, int y,
748 int *xx, int *yy) const
749{
750 if ( xx )
751 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
752 if ( yy )
753 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
754}
a58a12e9 755
29e1398f
VZ
756// ----------------------------------------------------------------------------
757// geometry
758// ----------------------------------------------------------------------------
139adb6a 759
29e1398f
VZ
760bool wxScrollHelperBase::ScrollLayout()
761{
762 if ( m_win->GetSizer() && m_targetWindow == m_win )
c801d85f 763 {
29e1398f
VZ
764 // If we're the scroll target, take into account the
765 // virtual size and scrolled position of the window.
139adb6a 766
29e1398f
VZ
767 int x = 0, y = 0, w = 0, h = 0;
768 CalcScrolledPosition(0,0, &x,&y);
769 m_win->GetVirtualSize(&w, &h);
770 m_win->GetSizer()->SetDimension(x, y, w, h);
771 return true;
b97dc9c8 772 }
d2c52078 773
29e1398f
VZ
774 // fall back to default for LayoutConstraints
775 return m_win->wxWindow::Layout();
776}
b97dc9c8 777
29e1398f
VZ
778void wxScrollHelperBase::ScrollDoSetVirtualSize(int x, int y)
779{
780 m_win->wxWindow::DoSetVirtualSize( x, y );
781 AdjustScrollbars();
d32e78bd
VZ
782
783 if (m_win->GetAutoLayout())
784 m_win->Layout();
785}
786
787// wxWindow's GetBestVirtualSize returns the actual window size,
788// whereas we want to return the virtual size
29e1398f 789wxSize wxScrollHelperBase::ScrollGetBestVirtualSize() const
d32e78bd
VZ
790{
791 wxSize clientSize(m_win->GetClientSize());
792 if ( m_win->GetSizer() )
793 clientSize.IncTo(m_win->GetSizer()->CalcMin());
794
795 return clientSize;
796}
797
d80cd92a
VZ
798// ----------------------------------------------------------------------------
799// event handlers
800// ----------------------------------------------------------------------------
801
802// Default OnSize resets scrollbars, if any
29e1398f 803void wxScrollHelperBase::HandleOnSize(wxSizeEvent& WXUNUSED(event))
d80cd92a 804{
c376d80f 805 if ( m_targetWindow->GetAutoLayout() )
2b5f62a0 806 {
c376d80f 807 wxSize size = m_targetWindow->GetBestVirtualSize();
d32e78bd 808
c376d80f 809 // This will call ::Layout() and ::AdjustScrollbars()
168f82ce 810 m_win->SetVirtualSize( size );
2b5f62a0
VZ
811 }
812 else
c376d80f 813 {
2b5f62a0 814 AdjustScrollbars();
c376d80f 815 }
d80cd92a
VZ
816}
817
818// This calls OnDraw, having adjusted the origin according to the current
819// scroll position
29e1398f 820void wxScrollHelperBase::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
d80cd92a 821{
af4088f1
VZ
822 // don't use m_targetWindow here, this is always called for ourselves
823 wxPaintDC dc(m_win);
1e6feb95 824 DoPrepareDC(dc);
d80cd92a
VZ
825
826 OnDraw(dc);
827}
828
438e3558
VZ
829// kbd handling: notice that we use OnChar() and not OnKeyDown() for
830// compatibility here - if we used OnKeyDown(), the programs which process
831// arrows themselves in their OnChar() would never get the message and like
832// this they always have the priority
29e1398f 833void wxScrollHelperBase::HandleOnChar(wxKeyEvent& event)
d80cd92a 834{
d6a658ff
VZ
835 if ( !m_kbdScrollingEnabled )
836 {
837 event.Skip();
838 return;
839 }
840
e2495f72
VZ
841 // prepare the event this key press maps to
842 wxScrollWinEvent newEvent;
d80cd92a 843
e2495f72
VZ
844 newEvent.SetPosition(0);
845 newEvent.SetEventObject(m_win);
d148c294 846 newEvent.SetId(m_win->GetId());
56dade3c 847
e2495f72
VZ
848 // this is the default, it's changed to wxHORIZONTAL below if needed
849 newEvent.SetOrientation(wxVERTICAL);
d80cd92a 850
e2495f72
VZ
851 // some key events result in scrolling in both horizontal and vertical
852 // direction, e.g. Ctrl-{Home,End}, if this flag is true we should generate
853 // a second event in horizontal direction in addition to the primary one
854 bool sendHorizontalToo = false;
39c869a6 855
4995ca80 856 switch ( event.GetKeyCode() )
d80cd92a
VZ
857 {
858 case WXK_PAGEUP:
e2495f72 859 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
d80cd92a
VZ
860 break;
861
862 case WXK_PAGEDOWN:
e2495f72 863 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
d80cd92a
VZ
864 break;
865
d80cd92a 866 case WXK_HOME:
e2495f72 867 newEvent.SetEventType(wxEVT_SCROLLWIN_TOP);
d80cd92a 868
e2495f72 869 sendHorizontalToo = event.ControlDown();
d80cd92a
VZ
870 break;
871
e2495f72
VZ
872 case WXK_END:
873 newEvent.SetEventType(wxEVT_SCROLLWIN_BOTTOM);
d80cd92a 874
e2495f72 875 sendHorizontalToo = event.ControlDown();
d80cd92a
VZ
876 break;
877
878 case WXK_LEFT:
e2495f72
VZ
879 newEvent.SetOrientation(wxHORIZONTAL);
880 // fall through
881
882 case WXK_UP:
883 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
d80cd92a
VZ
884 break;
885
886 case WXK_RIGHT:
e2495f72
VZ
887 newEvent.SetOrientation(wxHORIZONTAL);
888 // fall through
889
890 case WXK_DOWN:
891 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
d80cd92a
VZ
892 break;
893
894 default:
e2495f72 895 // not a scrolling key
d80cd92a 896 event.Skip();
e2495f72 897 return;
d80cd92a 898 }
39c869a6 899
e2495f72 900 m_win->ProcessWindowEvent(newEvent);
39c869a6 901
e2495f72 902 if ( sendHorizontalToo )
39c869a6 903 {
e2495f72
VZ
904 newEvent.SetOrientation(wxHORIZONTAL);
905 m_win->ProcessWindowEvent(newEvent);
39c869a6 906 }
d80cd92a 907}
d2c52078 908
1e6feb95
VZ
909// ----------------------------------------------------------------------------
910// autoscroll stuff: these functions deal with sending fake scroll events when
911// a captured mouse is being held outside the window
912// ----------------------------------------------------------------------------
913
29e1398f 914bool wxScrollHelperBase::SendAutoScrollEvents(wxScrollWinEvent& event) const
1e6feb95
VZ
915{
916 // only send the event if the window is scrollable in this direction
917 wxWindow *win = (wxWindow *)event.GetEventObject();
918 return win->HasScrollbar(event.GetOrientation());
919}
920
29e1398f 921void wxScrollHelperBase::StopAutoScrolling()
1e6feb95 922{
4b316329 923#if wxUSE_TIMER
5276b0a5 924 wxDELETE(m_timerAutoScroll);
4b316329 925#endif
1e6feb95 926}
d2c52078 927
29e1398f 928void wxScrollHelperBase::HandleOnMouseEnter(wxMouseEvent& event)
d2c52078 929{
1e6feb95
VZ
930 StopAutoScrolling();
931
932 event.Skip();
933}
d2c52078 934
29e1398f 935void wxScrollHelperBase::HandleOnMouseLeave(wxMouseEvent& event)
1e6feb95
VZ
936{
937 // don't prevent the usual processing of the event from taking place
938 event.Skip();
939
940 // when a captured mouse leave a scrolled window we start generate
941 // scrolling events to allow, for example, extending selection beyond the
942 // visible area in some controls
943 if ( wxWindow::GetCapture() == m_targetWindow )
944 {
945 // where is the mouse leaving?
946 int pos, orient;
947 wxPoint pt = event.GetPosition();
948 if ( pt.x < 0 )
949 {
950 orient = wxHORIZONTAL;
951 pos = 0;
952 }
953 else if ( pt.y < 0 )
954 {
955 orient = wxVERTICAL;
956 pos = 0;
957 }
958 else // we're lower or to the right of the window
959 {
960 wxSize size = m_targetWindow->GetClientSize();
961 if ( pt.x > size.x )
962 {
963 orient = wxHORIZONTAL;
964 pos = m_xScrollLines;
965 }
966 else if ( pt.y > size.y )
967 {
968 orient = wxVERTICAL;
969 pos = m_yScrollLines;
970 }
971 else // this should be impossible
972 {
973 // but seems to happen sometimes under wxMSW - maybe it's a bug
974 // there but for now just ignore it
975
9a83f860 976 //wxFAIL_MSG( wxT("can't understand where has mouse gone") );
1e6feb95
VZ
977
978 return;
979 }
980 }
981
982 // only start the auto scroll timer if the window can be scrolled in
983 // this direction
984 if ( !m_targetWindow->HasScrollbar(orient) )
985 return;
986
4b316329 987#if wxUSE_TIMER
1e6feb95
VZ
988 delete m_timerAutoScroll;
989 m_timerAutoScroll = new wxAutoScrollTimer
990 (
991 m_targetWindow, this,
992 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
993 : wxEVT_SCROLLWIN_LINEDOWN,
994 pos,
995 orient
996 );
997 m_timerAutoScroll->Start(50); // FIXME: make configurable
534b127c
WS
998#else
999 wxUnusedVar(pos);
4b316329 1000#endif
1e6feb95
VZ
1001 }
1002}
1003
e421922f
VZ
1004#if wxUSE_MOUSEWHEEL
1005
29e1398f 1006void wxScrollHelperBase::HandleOnMouseWheel(wxMouseEvent& event)
1e6feb95 1007{
d2c52078 1008 m_wheelRotation += event.GetWheelRotation();
1e6feb95 1009 int lines = m_wheelRotation / event.GetWheelDelta();
d2c52078
RD
1010 m_wheelRotation -= lines * event.GetWheelDelta();
1011
1e6feb95
VZ
1012 if (lines != 0)
1013 {
1e6feb95 1014
b6b85bdc
JS
1015 wxScrollWinEvent newEvent;
1016
ac6f6ffc 1017 newEvent.SetPosition(0);
6682e732 1018 newEvent.SetOrientation( event.GetWheelAxis() == 0 ? wxVERTICAL : wxHORIZONTAL);
687706f5 1019 newEvent.SetEventObject(m_win);
b6b85bdc 1020
9b9337da 1021 if (event.IsPageScroll())
4b056ef5
RD
1022 {
1023 if (lines > 0)
687706f5 1024 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
4b056ef5 1025 else
687706f5 1026 newEvent.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
4b056ef5 1027
b6b85bdc 1028 m_win->GetEventHandler()->ProcessEvent(newEvent);
4b056ef5
RD
1029 }
1030 else
1031 {
1032 lines *= event.GetLinesPerAction();
1033 if (lines > 0)
687706f5 1034 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEUP);
4b056ef5 1035 else
687706f5 1036 newEvent.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
b6b85bdc 1037
4b056ef5
RD
1038 int times = abs(lines);
1039 for (; times > 0; times--)
1040 m_win->GetEventHandler()->ProcessEvent(newEvent);
1041 }
d2c52078
RD
1042 }
1043}
1044
e421922f
VZ
1045#endif // wxUSE_MOUSEWHEEL
1046
29e1398f 1047void wxScrollHelperBase::HandleOnChildFocus(wxChildFocusEvent& event)
06b9c2a2
VS
1048{
1049 // this event should be processed by all windows in parenthood chain,
1050 // e.g. so that nested wxScrolledWindows work correctly
1051 event.Skip();
1052
1053 // find the immediate child under which the window receiving focus is:
1054 wxWindow *win = event.GetWindow();
02a33891
VS
1055
1056 if ( win == m_targetWindow )
1057 return; // nothing to do
1058
2e1517a2 1059#if defined( __WXOSX__ ) && wxUSE_SCROLLBAR
380e1c0c
JS
1060 if (wxDynamicCast(win, wxScrollBar))
1061 return;
1062#endif
1063
3ec704b4
RD
1064 // Fixing ticket: http://trac.wxwidgets.org/ticket/9563
1065 // When a child inside a wxControlContainer receives a focus, the
1066 // wxControlContainer generates an artificial wxChildFocusEvent for
1067 // itself, telling its parent that 'it' received the focus. The effect is
1068 // that this->HandleOnChildFocus is called twice, first with the
1069 // artificial wxChildFocusEvent and then with the original event. We need
1070 // to ignore the artificial event here or otherwise HandleOnChildFocus
1071 // would first scroll the target window to make the entire
1072 // wxControlContainer visible and immediately afterwards scroll the target
1073 // window again to make the child widget visible. This leads to ugly
1074 // flickering when using nested wxPanels/wxScrolledWindows.
1075 //
1076 // Ignore this event if 'win' is derived from wxControlContainer AND its
1077 // parent is the m_targetWindow AND 'win' is not actually reciving the
1078 // focus (win != FindFocus). TODO: This affects all wxControlContainer
1079 // objects, but wxControlContainer is not part of the wxWidgets RTTI and
1080 // so wxDynamicCast(win, wxControlContainer) does not compile. Find a way
1081 // to determine if 'win' derives from wxControlContainer. Until then,
1082 // testing if 'win' derives from wxPanel will probably get >90% of all
1083 // cases.
1084
1085 wxWindow *actual_focus=wxWindow::FindFocus();
1086 if (win != actual_focus &&
1087 wxDynamicCast(win, wxPanel) != 0 &&
1088 win->GetParent() == m_targetWindow)
1089 // if win is a wxPanel and receives the focus, it should not be
1090 // scrolled into view
29e1398f 1091 return;
3ec704b4 1092
888e9638 1093 const wxRect viewRect(m_targetWindow->GetClientRect());
3ec704b4
RD
1094
1095 // For composite controls such as wxComboCtrl we should try to fit the
1096 // entire control inside the visible area of the target window, not just
1097 // the focused child of the control. Otherwise we'd make only the textctrl
1098 // part of a wxComboCtrl visible and the button would still be outside the
1099 // scrolled area. But do so only if the parent fits *entirely* inside the
1100 // scrolled window. In other situations, such as nested wxPanel or
d13b34d3 1101 // wxScrolledWindows, the parent might be way too big to fit inside the
3ec704b4
RD
1102 // scrolled window. If that is the case, then make only the focused window
1103 // visible
1104 if ( win->GetParent() != m_targetWindow)
06b9c2a2 1105 {
3ec704b4
RD
1106 wxWindow *parent=win->GetParent();
1107 wxSize parent_size=parent->GetSize();
888e9638
VZ
1108 if (parent_size.GetWidth() <= viewRect.GetWidth() &&
1109 parent_size.GetHeight() <= viewRect.GetHeight())
3ec704b4 1110 // make the immediate parent visible instead of the focused control
29e1398f 1111 win=parent;
06b9c2a2
VS
1112 }
1113
888e9638
VZ
1114 // make win position relative to the m_targetWindow viewing area instead of
1115 // its parent
1116 const wxRect
1117 winRect(m_targetWindow->ScreenToClient(win->GetScreenPosition()),
1118 win->GetSize());
1119
1120 // check if it's fully visible
1121 if ( viewRect.Contains(winRect) )
1122 {
1123 // it is, nothing to do
1124 return;
1125 }
1126
1127 // check if we can make it fully visible: this is only possible if it's not
1128 // larger than our view area
1129 if ( winRect.GetWidth() > viewRect.GetWidth() ||
1130 winRect.GetHeight() > viewRect.GetHeight() )
1131 {
1132 // we can't make it fit so avoid scrolling it at all, this is only
1133 // going to be confusing and not helpful
1134 return;
1135 }
1136
1137
1138 // do make the window fit inside the view area by scrolling to it
06b9c2a2
VS
1139 int stepx, stepy;
1140 GetScrollPixelsPerUnit(&stepx, &stepy);
1141
06b9c2a2
VS
1142 int startx, starty;
1143 GetViewStart(&startx, &starty);
1144
1145 // first in vertical direction:
1146 if ( stepy > 0 )
1147 {
1148 int diff = 0;
1149
888e9638 1150 if ( winRect.GetTop() < 0 )
06b9c2a2 1151 {
888e9638 1152 diff = winRect.GetTop();
06b9c2a2 1153 }
888e9638 1154 else if ( winRect.GetBottom() > viewRect.GetHeight() )
06b9c2a2 1155 {
888e9638 1156 diff = winRect.GetBottom() - viewRect.GetHeight() + 1;
06b9c2a2
VS
1157 // round up to next scroll step if we can't get exact position,
1158 // so that the window is fully visible:
1159 diff += stepy - 1;
1160 }
1161
1162 starty = (starty * stepy + diff) / stepy;
1163 }
1164
1165 // then horizontal:
1166 if ( stepx > 0 )
1167 {
1168 int diff = 0;
1169
888e9638 1170 if ( winRect.GetLeft() < 0 )
06b9c2a2 1171 {
888e9638 1172 diff = winRect.GetLeft();
06b9c2a2 1173 }
888e9638 1174 else if ( winRect.GetRight() > viewRect.GetWidth() )
06b9c2a2 1175 {
888e9638 1176 diff = winRect.GetRight() - viewRect.GetWidth() + 1;
06b9c2a2
VS
1177 // round up to next scroll step if we can't get exact position,
1178 // so that the window is fully visible:
1179 diff += stepx - 1;
1180 }
1181
1182 startx = (startx * stepx + diff) / stepx;
1183 }
1184
1185 Scroll(startx, starty);
1186}
1187
29e1398f
VZ
1188
1189#ifdef wxHAS_GENERIC_SCROLLWIN
1190
1191// ----------------------------------------------------------------------------
1192// wxScrollHelper implementation
1193// ----------------------------------------------------------------------------
1194
1195wxScrollHelper::wxScrollHelper(wxWindow *winToScroll)
1196 : wxScrollHelperBase(winToScroll)
1197{
1198 m_xVisibility =
1199 m_yVisibility = wxSHOW_SB_DEFAULT;
1200}
1201
1202void wxScrollHelper::DoShowScrollbars(wxScrollbarVisibility horz,
1203 wxScrollbarVisibility vert)
1204{
1205 if ( horz != m_xVisibility || vert != m_yVisibility )
1206 {
1207 m_xVisibility = horz;
1208 m_yVisibility = vert;
1209
1210 AdjustScrollbars();
1211 }
1212}
1213
1214void
1215wxScrollHelper::DoAdjustScrollbar(int orient,
1216 int clientSize,
1217 int virtSize,
1ddb6d28 1218 int pixelsPerUnit,
29e1398f
VZ
1219 int& scrollUnits,
1220 int& scrollPosition,
1ddb6d28 1221 int& scrollLinesPerPage,
29e1398f
VZ
1222 wxScrollbarVisibility visibility)
1223{
29e1398f 1224 // scroll lines per page: if 0, no scrolling is needed
29e1398f 1225 // check if we need scrollbar in this direction at all
1ddb6d28 1226 if ( pixelsPerUnit == 0 || clientSize >= virtSize )
29e1398f
VZ
1227 {
1228 // scrolling is disabled or unnecessary
1229 scrollUnits =
1230 scrollPosition = 0;
1ddb6d28 1231 scrollLinesPerPage = 0;
29e1398f
VZ
1232 }
1233 else // might need scrolling
1234 {
1235 // Round up integer division to catch any "leftover" client space.
1236 scrollUnits = (virtSize + pixelsPerUnit - 1) / pixelsPerUnit;
1237
1238 // Calculate the number of fully scroll units
1ddb6d28 1239 scrollLinesPerPage = clientSize / pixelsPerUnit;
29e1398f 1240
1ddb6d28 1241 if ( scrollLinesPerPage >= scrollUnits )
29e1398f
VZ
1242 {
1243 // we're big enough to not need scrolling
1244 scrollUnits =
1245 scrollPosition = 0;
1ddb6d28 1246 scrollLinesPerPage = 0;
29e1398f
VZ
1247 }
1248 else // we do need a scrollbar
1249 {
1ddb6d28
VZ
1250 if ( scrollLinesPerPage < 1 )
1251 scrollLinesPerPage = 1;
29e1398f
VZ
1252
1253 // Correct position if greater than extent of canvas minus
1254 // the visible portion of it or if below zero
1ddb6d28 1255 const int posMax = scrollUnits - scrollLinesPerPage;
29e1398f
VZ
1256 if ( scrollPosition > posMax )
1257 scrollPosition = posMax;
1258 else if ( scrollPosition < 0 )
1259 scrollPosition = 0;
1260 }
1261 }
1262
1ddb6d28
VZ
1263 // in wxSHOW_SB_NEVER case don't show the scrollbar even if it's needed, in
1264 // wxSHOW_SB_ALWAYS case show the scrollbar even if it's not needed by
1265 // passing a special range value to SetScrollbar()
36e5a9a7 1266 int range;
1ddb6d28
VZ
1267 switch ( visibility )
1268 {
1269 case wxSHOW_SB_NEVER:
1270 range = 0;
1271 break;
1272
36e5a9a7
VZ
1273 case wxSHOW_SB_ALWAYS:
1274 range = scrollUnits ? scrollUnits : -1;
1275 break;
1276
1277 default:
1278 wxFAIL_MSG( wxS("unknown scrollbar visibility") );
1279 // fall through
1280
1ddb6d28
VZ
1281 case wxSHOW_SB_DEFAULT:
1282 range = scrollUnits;
1283 break;
1284
1ddb6d28 1285 }
29e1398f 1286
1ddb6d28 1287 m_win->SetScrollbar(orient, scrollPosition, scrollLinesPerPage, range);
29e1398f
VZ
1288}
1289
1290void wxScrollHelper::AdjustScrollbars()
1291{
1292 static wxRecursionGuardFlag s_flagReentrancy;
1293 wxRecursionGuard guard(s_flagReentrancy);
1294 if ( guard.IsInside() )
1295 {
1296 // don't reenter AdjustScrollbars() while another call to
1297 // AdjustScrollbars() is in progress because this may lead to calling
1298 // ScrollWindow() twice and this can really happen under MSW if
1299 // SetScrollbar() call below adds or removes the scrollbar which
1300 // changes the window size and hence results in another
1301 // AdjustScrollbars() call
1302 return;
1303 }
1304
1305 int oldXScroll = m_xScrollPosition;
1306 int oldYScroll = m_yScrollPosition;
1307
1308 // we may need to readjust the scrollbars several times as enabling one of
1309 // them reduces the area available for the window contents and so can make
1310 // the other scrollbar necessary now although it wasn't necessary before
1311 //
1312 // VZ: normally this loop should be over in at most 2 iterations, I don't
1313 // know why do we need 5 of them
1314 for ( int iterationCount = 0; iterationCount < 5; iterationCount++ )
1315 {
1316 wxSize clientSize = GetTargetSize();
1317 const wxSize virtSize = m_targetWindow->GetVirtualSize();
1318
1319 // this block of code tries to work around the following problem: the
1320 // window could have been just resized to have enough space to show its
1321 // full contents without the scrollbars, but its client size could be
1322 // not big enough because it does have the scrollbars right now and so
1323 // the scrollbars would remain even though we don't need them any more
1324 //
1325 // to prevent this from happening, check if we have enough space for
1326 // everything without the scrollbars and explicitly disable them then
1327 const wxSize availSize = GetSizeAvailableForScrollTarget(
1328 m_win->GetSize() - m_win->GetWindowBorderSize());
1329 if ( availSize != clientSize )
1330 {
1331 if ( availSize.x >= virtSize.x && availSize.y >= virtSize.y )
1332 {
1333 // this will be enough to make the scrollbars disappear below
1334 // and then the client size will indeed become equal to the
1335 // full available size
1336 clientSize = availSize;
1337 }
1338 }
1339
1340
1341 DoAdjustScrollbar(wxHORIZONTAL,
1342 clientSize.x,
1343 virtSize.x,
1344 m_xScrollPixelsPerLine,
1345 m_xScrollLines,
1346 m_xScrollPosition,
1ddb6d28 1347 m_xScrollLinesPerPage,
29e1398f
VZ
1348 m_xVisibility);
1349
1350 DoAdjustScrollbar(wxVERTICAL,
1351 clientSize.y,
1352 virtSize.y,
1353 m_yScrollPixelsPerLine,
1354 m_yScrollLines,
1355 m_yScrollPosition,
1ddb6d28 1356 m_yScrollLinesPerPage,
29e1398f
VZ
1357 m_yVisibility);
1358
1359
1360 // If a scrollbar (dis)appeared as a result of this, we need to adjust
1361 // them again but if the client size didn't change, then we're done
1362 if ( GetTargetSize() == clientSize )
1363 break;
1364 }
1365
1366#ifdef __WXMOTIF__
1367 // Sorry, some Motif-specific code to implement a backing pixmap
1368 // for the wxRETAINED style. Implementing a backing store can't
1369 // be entirely generic because it relies on the wxWindowDC implementation
1370 // to duplicate X drawing calls for the backing pixmap.
1371
1372 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
1373 {
1374 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
1375
1376 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
1377 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
1378 if (m_targetWindow->GetBackingPixmap() &&
1379 !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
1380 (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
1381 {
1382 XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
1383 m_targetWindow->SetBackingPixmap((WXPixmap) 0);
1384 }
1385
1386 if (!m_targetWindow->GetBackingPixmap() &&
1387 (m_xScrollLines != 0) && (m_yScrollLines != 0))
1388 {
1389 int depth = wxDisplayDepth();
1390 m_targetWindow->SetPixmapWidth(totalPixelWidth);
1391 m_targetWindow->SetPixmapHeight(totalPixelHeight);
1392 m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
1393 m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth));
1394 }
1395
1396 }
1397#endif // Motif
1398
1399 if (oldXScroll != m_xScrollPosition)
1400 {
1401 if (m_xScrollingEnabled)
1402 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0,
1403 GetScrollRect() );
1404 else
1405 m_targetWindow->Refresh(true, GetScrollRect());
1406 }
1407
1408 if (oldYScroll != m_yScrollPosition)
1409 {
1410 if (m_yScrollingEnabled)
1411 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
1412 GetScrollRect() );
1413 else
1414 m_targetWindow->Refresh(true, GetScrollRect());
1415 }
1416}
1417
1418void wxScrollHelper::DoScroll( int x_pos, int y_pos )
1419{
1420 if (!m_targetWindow)
1421 return;
1422
1423 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
1424 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
1425
1426 int w = 0, h = 0;
1427 GetTargetSize(&w, &h);
1428
1429 // compute new position:
1430 int new_x = m_xScrollPosition;
1431 int new_y = m_yScrollPosition;
1432
1433 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
1434 {
1435 new_x = x_pos;
1436
1437 // Calculate page size i.e. number of scroll units you get on the
1438 // current client window
1439 int noPagePositions = w/m_xScrollPixelsPerLine;
1440 if (noPagePositions < 1) noPagePositions = 1;
1441
1442 // Correct position if greater than extent of canvas minus
1443 // the visible portion of it or if below zero
1444 new_x = wxMin( m_xScrollLines-noPagePositions, new_x );
1445 new_x = wxMax( 0, new_x );
1446 }
1447 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
1448 {
1449 new_y = y_pos;
1450
1451 // Calculate page size i.e. number of scroll units you get on the
1452 // current client window
1453 int noPagePositions = h/m_yScrollPixelsPerLine;
1454 if (noPagePositions < 1) noPagePositions = 1;
1455
1456 // Correct position if greater than extent of canvas minus
1457 // the visible portion of it or if below zero
1458 new_y = wxMin( m_yScrollLines-noPagePositions, new_y );
1459 new_y = wxMax( 0, new_y );
1460 }
1461
1462 if ( new_x == m_xScrollPosition && new_y == m_yScrollPosition )
1463 return; // nothing to do, the position didn't change
1464
1465 // flush all pending repaints before we change m_{x,y}ScrollPosition, as
1466 // otherwise invalidated area could be updated incorrectly later when
1467 // ScrollWindow() makes sure they're repainted before scrolling them
1468 m_targetWindow->Update();
1469
1470 // update the position and scroll the window now:
1471 if (m_xScrollPosition != new_x)
1472 {
1473 int old_x = m_xScrollPosition;
1474 m_xScrollPosition = new_x;
1475 m_win->SetScrollPos( wxHORIZONTAL, new_x );
1476 m_targetWindow->ScrollWindow( (old_x-new_x)*m_xScrollPixelsPerLine, 0,
1477 GetScrollRect() );
1478 }
1479
1480 if (m_yScrollPosition != new_y)
1481 {
1482 int old_y = m_yScrollPosition;
1483 m_yScrollPosition = new_y;
1484 m_win->SetScrollPos( wxVERTICAL, new_y );
1485 m_targetWindow->ScrollWindow( 0, (old_y-new_y)*m_yScrollPixelsPerLine,
1486 GetScrollRect() );
1487 }
1488}
1489
1490#endif // wxHAS_GENERIC_SCROLLWIN
1491
1e6feb95 1492// ----------------------------------------------------------------------------
16361ec9 1493// wxScrolled<T> and wxScrolledWindow implementation
1e6feb95
VZ
1494// ----------------------------------------------------------------------------
1495
16361ec9 1496wxSize wxScrolledT_Helper::FilterBestSize(const wxWindow *win,
29e1398f 1497 const wxScrollHelper *helper,
16361ec9 1498 const wxSize& origBest)
7d616e99
VS
1499{
1500 // NB: We don't do this in WX_FORWARD_TO_SCROLL_HELPER, because not
1501 // all scrollable windows should behave like this, only those that
1502 // contain children controls within scrollable area
1503 // (i.e., wxScrolledWindow) and other some scrollable windows may
1504 // have different DoGetBestSize() implementation (e.g. wxTreeCtrl).
1505
16361ec9 1506 wxSize best = origBest;
7d616e99 1507
16361ec9 1508 if ( win->GetAutoLayout() )
7d616e99
VS
1509 {
1510 // Only use the content to set the window size in the direction
1511 // where there's no scrolling; otherwise we're going to get a huge
1512 // window in the direction in which scrolling is enabled
1513 int ppuX, ppuY;
16361ec9 1514 helper->GetScrollPixelsPerUnit(&ppuX, &ppuY);
7d616e99
VS
1515
1516 // NB: This code used to use *current* size if min size wasn't
1517 // specified, presumably to get some reasonable (i.e., larger than
1518 // minimal) size. But that's a wrong thing to do in GetBestSize(),
1519 // so we use minimal size as specified. If the app needs some
1520 // minimal size for its scrolled window, it should set it and put
1521 // the window into sizer as expandable so that it can use all space
1522 // available to it.
1523 //
1524 // See also http://svn.wxwidgets.org/viewvc/wx?view=rev&revision=45864
1525
16361ec9 1526 wxSize minSize = win->GetMinSize();
7d616e99
VS
1527
1528 if ( ppuX > 0 )
1529 best.x = minSize.x + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1530
1531 if ( ppuY > 0 )
1532 best.y = minSize.y + wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
1533 }
1534
1535 return best;
1536}
1537
0cf5b099 1538#ifdef __WXMSW__
f2a6c918 1539WXLRESULT wxScrolledT_Helper::FilterMSWWindowProc(WXUINT nMsg, WXLRESULT rc)
0cf5b099 1540{
4676948b 1541#ifndef __WXWINCE__
0cf5b099
VZ
1542 // we need to process arrows ourselves for scrolling
1543 if ( nMsg == WM_GETDLGCODE )
1544 {
1545 rc |= DLGC_WANTARROWS;
1546 }
4676948b 1547#endif
0cf5b099
VZ
1548 return rc;
1549}
0cf5b099 1550#endif // __WXMSW__
16361ec9
VS
1551
1552// NB: skipping wxScrolled<T> in wxRTTI information because being a templte,
1553// it doesn't and can't implement wxRTTI support
1554IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)