]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
d80cd92a | 2 | // Name: generic/scrolwin.cpp |
fa3541bd | 3 | // Purpose: wxGenericScrolledWindow implementation |
c801d85f | 4 | // Author: Julian Smart |
566d84a7 RL |
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. | |
c801d85f KB |
7 | // Created: 01/02/97 |
8 | // RCS-ID: $Id$ | |
1e6feb95 | 9 | // Copyright: (c) wxWindows team |
6aa89a22 | 10 | // Licence: wxWindows licence |
c801d85f KB |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
d80cd92a VZ |
13 | // ============================================================================ |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
14f355c2 | 21 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
3a8c693a | 22 | #pragma implementation "genscrolwin.h" |
c801d85f KB |
23 | #endif |
24 | ||
bcd055ae JJ |
25 | #ifdef __VMS |
26 | #define XtDisplay XTDISPLAY | |
27 | #endif | |
28 | ||
c801d85f KB |
29 | // For compilers that support precompilation, includes "wx.h". |
30 | #include "wx/wxprec.h" | |
31 | ||
c801d85f | 32 | #ifdef __BORLANDC__ |
d80cd92a | 33 | #pragma hdrstop |
c801d85f KB |
34 | #endif |
35 | ||
3a8c693a VZ |
36 | #if !defined(__WXGTK__) || defined(__WXUNIVERSAL__) |
37 | ||
d80cd92a VZ |
38 | #include "wx/utils.h" |
39 | #include "wx/dcclient.h" | |
40 | ||
1e6feb95 | 41 | #include "wx/scrolwin.h" |
053f9cc1 | 42 | #include "wx/panel.h" |
4b316329 | 43 | #if wxUSE_TIMER |
1e6feb95 | 44 | #include "wx/timer.h" |
4b316329 | 45 | #endif |
95d60b24 | 46 | #include "wx/sizer.h" |
c801d85f | 47 | |
48d1144b | 48 | #ifdef __WXMSW__ |
1e6feb95 | 49 | #include <windows.h> // for DLGC_WANTARROWS |
48d1144b JS |
50 | #endif |
51 | ||
a91b47e8 JS |
52 | #ifdef __WXMOTIF__ |
53 | // For wxRETAINED implementation | |
338dd992 JJ |
54 | #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++ |
55 | //This code switches off the compiler warnings | |
56 | # pragma message disable nosimpint | |
57 | #endif | |
a91b47e8 | 58 | #include <Xm/Xm.h> |
338dd992 JJ |
59 | #ifdef __VMS__ |
60 | # pragma message enable nosimpint | |
61 | #endif | |
a91b47e8 JS |
62 | #endif |
63 | ||
fa3541bd | 64 | IMPLEMENT_CLASS(wxScrolledWindow, wxGenericScrolledWindow) |
fa3541bd | 65 | |
066f1b7a SC |
66 | /* |
67 | TODO PROPERTIES | |
68 | style wxHSCROLL | wxVSCROLL | |
69 | */ | |
70 | ||
d80cd92a | 71 | // ---------------------------------------------------------------------------- |
1e6feb95 VZ |
72 | // wxScrollHelperEvtHandler: intercept the events from the window and forward |
73 | // them to wxScrollHelper | |
d80cd92a VZ |
74 | // ---------------------------------------------------------------------------- |
75 | ||
349efbaa | 76 | class WXDLLEXPORT wxScrollHelperEvtHandler : public wxEvtHandler |
1e6feb95 VZ |
77 | { |
78 | public: | |
79 | wxScrollHelperEvtHandler(wxScrollHelper *scrollHelper) | |
80 | { | |
81 | m_scrollHelper = scrollHelper; | |
82 | } | |
d80cd92a | 83 | |
1e6feb95 VZ |
84 | virtual bool ProcessEvent(wxEvent& event); |
85 | ||
349efbaa VZ |
86 | void ResetDrawnFlag() { m_hasDrawnWindow = FALSE; } |
87 | ||
1e6feb95 VZ |
88 | private: |
89 | wxScrollHelper *m_scrollHelper; | |
349efbaa VZ |
90 | |
91 | bool m_hasDrawnWindow; | |
22f3361e VZ |
92 | |
93 | DECLARE_NO_COPY_CLASS(wxScrollHelperEvtHandler) | |
1e6feb95 VZ |
94 | }; |
95 | ||
4b316329 | 96 | #if wxUSE_TIMER |
1e6feb95 VZ |
97 | // ---------------------------------------------------------------------------- |
98 | // wxAutoScrollTimer: the timer used to generate a stream of scroll events when | |
99 | // a captured mouse is held outside the window | |
100 | // ---------------------------------------------------------------------------- | |
101 | ||
102 | class wxAutoScrollTimer : public wxTimer | |
103 | { | |
104 | public: | |
105 | wxAutoScrollTimer(wxWindow *winToScroll, wxScrollHelper *scroll, | |
106 | wxEventType eventTypeToSend, | |
107 | int pos, int orient); | |
108 | ||
109 | virtual void Notify(); | |
110 | ||
111 | private: | |
112 | wxWindow *m_win; | |
113 | wxScrollHelper *m_scrollHelper; | |
114 | wxEventType m_eventType; | |
115 | int m_pos, | |
116 | m_orient; | |
22f3361e VZ |
117 | |
118 | DECLARE_NO_COPY_CLASS(wxAutoScrollTimer) | |
1e6feb95 | 119 | }; |
d80cd92a VZ |
120 | |
121 | // ============================================================================ | |
122 | // implementation | |
123 | // ============================================================================ | |
124 | ||
125 | // ---------------------------------------------------------------------------- | |
1e6feb95 | 126 | // wxAutoScrollTimer |
d80cd92a VZ |
127 | // ---------------------------------------------------------------------------- |
128 | ||
1e6feb95 VZ |
129 | wxAutoScrollTimer::wxAutoScrollTimer(wxWindow *winToScroll, |
130 | wxScrollHelper *scroll, | |
131 | wxEventType eventTypeToSend, | |
132 | int pos, int orient) | |
c801d85f | 133 | { |
1e6feb95 VZ |
134 | m_win = winToScroll; |
135 | m_scrollHelper = scroll; | |
136 | m_eventType = eventTypeToSend; | |
137 | m_pos = pos; | |
138 | m_orient = orient; | |
c801d85f KB |
139 | } |
140 | ||
1e6feb95 | 141 | void wxAutoScrollTimer::Notify() |
c801d85f | 142 | { |
1e6feb95 VZ |
143 | // only do all this as long as the window is capturing the mouse |
144 | if ( wxWindow::GetCapture() != m_win ) | |
145 | { | |
146 | Stop(); | |
147 | } | |
148 | else // we still capture the mouse, continue generating events | |
149 | { | |
150 | // first scroll the window if we are allowed to do it | |
151 | wxScrollWinEvent event1(m_eventType, m_pos, m_orient); | |
152 | event1.SetEventObject(m_win); | |
153 | if ( m_scrollHelper->SendAutoScrollEvents(event1) && | |
154 | m_win->GetEventHandler()->ProcessEvent(event1) ) | |
155 | { | |
156 | // and then send a pseudo mouse-move event to refresh the selection | |
157 | wxMouseEvent event2(wxEVT_MOTION); | |
158 | wxGetMousePosition(&event2.m_x, &event2.m_y); | |
159 | ||
160 | // the mouse event coordinates should be client, not screen as | |
161 | // returned by wxGetMousePosition | |
162 | wxWindow *parentTop = m_win; | |
163 | while ( parentTop->GetParent() ) | |
164 | parentTop = parentTop->GetParent(); | |
165 | wxPoint ptOrig = parentTop->GetPosition(); | |
166 | event2.m_x -= ptOrig.x; | |
167 | event2.m_y -= ptOrig.y; | |
168 | ||
169 | event2.SetEventObject(m_win); | |
170 | ||
171 | // FIXME: we don't fill in the other members - ok? | |
172 | ||
173 | m_win->GetEventHandler()->ProcessEvent(event2); | |
174 | } | |
175 | else // can't scroll further, stop | |
176 | { | |
177 | Stop(); | |
178 | } | |
179 | } | |
180 | } | |
4b316329 | 181 | #endif |
1e6feb95 VZ |
182 | |
183 | // ---------------------------------------------------------------------------- | |
184 | // wxScrollHelperEvtHandler | |
185 | // ---------------------------------------------------------------------------- | |
186 | ||
187 | bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event) | |
188 | { | |
9a268018 | 189 | wxEventType evType = event.GetEventType(); |
2feed004 | 190 | |
349efbaa VZ |
191 | // the explanation of wxEVT_PAINT processing hack: for historic reasons |
192 | // there are 2 ways to process this event in classes deriving from | |
193 | // wxScrolledWindow. The user code may | |
194 | // | |
195 | // 1. override wxScrolledWindow::OnDraw(dc) | |
196 | // 2. define its own OnPaint() handler | |
197 | // | |
198 | // In addition, in wxUniversal wxWindow defines OnPaint() itself and | |
199 | // always processes the draw event, so we can't just try the window | |
200 | // OnPaint() first and call our HandleOnPaint() if it doesn't process it | |
201 | // (the latter would never be called in wxUniversal). | |
202 | // | |
203 | // So the solution is to have a flag telling us whether the user code drew | |
204 | // anything in the window. We set it to true here but reset it to false in | |
205 | // wxScrolledWindow::OnPaint() handler (which wouldn't be called if the | |
206 | // user code defined OnPaint() in the derived class) | |
207 | m_hasDrawnWindow = TRUE; | |
208 | ||
ff186591 VZ |
209 | // pass it on to the real handler |
210 | bool processed = wxEvtHandler::ProcessEvent(event); | |
211 | ||
212 | // always process the size events ourselves, even if the user code handles | |
213 | // them as well, as we need to AdjustScrollbars() | |
214 | // | |
215 | // NB: it is important to do it after processing the event in the normal | |
216 | // way as HandleOnSize() may generate a wxEVT_SIZE itself if the | |
217 | // scrollbar[s] (dis)appear and it should be seen by the user code | |
218 | // after this one | |
219 | if ( evType == wxEVT_SIZE ) | |
220 | { | |
221 | m_scrollHelper->HandleOnSize((wxSizeEvent &)event); | |
222 | ||
223 | return TRUE; | |
224 | } | |
225 | ||
226 | if ( processed ) | |
349efbaa VZ |
227 | { |
228 | // normally, nothing more to do here - except if it was a paint event | |
229 | // which wasn't really processed, then we'll try to call our | |
230 | // OnDraw() below (from HandleOnPaint) | |
993cfa87 | 231 | if ( m_hasDrawnWindow ) |
349efbaa VZ |
232 | { |
233 | return TRUE; | |
234 | } | |
235 | } | |
2feed004 | 236 | |
1e6feb95 VZ |
237 | // reset the skipped flag to FALSE as it might have been set to TRUE in |
238 | // ProcessEvent() above | |
239 | event.Skip(FALSE); | |
240 | ||
e421922f VZ |
241 | if ( evType == wxEVT_PAINT ) |
242 | { | |
243 | m_scrollHelper->HandleOnPaint((wxPaintEvent &)event); | |
244 | return TRUE; | |
245 | } | |
1e6feb95 | 246 | |
e421922f VZ |
247 | if ( evType == wxEVT_SCROLLWIN_TOP || |
248 | evType == wxEVT_SCROLLWIN_BOTTOM || | |
249 | evType == wxEVT_SCROLLWIN_LINEUP || | |
250 | evType == wxEVT_SCROLLWIN_LINEDOWN || | |
251 | evType == wxEVT_SCROLLWIN_PAGEUP || | |
252 | evType == wxEVT_SCROLLWIN_PAGEDOWN || | |
253 | evType == wxEVT_SCROLLWIN_THUMBTRACK || | |
254 | evType == wxEVT_SCROLLWIN_THUMBRELEASE ) | |
255 | { | |
256 | m_scrollHelper->HandleOnScroll((wxScrollWinEvent &)event); | |
1e6feb95 | 257 | return !event.GetSkipped(); |
e421922f | 258 | } |
1e6feb95 | 259 | |
e421922f VZ |
260 | if ( evType == wxEVT_ENTER_WINDOW ) |
261 | { | |
262 | m_scrollHelper->HandleOnMouseEnter((wxMouseEvent &)event); | |
263 | } | |
264 | else if ( evType == wxEVT_LEAVE_WINDOW ) | |
265 | { | |
266 | m_scrollHelper->HandleOnMouseLeave((wxMouseEvent &)event); | |
267 | } | |
268 | #if wxUSE_MOUSEWHEEL | |
269 | else if ( evType == wxEVT_MOUSEWHEEL ) | |
270 | { | |
271 | m_scrollHelper->HandleOnMouseWheel((wxMouseEvent &)event); | |
272 | } | |
273 | #endif // wxUSE_MOUSEWHEEL | |
e421922f VZ |
274 | else if ( evType == wxEVT_CHAR ) |
275 | { | |
276 | m_scrollHelper->HandleOnChar((wxKeyEvent &)event); | |
277 | return !event.GetSkipped(); | |
1e6feb95 VZ |
278 | } |
279 | ||
280 | return FALSE; | |
281 | } | |
282 | ||
283 | // ---------------------------------------------------------------------------- | |
284 | // wxScrollHelper construction | |
285 | // ---------------------------------------------------------------------------- | |
286 | ||
287 | wxScrollHelper::wxScrollHelper(wxWindow *win) | |
288 | { | |
289 | m_xScrollPixelsPerLine = | |
290 | m_yScrollPixelsPerLine = | |
291 | m_xScrollPosition = | |
292 | m_yScrollPosition = | |
293 | m_xScrollLines = | |
294 | m_yScrollLines = | |
295 | m_xScrollLinesPerPage = | |
139adb6a | 296 | m_yScrollLinesPerPage = 0; |
1e6feb95 VZ |
297 | |
298 | m_xScrollingEnabled = | |
299 | m_yScrollingEnabled = TRUE; | |
300 | ||
301 | m_scaleX = | |
139adb6a | 302 | m_scaleY = 1.0; |
24ce4c18 | 303 | #if wxUSE_MOUSEWHEEL |
d2c52078 | 304 | m_wheelRotation = 0; |
24ce4c18 | 305 | #endif |
a58a12e9 | 306 | |
1e6feb95 VZ |
307 | m_win = |
308 | m_targetWindow = (wxWindow *)NULL; | |
139adb6a | 309 | |
1e6feb95 | 310 | m_timerAutoScroll = (wxTimer *)NULL; |
d7da9756 | 311 | |
349efbaa VZ |
312 | m_handler = NULL; |
313 | ||
1e6feb95 VZ |
314 | if ( win ) |
315 | SetWindow(win); | |
316 | } | |
d7da9756 | 317 | |
1e6feb95 | 318 | wxScrollHelper::~wxScrollHelper() |
d80cd92a | 319 | { |
1e6feb95 VZ |
320 | StopAutoScrolling(); |
321 | ||
349efbaa | 322 | DeleteEvtHandler(); |
d80cd92a VZ |
323 | } |
324 | ||
325 | // ---------------------------------------------------------------------------- | |
326 | // setting scrolling parameters | |
327 | // ---------------------------------------------------------------------------- | |
328 | ||
1e6feb95 VZ |
329 | void wxScrollHelper::SetScrollbars(int pixelsPerUnitX, |
330 | int pixelsPerUnitY, | |
331 | int noUnitsX, | |
332 | int noUnitsY, | |
333 | int xPos, | |
334 | int yPos, | |
335 | bool noRefresh) | |
c801d85f | 336 | { |
b0486e0d SN |
337 | int xpos, ypos; |
338 | ||
339 | CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos); | |
139adb6a RR |
340 | bool do_refresh = |
341 | ( | |
c801d85f | 342 | (noUnitsX != 0 && m_xScrollLines == 0) || |
566d84a7 | 343 | (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX * noUnitsX) || |
b0486e0d | 344 | |
c801d85f | 345 | (noUnitsY != 0 && m_yScrollLines == 0) || |
566d84a7 | 346 | (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY * noUnitsY) || |
c801d85f | 347 | (xPos != m_xScrollPosition) || |
b0486e0d | 348 | (yPos != m_yScrollPosition) |
139adb6a | 349 | ); |
a58a12e9 | 350 | |
139adb6a RR |
351 | m_xScrollPixelsPerLine = pixelsPerUnitX; |
352 | m_yScrollPixelsPerLine = pixelsPerUnitY; | |
353 | m_xScrollPosition = xPos; | |
354 | m_yScrollPosition = yPos; | |
a91b47e8 | 355 | |
150c8d89 RL |
356 | int w = noUnitsX * pixelsPerUnitX; |
357 | int h = noUnitsY * pixelsPerUnitY; | |
358 | ||
2b5f62a0 VZ |
359 | // For better backward compatibility we set persisting limits |
360 | // here not just the size. It makes SetScrollbars 'sticky' | |
361 | // emulating the old non-autoscroll behaviour. | |
362 | ||
150c8d89 | 363 | m_targetWindow->SetVirtualSizeHints( w, h ); |
a58a12e9 | 364 | |
2b5f62a0 VZ |
365 | // The above should arguably be deprecated, this however we still need. |
366 | ||
150c8d89 | 367 | m_targetWindow->SetVirtualSize( w, h ); |
dc429f89 | 368 | |
a58a12e9 | 369 | if (do_refresh && !noRefresh) |
d5d58a93 | 370 | m_targetWindow->Refresh(TRUE, GetScrollRect()); |
a58a12e9 | 371 | |
1f066698 VZ |
372 | #ifndef __WXUNIVERSAL__ |
373 | // If the target is not the same as the window with the scrollbars, | |
374 | // then we need to update the scrollbars here, since they won't have | |
375 | // been updated by SetVirtualSize(). | |
376 | if ( m_targetWindow != m_win ) | |
377 | #endif // !__WXUNIVERSAL__ | |
378 | { | |
379 | AdjustScrollbars(); | |
380 | } | |
381 | #ifndef __WXUNIVERSAL__ | |
382 | else | |
383 | { | |
384 | // otherwise this has been done by AdjustScrollbars, above | |
7c74e7fe | 385 | #ifdef __WXMAC__ |
1f066698 | 386 | m_targetWindow->Update() ; |
03d1ae17 | 387 | #endif |
1f066698 VZ |
388 | } |
389 | #endif // !__WXUNIVERSAL__ | |
c801d85f KB |
390 | } |
391 | ||
d80cd92a | 392 | // ---------------------------------------------------------------------------- |
af4088f1 | 393 | // [target] window handling |
d80cd92a | 394 | // ---------------------------------------------------------------------------- |
ecab4dba | 395 | |
349efbaa | 396 | void wxScrollHelper::DeleteEvtHandler() |
ecab4dba | 397 | { |
248d771c VZ |
398 | // search for m_handler in the handler list |
399 | if ( m_win && m_handler ) | |
349efbaa | 400 | { |
74f6bbf9 | 401 | if ( m_win->RemoveEventHandler(m_handler) ) |
248d771c | 402 | { |
74f6bbf9 | 403 | delete m_handler; |
248d771c | 404 | } |
74f6bbf9 VZ |
405 | //else: something is very wrong, so better [maybe] leak memory than |
406 | // risk a crash because of double deletion | |
248d771c | 407 | |
74f6bbf9 | 408 | m_handler = NULL; |
349efbaa VZ |
409 | } |
410 | } | |
411 | ||
412 | void wxScrollHelper::SetWindow(wxWindow *win) | |
413 | { | |
414 | wxCHECK_RET( win, _T("wxScrollHelper needs a window to scroll") ); | |
415 | ||
416 | m_win = win; | |
417 | ||
af4088f1 VZ |
418 | // by default, the associated window is also the target window |
419 | DoSetTargetWindow(win); | |
349efbaa VZ |
420 | } |
421 | ||
af4088f1 | 422 | void wxScrollHelper::DoSetTargetWindow(wxWindow *target) |
349efbaa | 423 | { |
ecab4dba | 424 | m_targetWindow = target; |
349efbaa VZ |
425 | |
426 | // install the event handler which will intercept the events we're | |
af4088f1 VZ |
427 | // interested in (but only do it for our real window, not the target window |
428 | // which we scroll - we don't need to hijack its events) | |
429 | if ( m_targetWindow == m_win ) | |
13ff9344 | 430 | { |
248d771c VZ |
431 | // if we already have a handler, delete it first |
432 | DeleteEvtHandler(); | |
433 | ||
13ff9344 JS |
434 | m_handler = new wxScrollHelperEvtHandler(this); |
435 | m_targetWindow->PushEventHandler(m_handler); | |
436 | } | |
349efbaa VZ |
437 | } |
438 | ||
af4088f1 | 439 | void wxScrollHelper::SetTargetWindow(wxWindow *target) |
349efbaa VZ |
440 | { |
441 | wxCHECK_RET( target, wxT("target window must not be NULL") ); | |
442 | ||
443 | if ( target == m_targetWindow ) | |
444 | return; | |
445 | ||
af4088f1 | 446 | DoSetTargetWindow(target); |
ecab4dba RR |
447 | } |
448 | ||
1e6feb95 | 449 | wxWindow *wxScrollHelper::GetTargetWindow() const |
ecab4dba RR |
450 | { |
451 | return m_targetWindow; | |
452 | } | |
453 | ||
d80cd92a VZ |
454 | // ---------------------------------------------------------------------------- |
455 | // scrolling implementation itself | |
456 | // ---------------------------------------------------------------------------- | |
457 | ||
1e6feb95 | 458 | void wxScrollHelper::HandleOnScroll(wxScrollWinEvent& event) |
c801d85f | 459 | { |
139adb6a | 460 | int nScrollInc = CalcScrollInc(event); |
1e6feb95 | 461 | if ( nScrollInc == 0 ) |
139adb6a | 462 | { |
1e6feb95 VZ |
463 | // can't scroll further |
464 | event.Skip(); | |
465 | ||
466 | return; | |
139adb6a | 467 | } |
c801d85f | 468 | |
1e6feb95 | 469 | int orient = event.GetOrientation(); |
139adb6a RR |
470 | if (orient == wxHORIZONTAL) |
471 | { | |
472 | m_xScrollPosition += nScrollInc; | |
5f3286d1 | 473 | m_win->SetScrollPos(wxHORIZONTAL, m_xScrollPosition); |
139adb6a | 474 | } |
c801d85f | 475 | else |
139adb6a RR |
476 | { |
477 | m_yScrollPosition += nScrollInc; | |
5f3286d1 | 478 | m_win->SetScrollPos(wxVERTICAL, m_yScrollPosition); |
139adb6a | 479 | } |
a58a12e9 | 480 | |
1e6feb95 VZ |
481 | bool needsRefresh = FALSE; |
482 | int dx = 0, | |
483 | dy = 0; | |
139adb6a RR |
484 | if (orient == wxHORIZONTAL) |
485 | { | |
1e6feb95 VZ |
486 | if ( m_xScrollingEnabled ) |
487 | { | |
488 | dx = -m_xScrollPixelsPerLine * nScrollInc; | |
489 | } | |
139adb6a | 490 | else |
1e6feb95 VZ |
491 | { |
492 | needsRefresh = TRUE; | |
493 | } | |
139adb6a | 494 | } |
c801d85f | 495 | else |
139adb6a | 496 | { |
1e6feb95 VZ |
497 | if ( m_yScrollingEnabled ) |
498 | { | |
499 | dy = -m_yScrollPixelsPerLine * nScrollInc; | |
500 | } | |
139adb6a | 501 | else |
1e6feb95 VZ |
502 | { |
503 | needsRefresh = TRUE; | |
504 | } | |
505 | } | |
506 | ||
507 | if ( needsRefresh ) | |
508 | { | |
d5d58a93 | 509 | m_targetWindow->Refresh(TRUE, GetScrollRect()); |
1e6feb95 VZ |
510 | } |
511 | else | |
512 | { | |
d5d58a93 | 513 | m_targetWindow->ScrollWindow(dx, dy, GetScrollRect()); |
3d2b9c20 | 514 | } |
1e6feb95 | 515 | |
7c74e7fe | 516 | #ifdef __WXMAC__ |
1f066698 | 517 | m_targetWindow->Update() ; |
7c74e7fe | 518 | #endif |
c801d85f KB |
519 | } |
520 | ||
1e6feb95 | 521 | int wxScrollHelper::CalcScrollInc(wxScrollWinEvent& event) |
c801d85f | 522 | { |
ecab4dba RR |
523 | int pos = event.GetPosition(); |
524 | int orient = event.GetOrientation(); | |
c801d85f | 525 | |
ecab4dba | 526 | int nScrollInc = 0; |
1a8caf94 | 527 | if (event.GetEventType() == wxEVT_SCROLLWIN_TOP) |
c801d85f | 528 | { |
ecab4dba RR |
529 | if (orient == wxHORIZONTAL) |
530 | nScrollInc = - m_xScrollPosition; | |
531 | else | |
532 | nScrollInc = - m_yScrollPosition; | |
1a8caf94 RR |
533 | } else |
534 | if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM) | |
535 | { | |
ecab4dba RR |
536 | if (orient == wxHORIZONTAL) |
537 | nScrollInc = m_xScrollLines - m_xScrollPosition; | |
538 | else | |
539 | nScrollInc = m_yScrollLines - m_yScrollPosition; | |
1a8caf94 RR |
540 | } else |
541 | if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP) | |
542 | { | |
ecab4dba | 543 | nScrollInc = -1; |
1a8caf94 RR |
544 | } else |
545 | if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN) | |
546 | { | |
ecab4dba | 547 | nScrollInc = 1; |
1a8caf94 RR |
548 | } else |
549 | if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP) | |
550 | { | |
ecab4dba RR |
551 | if (orient == wxHORIZONTAL) |
552 | nScrollInc = -GetScrollPageSize(wxHORIZONTAL); | |
553 | else | |
554 | nScrollInc = -GetScrollPageSize(wxVERTICAL); | |
1a8caf94 RR |
555 | } else |
556 | if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN) | |
557 | { | |
ecab4dba RR |
558 | if (orient == wxHORIZONTAL) |
559 | nScrollInc = GetScrollPageSize(wxHORIZONTAL); | |
560 | else | |
561 | nScrollInc = GetScrollPageSize(wxVERTICAL); | |
1a8caf94 RR |
562 | } else |
563 | if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) || | |
564 | (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE)) | |
565 | { | |
ecab4dba RR |
566 | if (orient == wxHORIZONTAL) |
567 | nScrollInc = pos - m_xScrollPosition; | |
568 | else | |
569 | nScrollInc = pos - m_yScrollPosition; | |
c801d85f | 570 | } |
88150e60 | 571 | |
ecab4dba RR |
572 | if (orient == wxHORIZONTAL) |
573 | { | |
a58a12e9 | 574 | if (m_xScrollPixelsPerLine > 0) |
ecab4dba RR |
575 | { |
576 | int w, h; | |
1e6feb95 | 577 | GetTargetSize(&w, &h); |
ecab4dba RR |
578 | |
579 | int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine; | |
580 | int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 ); | |
581 | if (noPositions < 0) | |
d80cd92a | 582 | noPositions = 0; |
ecab4dba RR |
583 | |
584 | if ( (m_xScrollPosition + nScrollInc) < 0 ) | |
d80cd92a | 585 | nScrollInc = -m_xScrollPosition; // As -ve as we can go |
ecab4dba | 586 | else if ( (m_xScrollPosition + nScrollInc) > noPositions ) |
d80cd92a | 587 | nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go |
ecab4dba RR |
588 | } |
589 | else | |
d5d58a93 | 590 | m_targetWindow->Refresh(TRUE, GetScrollRect()); |
9d9355c6 VZ |
591 | } |
592 | else | |
ecab4dba | 593 | { |
be9b0663 | 594 | if ( m_yScrollPixelsPerLine > 0 ) |
d80cd92a | 595 | { |
be9b0663 VZ |
596 | if ( m_yScrollPosition + nScrollInc < 0 ) |
597 | { | |
598 | // As -ve as we can go | |
599 | nScrollInc = -m_yScrollPosition; | |
600 | } | |
601 | else // check for the other bound | |
602 | { | |
603 | const int posMax = m_yScrollLines - m_yScrollLinesPerPage; | |
604 | if ( m_yScrollPosition + nScrollInc > posMax ) | |
605 | { | |
606 | // As +ve as we can go | |
607 | nScrollInc = posMax - m_yScrollPosition; | |
608 | } | |
609 | } | |
ecab4dba RR |
610 | } |
611 | else | |
be9b0663 VZ |
612 | { |
613 | // VZ: why do we do this? (FIXME) | |
d5d58a93 | 614 | m_targetWindow->Refresh(TRUE, GetScrollRect()); |
be9b0663 | 615 | } |
9d9355c6 | 616 | } |
9d9355c6 | 617 | |
ecab4dba | 618 | return nScrollInc; |
c801d85f KB |
619 | } |
620 | ||
621 | // Adjust the scrollbars - new version. | |
1e6feb95 | 622 | void wxScrollHelper::AdjustScrollbars() |
c801d85f | 623 | { |
aeb313f3 | 624 | #ifdef __WXMAC__ |
1f066698 | 625 | m_targetWindow->Update(); |
aeb313f3 SC |
626 | #endif |
627 | ||
566d84a7 RL |
628 | int w = 0, h = 0; |
629 | int oldw, oldh; | |
a58a12e9 | 630 | |
27d029c7 RR |
631 | int oldXScroll = m_xScrollPosition; |
632 | int oldYScroll = m_yScrollPosition; | |
c801d85f | 633 | |
be9b0663 VZ |
634 | // VZ: at least under Windows this loop is useless because when scrollbars |
635 | // [dis]appear we get a WM_SIZE resulting in another call to | |
636 | // AdjustScrollbars() anyhow. As it doesn't seem to do any harm I leave | |
637 | // it here for now but it would be better to ensure that all ports | |
638 | // generate EVT_SIZE when scrollbars [dis]appear, emulating it if | |
639 | // necessary, and remove it later | |
640 | do | |
641 | { | |
566d84a7 | 642 | GetTargetSize(&w, 0); |
c801d85f | 643 | |
566d84a7 RL |
644 | if (m_xScrollPixelsPerLine == 0) |
645 | { | |
646 | m_xScrollLines = 0; | |
647 | m_xScrollPosition = 0; | |
648 | m_win->SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE); | |
649 | } | |
650 | else | |
651 | { | |
652 | m_xScrollLines = m_targetWindow->GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine; | |
653 | ||
654 | // Calculate page size i.e. number of scroll units you get on the | |
655 | // current client window | |
656 | int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 ); | |
657 | if (noPagePositions < 1) noPagePositions = 1; | |
658 | if ( noPagePositions > m_xScrollLines ) | |
659 | noPagePositions = m_xScrollLines; | |
660 | ||
661 | // Correct position if greater than extent of canvas minus | |
662 | // the visible portion of it or if below zero | |
663 | m_xScrollPosition = wxMin( m_xScrollLines - noPagePositions, m_xScrollPosition); | |
664 | m_xScrollPosition = wxMax( 0, m_xScrollPosition ); | |
665 | ||
666 | m_win->SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines); | |
667 | // The amount by which we scroll when paging | |
668 | SetScrollPageSize(wxHORIZONTAL, noPagePositions); | |
669 | } | |
c801d85f | 670 | |
566d84a7 | 671 | GetTargetSize(0, &h); |
a58a12e9 | 672 | |
be9b0663 VZ |
673 | // scroll lines per page: if 0, no scrolling is needed |
674 | int linesPerPage; | |
675 | ||
676 | if ( m_yScrollPixelsPerLine == 0 ) | |
566d84a7 | 677 | { |
be9b0663 | 678 | // scrolling is disabled |
566d84a7 RL |
679 | m_yScrollLines = 0; |
680 | m_yScrollPosition = 0; | |
be9b0663 | 681 | linesPerPage = 0; |
566d84a7 | 682 | } |
be9b0663 | 683 | else // might need scrolling |
566d84a7 | 684 | { |
be9b0663 VZ |
685 | int hVirt = m_targetWindow->GetVirtualSize().GetHeight(); |
686 | m_yScrollLines = hVirt / m_yScrollPixelsPerLine; | |
566d84a7 RL |
687 | |
688 | // Calculate page size i.e. number of scroll units you get on the | |
689 | // current client window | |
be9b0663 VZ |
690 | linesPerPage = h / m_yScrollPixelsPerLine; |
691 | if ( linesPerPage >= m_yScrollLines ) | |
692 | { | |
693 | // we're big enough to not need scrolling | |
694 | linesPerPage = | |
695 | m_yScrollLines = | |
696 | m_yScrollPosition = 0; | |
697 | } | |
698 | else // we do need a scrollbar | |
699 | { | |
700 | if ( linesPerPage < 1 ) | |
701 | linesPerPage = 1; | |
702 | ||
703 | // Correct position if greater than extent of canvas minus | |
704 | // the visible portion of it or if below zero | |
705 | const int posMax = m_yScrollLines - linesPerPage; | |
706 | if ( m_yScrollPosition > posMax ) | |
707 | m_yScrollPosition = posMax; | |
708 | else if ( m_yScrollPosition < 0 ) | |
709 | m_yScrollPosition = 0; | |
710 | } | |
711 | } | |
566d84a7 | 712 | |
be9b0663 VZ |
713 | m_win->SetScrollbar(wxVERTICAL, m_yScrollPosition, |
714 | linesPerPage, m_yScrollLines); | |
566d84a7 | 715 | |
be9b0663 VZ |
716 | // The amount by which we scroll when paging |
717 | SetScrollPageSize(wxVERTICAL, linesPerPage); | |
c801d85f | 718 | |
139adb6a | 719 | |
be9b0663 | 720 | // If a scrollbar (dis)appeared as a result of this, adjust them again. |
566d84a7 RL |
721 | oldw = w; |
722 | oldh = h; | |
723 | ||
724 | GetTargetSize( &w, &h ); | |
725 | } while ( w != oldw && h != oldh ); | |
726 | ||
727 | #ifdef __WXMOTIF__ | |
728 | // Sorry, some Motif-specific code to implement a backing pixmap | |
729 | // for the wxRETAINED style. Implementing a backing store can't | |
730 | // be entirely generic because it relies on the wxWindowDC implementation | |
731 | // to duplicate X drawing calls for the backing pixmap. | |
732 | ||
733 | if ( m_targetWindow->GetWindowStyle() & wxRETAINED ) | |
139adb6a | 734 | { |
566d84a7 RL |
735 | Display* dpy = XtDisplay((Widget)m_targetWindow->GetMainWidget()); |
736 | ||
737 | int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine; | |
738 | int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine; | |
739 | if (m_targetWindow->GetBackingPixmap() && | |
740 | !((m_targetWindow->GetPixmapWidth() == totalPixelWidth) && | |
741 | (m_targetWindow->GetPixmapHeight() == totalPixelHeight))) | |
742 | { | |
743 | XFreePixmap (dpy, (Pixmap) m_targetWindow->GetBackingPixmap()); | |
744 | m_targetWindow->SetBackingPixmap((WXPixmap) 0); | |
745 | } | |
746 | ||
747 | if (!m_targetWindow->GetBackingPixmap() && | |
9b66c26a | 748 | (m_xScrollLines != 0) && (m_yScrollLines != 0)) |
566d84a7 RL |
749 | { |
750 | int depth = wxDisplayDepth(); | |
751 | m_targetWindow->SetPixmapWidth(totalPixelWidth); | |
752 | m_targetWindow->SetPixmapHeight(totalPixelHeight); | |
753 | m_targetWindow->SetBackingPixmap((WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)), | |
754 | m_targetWindow->GetPixmapWidth(), m_targetWindow->GetPixmapHeight(), depth)); | |
755 | } | |
756 | ||
139adb6a | 757 | } |
566d84a7 | 758 | #endif // Motif |
a58a12e9 | 759 | |
27d029c7 RR |
760 | if (oldXScroll != m_xScrollPosition) |
761 | { | |
762 | if (m_xScrollingEnabled) | |
566d84a7 | 763 | m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll - m_xScrollPosition), 0, |
d5d58a93 | 764 | GetScrollRect() ); |
27d029c7 | 765 | else |
d5d58a93 | 766 | m_targetWindow->Refresh(TRUE, GetScrollRect()); |
27d029c7 | 767 | } |
a58a12e9 | 768 | |
27d029c7 RR |
769 | if (oldYScroll != m_yScrollPosition) |
770 | { | |
771 | if (m_yScrollingEnabled) | |
1e6feb95 | 772 | m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), |
d5d58a93 | 773 | GetScrollRect() ); |
27d029c7 | 774 | else |
d5d58a93 | 775 | m_targetWindow->Refresh(TRUE, GetScrollRect()); |
27d029c7 | 776 | } |
aeb313f3 SC |
777 | |
778 | #ifdef __WXMAC__ | |
1f066698 | 779 | m_targetWindow->Update(); |
aeb313f3 | 780 | #endif |
c801d85f KB |
781 | } |
782 | ||
1e6feb95 | 783 | void wxScrollHelper::DoPrepareDC(wxDC& dc) |
c801d85f | 784 | { |
1e6feb95 VZ |
785 | wxPoint pt = dc.GetDeviceOrigin(); |
786 | dc.SetDeviceOrigin( pt.x - m_xScrollPosition * m_xScrollPixelsPerLine, | |
787 | pt.y - m_yScrollPosition * m_yScrollPixelsPerLine ); | |
139adb6a | 788 | dc.SetUserScale( m_scaleX, m_scaleY ); |
c801d85f KB |
789 | } |
790 | ||
566d84a7 RL |
791 | void wxScrollHelper::SetScrollRate( int xstep, int ystep ) |
792 | { | |
793 | int old_x = m_xScrollPixelsPerLine * m_xScrollPosition; | |
794 | int old_y = m_yScrollPixelsPerLine * m_yScrollPosition; | |
795 | ||
796 | m_xScrollPixelsPerLine = xstep; | |
797 | m_yScrollPixelsPerLine = ystep; | |
798 | ||
799 | int new_x = m_xScrollPixelsPerLine * m_xScrollPosition; | |
800 | int new_y = m_yScrollPixelsPerLine * m_yScrollPosition; | |
801 | ||
802 | m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition ); | |
803 | m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition ); | |
804 | m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y ); | |
805 | ||
806 | AdjustScrollbars(); | |
807 | } | |
808 | ||
1e6feb95 | 809 | void wxScrollHelper::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const |
c801d85f | 810 | { |
a0bc2c1d VZ |
811 | if ( x_unit ) |
812 | *x_unit = m_xScrollPixelsPerLine; | |
813 | if ( y_unit ) | |
814 | *y_unit = m_yScrollPixelsPerLine; | |
c801d85f KB |
815 | } |
816 | ||
1e6feb95 | 817 | int wxScrollHelper::GetScrollPageSize(int orient) const |
c801d85f KB |
818 | { |
819 | if ( orient == wxHORIZONTAL ) | |
820 | return m_xScrollLinesPerPage; | |
821 | else | |
822 | return m_yScrollLinesPerPage; | |
823 | } | |
824 | ||
1e6feb95 | 825 | void wxScrollHelper::SetScrollPageSize(int orient, int pageSize) |
c801d85f KB |
826 | { |
827 | if ( orient == wxHORIZONTAL ) | |
828 | m_xScrollLinesPerPage = pageSize; | |
829 | else | |
830 | m_yScrollLinesPerPage = pageSize; | |
831 | } | |
832 | ||
833 | /* | |
834 | * Scroll to given position (scroll position, not pixel position) | |
835 | */ | |
1e6feb95 | 836 | void wxScrollHelper::Scroll( int x_pos, int y_pos ) |
c801d85f | 837 | { |
8b089c5e JS |
838 | if (!m_targetWindow) |
839 | return; | |
840 | ||
a58a12e9 | 841 | if (((x_pos == -1) || (x_pos == m_xScrollPosition)) && |
139adb6a | 842 | ((y_pos == -1) || (y_pos == m_yScrollPosition))) return; |
a58a12e9 | 843 | |
aeb313f3 | 844 | #ifdef __WXMAC__ |
1f066698 | 845 | m_targetWindow->Update(); |
aeb313f3 SC |
846 | #endif |
847 | ||
139adb6a | 848 | int w, h; |
1e6feb95 | 849 | GetTargetSize(&w, &h); |
c801d85f | 850 | |
8775b357 | 851 | if ((x_pos != -1) && (m_xScrollPixelsPerLine)) |
c801d85f | 852 | { |
ed673c6a | 853 | int old_x = m_xScrollPosition; |
139adb6a | 854 | m_xScrollPosition = x_pos; |
a58a12e9 | 855 | |
3d2b9c20 RR |
856 | // Calculate page size i.e. number of scroll units you get on the |
857 | // current client window | |
ecab4dba | 858 | int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 ); |
139adb6a RR |
859 | if (noPagePositions < 1) noPagePositions = 1; |
860 | ||
861 | // Correct position if greater than extent of canvas minus | |
3d2b9c20 | 862 | // the visible portion of it or if below zero |
139adb6a RR |
863 | m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition ); |
864 | m_xScrollPosition = wxMax( 0, m_xScrollPosition ); | |
a58a12e9 | 865 | |
f6bcfd97 | 866 | if (old_x != m_xScrollPosition) { |
5f3286d1 | 867 | m_win->SetScrollPos( wxHORIZONTAL, m_xScrollPosition ); |
1e6feb95 | 868 | m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0, |
d5d58a93 | 869 | GetScrollRect() ); |
f6bcfd97 | 870 | } |
c801d85f | 871 | } |
8775b357 | 872 | if ((y_pos != -1) && (m_yScrollPixelsPerLine)) |
c801d85f | 873 | { |
ed673c6a | 874 | int old_y = m_yScrollPosition; |
139adb6a | 875 | m_yScrollPosition = y_pos; |
a58a12e9 | 876 | |
3d2b9c20 RR |
877 | // Calculate page size i.e. number of scroll units you get on the |
878 | // current client window | |
ecab4dba | 879 | int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 ); |
139adb6a RR |
880 | if (noPagePositions < 1) noPagePositions = 1; |
881 | ||
882 | // Correct position if greater than extent of canvas minus | |
3d2b9c20 | 883 | // the visible portion of it or if below zero |
139adb6a RR |
884 | m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition ); |
885 | m_yScrollPosition = wxMax( 0, m_yScrollPosition ); | |
d2c52078 | 886 | |
f6bcfd97 | 887 | if (old_y != m_yScrollPosition) { |
5f3286d1 | 888 | m_win->SetScrollPos( wxVERTICAL, m_yScrollPosition ); |
1e6feb95 | 889 | m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine, |
d5d58a93 | 890 | GetScrollRect() ); |
f6bcfd97 | 891 | } |
c801d85f | 892 | } |
a58a12e9 | 893 | |
7c74e7fe | 894 | #ifdef __WXMAC__ |
1f066698 | 895 | m_targetWindow->Update(); |
7c74e7fe | 896 | #endif |
aeb313f3 | 897 | |
c801d85f KB |
898 | } |
899 | ||
1e6feb95 | 900 | void wxScrollHelper::EnableScrolling (bool x_scroll, bool y_scroll) |
c801d85f | 901 | { |
139adb6a RR |
902 | m_xScrollingEnabled = x_scroll; |
903 | m_yScrollingEnabled = y_scroll; | |
c801d85f KB |
904 | } |
905 | ||
c801d85f | 906 | // Where the current view starts from |
1e6feb95 | 907 | void wxScrollHelper::GetViewStart (int *x, int *y) const |
c801d85f | 908 | { |
a0bc2c1d VZ |
909 | if ( x ) |
910 | *x = m_xScrollPosition; | |
911 | if ( y ) | |
912 | *y = m_yScrollPosition; | |
c801d85f KB |
913 | } |
914 | ||
8c2f3797 | 915 | void wxScrollHelper::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const |
c801d85f | 916 | { |
a0bc2c1d VZ |
917 | if ( xx ) |
918 | *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine; | |
919 | if ( yy ) | |
920 | *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine; | |
c801d85f KB |
921 | } |
922 | ||
8c2f3797 | 923 | void wxScrollHelper::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const |
c801d85f | 924 | { |
a0bc2c1d VZ |
925 | if ( xx ) |
926 | *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine; | |
927 | if ( yy ) | |
928 | *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine; | |
c801d85f | 929 | } |
d80cd92a VZ |
930 | |
931 | // ---------------------------------------------------------------------------- | |
932 | // event handlers | |
933 | // ---------------------------------------------------------------------------- | |
934 | ||
935 | // Default OnSize resets scrollbars, if any | |
1e6feb95 | 936 | void wxScrollHelper::HandleOnSize(wxSizeEvent& WXUNUSED(event)) |
d80cd92a | 937 | { |
150c8d89 | 938 | if( m_win->GetAutoLayout() || m_targetWindow->GetAutoLayout() ) |
2b5f62a0 VZ |
939 | { |
940 | if ( m_targetWindow != m_win ) | |
941 | m_targetWindow->FitInside(); | |
566d84a7 | 942 | |
2b5f62a0 | 943 | m_win->FitInside(); |
dc429f89 | 944 | |
150c8d89 RL |
945 | // FIXME: Something is really weird here... This should be |
946 | // called by FitInside above (and apparently is), yet the | |
947 | // scrollsub sample will get the scrollbar wrong if resized | |
948 | // quickly. This masks the bug, but is surely not the right | |
949 | // answer at all. | |
950 | AdjustScrollbars(); | |
2b5f62a0 VZ |
951 | } |
952 | else | |
953 | AdjustScrollbars(); | |
d80cd92a VZ |
954 | } |
955 | ||
956 | // This calls OnDraw, having adjusted the origin according to the current | |
957 | // scroll position | |
1e6feb95 | 958 | void wxScrollHelper::HandleOnPaint(wxPaintEvent& WXUNUSED(event)) |
d80cd92a | 959 | { |
af4088f1 VZ |
960 | // don't use m_targetWindow here, this is always called for ourselves |
961 | wxPaintDC dc(m_win); | |
1e6feb95 | 962 | DoPrepareDC(dc); |
d80cd92a VZ |
963 | |
964 | OnDraw(dc); | |
965 | } | |
966 | ||
438e3558 VZ |
967 | // kbd handling: notice that we use OnChar() and not OnKeyDown() for |
968 | // compatibility here - if we used OnKeyDown(), the programs which process | |
969 | // arrows themselves in their OnChar() would never get the message and like | |
970 | // this they always have the priority | |
1e6feb95 | 971 | void wxScrollHelper::HandleOnChar(wxKeyEvent& event) |
d80cd92a VZ |
972 | { |
973 | int stx, sty, // view origin | |
974 | szx, szy, // view size (total) | |
975 | clix, cliy; // view size (on screen) | |
976 | ||
1e6feb95 VZ |
977 | GetViewStart(&stx, &sty); |
978 | GetTargetSize(&clix, &cliy); | |
566d84a7 | 979 | m_targetWindow->GetVirtualSize(&szx, &szy); |
56dade3c RL |
980 | |
981 | if( m_xScrollPixelsPerLine ) | |
982 | { | |
983 | clix /= m_xScrollPixelsPerLine; | |
d7da9756 | 984 | szx /= m_xScrollPixelsPerLine; |
56dade3c RL |
985 | } |
986 | else | |
987 | { | |
988 | clix = 0; | |
d7da9756 | 989 | szx = -1; |
56dade3c RL |
990 | } |
991 | if( m_yScrollPixelsPerLine ) | |
992 | { | |
993 | cliy /= m_yScrollPixelsPerLine; | |
994 | szy /= m_yScrollPixelsPerLine; | |
995 | } | |
996 | else | |
997 | { | |
998 | cliy = 0; | |
d7da9756 | 999 | szy = -1; |
56dade3c | 1000 | } |
d80cd92a | 1001 | |
39c869a6 VZ |
1002 | int xScrollOld = m_xScrollPosition, |
1003 | yScrollOld = m_yScrollPosition; | |
1004 | ||
ecb01792 | 1005 | int dsty; |
4995ca80 | 1006 | switch ( event.GetKeyCode() ) |
d80cd92a VZ |
1007 | { |
1008 | case WXK_PAGEUP: | |
1009 | case WXK_PRIOR: | |
ecb01792 RL |
1010 | dsty = sty - (5 * cliy / 6); |
1011 | Scroll(-1, (dsty == -1) ? 0 : dsty); | |
d80cd92a VZ |
1012 | break; |
1013 | ||
1014 | case WXK_PAGEDOWN: | |
1015 | case WXK_NEXT: | |
1016 | Scroll(-1, sty + (5 * cliy / 6)); | |
1017 | break; | |
1018 | ||
d80cd92a VZ |
1019 | case WXK_HOME: |
1020 | Scroll(0, event.ControlDown() ? 0 : -1); | |
1021 | break; | |
1022 | ||
1023 | case WXK_END: | |
4acd108c | 1024 | Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1); |
d80cd92a VZ |
1025 | break; |
1026 | ||
1027 | case WXK_UP: | |
1028 | Scroll(-1, sty - 1); | |
1029 | break; | |
1030 | ||
1031 | case WXK_DOWN: | |
1032 | Scroll(-1, sty + 1); | |
1033 | break; | |
1034 | ||
1035 | case WXK_LEFT: | |
1036 | Scroll(stx - 1, -1); | |
1037 | break; | |
1038 | ||
1039 | case WXK_RIGHT: | |
1040 | Scroll(stx + 1, -1); | |
1041 | break; | |
1042 | ||
1043 | default: | |
1044 | // not for us | |
1045 | event.Skip(); | |
1046 | } | |
39c869a6 VZ |
1047 | |
1048 | if ( m_xScrollPosition != xScrollOld ) | |
1049 | { | |
1050 | wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_xScrollPosition, | |
1051 | wxHORIZONTAL); | |
1052 | event.SetEventObject(m_win); | |
1053 | m_win->GetEventHandler()->ProcessEvent(event); | |
1054 | } | |
1055 | ||
1056 | if ( m_yScrollPosition != yScrollOld ) | |
1057 | { | |
1058 | wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, m_yScrollPosition, | |
1059 | wxVERTICAL); | |
1060 | event.SetEventObject(m_win); | |
1061 | m_win->GetEventHandler()->ProcessEvent(event); | |
1062 | } | |
d80cd92a | 1063 | } |
d2c52078 | 1064 | |
1e6feb95 VZ |
1065 | // ---------------------------------------------------------------------------- |
1066 | // autoscroll stuff: these functions deal with sending fake scroll events when | |
1067 | // a captured mouse is being held outside the window | |
1068 | // ---------------------------------------------------------------------------- | |
1069 | ||
1070 | bool wxScrollHelper::SendAutoScrollEvents(wxScrollWinEvent& event) const | |
1071 | { | |
1072 | // only send the event if the window is scrollable in this direction | |
1073 | wxWindow *win = (wxWindow *)event.GetEventObject(); | |
1074 | return win->HasScrollbar(event.GetOrientation()); | |
1075 | } | |
1076 | ||
1077 | void wxScrollHelper::StopAutoScrolling() | |
1078 | { | |
4b316329 | 1079 | #if wxUSE_TIMER |
1e6feb95 VZ |
1080 | if ( m_timerAutoScroll ) |
1081 | { | |
1082 | delete m_timerAutoScroll; | |
1083 | m_timerAutoScroll = (wxTimer *)NULL; | |
1084 | } | |
4b316329 | 1085 | #endif |
1e6feb95 | 1086 | } |
d2c52078 | 1087 | |
1e6feb95 | 1088 | void wxScrollHelper::HandleOnMouseEnter(wxMouseEvent& event) |
d2c52078 | 1089 | { |
1e6feb95 VZ |
1090 | StopAutoScrolling(); |
1091 | ||
1092 | event.Skip(); | |
1093 | } | |
d2c52078 | 1094 | |
1e6feb95 VZ |
1095 | void wxScrollHelper::HandleOnMouseLeave(wxMouseEvent& event) |
1096 | { | |
1097 | // don't prevent the usual processing of the event from taking place | |
1098 | event.Skip(); | |
1099 | ||
1100 | // when a captured mouse leave a scrolled window we start generate | |
1101 | // scrolling events to allow, for example, extending selection beyond the | |
1102 | // visible area in some controls | |
1103 | if ( wxWindow::GetCapture() == m_targetWindow ) | |
1104 | { | |
1105 | // where is the mouse leaving? | |
1106 | int pos, orient; | |
1107 | wxPoint pt = event.GetPosition(); | |
1108 | if ( pt.x < 0 ) | |
1109 | { | |
1110 | orient = wxHORIZONTAL; | |
1111 | pos = 0; | |
1112 | } | |
1113 | else if ( pt.y < 0 ) | |
1114 | { | |
1115 | orient = wxVERTICAL; | |
1116 | pos = 0; | |
1117 | } | |
1118 | else // we're lower or to the right of the window | |
1119 | { | |
1120 | wxSize size = m_targetWindow->GetClientSize(); | |
1121 | if ( pt.x > size.x ) | |
1122 | { | |
1123 | orient = wxHORIZONTAL; | |
1124 | pos = m_xScrollLines; | |
1125 | } | |
1126 | else if ( pt.y > size.y ) | |
1127 | { | |
1128 | orient = wxVERTICAL; | |
1129 | pos = m_yScrollLines; | |
1130 | } | |
1131 | else // this should be impossible | |
1132 | { | |
1133 | // but seems to happen sometimes under wxMSW - maybe it's a bug | |
1134 | // there but for now just ignore it | |
1135 | ||
1136 | //wxFAIL_MSG( _T("can't understand where has mouse gone") ); | |
1137 | ||
1138 | return; | |
1139 | } | |
1140 | } | |
1141 | ||
1142 | // only start the auto scroll timer if the window can be scrolled in | |
1143 | // this direction | |
1144 | if ( !m_targetWindow->HasScrollbar(orient) ) | |
1145 | return; | |
1146 | ||
4b316329 | 1147 | #if wxUSE_TIMER |
1e6feb95 VZ |
1148 | delete m_timerAutoScroll; |
1149 | m_timerAutoScroll = new wxAutoScrollTimer | |
1150 | ( | |
1151 | m_targetWindow, this, | |
1152 | pos == 0 ? wxEVT_SCROLLWIN_LINEUP | |
1153 | : wxEVT_SCROLLWIN_LINEDOWN, | |
1154 | pos, | |
1155 | orient | |
1156 | ); | |
1157 | m_timerAutoScroll->Start(50); // FIXME: make configurable | |
4b316329 | 1158 | #endif |
1e6feb95 VZ |
1159 | } |
1160 | } | |
1161 | ||
e421922f VZ |
1162 | #if wxUSE_MOUSEWHEEL |
1163 | ||
1e6feb95 VZ |
1164 | void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event) |
1165 | { | |
d2c52078 | 1166 | m_wheelRotation += event.GetWheelRotation(); |
1e6feb95 | 1167 | int lines = m_wheelRotation / event.GetWheelDelta(); |
d2c52078 RD |
1168 | m_wheelRotation -= lines * event.GetWheelDelta(); |
1169 | ||
1e6feb95 VZ |
1170 | if (lines != 0) |
1171 | { | |
1e6feb95 | 1172 | |
b6b85bdc JS |
1173 | wxScrollWinEvent newEvent; |
1174 | ||
ac6f6ffc | 1175 | newEvent.SetPosition(0); |
b6b85bdc JS |
1176 | newEvent.SetOrientation(wxVERTICAL); |
1177 | newEvent.m_eventObject = m_win; | |
b6b85bdc | 1178 | |
9b9337da | 1179 | if (event.IsPageScroll()) |
4b056ef5 RD |
1180 | { |
1181 | if (lines > 0) | |
1182 | newEvent.m_eventType = wxEVT_SCROLLWIN_PAGEUP; | |
1183 | else | |
1184 | newEvent.m_eventType = wxEVT_SCROLLWIN_PAGEDOWN; | |
1185 | ||
b6b85bdc | 1186 | m_win->GetEventHandler()->ProcessEvent(newEvent); |
4b056ef5 RD |
1187 | } |
1188 | else | |
1189 | { | |
1190 | lines *= event.GetLinesPerAction(); | |
1191 | if (lines > 0) | |
1192 | newEvent.m_eventType = wxEVT_SCROLLWIN_LINEUP; | |
1193 | else | |
1194 | newEvent.m_eventType = wxEVT_SCROLLWIN_LINEDOWN; | |
b6b85bdc | 1195 | |
4b056ef5 RD |
1196 | int times = abs(lines); |
1197 | for (; times > 0; times--) | |
1198 | m_win->GetEventHandler()->ProcessEvent(newEvent); | |
1199 | } | |
d2c52078 RD |
1200 | } |
1201 | } | |
1202 | ||
e421922f VZ |
1203 | #endif // wxUSE_MOUSEWHEEL |
1204 | ||
1e6feb95 VZ |
1205 | // ---------------------------------------------------------------------------- |
1206 | // wxGenericScrolledWindow implementation | |
1207 | // ---------------------------------------------------------------------------- | |
1208 | ||
1209 | IMPLEMENT_DYNAMIC_CLASS(wxGenericScrolledWindow, wxPanel) | |
1210 | ||
349efbaa VZ |
1211 | BEGIN_EVENT_TABLE(wxGenericScrolledWindow, wxPanel) |
1212 | EVT_PAINT(wxGenericScrolledWindow::OnPaint) | |
1213 | END_EVENT_TABLE() | |
1214 | ||
1e6feb95 VZ |
1215 | bool wxGenericScrolledWindow::Create(wxWindow *parent, |
1216 | wxWindowID id, | |
1217 | const wxPoint& pos, | |
1218 | const wxSize& size, | |
1219 | long style, | |
1220 | const wxString& name) | |
1221 | { | |
1222 | m_targetWindow = this; | |
1223 | ||
1224 | bool ok = wxPanel::Create(parent, id, pos, size, style, name); | |
1225 | ||
1e6feb95 VZ |
1226 | return ok; |
1227 | } | |
1228 | ||
1229 | wxGenericScrolledWindow::~wxGenericScrolledWindow() | |
1230 | { | |
1231 | } | |
d2c52078 | 1232 | |
30486297 RD |
1233 | bool wxGenericScrolledWindow::Layout() |
1234 | { | |
566d84a7 | 1235 | if (GetSizer() && m_targetWindow == this) |
30486297 | 1236 | { |
566d84a7 RL |
1237 | // If we're the scroll target, take into account the |
1238 | // virtual size and scrolled position of the window. | |
1239 | ||
30486297 RD |
1240 | int x, y, w, h; |
1241 | CalcScrolledPosition(0,0, &x,&y); | |
1242 | GetVirtualSize(&w, &h); | |
1243 | GetSizer()->SetDimension(x, y, w, h); | |
1244 | return TRUE; | |
1245 | } | |
7152f0c6 VZ |
1246 | |
1247 | // fall back to default for LayoutConstraints | |
1248 | return wxPanel::Layout(); | |
30486297 RD |
1249 | } |
1250 | ||
2b5f62a0 VZ |
1251 | void wxGenericScrolledWindow::DoSetVirtualSize(int x, int y) |
1252 | { | |
1253 | wxPanel::DoSetVirtualSize( x, y ); | |
1254 | AdjustScrollbars(); | |
1255 | ||
1256 | #if wxUSE_CONSTRAINTS | |
1257 | if (GetAutoLayout()) | |
1258 | Layout(); | |
1259 | #endif | |
1260 | } | |
1261 | ||
349efbaa VZ |
1262 | void wxGenericScrolledWindow::OnPaint(wxPaintEvent& event) |
1263 | { | |
1264 | // the user code didn't really draw the window if we got here, so set this | |
1265 | // flag to try to call OnDraw() later | |
1266 | m_handler->ResetDrawnFlag(); | |
1267 | ||
1268 | event.Skip(); | |
1269 | } | |
1270 | ||
0cf5b099 VZ |
1271 | #ifdef __WXMSW__ |
1272 | long | |
1273 | wxGenericScrolledWindow::MSWWindowProc(WXUINT nMsg, | |
1274 | WXWPARAM wParam, | |
1275 | WXLPARAM lParam) | |
1276 | { | |
1277 | long rc = wxPanel::MSWWindowProc(nMsg, wParam, lParam); | |
1278 | ||
4676948b | 1279 | #ifndef __WXWINCE__ |
0cf5b099 VZ |
1280 | // we need to process arrows ourselves for scrolling |
1281 | if ( nMsg == WM_GETDLGCODE ) | |
1282 | { | |
1283 | rc |= DLGC_WANTARROWS; | |
1284 | } | |
4676948b | 1285 | #endif |
0cf5b099 VZ |
1286 | |
1287 | return rc; | |
1288 | } | |
1289 | ||
1290 | #endif // __WXMSW__ | |
1291 | ||
1e6feb95 | 1292 | #if WXWIN_COMPATIBILITY |
349efbaa | 1293 | |
1e6feb95 VZ |
1294 | void wxGenericScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const |
1295 | { | |
1296 | *x_page = GetScrollPageSize(wxHORIZONTAL); | |
1297 | *y_page = GetScrollPageSize(wxVERTICAL); | |
1298 | } | |
d2c52078 | 1299 | |
1e6feb95 VZ |
1300 | void wxGenericScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const |
1301 | { | |
1302 | if ( xx ) | |
1303 | *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine); | |
1304 | if ( yy ) | |
1305 | *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine); | |
1306 | } | |
349efbaa | 1307 | |
1e6feb95 | 1308 | #endif // WXWIN_COMPATIBILITY |
d2c52078 | 1309 | |
3a8c693a VZ |
1310 | #endif // !wxGTK |
1311 | ||
566d84a7 | 1312 | // vi:sts=4:sw=4:et |