]> git.saurik.com Git - wxWidgets.git/blob - src/generic/scrolwin.cpp
f029987f68b743970704168fe2e019feac48c072
[wxWidgets.git] / src / generic / scrolwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/scrolwin.cpp
3 // Purpose: wxScrolledWindow implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "scrolwin.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/utils.h"
32 #include "wx/dcclient.h"
33
34 #include "wx/generic/scrolwin.h"
35 #include "wx/panel.h"
36
37 #ifdef __WXMSW__
38 #include "windows.h"
39 #endif
40
41 #ifdef __WXMOTIF__
42 // For wxRETAINED implementation
43 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
44 //This code switches off the compiler warnings
45 # pragma message disable nosimpint
46 #endif
47 #include <Xm/Xm.h>
48 #ifdef __VMS__
49 # pragma message enable nosimpint
50 #endif
51 #endif
52
53 // ----------------------------------------------------------------------------
54 // event tables
55 // ----------------------------------------------------------------------------
56
57 BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
58 EVT_SCROLLWIN(wxScrolledWindow::OnScroll)
59 EVT_SIZE(wxScrolledWindow::OnSize)
60 EVT_PAINT(wxScrolledWindow::OnPaint)
61 EVT_KEY_DOWN(wxScrolledWindow::OnKeyDown)
62 END_EVENT_TABLE()
63
64 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
65
66 // ============================================================================
67 // implementation
68 // ============================================================================
69
70 // ----------------------------------------------------------------------------
71 // wxScrolledWindow creation
72 // ----------------------------------------------------------------------------
73
74 wxScrolledWindow::wxScrolledWindow()
75 {
76 m_xScrollPixelsPerLine = 0;
77 m_yScrollPixelsPerLine = 0;
78 m_xScrollingEnabled = TRUE;
79 m_yScrollingEnabled = TRUE;
80 m_xScrollPosition = 0;
81 m_yScrollPosition = 0;
82 m_xScrollLines = 0;
83 m_yScrollLines = 0;
84 m_xScrollLinesPerPage = 0;
85 m_yScrollLinesPerPage = 0;
86 m_scaleX = 1.0;
87 m_scaleY = 1.0;
88 }
89
90 bool wxScrolledWindow::Create(wxWindow *parent,
91 wxWindowID id,
92 const wxPoint& pos,
93 const wxSize& size,
94 long style,
95 const wxString& name)
96 {
97 m_xScrollPixelsPerLine = 0;
98 m_yScrollPixelsPerLine = 0;
99 m_xScrollingEnabled = TRUE;
100 m_yScrollingEnabled = TRUE;
101 m_xScrollPosition = 0;
102 m_yScrollPosition = 0;
103 m_xScrollLines = 0;
104 m_yScrollLines = 0;
105 m_xScrollLinesPerPage = 0;
106 m_yScrollLinesPerPage = 0;
107 m_scaleX = 1.0;
108 m_scaleY = 1.0;
109
110 m_targetWindow = this;
111
112 // we need wxWANTS_CHARS to process arrows ourselves
113 return wxPanel::Create(parent, id, pos, size, style | wxWANTS_CHARS, name);
114 }
115
116 wxScrolledWindow::~wxScrolledWindow()
117 {
118 }
119
120 // ----------------------------------------------------------------------------
121 // setting scrolling parameters
122 // ----------------------------------------------------------------------------
123
124 /*
125 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
126 * noUnitsX/noUnitsY: : no. units per scrollbar
127 */
128 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
129 int noUnitsX, int noUnitsY,
130 int xPos, int yPos, bool noRefresh )
131 {
132 bool do_refresh =
133 (
134 (noUnitsX != 0 && m_xScrollLines == 0) ||
135 (noUnitsX < m_xScrollLines) ||
136 (noUnitsY != 0 && m_yScrollLines == 0) ||
137 (noUnitsY < m_yScrollLines) ||
138 (xPos != m_xScrollPosition) ||
139 (yPos != m_yScrollPosition) ||
140 (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
141 (pixelsPerUnitY != m_yScrollPixelsPerLine)
142 );
143
144 m_xScrollPixelsPerLine = pixelsPerUnitX;
145 m_yScrollPixelsPerLine = pixelsPerUnitY;
146 m_xScrollPosition = xPos;
147 m_yScrollPosition = yPos;
148 m_xScrollLines = noUnitsX;
149 m_yScrollLines = noUnitsY;
150
151 #ifdef __WXMOTIF__
152 // Sorry, some Motif-specific code to implement a backing pixmap
153 // for the wxRETAINED style. Implementing a backing store can't
154 // be entirely generic because it relies on the wxWindowDC implementation
155 // to duplicate X drawing calls for the backing pixmap.
156
157 if ((m_windowStyle & wxRETAINED) == wxRETAINED)
158 {
159 Display* dpy = XtDisplay((Widget) GetMainWidget());
160
161 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
162 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
163 if (m_backingPixmap &&
164 !((m_pixmapWidth == totalPixelWidth) &&
165 (m_pixmapHeight == totalPixelHeight)))
166 {
167 XFreePixmap (dpy, (Pixmap) m_backingPixmap);
168 m_backingPixmap = (WXPixmap) 0;
169 }
170
171 if (!m_backingPixmap &&
172 (noUnitsX != 0) && (noUnitsY != 0))
173 {
174 int depth = wxDisplayDepth();
175 m_pixmapWidth = totalPixelWidth;
176 m_pixmapHeight = totalPixelHeight;
177 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
178 m_pixmapWidth, m_pixmapHeight, depth);
179 }
180
181 }
182 #endif // Motif
183
184 AdjustScrollbars();
185
186 if (do_refresh && !noRefresh)
187 m_targetWindow->Refresh();
188
189 #ifdef __WXMSW__
190 // GRG: if this turns out to be really necessary, we could
191 // at least move it to the above if { ... } so that it is
192 // only done if noRefresh = FALSE (the default). OTOH, if
193 // this doesn't break anything, which seems to be the
194 // case, we could just leave it out.
195
196 // Necessary?
197 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
198 #endif
199 #ifdef __WXMAC__
200 m_targetWindow->MacUpdateImmediately() ;
201 #endif
202 }
203
204 // ----------------------------------------------------------------------------
205 // target window handling
206 // ----------------------------------------------------------------------------
207
208 void wxScrolledWindow::SetTargetWindow( wxWindow *target )
209 {
210 wxASSERT_MSG( target, wxT("target window must not be NULL") );
211 m_targetWindow = target;
212 }
213
214 wxWindow *wxScrolledWindow::GetTargetWindow()
215 {
216 return m_targetWindow;
217 }
218
219 // ----------------------------------------------------------------------------
220 // scrolling implementation itself
221 // ----------------------------------------------------------------------------
222
223 void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
224 {
225 int orient = event.GetOrientation();
226
227 int nScrollInc = CalcScrollInc(event);
228 if (nScrollInc == 0) return;
229
230 if (orient == wxHORIZONTAL)
231 {
232 int newPos = m_xScrollPosition + nScrollInc;
233 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
234 }
235 else
236 {
237 int newPos = m_yScrollPosition + nScrollInc;
238 SetScrollPos(wxVERTICAL, newPos, TRUE );
239 }
240
241 if (orient == wxHORIZONTAL)
242 {
243 m_xScrollPosition += nScrollInc;
244 }
245 else
246 {
247 m_yScrollPosition += nScrollInc;
248 }
249
250 if (orient == wxHORIZONTAL)
251 {
252 if (m_xScrollingEnabled)
253 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
254 else
255 m_targetWindow->Refresh();
256 }
257 else
258 {
259 if (m_yScrollingEnabled)
260 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
261 else
262 m_targetWindow->Refresh();
263 }
264 #ifdef __WXMAC__
265 m_targetWindow->MacUpdateImmediately() ;
266 #endif
267 }
268
269 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
270 {
271 int pos = event.GetPosition();
272 int orient = event.GetOrientation();
273
274 int nScrollInc = 0;
275 switch (event.GetEventType())
276 {
277 case wxEVT_SCROLLWIN_TOP:
278 {
279 if (orient == wxHORIZONTAL)
280 nScrollInc = - m_xScrollPosition;
281 else
282 nScrollInc = - m_yScrollPosition;
283 break;
284 }
285 case wxEVT_SCROLLWIN_BOTTOM:
286 {
287 if (orient == wxHORIZONTAL)
288 nScrollInc = m_xScrollLines - m_xScrollPosition;
289 else
290 nScrollInc = m_yScrollLines - m_yScrollPosition;
291 break;
292 }
293 case wxEVT_SCROLLWIN_LINEUP:
294 {
295 nScrollInc = -1;
296 break;
297 }
298 case wxEVT_SCROLLWIN_LINEDOWN:
299 {
300 nScrollInc = 1;
301 break;
302 }
303 case wxEVT_SCROLLWIN_PAGEUP:
304 {
305 if (orient == wxHORIZONTAL)
306 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
307 else
308 nScrollInc = -GetScrollPageSize(wxVERTICAL);
309 break;
310 }
311 case wxEVT_SCROLLWIN_PAGEDOWN:
312 {
313 if (orient == wxHORIZONTAL)
314 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
315 else
316 nScrollInc = GetScrollPageSize(wxVERTICAL);
317 break;
318 }
319 case wxEVT_SCROLLWIN_THUMBTRACK:
320 {
321 if (orient == wxHORIZONTAL)
322 nScrollInc = pos - m_xScrollPosition;
323 else
324 nScrollInc = pos - m_yScrollPosition;
325 break;
326 }
327 default:
328 {
329 break;
330 }
331 }
332
333 if (orient == wxHORIZONTAL)
334 {
335 if (m_xScrollPixelsPerLine > 0)
336 {
337 int w, h;
338 m_targetWindow->GetClientSize(&w, &h);
339
340 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
341 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
342 if (noPositions < 0)
343 noPositions = 0;
344
345 if ( (m_xScrollPosition + nScrollInc) < 0 )
346 nScrollInc = -m_xScrollPosition; // As -ve as we can go
347 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
348 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
349 }
350 else
351 m_targetWindow->Refresh();
352 }
353 else
354 {
355 if (m_yScrollPixelsPerLine > 0)
356 {
357 int w, h;
358 m_targetWindow->GetClientSize(&w, &h);
359
360 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
361 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
362 if (noPositions < 0)
363 noPositions = 0;
364
365 if ( (m_yScrollPosition + nScrollInc) < 0 )
366 nScrollInc = -m_yScrollPosition; // As -ve as we can go
367 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
368 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
369 }
370 else
371 m_targetWindow->Refresh();
372 }
373
374 return nScrollInc;
375 }
376
377 // Adjust the scrollbars - new version.
378 void wxScrolledWindow::AdjustScrollbars()
379 {
380 int w, h;
381 m_targetWindow->GetClientSize(&w, &h);
382
383 int oldXScroll = m_xScrollPosition;
384 int oldYScroll = m_yScrollPosition;
385
386 if (m_xScrollLines > 0)
387 {
388 // Calculate page size i.e. number of scroll units you get on the
389 // current client window
390 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
391 if (noPagePositions < 1) noPagePositions = 1;
392
393 // Correct position if greater than extent of canvas minus
394 // the visible portion of it or if below zero
395 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
396 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
397
398 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
399 // The amount by which we scroll when paging
400 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
401 }
402 else
403 {
404 m_xScrollPosition = 0;
405 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
406 }
407
408 if (m_yScrollLines > 0)
409 {
410 // Calculate page size i.e. number of scroll units you get on the
411 // current client window
412 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
413 if (noPagePositions < 1) noPagePositions = 1;
414
415 // Correct position if greater than extent of canvas minus
416 // the visible portion of it or if below zero
417 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
418 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
419
420 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
421 // The amount by which we scroll when paging
422 SetScrollPageSize(wxVERTICAL, noPagePositions);
423 }
424 else
425 {
426 m_yScrollPosition = 0;
427 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
428 }
429
430 if (oldXScroll != m_xScrollPosition)
431 {
432 if (m_xScrollingEnabled)
433 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0, (const wxRect *) NULL );
434 else
435 m_targetWindow->Refresh();
436 }
437
438 if (oldYScroll != m_yScrollPosition)
439 {
440 if (m_yScrollingEnabled)
441 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), (const wxRect *) NULL );
442 else
443 m_targetWindow->Refresh();
444 }
445 }
446
447 // Override this function if you don't want to have wxScrolledWindow
448 // automatically change the origin according to the scroll position.
449 void wxScrolledWindow::PrepareDC(wxDC& dc)
450 {
451 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
452 -m_yScrollPosition * m_yScrollPixelsPerLine );
453 dc.SetUserScale( m_scaleX, m_scaleY );
454 }
455
456 #if WXWIN_COMPATIBILITY
457 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
458 {
459 *x_page = GetScrollPageSize(wxHORIZONTAL);
460 *y_page = GetScrollPageSize(wxVERTICAL);
461 }
462
463 void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
464 {
465 if ( xx )
466 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
467 if ( yy )
468 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
469 }
470 #endif // WXWIN_COMPATIBILITY
471
472 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
473 {
474 if ( x_unit )
475 *x_unit = m_xScrollPixelsPerLine;
476 if ( y_unit )
477 *y_unit = m_yScrollPixelsPerLine;
478 }
479
480 int wxScrolledWindow::GetScrollPageSize(int orient) const
481 {
482 if ( orient == wxHORIZONTAL )
483 return m_xScrollLinesPerPage;
484 else
485 return m_yScrollLinesPerPage;
486 }
487
488 void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
489 {
490 if ( orient == wxHORIZONTAL )
491 m_xScrollLinesPerPage = pageSize;
492 else
493 m_yScrollLinesPerPage = pageSize;
494 }
495
496 /*
497 * Scroll to given position (scroll position, not pixel position)
498 */
499 void wxScrolledWindow::Scroll( int x_pos, int y_pos )
500 {
501 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
502 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
503
504 int w, h;
505 m_targetWindow->GetClientSize(&w, &h);
506
507 if (x_pos != -1)
508 {
509 int old_x = m_xScrollPosition;
510 m_xScrollPosition = x_pos;
511
512 // Calculate page size i.e. number of scroll units you get on the
513 // current client window
514 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
515 if (noPagePositions < 1) noPagePositions = 1;
516
517 // Correct position if greater than extent of canvas minus
518 // the visible portion of it or if below zero
519 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
520 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
521
522 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE );
523
524 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
525 }
526 if (y_pos != -1)
527 {
528 int old_y = m_yScrollPosition;
529 m_yScrollPosition = y_pos;
530
531 // Calculate page size i.e. number of scroll units you get on the
532 // current client window
533 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
534 if (noPagePositions < 1) noPagePositions = 1;
535
536 // Correct position if greater than extent of canvas minus
537 // the visible portion of it or if below zero
538 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
539 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
540
541 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
542
543 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
544 }
545
546
547 #ifdef __WXMAC__
548 m_targetWindow->MacUpdateImmediately() ;
549 #endif
550 }
551
552 void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
553 {
554 m_xScrollingEnabled = x_scroll;
555 m_yScrollingEnabled = y_scroll;
556 }
557
558 void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
559 {
560 if ( x )
561 *x = m_xScrollPixelsPerLine * m_xScrollLines;
562 if ( y )
563 *y = m_yScrollPixelsPerLine * m_yScrollLines;
564 }
565
566 // Where the current view starts from
567 void wxScrolledWindow::GetViewStart (int *x, int *y) const
568 {
569 if ( x )
570 *x = m_xScrollPosition;
571 if ( y )
572 *y = m_yScrollPosition;
573 }
574
575 void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
576 {
577 if ( xx )
578 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
579 if ( yy )
580 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
581 }
582
583 void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
584 {
585 if ( xx )
586 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
587 if ( yy )
588 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
589 }
590
591 // ----------------------------------------------------------------------------
592 // event handlers
593 // ----------------------------------------------------------------------------
594
595 // Default OnSize resets scrollbars, if any
596 void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
597 {
598 #if wxUSE_CONSTRAINTS
599 if (GetAutoLayout())
600 Layout();
601 #endif
602
603 AdjustScrollbars();
604 }
605
606 // This calls OnDraw, having adjusted the origin according to the current
607 // scroll position
608 void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
609 {
610 wxPaintDC dc(this);
611 PrepareDC(dc);
612
613 OnDraw(dc);
614 }
615
616 // kbd handling
617 void wxScrolledWindow::OnKeyDown(wxKeyEvent& event)
618 {
619 if ( !m_xScrollPixelsPerLine || !m_yScrollPixelsPerLine )
620 {
621 // stop now - no scroll line size
622 event.Skip();
623
624 return;
625 }
626
627 int stx, sty, // view origin
628 szx, szy, // view size (total)
629 clix, cliy; // view size (on screen)
630
631 ViewStart(&stx, &sty);
632 GetClientSize(&clix, &cliy);
633 clix /= m_xScrollPixelsPerLine;
634 cliy /= m_yScrollPixelsPerLine;
635 GetVirtualSize(&szx, &szy);
636 szx /= m_xScrollPixelsPerLine;
637 szy /= m_yScrollPixelsPerLine;
638
639 switch ( event.KeyCode() )
640 {
641 case WXK_PAGEUP:
642 case WXK_PRIOR:
643 Scroll(-1, sty - (5 * cliy / 6));
644 break;
645
646 case WXK_PAGEDOWN:
647 case WXK_NEXT:
648 Scroll(-1, sty + (5 * cliy / 6));
649 break;
650
651 // notice that handling of Ctrl-Home/End is asymmetrical: Home goes to
652 // the beginning of the current line, Ctrl-Home returns to the origin
653 // while End goes to the bottom without changing horizontal position
654 // and only Ctrl-End does go to the most rightmost position as well
655 case WXK_HOME:
656 Scroll(0, event.ControlDown() ? 0 : -1);
657 break;
658
659 case WXK_END:
660 Scroll(event.ControlDown() ? szx - clix : -1, szy - cliy);
661 break;
662
663 case WXK_UP:
664 Scroll(-1, sty - 1);
665 break;
666
667 case WXK_DOWN:
668 Scroll(-1, sty + 1);
669 break;
670
671 case WXK_LEFT:
672 Scroll(stx - 1, -1);
673 break;
674
675 case WXK_RIGHT:
676 Scroll(stx + 1, -1);
677 break;
678
679 default:
680 // not for us
681 event.Skip();
682 }
683 }