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