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