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