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