]> git.saurik.com Git - wxWidgets.git/blob - src/generic/scrolwin.cpp
1. wxMenu{Item|Bar} modifications for wxMotif
[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) GetHWND());
157 #endif
158 }
159
160 wxScrolledWindow::~wxScrolledWindow()
161 {
162 }
163
164 void wxScrolledWindow::SetTargetWindow( wxWindow *target )
165 {
166 wxASSERT_MSG( target, wxT("target window must not be NULL") );
167 m_targetWindow = target;
168 }
169
170 wxWindow *wxScrolledWindow::GetTargetWindow()
171 {
172 return m_targetWindow;
173 }
174
175 void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
176 {
177 int orient = event.GetOrientation();
178
179 int nScrollInc = CalcScrollInc(event);
180 if (nScrollInc == 0) return;
181
182 if (orient == wxHORIZONTAL)
183 {
184 int newPos = m_xScrollPosition + nScrollInc;
185 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
186 }
187 else
188 {
189 int newPos = m_yScrollPosition + nScrollInc;
190 SetScrollPos(wxVERTICAL, newPos, TRUE );
191 }
192
193 if (orient == wxHORIZONTAL)
194 {
195 m_xScrollPosition += nScrollInc;
196 }
197 else
198 {
199 m_yScrollPosition += nScrollInc;
200 }
201
202 if (orient == wxHORIZONTAL)
203 {
204 if (m_xScrollingEnabled)
205 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
206 else
207 m_targetWindow->Refresh();
208 }
209 else
210 {
211 if (m_yScrollingEnabled)
212 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
213 else
214 m_targetWindow->Refresh();
215 }
216 }
217
218 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
219 {
220 int pos = event.GetPosition();
221 int orient = event.GetOrientation();
222
223 int nScrollInc = 0;
224 switch (event.GetEventType())
225 {
226 case wxEVT_SCROLLWIN_TOP:
227 {
228 if (orient == wxHORIZONTAL)
229 nScrollInc = - m_xScrollPosition;
230 else
231 nScrollInc = - m_yScrollPosition;
232 break;
233 }
234 case wxEVT_SCROLLWIN_BOTTOM:
235 {
236 if (orient == wxHORIZONTAL)
237 nScrollInc = m_xScrollLines - m_xScrollPosition;
238 else
239 nScrollInc = m_yScrollLines - m_yScrollPosition;
240 break;
241 }
242 case wxEVT_SCROLLWIN_LINEUP:
243 {
244 nScrollInc = -1;
245 break;
246 }
247 case wxEVT_SCROLLWIN_LINEDOWN:
248 {
249 nScrollInc = 1;
250 break;
251 }
252 case wxEVT_SCROLLWIN_PAGEUP:
253 {
254 if (orient == wxHORIZONTAL)
255 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
256 else
257 nScrollInc = -GetScrollPageSize(wxVERTICAL);
258 break;
259 }
260 case wxEVT_SCROLLWIN_PAGEDOWN:
261 {
262 if (orient == wxHORIZONTAL)
263 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
264 else
265 nScrollInc = GetScrollPageSize(wxVERTICAL);
266 break;
267 }
268 case wxEVT_SCROLLWIN_THUMBTRACK:
269 {
270 if (orient == wxHORIZONTAL)
271 nScrollInc = pos - m_xScrollPosition;
272 else
273 nScrollInc = pos - m_yScrollPosition;
274 break;
275 }
276 default:
277 {
278 break;
279 }
280 }
281
282 if (orient == wxHORIZONTAL)
283 {
284 if (m_xScrollPixelsPerLine > 0)
285 {
286 int w, h;
287 m_targetWindow->GetClientSize(&w, &h);
288
289 int nMaxWidth = m_xScrollLines*m_xScrollPixelsPerLine;
290 int noPositions = (int) ( ((nMaxWidth - w)/(double)m_xScrollPixelsPerLine) + 0.5 );
291 if (noPositions < 0)
292 noPositions = 0;
293
294 if ( (m_xScrollPosition + nScrollInc) < 0 )
295 nScrollInc = -m_xScrollPosition; // As -ve as we can go
296 else if ( (m_xScrollPosition + nScrollInc) > noPositions )
297 nScrollInc = noPositions - m_xScrollPosition; // As +ve as we can go
298 }
299 else
300 m_targetWindow->Refresh();
301 }
302 else
303 {
304 if (m_yScrollPixelsPerLine > 0)
305 {
306 int w, h;
307 m_targetWindow->GetClientSize(&w, &h);
308
309 int nMaxHeight = m_yScrollLines*m_yScrollPixelsPerLine;
310 int noPositions = (int) ( ((nMaxHeight - h)/(double)m_yScrollPixelsPerLine) + 0.5 );
311 if (noPositions < 0)
312 noPositions = 0;
313
314 if ( (m_yScrollPosition + nScrollInc) < 0 )
315 nScrollInc = -m_yScrollPosition; // As -ve as we can go
316 else if ( (m_yScrollPosition + nScrollInc) > noPositions )
317 nScrollInc = noPositions - m_yScrollPosition; // As +ve as we can go
318 }
319 else
320 m_targetWindow->Refresh();
321 }
322
323 return nScrollInc;
324 }
325
326 // Adjust the scrollbars - new version.
327 void wxScrolledWindow::AdjustScrollbars()
328 {
329 int w, h;
330 m_targetWindow->GetClientSize(&w, &h);
331
332 int oldXScroll = m_xScrollPosition;
333 int oldYScroll = m_yScrollPosition;
334
335 if (m_xScrollLines > 0)
336 {
337 // Calculate page size i.e. number of scroll units you get on the
338 // current client window
339 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
340 if (noPagePositions < 1) noPagePositions = 1;
341
342 // Correct position if greater than extent of canvas minus
343 // the visible portion of it or if below zero
344 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition);
345 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
346
347 SetScrollbar(wxHORIZONTAL, m_xScrollPosition, noPagePositions, m_xScrollLines);
348 // The amount by which we scroll when paging
349 SetScrollPageSize(wxHORIZONTAL, noPagePositions);
350 }
351 else
352 {
353 m_xScrollPosition = 0;
354 SetScrollbar (wxHORIZONTAL, 0, 0, 0, FALSE);
355 }
356
357 if (m_yScrollLines > 0)
358 {
359 // Calculate page size i.e. number of scroll units you get on the
360 // current client window
361 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
362 if (noPagePositions < 1) noPagePositions = 1;
363
364 // Correct position if greater than extent of canvas minus
365 // the visible portion of it or if below zero
366 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
367 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
368
369 SetScrollbar(wxVERTICAL, m_yScrollPosition, noPagePositions, m_yScrollLines);
370 // The amount by which we scroll when paging
371 SetScrollPageSize(wxVERTICAL, noPagePositions);
372 }
373 else
374 {
375 m_yScrollPosition = 0;
376 SetScrollbar (wxVERTICAL, 0, 0, 0, FALSE);
377 }
378
379 if (oldXScroll != m_xScrollPosition)
380 {
381 if (m_xScrollingEnabled)
382 m_targetWindow->ScrollWindow( m_xScrollPixelsPerLine * (oldXScroll-m_xScrollPosition), 0, (const wxRect *) NULL );
383 else
384 m_targetWindow->Refresh();
385 }
386
387 if (oldYScroll != m_yScrollPosition)
388 {
389 if (m_yScrollingEnabled)
390 m_targetWindow->ScrollWindow( 0, m_yScrollPixelsPerLine * (oldYScroll-m_yScrollPosition), (const wxRect *) NULL );
391 else
392 m_targetWindow->Refresh();
393 }
394 }
395
396 // Default OnSize resets scrollbars, if any
397 void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
398 {
399 #if wxUSE_CONSTRAINTS
400 if (GetAutoLayout()) Layout();
401 #endif
402
403 AdjustScrollbars();
404 }
405
406 // This calls OnDraw, having adjusted the origin according to the current
407 // scroll position
408 void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
409 {
410 wxPaintDC dc(this);
411 PrepareDC(dc);
412
413 OnDraw(dc);
414 }
415
416 // Override this function if you don't want to have wxScrolledWindow
417 // automatically change the origin according to the scroll position.
418 void wxScrolledWindow::PrepareDC(wxDC& dc)
419 {
420 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
421 -m_yScrollPosition * m_yScrollPixelsPerLine );
422 dc.SetUserScale( m_scaleX, m_scaleY );
423 }
424
425 #if WXWIN_COMPATIBILITY
426 void wxScrolledWindow::GetScrollUnitsPerPage (int *x_page, int *y_page) const
427 {
428 *x_page = GetScrollPageSize(wxHORIZONTAL);
429 *y_page = GetScrollPageSize(wxVERTICAL);
430 }
431
432 void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, float *xx, float *yy) const
433 {
434 if ( xx )
435 *xx = (float)(x + m_xScrollPosition * m_xScrollPixelsPerLine);
436 if ( yy )
437 *yy = (float)(y + m_yScrollPosition * m_yScrollPixelsPerLine);
438 }
439 #endif // WXWIN_COMPATIBILITY
440
441 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
442 {
443 if ( x_unit )
444 *x_unit = m_xScrollPixelsPerLine;
445 if ( y_unit )
446 *y_unit = m_yScrollPixelsPerLine;
447 }
448
449 int wxScrolledWindow::GetScrollPageSize(int orient) const
450 {
451 if ( orient == wxHORIZONTAL )
452 return m_xScrollLinesPerPage;
453 else
454 return m_yScrollLinesPerPage;
455 }
456
457 void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
458 {
459 if ( orient == wxHORIZONTAL )
460 m_xScrollLinesPerPage = pageSize;
461 else
462 m_yScrollLinesPerPage = pageSize;
463 }
464
465 /*
466 * Scroll to given position (scroll position, not pixel position)
467 */
468 void wxScrolledWindow::Scroll( int x_pos, int y_pos )
469 {
470 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
471 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
472
473 int w, h;
474 m_targetWindow->GetClientSize(&w, &h);
475
476 if (x_pos != -1)
477 {
478 int old_x = m_xScrollPosition;
479 m_xScrollPosition = x_pos;
480
481 // Calculate page size i.e. number of scroll units you get on the
482 // current client window
483 int noPagePositions = (int) ( (w/(double)m_xScrollPixelsPerLine) + 0.5 );
484 if (noPagePositions < 1) noPagePositions = 1;
485
486 // Correct position if greater than extent of canvas minus
487 // the visible portion of it or if below zero
488 m_xScrollPosition = wxMin( m_xScrollLines-noPagePositions, m_xScrollPosition );
489 m_xScrollPosition = wxMax( 0, m_xScrollPosition );
490
491 m_targetWindow->SetScrollPos( wxHORIZONTAL, m_xScrollPosition, TRUE );
492
493 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
494 }
495 if (y_pos != -1)
496 {
497 int old_y = m_yScrollPosition;
498 m_yScrollPosition = y_pos;
499
500 // Calculate page size i.e. number of scroll units you get on the
501 // current client window
502 int noPagePositions = (int) ( (h/(double)m_yScrollPixelsPerLine) + 0.5 );
503 if (noPagePositions < 1) noPagePositions = 1;
504
505 // Correct position if greater than extent of canvas minus
506 // the visible portion of it or if below zero
507 m_yScrollPosition = wxMin( m_yScrollLines-noPagePositions, m_yScrollPosition );
508 m_yScrollPosition = wxMax( 0, m_yScrollPosition );
509
510 m_targetWindow->SetScrollPos( wxVERTICAL, m_yScrollPosition, TRUE );
511
512 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
513 }
514
515
516 #ifdef __WXMSW__
517 // ::UpdateWindow ((HWND) GetHWND());
518 #else
519 // Refresh();
520 #endif
521 }
522
523 void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
524 {
525 m_xScrollingEnabled = x_scroll;
526 m_yScrollingEnabled = y_scroll;
527 }
528
529 void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
530 {
531 if ( x )
532 *x = m_xScrollPixelsPerLine * m_xScrollLines;
533 if ( y )
534 *y = m_yScrollPixelsPerLine * m_yScrollLines;
535 }
536
537 // Where the current view starts from
538 void wxScrolledWindow::ViewStart (int *x, int *y) const
539 {
540 if ( x )
541 *x = m_xScrollPosition;
542 if ( y )
543 *y = m_yScrollPosition;
544 }
545
546 void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
547 {
548 if ( xx )
549 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
550 if ( yy )
551 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
552 }
553
554 void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
555 {
556 if ( xx )
557 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
558 if ( yy )
559 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
560 }