]> git.saurik.com Git - wxWidgets.git/blob - src/generic/scrolwin.cpp
VTK wrapper of vtkRenderWindow for wxPython. Tested on MSW so far.
[wxWidgets.git] / src / generic / scrolwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
13 #pragma implementation
14 #pragma implementation "scrolwin.h"
15 #endif
16
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19
20 #include "wx/utils.h"
21 #include "wx/dcclient.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/generic/scrolwin.h"
28 #include "wx/panel.h"
29
30 #if !USE_SHARED_LIBRARY
31 BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
32 EVT_SCROLLWIN(wxScrolledWindow::OnScroll)
33 EVT_SIZE(wxScrolledWindow::OnSize)
34 EVT_PAINT(wxScrolledWindow::OnPaint)
35 END_EVENT_TABLE()
36
37 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
38 #endif
39
40 #ifdef __WXMSW__
41 #include "windows.h"
42 #endif
43
44 #ifdef __WXMOTIF__
45 // For wxRETAINED implementation
46 #include <Xm/Xm.h>
47 #endif
48
49 wxScrolledWindow::wxScrolledWindow()
50 {
51 m_xScrollPixelsPerLine = 0;
52 m_yScrollPixelsPerLine = 0;
53 m_xScrollingEnabled = TRUE;
54 m_yScrollingEnabled = TRUE;
55 m_xScrollPosition = 0;
56 m_yScrollPosition = 0;
57 m_xScrollLines = 0;
58 m_yScrollLines = 0;
59 m_xScrollLinesPerPage = 0;
60 m_yScrollLinesPerPage = 0;
61 m_scaleX = 1.0;
62 m_scaleY = 1.0;
63 }
64
65 bool wxScrolledWindow::Create(wxWindow *parent, wxWindowID id,
66 const wxPoint& pos,
67 const wxSize& size,
68 long style,
69 const wxString& name)
70 {
71 m_xScrollPixelsPerLine = 0;
72 m_yScrollPixelsPerLine = 0;
73 m_xScrollingEnabled = TRUE;
74 m_yScrollingEnabled = TRUE;
75 m_xScrollPosition = 0;
76 m_yScrollPosition = 0;
77 m_xScrollLines = 0;
78 m_yScrollLines = 0;
79 m_xScrollLinesPerPage = 0;
80 m_yScrollLinesPerPage = 0;
81 m_scaleX = 1.0;
82 m_scaleY = 1.0;
83
84 m_targetWindow = this;
85
86 return wxPanel::Create(parent, id, pos, size, style, name);
87 }
88
89 /*
90 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
91 * noUnitsX/noUnitsY: : no. units per scrollbar
92 */
93 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
94 int noUnitsX, int noUnitsY,
95 int xPos, int yPos, bool noRefresh )
96 {
97 bool do_refresh =
98 (
99 (noUnitsX != 0 && m_xScrollLines == 0) ||
100 (noUnitsX < m_xScrollLines) ||
101 (noUnitsY != 0 && m_yScrollLines == 0) ||
102 (noUnitsY < m_yScrollLines) ||
103 (xPos != m_xScrollPosition) ||
104 (yPos != m_yScrollPosition) ||
105 (pixelsPerUnitX != m_xScrollPixelsPerLine) ||
106 (pixelsPerUnitY != m_yScrollPixelsPerLine)
107 );
108
109 m_xScrollPixelsPerLine = pixelsPerUnitX;
110 m_yScrollPixelsPerLine = pixelsPerUnitY;
111 m_xScrollPosition = xPos;
112 m_yScrollPosition = yPos;
113 m_xScrollLines = noUnitsX;
114 m_yScrollLines = noUnitsY;
115
116 #ifdef __WXMOTIF__
117 // Sorry, some Motif-specific code to implement a backing pixmap
118 // for the wxRETAINED style. Implementing a backing store can't
119 // be entirely generic because it relies on the wxWindowDC implementation
120 // to duplicate X drawing calls for the backing pixmap.
121
122 if ((m_windowStyle & wxRETAINED) == wxRETAINED)
123 {
124 Display* dpy = XtDisplay((Widget) GetMainWidget());
125
126 int totalPixelWidth = m_xScrollLines * m_xScrollPixelsPerLine;
127 int totalPixelHeight = m_yScrollLines * m_yScrollPixelsPerLine;
128 if (m_backingPixmap &&
129 !((m_pixmapWidth == totalPixelWidth) &&
130 (m_pixmapHeight == totalPixelHeight)))
131 {
132 XFreePixmap (dpy, (Pixmap) m_backingPixmap);
133 m_backingPixmap = (WXPixmap) 0;
134 }
135
136 if (!m_backingPixmap &&
137 (noUnitsX != 0) && (noUnitsY != 0))
138 {
139 int depth = wxDisplayDepth();
140 m_pixmapWidth = totalPixelWidth;
141 m_pixmapHeight = totalPixelHeight;
142 m_backingPixmap = (WXPixmap) XCreatePixmap (dpy, RootWindow (dpy, DefaultScreen (dpy)),
143 m_pixmapWidth, m_pixmapHeight, depth);
144 }
145
146 }
147 #endif
148
149 AdjustScrollbars();
150
151 if (do_refresh && !noRefresh)
152 m_targetWindow->Refresh();
153
154 #ifdef __WXMSW__
155 // Necessary?
156 UpdateWindow ((HWND) m_targetWindow->GetHWND());
157 #endif
158 #ifdef __WXMAC__
159 m_targetWindow->MacUpdateImmediately() ;
160 #endif
161 }
162
163 wxScrolledWindow::~wxScrolledWindow()
164 {
165 }
166
167 void wxScrolledWindow::SetTargetWindow( wxWindow *target )
168 {
169 wxASSERT_MSG( target, wxT("target window must not be NULL") );
170 m_targetWindow = target;
171 }
172
173 wxWindow *wxScrolledWindow::GetTargetWindow()
174 {
175 return m_targetWindow;
176 }
177
178 void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
179 {
180 int orient = event.GetOrientation();
181
182 int nScrollInc = CalcScrollInc(event);
183 if (nScrollInc == 0) return;
184
185 if (orient == wxHORIZONTAL)
186 {
187 int newPos = m_xScrollPosition + nScrollInc;
188 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
189 }
190 else
191 {
192 int newPos = m_yScrollPosition + nScrollInc;
193 SetScrollPos(wxVERTICAL, newPos, TRUE );
194 }
195
196 if (orient == wxHORIZONTAL)
197 {
198 m_xScrollPosition += nScrollInc;
199 }
200 else
201 {
202 m_yScrollPosition += nScrollInc;
203 }
204
205 if (orient == wxHORIZONTAL)
206 {
207 if (m_xScrollingEnabled)
208 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
209 else
210 m_targetWindow->Refresh();
211 }
212 else
213 {
214 if (m_yScrollingEnabled)
215 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
216 else
217 m_targetWindow->Refresh();
218 }
219 #ifdef __WXMAC__
220 m_targetWindow->MacUpdateImmediately() ;
221 #endif
222 }
223
224 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
225 {
226 int pos = event.GetPosition();
227 int orient = event.GetOrientation();
228
229 int nScrollInc = 0;
230 switch (event.GetEventType())
231 {
232 case wxEVT_SCROLLWIN_TOP:
233 {
234 if (orient == wxHORIZONTAL)
235 nScrollInc = - m_xScrollPosition;
236 else
237 nScrollInc = - m_yScrollPosition;
238 break;
239 }
240 case wxEVT_SCROLLWIN_BOTTOM:
241 {
242 if (orient == wxHORIZONTAL)
243 nScrollInc = m_xScrollLines - m_xScrollPosition;
244 else
245 nScrollInc = m_yScrollLines - m_yScrollPosition;
246 break;
247 }
248 case wxEVT_SCROLLWIN_LINEUP:
249 {
250 nScrollInc = -1;
251 break;
252 }
253 case wxEVT_SCROLLWIN_LINEDOWN:
254 {
255 nScrollInc = 1;
256 break;
257 }
258 case wxEVT_SCROLLWIN_PAGEUP:
259 {
260 if (orient == wxHORIZONTAL)
261 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
262 else
263 nScrollInc = -GetScrollPageSize(wxVERTICAL);
264 break;
265 }
266 case wxEVT_SCROLLWIN_PAGEDOWN:
267 {
268 if (orient == wxHORIZONTAL)
269 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
270 else
271 nScrollInc = GetScrollPageSize(wxVERTICAL);
272 break;
273 }
274 case wxEVT_SCROLLWIN_THUMBTRACK:
275 {
276 if (orient == wxHORIZONTAL)
277 nScrollInc = pos - m_xScrollPosition;
278 else
279 nScrollInc = pos - m_yScrollPosition;
280 break;
281 }
282 default:
283 {
284 break;
285 }
286 }
287
288 if (orient == wxHORIZONTAL)
289 {
290 if (m_xScrollPixelsPerLine > 0)
291 {
292 int w, h;
293 m_targetWindow->GetClientSize(&w, &h);
294
295 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
296 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
297 if (noPositions < 0)
298 noPositions = 0;
299
300 if ( (m_xScrollPosition + nScrollInc) < 0 )
301 nScrollInc = -m_xScrollPosition; // As -ve as we can go
302 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
303 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
304 }
305 else
306 m_targetWindow->Refresh();
307 }
308 else
309 {
310 if (m_yScrollPixelsPerLine > 0)
311 {
312 int w, h;
313 m_targetWindow->GetClientSize(&w, &h);
314
315 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
316 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
317 if (noPositions < 0)
318 noPositions = 0;
319
320 if ( (m_yScrollPosition + nScrollInc) < 0 )
321 nScrollInc = -m_yScrollPosition; // As -ve as we can go
322 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
323 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
324 }
325 else
326 m_targetWindow->Refresh();
327 }
328
329 return nScrollInc;
330 }
331
332 // Adjust the scrollbars - new version.
333 void wxScrolledWindow::AdjustScrollbars()
334 {
335 int w, h;
336 m_targetWindow->GetClientSize(&w, &h);
337
338 int oldXScroll = m_xScrollPosition;
339 int oldYScroll = m_yScrollPosition;
340
341 if (m_xScrollLines > 0)
342 {
343 // Calculate page size i.e. number of scroll units you get on the
344 // current client window
345 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
346 if (noPagePositions < 1) noPagePositions = 1;
347
348 // Correct position if greater than extent of canvas minus
349 // the visible portion of it or if below zero
350 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
351 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
352
353 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
354 // The amount by which we scroll when paging
355 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
356 }
357 else
358 {
359 m_xScrollPosition = 0;
360 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
361 }
362
363 if (m_yScrollLines > 0)
364 {
365 // Calculate page size i.e. number of scroll units you get on the
366 // current client window
367 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
368 if (noPagePositions < 1) noPagePositions = 1;
369
370 // Correct position if greater than extent of canvas minus
371 // the visible portion of it or if below zero
372 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
373 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
374
375 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
376 // The amount by which we scroll when paging
377 SetScrollPageSize(wxVERTICAL, noPagePositions);
378 }
379 else
380 {
381 m_yScrollPosition = 0;
382 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
383 }
384
385 if (oldXScroll != m_xScrollPosition)
386 {
387 if (m_xScrollingEnabled)
388 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0, (const wxRect *) NULL );
389 else
390 m_targetWindow->Refresh();
391 }
392
393 if (oldYScroll != m_yScrollPosition)
394 {
395 if (m_yScrollingEnabled)
396 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), (const wxRect *) NULL );
397 else
398 m_targetWindow->Refresh();
399 }
400 }
401
402 // Default OnSize resets scrollbars, if any
403 void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
404 {
405 #if wxUSE_CONSTRAINTS
406 if (GetAutoLayout()) Layout();
407 #endif
408
409 AdjustScrollbars();
410 }
411
412 // This calls OnDraw, having adjusted the origin according to the current
413 // scroll position
414 void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
415 {
416 wxPaintDC dc(this);
417 PrepareDC(dc);
418
419 OnDraw(dc);
420 }
421
422 // Override this function if you don't want to have wxScrolledWindow
423 // automatically change the origin according to the scroll position.
424 void wxScrolledWindow::PrepareDC(wxDC& dc)
425 {
426 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
427 -m_yScrollPosition * m_yScrollPixelsPerLine );
428 dc.SetUserScale( m_scaleX, m_scaleY );
429 }
430
431 #if WXWIN_COMPATIBILITY
432 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
433 {
434 *x_page = GetScrollPageSize(wxHORIZONTAL);
435 *y_page = GetScrollPageSize(wxVERTICAL);
436 }
437
438 void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
439 {
440 if ( xx )
441 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
442 if ( yy )
443 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
444 }
445 #endif // WXWIN_COMPATIBILITY
446
447 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
448 {
449 if ( x_unit )
450 *x_unit = m_xScrollPixelsPerLine;
451 if ( y_unit )
452 *y_unit = m_yScrollPixelsPerLine;
453 }
454
455 int wxScrolledWindow::GetScrollPageSize(int orient) const
456 {
457 if ( orient == wxHORIZONTAL )
458 return m_xScrollLinesPerPage;
459 else
460 return m_yScrollLinesPerPage;
461 }
462
463 void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
464 {
465 if ( orient == wxHORIZONTAL )
466 m_xScrollLinesPerPage = pageSize;
467 else
468 m_yScrollLinesPerPage = pageSize;
469 }
470
471 /*
472 * Scroll to given position (scroll position, not pixel position)
473 */
474 void wxScrolledWindow::Scroll( int x_pos, int y_pos )
475 {
476 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
477 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
478
479 int w, h;
480 m_targetWindow->GetClientSize(&w, &h);
481
482 if (x_pos != -1)
483 {
484 int old_x = m_xScrollPosition;
485 m_xScrollPosition = x_pos;
486
487 // Calculate page size i.e. number of scroll units you get on the
488 // current client window
489 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
490 if (noPagePositions < 1) noPagePositions = 1;
491
492 // Correct position if greater than extent of canvas minus
493 // the visible portion of it or if below zero
494 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
495 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
496
497 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE );
498
499 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
500 }
501 if (y_pos != -1)
502 {
503 int old_y = m_yScrollPosition;
504 m_yScrollPosition = y_pos;
505
506 // Calculate page size i.e. number of scroll units you get on the
507 // current client window
508 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
509 if (noPagePositions < 1) noPagePositions = 1;
510
511 // Correct position if greater than extent of canvas minus
512 // the visible portion of it or if below zero
513 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
514 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
515
516 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
517
518 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
519 }
520
521
522 #ifdef __WXMSW__
523 // ::UpdateWindow ((HWND) GetHWND());
524 #else
525 // Refresh();
526 #endif
527 #ifdef __WXMAC__
528 m_targetWindow->MacUpdateImmediately() ;
529 #endif
530 }
531
532 void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
533 {
534 m_xScrollingEnabled = x_scroll;
535 m_yScrollingEnabled = y_scroll;
536 }
537
538 void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
539 {
540 if ( x )
541 *x = m_xScrollPixelsPerLine * m_xScrollLines;
542 if ( y )
543 *y = m_yScrollPixelsPerLine * m_yScrollLines;
544 }
545
546 // Where the current view starts from
547 void wxScrolledWindow::ViewStart (int *x, int *y) const
548 {
549 if ( x )
550 *x = m_xScrollPosition;
551 if ( y )
552 *y = m_yScrollPosition;
553 }
554
555 void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
556 {
557 if ( xx )
558 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
559 if ( yy )
560 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
561 }
562
563 void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
564 {
565 if ( xx )
566 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
567 if ( yy )
568 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
569 }