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