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