]> git.saurik.com Git - wxWidgets.git/blame - src/generic/scrlwing.cpp
added conversion to mac native filenames from local filesystem handler
[wxWidgets.git] / src / generic / scrlwing.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
d80cd92a 2// Name: generic/scrolwin.cpp
fa3541bd 3// Purpose: wxGenericScrolledWindow implementation
c801d85f 4// Author: Julian Smart
1e6feb95 5// Modified by: Vadim Zeitlin on 31.08.00: wxScrollHelper allows to implement
c801d85f
KB
6// Created: 01/02/97
7// RCS-ID: $Id$
1e6feb95 8// Copyright: (c) wxWindows team
a58a12e9 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
d80cd92a
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f 20#ifdef __GNUG__
3a8c693a 21 #pragma implementation "genscrolwin.h"
c801d85f
KB
22#endif
23
bcd055ae
JJ
24#ifdef __VMS
25#define XtDisplay XTDISPLAY
26#endif
27
c801d85f
KB
28// For compilers that support precompilation, includes "wx.h".
29#include "wx/wxprec.h"
30
c801d85f 31#ifdef __BORLANDC__
d80cd92a 32 #pragma hdrstop
c801d85f
KB
33#endif
34
3a8c693a
VZ
35#if !defined(__WXGTK__) || defined(__WXUNIVERSAL__)
36
d80cd92a
VZ
37#include "wx/utils.h"
38#include "wx/dcclient.h"
39
1e6feb95 40#include "wx/scrolwin.h"
053f9cc1 41#include "wx/panel.h"
1e6feb95 42#include "wx/timer.h"
c801d85f 43
48d1144b 44#ifdef __WXMSW__
1e6feb95 45 #include <windows.h> // for DLGC_WANTARROWS
48d1144b
JS
46#endif
47
a91b47e8
JS
48#ifdef __WXMOTIF__
49// For wxRETAINED implementation
338dd992
JJ
50#ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
51 //This code switches off the compiler warnings
52# pragma message disable nosimpint
53#endif
a91b47e8 54#include <Xm/Xm.h>
338dd992
JJ
55#ifdef __VMS__
56# pragma message enable nosimpint
57#endif
a91b47e8
JS
58#endif
59
fa3541bd 60IMPLEMENT_CLASS(wxScrolledWindow, wxGenericScrolledWindow)
fa3541bd 61
d80cd92a 62// ----------------------------------------------------------------------------
1e6feb95
VZ
63// wxScrollHelperEvtHandler: intercept the events from the window and forward
64// them to wxScrollHelper
d80cd92a
VZ
65// ----------------------------------------------------------------------------
66
349efbaa 67class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler
1e6feb95
VZ
68{
69public:
70 wxScrollHelperEvtHandler(wxScrollHelper *scrollHelper)
71 {
72 m_scrollHelper = scrollHelper;
73 }
d80cd92a 74
1e6feb95
VZ
75 virtual bool ProcessEvent(wxEvent& event);
76
349efbaa
VZ
77 void ResetDrawnFlag() { m_hasDrawnWindow = FALSE; }
78
1e6feb95
VZ
79private:
80 wxScrollHelper *m_scrollHelper;
349efbaa
VZ
81
82 bool m_hasDrawnWindow;
1e6feb95
VZ
83};
84
85// ----------------------------------------------------------------------------
86// wxAutoScrollTimer: the timer used to generate a stream of scroll events when
87// a captured mouse is held outside the window
88// ----------------------------------------------------------------------------
89
90class wxAutoScrollTimer : public wxTimer
91{
92public:
93 wxAutoScrollTimer(wxWindow *winToScroll, wxScrollHelper *scroll,
94 wxEventType eventTypeToSend,
95 int pos, int orient);
96
97 virtual void Notify();
98
99private:
100 wxWindow *m_win;
101 wxScrollHelper *m_scrollHelper;
102 wxEventType m_eventType;
103 int m_pos,
104 m_orient;
105};
d80cd92a
VZ
106
107// ============================================================================
108// implementation
109// ============================================================================
110
111// ----------------------------------------------------------------------------
1e6feb95 112// wxAutoScrollTimer
d80cd92a
VZ
113// ----------------------------------------------------------------------------
114
1e6feb95
VZ
115wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll,
116 wxScrollHelper *scroll,
117 wxEventType eventTypeToSend,
118 int pos, int orient)
c801d85f 119{
1e6feb95
VZ
120 m_win = winToScroll;
121 m_scrollHelper = scroll;
122 m_eventType = eventTypeToSend;
123 m_pos = pos;
124 m_orient = orient;
c801d85f
KB
125}
126
1e6feb95 127void wxAutoScrollTimer::Notify()
c801d85f 128{
1e6feb95
VZ
129 // only do all this as long as the window is capturing the mouse
130 if ( wxWindow::GetCapture() != m_win )
131 {
132 Stop();
133 }
134 else // we still capture the mouse, continue generating events
135 {
136 // first scroll the window if we are allowed to do it
137 wxScrollWinEvent event1(m_eventType, m_pos, m_orient);
138 event1.SetEventObject(m_win);
139 if ( m_scrollHelper->SendAutoScrollEvents(event1) &&
140 m_win->GetEventHandler()->ProcessEvent(event1) )
141 {
142 // and then send a pseudo mouse-move event to refresh the selection
143 wxMouseEvent event2(wxEVT_MOTION);
144 wxGetMousePosition(&event2.m_x, &event2.m_y);
145
146 // the mouse event coordinates should be client, not screen as
147 // returned by wxGetMousePosition
148 wxWindow *parentTop = m_win;
149 while ( parentTop->GetParent() )
150 parentTop = parentTop->GetParent();
151 wxPoint ptOrig = parentTop->GetPosition();
152 event2.m_x -= ptOrig.x;
153 event2.m_y -= ptOrig.y;
154
155 event2.SetEventObject(m_win);
156
157 // FIXME: we don't fill in the other members - ok?
158
159 m_win->GetEventHandler()->ProcessEvent(event2);
160 }
161 else // can't scroll further, stop
162 {
163 Stop();
164 }
165 }
166}
167
168// ----------------------------------------------------------------------------
169// wxScrollHelperEvtHandler
170// ----------------------------------------------------------------------------
171
172bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event)
173{
9a268018 174 wxEventType evType = event.GetEventType();
2feed004 175
349efbaa
VZ
176 // always process the size events ourselves, even if the user code handles
177 // them as well, as we need to AdjustScrollbars()
2feed004 178 if ( evType == wxEVT_SIZE )
9a268018
RR
179 {
180 m_scrollHelper->HandleOnSize((wxSizeEvent &)event);
9a268018 181 }
2feed004 182
349efbaa
VZ
183 // the explanation of wxEVT_PAINT processing hack: for historic reasons
184 // there are 2 ways to process this event in classes deriving from
185 // wxScrolledWindow. The user code may
186 //
187 // 1. override wxScrolledWindow::OnDraw(dc)
188 // 2. define its own OnPaint() handler
189 //
190 // In addition, in wxUniversal wxWindow defines OnPaint() itself and
191 // always processes the draw event, so we can't just try the window
192 // OnPaint() first and call our HandleOnPaint() if it doesn't process it
193 // (the latter would never be called in wxUniversal).
194 //
195 // So the solution is to have a flag telling us whether the user code drew
196 // anything in the window. We set it to true here but reset it to false in
197 // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the
198 // user code defined OnPaint() in the derived class)
199 m_hasDrawnWindow = TRUE;
200
1e6feb95 201 if ( wxEvtHandler::ProcessEvent(event) )
349efbaa
VZ
202 {
203 // normally, nothing more to do here - except if it was a paint event
204 // which wasn't really processed, then we'll try to call our
205 // OnDraw() below (from HandleOnPaint)
206 if ( !m_hasDrawnWindow )
207 {
208 return TRUE;
209 }
210 }
2feed004 211
1e6feb95
VZ
212 // reset the skipped flag to FALSE as it might have been set to TRUE in
213 // ProcessEvent() above
214 event.Skip(FALSE);
215
e421922f
VZ
216 if ( evType == wxEVT_PAINT )
217 {
218 m_scrollHelper->HandleOnPaint((wxPaintEvent &)event);
219 return TRUE;
220 }
1e6feb95 221
e421922f
VZ
222 if ( evType == wxEVT_SCROLLWIN_TOP ||
223 evType == wxEVT_SCROLLWIN_BOTTOM ||
224 evType == wxEVT_SCROLLWIN_LINEUP ||
225 evType == wxEVT_SCROLLWIN_LINEDOWN ||
226 evType == wxEVT_SCROLLWIN_PAGEUP ||
227 evType == wxEVT_SCROLLWIN_PAGEDOWN ||
228 evType == wxEVT_SCROLLWIN_THUMBTRACK ||
229 evType == wxEVT_SCROLLWIN_THUMBRELEASE )
230 {
231 m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event);
1e6feb95 232 return !event.GetSkipped();
e421922f 233 }
1e6feb95 234
e421922f
VZ
235 if ( evType == wxEVT_ENTER_WINDOW )
236 {
237 m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event);
238 }
239 else if ( evType == wxEVT_LEAVE_WINDOW )
240 {
241 m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event);
242 }
243#if wxUSE_MOUSEWHEEL
244 else if ( evType == wxEVT_MOUSEWHEEL )
245 {
246 m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event);
247 }
248#endif // wxUSE_MOUSEWHEEL
e421922f
VZ
249 else if ( evType == wxEVT_CHAR )
250 {
251 m_scrollHelper->HandleOnChar((wxKeyEvent &)event);
252 return !event.GetSkipped();
1e6feb95
VZ
253 }
254
255 return FALSE;
256}
257
258// ----------------------------------------------------------------------------
259// wxScrollHelper construction
260// ----------------------------------------------------------------------------
261
262wxScrollHelper::wxScrollHelper(wxWindow *win)
263{
264 m_xScrollPixelsPerLine =
265 m_yScrollPixelsPerLine =
266 m_xScrollPosition =
267 m_yScrollPosition =
268 m_xScrollLines =
269 m_yScrollLines =
270 m_xScrollLinesPerPage =
139adb6a 271 m_yScrollLinesPerPage = 0;
1e6feb95
VZ
272
273 m_xScrollingEnabled =
274 m_yScrollingEnabled = TRUE;
275
276 m_scaleX =
139adb6a 277 m_scaleY = 1.0;
24ce4c18 278#if wxUSE_MOUSEWHEEL
d2c52078 279 m_wheelRotation = 0;
24ce4c18 280#endif
a58a12e9 281
1e6feb95
VZ
282 m_win =
283 m_targetWindow = (wxWindow *)NULL;
139adb6a 284
1e6feb95 285 m_timerAutoScroll = (wxTimer *)NULL;
d7da9756 286
349efbaa
VZ
287 m_handler = NULL;
288
1e6feb95
VZ
289 if ( win )
290 SetWindow(win);
291}
d7da9756 292
1e6feb95 293wxScrollHelper::~wxScrollHelper()
d80cd92a 294{
1e6feb95
VZ
295 StopAutoScrolling();
296
349efbaa 297 DeleteEvtHandler();
d80cd92a
VZ
298}
299
300// ----------------------------------------------------------------------------
301// setting scrolling parameters
302// ----------------------------------------------------------------------------
303
1e6feb95
VZ
304void wxScrollHelper::SetScrollbars(int pixelsPerUnitX,
305 int pixelsPerUnitY,
306 int noUnitsX,
307 int noUnitsY,
308 int xPos,
309 int yPos,
310 bool noRefresh)
c801d85f 311{
b0486e0d
SN
312 int xpos, ypos;
313
314 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
139adb6a
RR
315 bool do_refresh =
316 (
c801d85f 317 (noUnitsX != 0 && m_xScrollLines == 0) ||
d2c52078 318 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) ||
b0486e0d 319
c801d85f 320 (noUnitsY != 0 && m_yScrollLines == 0) ||
b0486e0d 321 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY*noUnitsY) ||
c801d85f 322 (xPos != m_xScrollPosition) ||
b0486e0d 323 (yPos != m_yScrollPosition)
139adb6a 324 );
a58a12e9 325
139adb6a
RR
326 m_xScrollPixelsPerLine = pixelsPerUnitX;
327 m_yScrollPixelsPerLine = pixelsPerUnitY;
328 m_xScrollPosition = xPos;
329 m_yScrollPosition = yPos;
330 m_xScrollLines = noUnitsX;
331 m_yScrollLines = noUnitsY;
a91b47e8
JS
332
333#ifdef __WXMOTIF__
334 // Sorry, some Motif-specific code to implement a backing pixmap
335 // for the wxRETAINED style. Implementing a backing store can't
336 // be entirely generic because it relies on the wxWindowDC implementation
337 // to duplicate X drawing calls for the backing pixmap.
338
1e6feb95 339 if ( m_targetWindow->GetWindowStyle() & wxRETAINED )
a91b47e8 340 {
1e6feb95 341 Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget());
a91b47e8
JS
342
343 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
344 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
9806a47c
JS
345 if (m_targetWindow->GetBackingPixmap() &&
346 !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) &&
347 (m_targetWindow->GetPixmapHeight() == totalPixelHeight)))
a91b47e8 348 {
9806a47c
JS
349 XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap());
350 m_targetWindow->SetBackingPixmap((WXPixmap) 0);
a91b47e8
JS
351 }
352
9806a47c 353 if (!m_targetWindow->GetBackingPixmap() &&
a91b47e8
JS
354 (noUnitsX != 0) && (noUnitsY != 0))
355 {
356 int depth = wxDisplayDepth();
9806a47c
JS
357 m_targetWindow->SetPixmapWidth(totalPixelWidth);
358 m_targetWindow->SetPixmapHeight(totalPixelHeight);
359 m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
360 m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth));
d80cd92a 361 }
a91b47e8
JS
362
363 }
d80cd92a 364#endif // Motif
a58a12e9 365
139adb6a 366 AdjustScrollbars();
a58a12e9
VZ
367
368 if (do_refresh && !noRefresh)
1e6feb95 369 m_targetWindow->Refresh(TRUE, GetRect());
a58a12e9 370
7c74e7fe 371#ifdef __WXMAC__
d80cd92a 372 m_targetWindow->MacUpdateImmediately() ;
c801d85f
KB
373#endif
374}
375
d80cd92a
VZ
376// ----------------------------------------------------------------------------
377// target window handling
378// ----------------------------------------------------------------------------
ecab4dba 379
349efbaa 380void wxScrollHelper::DeleteEvtHandler()
ecab4dba 381{
349efbaa
VZ
382 // FIXME: we should search for m_handler in the handler list
383 if ( m_targetWindow )
384 {
ac6f6ffc 385 m_targetWindow->PopEventHandler(TRUE /* Delete old event handler*/);
349efbaa
VZ
386 }
387}
388
389void wxScrollHelper::SetWindow(wxWindow *win)
390{
391 wxCHECK_RET( win, _T("wxScrollHelper needs a window to scroll") );
392
393 m_win = win;
394
395 DoSetTargetWindow(win);
396}
397
398void wxScrollHelper::DoSetTargetWindow(wxWindow *target)
399{
ecab4dba 400 m_targetWindow = target;
349efbaa
VZ
401
402 // install the event handler which will intercept the events we're
403 // interested in
404 m_handler = new wxScrollHelperEvtHandler(this);
405 m_targetWindow->PushEventHandler(m_handler);
406}
407
408void wxScrollHelper::SetTargetWindow( wxWindow *target )
409{
410 wxCHECK_RET( target, wxT("target window must not be NULL") );
411
412 if ( target == m_targetWindow )
413 return;
414
415 DeleteEvtHandler();
416
417 DoSetTargetWindow(target);
ecab4dba
RR
418}
419
1e6feb95 420wxWindow *wxScrollHelper::GetTargetWindow() const
ecab4dba
RR
421{
422 return m_targetWindow;
423}
424
d80cd92a
VZ
425// ----------------------------------------------------------------------------
426// scrolling implementation itself
427// ----------------------------------------------------------------------------
428
1e6feb95 429void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event)
c801d85f 430{
139adb6a 431 int nScrollInc = CalcScrollInc(event);
1e6feb95 432 if ( nScrollInc == 0 )
139adb6a 433 {
1e6feb95
VZ
434 // can't scroll further
435 event.Skip();
436
437 return;
139adb6a 438 }
c801d85f 439
1e6feb95 440 int orient = event.GetOrientation();
139adb6a
RR
441 if (orient == wxHORIZONTAL)
442 {
443 m_xScrollPosition += nScrollInc;
a85c1552 444 m_targetWindow->SetScrollPos(wxHORIZONTAL, m_xScrollPosition);
139adb6a 445 }
c801d85f 446 else
139adb6a
RR
447 {
448 m_yScrollPosition += nScrollInc;
a85c1552 449 m_targetWindow->SetScrollPos(wxVERTICAL, m_yScrollPosition);
139adb6a 450 }
a58a12e9 451
1e6feb95
VZ
452 bool needsRefresh = FALSE;
453 int dx = 0,
454 dy = 0;
139adb6a
RR
455 if (orient == wxHORIZONTAL)
456 {
1e6feb95
VZ
457 if ( m_xScrollingEnabled )
458 {
459 dx = -m_xScrollPixelsPerLine * nScrollInc;
460 }
139adb6a 461 else
1e6feb95
VZ
462 {
463 needsRefresh = TRUE;
464 }
139adb6a 465 }
c801d85f 466 else
139adb6a 467 {
1e6feb95
VZ
468 if ( m_yScrollingEnabled )
469 {
470 dy = -m_yScrollPixelsPerLine * nScrollInc;
471 }
139adb6a 472 else
1e6feb95
VZ
473 {
474 needsRefresh = TRUE;
475 }
476 }
477
478 if ( needsRefresh )
479 {
480 m_targetWindow->Refresh(TRUE, GetRect());
481 }
482 else
483 {
484 m_targetWindow->ScrollWindow(dx, dy, GetRect());
3d2b9c20 485 }
1e6feb95 486
7c74e7fe 487#ifdef __WXMAC__
d80cd92a 488 m_targetWindow->MacUpdateImmediately() ;
7c74e7fe 489#endif
c801d85f
KB
490}
491
1e6feb95 492int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event)
c801d85f 493{
ecab4dba
RR
494 int pos = event.GetPosition();
495 int orient = event.GetOrientation();
c801d85f 496
ecab4dba 497 int nScrollInc = 0;
1a8caf94 498 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
c801d85f 499 {
ecab4dba
RR
500 if (orient == wxHORIZONTAL)
501 nScrollInc = - m_xScrollPosition;
502 else
503 nScrollInc = - m_yScrollPosition;
1a8caf94
RR
504 } else
505 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
506 {
ecab4dba
RR
507 if (orient == wxHORIZONTAL)
508 nScrollInc = m_xScrollLines - m_xScrollPosition;
509 else
510 nScrollInc = m_yScrollLines - m_yScrollPosition;
1a8caf94
RR
511 } else
512 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
513 {
ecab4dba 514 nScrollInc = -1;
1a8caf94
RR
515 } else
516 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
517 {
ecab4dba 518 nScrollInc = 1;
1a8caf94
RR
519 } else
520 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
521 {
ecab4dba
RR
522 if (orient == wxHORIZONTAL)
523 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
524 else
525 nScrollInc = -GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
526 } else
527 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
528 {
ecab4dba
RR
529 if (orient == wxHORIZONTAL)
530 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
531 else
532 nScrollInc = GetScrollPageSize(wxVERTICAL);
1a8caf94
RR
533 } else
534 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
535 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
536 {
ecab4dba
RR
537 if (orient == wxHORIZONTAL)
538 nScrollInc = pos - m_xScrollPosition;
539 else
540 nScrollInc = pos - m_yScrollPosition;
c801d85f 541 }
88150e60 542
ecab4dba
RR
543 if (orient == wxHORIZONTAL)
544 {
a58a12e9 545 if (m_xScrollPixelsPerLine > 0)
ecab4dba
RR
546 {
547 int w, h;
1e6feb95 548 GetTargetSize(&w, &h);
ecab4dba
RR
549
550 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
551 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
552 if (noPositions < 0)
d80cd92a 553 noPositions = 0;
ecab4dba
RR
554
555 if ( (m_xScrollPosition + nScrollInc) < 0 )
d80cd92a 556 nScrollInc = -m_xScrollPosition; // As -ve as we can go
ecab4dba 557 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
d80cd92a 558 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
ecab4dba
RR
559 }
560 else
1e6feb95 561 m_targetWindow->Refresh(TRUE, GetRect());
9d9355c6
VZ
562 }
563 else
ecab4dba 564 {
a58a12e9 565 if (m_yScrollPixelsPerLine > 0)
d80cd92a 566 {
ecab4dba 567 int w, h;
1e6feb95 568 GetTargetSize(&w, &h);
a58a12e9 569
ecab4dba
RR
570 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
571 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
572 if (noPositions < 0)
d80cd92a 573 noPositions = 0;
a58a12e9 574
ecab4dba 575 if ( (m_yScrollPosition + nScrollInc) < 0 )
d80cd92a 576 nScrollInc = -m_yScrollPosition; // As -ve as we can go
ecab4dba 577 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
d80cd92a 578 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
ecab4dba
RR
579 }
580 else
1e6feb95 581 m_targetWindow->Refresh(TRUE, GetRect());
9d9355c6 582 }
9d9355c6 583
ecab4dba 584 return nScrollInc;
c801d85f
KB
585}
586
587// Adjust the scrollbars - new version.
1e6feb95 588void wxScrollHelper::AdjustScrollbars()
c801d85f 589{
aeb313f3
SC
590#ifdef __WXMAC__
591 m_targetWindow->MacUpdateImmediately();
592#endif
593
139adb6a 594 int w, h;
1e6feb95 595 GetTargetSize(&w, &h);
a58a12e9 596
27d029c7
RR
597 int oldXScroll = m_xScrollPosition;
598 int oldYScroll = m_yScrollPosition;
c801d85f 599
139adb6a
RR
600 if (m_xScrollLines > 0)
601 {
3d2b9c20
RR
602 // Calculate page size i.e. number of scroll units you get on the
603 // current client window
ecab4dba 604 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a 605 if (noPagePositions < 1) noPagePositions = 1;
1e6feb95
VZ
606 if ( noPagePositions > m_xScrollLines )
607 noPagePositions = m_xScrollLines;
c801d85f 608
139adb6a 609 // Correct position if greater than extent of canvas minus
3d2b9c20
RR
610 // the visible portion of it or if below zero
611 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
139adb6a 612 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
c801d85f 613
1e6feb95 614 m_targetWindow->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
88150e60
JS
615 // The amount by which we scroll when paging
616 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
139adb6a
RR
617 }
618 else
a58a12e9 619 {
139adb6a 620 m_xScrollPosition = 0;
1e6feb95 621 m_targetWindow->SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
139adb6a 622 }
a58a12e9 623
139adb6a
RR
624 if (m_yScrollLines > 0)
625 {
3d2b9c20
RR
626 // Calculate page size i.e. number of scroll units you get on the
627 // current client window
ecab4dba 628 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a 629 if (noPagePositions < 1) noPagePositions = 1;
1e6feb95
VZ
630 if ( noPagePositions > m_yScrollLines )
631 noPagePositions = m_yScrollLines;
c801d85f 632
139adb6a 633 // Correct position if greater than extent of canvas minus
3d2b9c20 634 // the visible portion of it or if below zero
139adb6a
RR
635 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
636 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
637
1e6feb95 638 m_targetWindow->SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
88150e60
JS
639 // The amount by which we scroll when paging
640 SetScrollPageSize(wxVERTICAL, noPagePositions);
139adb6a
RR
641 }
642 else
643 {
644 m_yScrollPosition = 0;
1e6feb95 645 m_targetWindow->SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
139adb6a 646 }
a58a12e9 647
27d029c7
RR
648 if (oldXScroll != m_xScrollPosition)
649 {
650 if (m_xScrollingEnabled)
1e6feb95
VZ
651 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0,
652 GetRect() );
27d029c7 653 else
1e6feb95 654 m_targetWindow->Refresh(TRUE, GetRect());
27d029c7 655 }
a58a12e9 656
27d029c7
RR
657 if (oldYScroll != m_yScrollPosition)
658 {
659 if (m_yScrollingEnabled)
1e6feb95
VZ
660 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition),
661 GetRect() );
27d029c7 662 else
1e6feb95 663 m_targetWindow->Refresh(TRUE, GetRect());
27d029c7 664 }
aeb313f3
SC
665
666#ifdef __WXMAC__
667 m_targetWindow->MacUpdateImmediately();
668#endif
c801d85f
KB
669}
670
1e6feb95 671void wxScrollHelper::DoPrepareDC(wxDC& dc)
c801d85f 672{
1e6feb95
VZ
673 wxPoint pt = dc.GetDeviceOrigin();
674 dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine,
675 pt.y - m_yScrollPosition * m_yScrollPixelsPerLine );
139adb6a 676 dc.SetUserScale( m_scaleX, m_scaleY );
c801d85f
KB
677}
678
1e6feb95 679void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
c801d85f 680{
a0bc2c1d
VZ
681 if ( x_unit )
682 *x_unit = m_xScrollPixelsPerLine;
683 if ( y_unit )
684 *y_unit = m_yScrollPixelsPerLine;
c801d85f
KB
685}
686
1e6feb95 687int wxScrollHelper::GetScrollPageSize(int orient) const
c801d85f
KB
688{
689 if ( orient == wxHORIZONTAL )
690 return m_xScrollLinesPerPage;
691 else
692 return m_yScrollLinesPerPage;
693}
694
1e6feb95 695void wxScrollHelper::SetScrollPageSize(int orient, int pageSize)
c801d85f
KB
696{
697 if ( orient == wxHORIZONTAL )
698 m_xScrollLinesPerPage = pageSize;
699 else
700 m_yScrollLinesPerPage = pageSize;
701}
702
703/*
704 * Scroll to given position (scroll position, not pixel position)
705 */
1e6feb95 706void wxScrollHelper::Scroll( int x_pos, int y_pos )
c801d85f 707{
8b089c5e
JS
708 if (!m_targetWindow)
709 return;
710
a58a12e9 711 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
139adb6a 712 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
a58a12e9 713
aeb313f3
SC
714#ifdef __WXMAC__
715 m_targetWindow->MacUpdateImmediately();
716#endif
717
139adb6a 718 int w, h;
1e6feb95 719 GetTargetSize(&w, &h);
c801d85f 720
8775b357 721 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
c801d85f 722 {
ed673c6a 723 int old_x = m_xScrollPosition;
139adb6a 724 m_xScrollPosition = x_pos;
a58a12e9 725
3d2b9c20
RR
726 // Calculate page size i.e. number of scroll units you get on the
727 // current client window
ecab4dba 728 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
139adb6a
RR
729 if (noPagePositions < 1) noPagePositions = 1;
730
731 // Correct position if greater than extent of canvas minus
3d2b9c20 732 // the visible portion of it or if below zero
139adb6a
RR
733 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
734 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
a58a12e9 735
f6bcfd97 736 if (old_x != m_xScrollPosition) {
a85c1552 737 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition );
1e6feb95
VZ
738 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0,
739 GetRect() );
f6bcfd97 740 }
c801d85f 741 }
8775b357 742 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
c801d85f 743 {
ed673c6a 744 int old_y = m_yScrollPosition;
139adb6a 745 m_yScrollPosition = y_pos;
a58a12e9 746
3d2b9c20
RR
747 // Calculate page size i.e. number of scroll units you get on the
748 // current client window
ecab4dba 749 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
139adb6a
RR
750 if (noPagePositions < 1) noPagePositions = 1;
751
752 // Correct position if greater than extent of canvas minus
3d2b9c20 753 // the visible portion of it or if below zero
139adb6a
RR
754 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
755 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
d2c52078 756
f6bcfd97 757 if (old_y != m_yScrollPosition) {
a85c1552 758 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition );
1e6feb95
VZ
759 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine,
760 GetRect() );
f6bcfd97 761 }
c801d85f 762 }
a58a12e9 763
7c74e7fe 764#ifdef __WXMAC__
3d2b9c20 765 m_targetWindow->MacUpdateImmediately();
7c74e7fe 766#endif
aeb313f3 767
c801d85f
KB
768}
769
1e6feb95 770void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll)
c801d85f 771{
139adb6a
RR
772 m_xScrollingEnabled = x_scroll;
773 m_yScrollingEnabled = y_scroll;
c801d85f
KB
774}
775
1e6feb95 776void wxScrollHelper::GetVirtualSize (int *x, int *y) const
c801d85f 777{
a0bc2c1d
VZ
778 if ( x )
779 *x = m_xScrollPixelsPerLine * m_xScrollLines;
780 if ( y )
781 *y = m_yScrollPixelsPerLine * m_yScrollLines;
c801d85f
KB
782}
783
784// Where the current view starts from
1e6feb95 785void wxScrollHelper::GetViewStart (int *x, int *y) const
c801d85f 786{
a0bc2c1d
VZ
787 if ( x )
788 *x = m_xScrollPosition;
789 if ( y )
790 *y = m_yScrollPosition;
c801d85f
KB
791}
792
1e6feb95 793void wxScrollHelper::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 794{
a0bc2c1d
VZ
795 if ( xx )
796 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
797 if ( yy )
798 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f
KB
799}
800
1e6feb95 801void wxScrollHelper::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
c801d85f 802{
a0bc2c1d
VZ
803 if ( xx )
804 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
805 if ( yy )
806 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
c801d85f 807}
d80cd92a
VZ
808
809// ----------------------------------------------------------------------------
810// event handlers
811// ----------------------------------------------------------------------------
812
813// Default OnSize resets scrollbars, if any
1e6feb95 814void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event))
d80cd92a
VZ
815{
816#if wxUSE_CONSTRAINTS
1e6feb95
VZ
817 if ( m_targetWindow->GetAutoLayout() )
818 m_targetWindow->Layout();
d80cd92a
VZ
819#endif
820
821 AdjustScrollbars();
822}
823
824// This calls OnDraw, having adjusted the origin according to the current
825// scroll position
1e6feb95 826void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event))
d80cd92a 827{
1e6feb95
VZ
828 wxPaintDC dc(m_targetWindow);
829 DoPrepareDC(dc);
d80cd92a
VZ
830
831 OnDraw(dc);
832}
833
438e3558
VZ
834// kbd handling: notice that we use OnChar() and not OnKeyDown() for
835// compatibility here - if we used OnKeyDown(), the programs which process
836// arrows themselves in their OnChar() would never get the message and like
837// this they always have the priority
1e6feb95 838void wxScrollHelper::HandleOnChar(wxKeyEvent& event)
d80cd92a
VZ
839{
840 int stx, sty, // view origin
841 szx, szy, // view size (total)
842 clix, cliy; // view size (on screen)
843
1e6feb95
VZ
844 GetViewStart(&stx, &sty);
845 GetTargetSize(&clix, &cliy);
d80cd92a 846 GetVirtualSize(&szx, &szy);
56dade3c
RL
847
848 if( m_xScrollPixelsPerLine )
849 {
850 clix /= m_xScrollPixelsPerLine;
d7da9756 851 szx /= m_xScrollPixelsPerLine;
56dade3c
RL
852 }
853 else
854 {
855 clix = 0;
d7da9756 856 szx = -1;
56dade3c
RL
857 }
858 if( m_yScrollPixelsPerLine )
859 {
860 cliy /= m_yScrollPixelsPerLine;
861 szy /= m_yScrollPixelsPerLine;
862 }
863 else
864 {
865 cliy = 0;
d7da9756 866 szy = -1;
56dade3c 867 }
d80cd92a 868
39c869a6
VZ
869 int xScrollOld = m_xScrollPosition,
870 yScrollOld = m_yScrollPosition;
871
ecb01792 872 int dsty;
d80cd92a
VZ
873 switch ( event.KeyCode() )
874 {
875 case WXK_PAGEUP:
876 case WXK_PRIOR:
ecb01792
RL
877 dsty = sty - (5 * cliy / 6);
878 Scroll(-1, (dsty == -1) ? 0 : dsty);
d80cd92a
VZ
879 break;
880
881 case WXK_PAGEDOWN:
882 case WXK_NEXT:
883 Scroll(-1, sty + (5 * cliy / 6));
884 break;
885
d80cd92a
VZ
886 case WXK_HOME:
887 Scroll(0, event.ControlDown() ? 0 : -1);
888 break;
889
890 case WXK_END:
4acd108c 891 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
d80cd92a
VZ
892 break;
893
894 case WXK_UP:
895 Scroll(-1, sty - 1);
896 break;
897
898 case WXK_DOWN:
899 Scroll(-1, sty + 1);
900 break;
901
902 case WXK_LEFT:
903 Scroll(stx - 1, -1);
904 break;
905
906 case WXK_RIGHT:
907 Scroll(stx + 1, -1);
908 break;
909
910 default:
911 // not for us
912 event.Skip();
913 }
39c869a6
VZ
914
915 if ( m_xScrollPosition != xScrollOld )
916 {
917 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition,
918 wxHORIZONTAL);
919 event.SetEventObject(m_win);
920 m_win->GetEventHandler()->ProcessEvent(event);
921 }
922
923 if ( m_yScrollPosition != yScrollOld )
924 {
925 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition,
926 wxVERTICAL);
927 event.SetEventObject(m_win);
928 m_win->GetEventHandler()->ProcessEvent(event);
929 }
d80cd92a 930}
d2c52078 931
1e6feb95
VZ
932// ----------------------------------------------------------------------------
933// autoscroll stuff: these functions deal with sending fake scroll events when
934// a captured mouse is being held outside the window
935// ----------------------------------------------------------------------------
936
937bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const
938{
939 // only send the event if the window is scrollable in this direction
940 wxWindow *win = (wxWindow *)event.GetEventObject();
941 return win->HasScrollbar(event.GetOrientation());
942}
943
944void wxScrollHelper::StopAutoScrolling()
945{
946 if ( m_timerAutoScroll )
947 {
948 delete m_timerAutoScroll;
949 m_timerAutoScroll = (wxTimer *)NULL;
950 }
951}
d2c52078 952
1e6feb95 953void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event)
d2c52078 954{
1e6feb95
VZ
955 StopAutoScrolling();
956
957 event.Skip();
958}
d2c52078 959
1e6feb95
VZ
960void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event)
961{
962 // don't prevent the usual processing of the event from taking place
963 event.Skip();
964
965 // when a captured mouse leave a scrolled window we start generate
966 // scrolling events to allow, for example, extending selection beyond the
967 // visible area in some controls
968 if ( wxWindow::GetCapture() == m_targetWindow )
969 {
970 // where is the mouse leaving?
971 int pos, orient;
972 wxPoint pt = event.GetPosition();
973 if ( pt.x < 0 )
974 {
975 orient = wxHORIZONTAL;
976 pos = 0;
977 }
978 else if ( pt.y < 0 )
979 {
980 orient = wxVERTICAL;
981 pos = 0;
982 }
983 else // we're lower or to the right of the window
984 {
985 wxSize size = m_targetWindow->GetClientSize();
986 if ( pt.x > size.x )
987 {
988 orient = wxHORIZONTAL;
989 pos = m_xScrollLines;
990 }
991 else if ( pt.y > size.y )
992 {
993 orient = wxVERTICAL;
994 pos = m_yScrollLines;
995 }
996 else // this should be impossible
997 {
998 // but seems to happen sometimes under wxMSW - maybe it's a bug
999 // there but for now just ignore it
1000
1001 //wxFAIL_MSG( _T("can't understand where has mouse gone") );
1002
1003 return;
1004 }
1005 }
1006
1007 // only start the auto scroll timer if the window can be scrolled in
1008 // this direction
1009 if ( !m_targetWindow->HasScrollbar(orient) )
1010 return;
1011
1012 delete m_timerAutoScroll;
1013 m_timerAutoScroll = new wxAutoScrollTimer
1014 (
1015 m_targetWindow, this,
1016 pos == 0 ? wxEVT_SCROLLWIN_LINEUP
1017 : wxEVT_SCROLLWIN_LINEDOWN,
1018 pos,
1019 orient
1020 );
1021 m_timerAutoScroll->Start(50); // FIXME: make configurable
1022 }
1023}
1024
e421922f
VZ
1025#if wxUSE_MOUSEWHEEL
1026
1e6feb95
VZ
1027void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
1028{
d2c52078 1029 m_wheelRotation += event.GetWheelRotation();
1e6feb95 1030 int lines = m_wheelRotation / event.GetWheelDelta();
d2c52078
RD
1031 m_wheelRotation -= lines * event.GetWheelDelta();
1032
1e6feb95
VZ
1033 if (lines != 0)
1034 {
d2c52078 1035 lines *= event.GetLinesPerAction();
1e6feb95 1036
b6b85bdc
JS
1037 wxScrollWinEvent newEvent;
1038
ac6f6ffc 1039 newEvent.SetPosition(0);
b6b85bdc
JS
1040 newEvent.SetOrientation(wxVERTICAL);
1041 newEvent.m_eventObject = m_win;
1042 if (lines > 0)
1043 newEvent.m_eventType = wxEVT_SCROLLWIN_LINEUP;
1044 else
1045 newEvent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN;
1046
1047 int times = abs(lines);
1048 for (; times > 0; times --)
1049 m_win->GetEventHandler()->ProcessEvent(newEvent);
1050
1051 /* Old Way */
1052 // int vsx, vsy;
1053 // GetViewStart(&vsx, &vsy);
ac6f6ffc 1054 // Scroll(-1, vsy - lines);
d2c52078
RD
1055 }
1056}
1057
e421922f
VZ
1058#endif // wxUSE_MOUSEWHEEL
1059
1e6feb95
VZ
1060// ----------------------------------------------------------------------------
1061// wxGenericScrolledWindow implementation
1062// ----------------------------------------------------------------------------
1063
1064IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel)
1065
349efbaa
VZ
1066BEGIN_EVENT_TABLE(wxGenericScrolledWindow, wxPanel)
1067 EVT_PAINT(wxGenericScrolledWindow::OnPaint)
1068END_EVENT_TABLE()
1069
1e6feb95
VZ
1070bool wxGenericScrolledWindow::Create(wxWindow *parent,
1071 wxWindowID id,
1072 const wxPoint& pos,
1073 const wxSize& size,
1074 long style,
1075 const wxString& name)
1076{
1077 m_targetWindow = this;
1078
1079 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
1080
1081#ifdef __WXMSW__
1082 // we need to process arrows ourselves for scrolling
1083 m_lDlgCode |= DLGC_WANTARROWS;
1084#endif // __WXMSW__
1085
1086 return ok;
1087}
1088
1089wxGenericScrolledWindow::~wxGenericScrolledWindow()
1090{
1091}
d2c52078 1092
349efbaa
VZ
1093void wxGenericScrolledWindow::OnPaint(wxPaintEvent& event)
1094{
1095 // the user code didn't really draw the window if we got here, so set this
1096 // flag to try to call OnDraw() later
1097 m_handler->ResetDrawnFlag();
1098
1099 event.Skip();
1100}
1101
1e6feb95 1102#if WXWIN_COMPATIBILITY
349efbaa 1103
1e6feb95
VZ
1104void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
1105{
1106 *x_page = GetScrollPageSize(wxHORIZONTAL);
1107 *y_page = GetScrollPageSize(wxVERTICAL);
1108}
d2c52078 1109
1e6feb95
VZ
1110void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
1111{
1112 if ( xx )
1113 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
1114 if ( yy )
1115 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
1116}
349efbaa 1117
1e6feb95 1118#endif // WXWIN_COMPATIBILITY
d2c52078 1119
3a8c693a
VZ
1120#endif // !wxGTK
1121