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