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