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