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