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