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