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