1 /////////////////////////////////////////////////////////////////////////////
2 // Name: ecscrolwin.cpp
3 // Purpose: ecScrolledWindow implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "ecscrolwin.h"
25 #define XtDisplay XTDISPLAY
28 // Includes other headers for precompiled compilation
36 #include "wx/dcclient.h"
39 #include "ecscrolwin.h"
46 // For wxRETAINED implementation
47 #ifdef __VMS__ //VMS's Xm.h is not (yet) compatible with C++
48 //This code switches off the compiler warnings
49 # pragma message disable nosimpint
53 # pragma message enable nosimpint
57 #if !ecUSE_OWN_SCROLLED_WINDOW
59 IMPLEMENT_CLASS(ecScrolledWindow, wxScrolledWindow)
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 BEGIN_EVENT_TABLE(ecScrolledWindow, wxPanel)
68 EVT_SCROLLWIN(ecScrolledWindow::OnScroll)
69 EVT_SIZE(ecScrolledWindow::OnSize)
70 EVT_PAINT(ecScrolledWindow::OnPaint)
71 EVT_CHAR(ecScrolledWindow::OnChar)
74 IMPLEMENT_DYNAMIC_CLASS(ecScrolledWindow, wxPanel)
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
81 // ecScrolledWindow creation
82 // ----------------------------------------------------------------------------
84 ecScrolledWindow::ecScrolledWindow()
86 m_xScrollPixelsPerLine = 0;
87 m_yScrollPixelsPerLine = 0;
88 m_xScrollingEnabled = TRUE;
89 m_yScrollingEnabled = TRUE;
90 m_xScrollPosition = 0;
91 m_yScrollPosition = 0;
94 m_xScrollLinesPerPage = 0;
95 m_yScrollLinesPerPage = 0;
98 m_targetWindow = (wxWindow*) NULL;
101 bool ecScrolledWindow::Create(wxWindow *parent,
106 const wxString& name)
108 m_xScrollPixelsPerLine = 0;
109 m_yScrollPixelsPerLine = 0;
110 m_xScrollingEnabled = TRUE;
111 m_yScrollingEnabled = TRUE;
112 m_xScrollPosition = 0;
113 m_yScrollPosition = 0;
116 m_xScrollLinesPerPage = 0;
117 m_yScrollLinesPerPage = 0;
121 m_targetWindow = this;
123 bool ok = wxPanel::Create(parent, id, pos, size, style, name);
125 #if defined(__WXMSW__) && (wxVERSION_NUMBER < 2302)
126 // we need to process arrows ourselves for scrolling
127 m_lDlgCode |= DLGC_WANTARROWS;
133 ecScrolledWindow::~ecScrolledWindow()
137 // ----------------------------------------------------------------------------
138 // setting scrolling parameters
139 // ----------------------------------------------------------------------------
142 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
143 * noUnitsX/noUnitsY: : no. units per scrollbar
145 void ecScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
146 int noUnitsX, int noUnitsY,
147 int xPos, int yPos, bool noRefresh )
151 CalcUnscrolledPosition(xPos, yPos, &xpos, &ypos);
154 (noUnitsX != 0 && m_xScrollLines == 0) ||
155 (noUnitsX < m_xScrollLines && xpos > pixelsPerUnitX*noUnitsX) ||
157 (noUnitsY != 0 && m_yScrollLines == 0) ||
158 (noUnitsY < m_yScrollLines && ypos > pixelsPerUnitY*noUnitsY) ||
159 (xPos != m_xScrollPosition) ||
160 (yPos != m_yScrollPosition)
161 // (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
162 // (pixelsPerUnitY != m_yScrollPixelsPerLine)
165 m_xScrollPixelsPerLine = pixelsPerUnitX;
166 m_yScrollPixelsPerLine = pixelsPerUnitY;
167 m_xScrollPosition = xPos;
168 m_yScrollPosition = yPos;
169 m_xScrollLines = noUnitsX;
170 m_yScrollLines = noUnitsY;
173 // Sorry, some Motif-specific code to implement a backing pixmap
174 // for the wxRETAINED style. Implementing a backing store can't
175 // be entirely generic because it relies on the wxWindowDC implementation
176 // to duplicate X drawing calls for the backing pixmap.
178 if ((m_windowStyle & wxRETAINED) == wxRETAINED)
180 Display* dpy = XtDisplay((Widget) GetMainWidget());
182 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
183 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
184 if (m_backingPixmap &&
185 !((m_pixmapWidth == totalPixelWidth) &&
186 (m_pixmapHeight == totalPixelHeight)))
188 XFreePixmap (dpy, (Pixmap) m_backingPixmap);
189 m_backingPixmap = (WXPixmap) 0;
192 if (!m_backingPixmap &&
193 (noUnitsX != 0) && (noUnitsY != 0))
195 int depth = wxDisplayDepth();
196 m_pixmapWidth = totalPixelWidth;
197 m_pixmapHeight = totalPixelHeight;
198 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
199 m_pixmapWidth, m_pixmapHeight, depth);
207 if (do_refresh && !noRefresh)
208 m_targetWindow->Refresh();
211 // GRG: if this turns out to be really necessary, we could
212 // at least move it to the above if { ... } so that it is
213 // only done if noRefresh = FALSE (the default). OTOH, if
214 // this doesn't break anything, which seems to be the
215 // case, we could just leave it out.
218 // UpdateWindow ((HWND) m_targetWindow->GetHWND());
221 m_targetWindow->MacUpdateImmediately() ;
225 // ----------------------------------------------------------------------------
226 // target window handling
227 // ----------------------------------------------------------------------------
229 void ecScrolledWindow::SetTargetWindow( wxWindow *target )
231 wxASSERT_MSG( target, wxT("target window must not be NULL") );
232 m_targetWindow = target;
235 wxWindow *ecScrolledWindow::GetTargetWindow()
237 return m_targetWindow;
240 // ----------------------------------------------------------------------------
241 // scrolling implementation itself
242 // ----------------------------------------------------------------------------
244 void ecScrolledWindow::OnScroll(wxScrollWinEvent& event)
246 int orient = event.GetOrientation();
248 int nScrollInc = CalcScrollInc(event);
249 if (nScrollInc == 0) return;
251 if (orient == wxHORIZONTAL)
253 int newPos = m_xScrollPosition + nScrollInc;
254 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
258 int newPos = m_yScrollPosition + nScrollInc;
259 SetScrollPos(wxVERTICAL, newPos, TRUE );
262 if (orient == wxHORIZONTAL)
264 m_xScrollPosition += nScrollInc;
268 m_yScrollPosition += nScrollInc;
271 if (orient == wxHORIZONTAL)
273 if (m_xScrollingEnabled)
274 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
276 m_targetWindow->Refresh();
280 if (m_yScrollingEnabled)
281 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
283 m_targetWindow->Refresh();
286 m_targetWindow->MacUpdateImmediately() ;
290 int ecScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
292 int pos = event.GetPosition();
293 int orient = event.GetOrientation();
296 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
298 if (orient == wxHORIZONTAL)
299 nScrollInc = - m_xScrollPosition;
301 nScrollInc = - m_yScrollPosition;
303 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
305 if (orient == wxHORIZONTAL)
306 nScrollInc = m_xScrollLines - m_xScrollPosition;
308 nScrollInc = m_yScrollLines - m_yScrollPosition;
310 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
314 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
318 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
320 if (orient == wxHORIZONTAL)
321 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
323 nScrollInc = -GetScrollPageSize(wxVERTICAL);
325 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
327 if (orient == wxHORIZONTAL)
328 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
330 nScrollInc = GetScrollPageSize(wxVERTICAL);
332 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
333 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
335 if (orient == wxHORIZONTAL)
336 nScrollInc = pos - m_xScrollPosition;
338 nScrollInc = pos - m_yScrollPosition;
341 if (orient == wxHORIZONTAL)
343 if (m_xScrollPixelsPerLine > 0)
346 m_targetWindow->GetClientSize(&w, &h);
348 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
349 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
353 if ( (m_xScrollPosition + nScrollInc) < 0 )
354 nScrollInc = -m_xScrollPosition; // As -ve as we can go
355 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
356 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
359 m_targetWindow->Refresh();
363 if (m_yScrollPixelsPerLine > 0)
366 m_targetWindow->GetClientSize(&w, &h);
368 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
369 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
373 if ( (m_yScrollPosition + nScrollInc) < 0 )
374 nScrollInc = -m_yScrollPosition; // As -ve as we can go
375 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
376 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
379 m_targetWindow->Refresh();
385 // Adjust the scrollbars - new version.
386 void ecScrolledWindow::AdjustScrollbars()
389 m_targetWindow->GetClientSize(&w, &h);
391 int oldXScroll = m_xScrollPosition;
392 int oldYScroll = m_yScrollPosition;
394 if (m_xScrollLines > 0)
396 // Calculate page size i.e. number of scroll units you get on the
397 // current client window
398 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
399 if (noPagePositions < 1) noPagePositions = 1;
401 // Correct position if greater than extent of canvas minus
402 // the visible portion of it or if below zero
403 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
404 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
406 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
407 // The amount by which we scroll when paging
408 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
412 m_xScrollPosition = 0;
413 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
416 if (m_yScrollLines > 0)
418 // Calculate page size i.e. number of scroll units you get on the
419 // current client window
420 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
421 if (noPagePositions < 1) noPagePositions = 1;
423 // Correct position if greater than extent of canvas minus
424 // the visible portion of it or if below zero
425 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
426 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
428 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
429 // The amount by which we scroll when paging
430 SetScrollPageSize(wxVERTICAL, noPagePositions);
434 m_yScrollPosition = 0;
435 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
438 if (oldXScroll != m_xScrollPosition)
440 if (m_xScrollingEnabled)
441 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0, (const wxRect *) NULL );
443 m_targetWindow->Refresh();
446 if (oldYScroll != m_yScrollPosition)
448 if (m_yScrollingEnabled)
449 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), (const wxRect *) NULL );
451 m_targetWindow->Refresh();
455 // Override this function if you don't want to have ecScrolledWindow
456 // automatically change the origin according to the scroll position.
457 void ecScrolledWindow::PrepareDC(wxDC& dc)
459 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
460 -m_yScrollPosition * m_yScrollPixelsPerLine );
461 dc.SetUserScale( m_scaleX, m_scaleY );
464 #if WXWIN_COMPATIBILITY
465 void ecScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
467 *x_page = GetScrollPageSize(wxHORIZONTAL);
468 *y_page = GetScrollPageSize(wxVERTICAL);
471 void ecScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
474 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
476 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
478 #endif // WXWIN_COMPATIBILITY
480 void ecScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
483 *x_unit = m_xScrollPixelsPerLine;
485 *y_unit = m_yScrollPixelsPerLine;
488 int ecScrolledWindow::GetScrollPageSize(int orient) const
490 if ( orient == wxHORIZONTAL )
491 return m_xScrollLinesPerPage;
493 return m_yScrollLinesPerPage;
496 void ecScrolledWindow::SetScrollPageSize(int orient, int pageSize)
498 if ( orient == wxHORIZONTAL )
499 m_xScrollLinesPerPage = pageSize;
501 m_yScrollLinesPerPage = pageSize;
505 * Scroll to given position (scroll position, not pixel position)
507 void ecScrolledWindow::Scroll( int x_pos, int y_pos )
512 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
513 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
516 m_targetWindow->GetClientSize(&w, &h);
518 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
520 int old_x = m_xScrollPosition;
521 m_xScrollPosition = x_pos;
523 // Calculate page size i.e. number of scroll units you get on the
524 // current client window
525 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
526 if (noPagePositions < 1) noPagePositions = 1;
528 // Correct position if greater than extent of canvas minus
529 // the visible portion of it or if below zero
530 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
531 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
533 if (old_x != m_xScrollPosition) {
534 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE );
535 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
538 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
540 int old_y = m_yScrollPosition;
541 m_yScrollPosition = y_pos;
543 // Calculate page size i.e. number of scroll units you get on the
544 // current client window
545 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
546 if (noPagePositions < 1) noPagePositions = 1;
548 // Correct position if greater than extent of canvas minus
549 // the visible portion of it or if below zero
550 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
551 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
553 if (old_y != m_yScrollPosition) {
554 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
555 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
560 m_targetWindow->MacUpdateImmediately();
564 void ecScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
566 m_xScrollingEnabled = x_scroll;
567 m_yScrollingEnabled = y_scroll;
570 void ecScrolledWindow::GetVirtualSize (int *x, int *y) const
573 *x = m_xScrollPixelsPerLine * m_xScrollLines;
575 *y = m_yScrollPixelsPerLine * m_yScrollLines;
578 // Where the current view starts from
579 void ecScrolledWindow::GetViewStart (int *x, int *y) const
582 *x = m_xScrollPosition;
584 *y = m_yScrollPosition;
587 void ecScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
590 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
592 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
595 void ecScrolledWindow::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
598 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
600 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
603 // ----------------------------------------------------------------------------
605 // ----------------------------------------------------------------------------
607 // Default OnSize resets scrollbars, if any
608 void ecScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
610 #if wxUSE_CONSTRAINTS
618 // This calls OnDraw, having adjusted the origin according to the current
620 void ecScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
628 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
629 // compatibility here - if we used OnKeyDown(), the programs which process
630 // arrows themselves in their OnChar() would never get the message and like
631 // this they always have the priority
632 void ecScrolledWindow::OnChar(wxKeyEvent& event)
634 int stx, sty, // view origin
635 szx, szy, // view size (total)
636 clix, cliy; // view size (on screen)
638 ViewStart(&stx, &sty);
639 GetClientSize(&clix, &cliy);
640 GetVirtualSize(&szx, &szy);
642 if( m_xScrollPixelsPerLine )
644 clix /= m_xScrollPixelsPerLine;
645 szx /= m_xScrollPixelsPerLine;
652 if( m_yScrollPixelsPerLine )
654 cliy /= m_yScrollPixelsPerLine;
655 szy /= m_yScrollPixelsPerLine;
664 switch ( event.KeyCode() )
668 dsty = sty - (5 * cliy / 6);
669 Scroll(-1, (dsty == -1) ? 0 : dsty);
674 Scroll(-1, sty + (5 * cliy / 6));
678 Scroll(0, event.ControlDown() ? 0 : -1);
682 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
707 // ecUSE_OWN_SCROLLED_WINDOW