Fixed GetVirtualSize for wxGTK's wxScrolledWindow
[wxWidgets.git] / src / gtk1 / scrolwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "scrolwin.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/utils.h"
32 #include "wx/dcclient.h"
33
34 #include "wx/scrolwin.h"
35 #include "wx/panel.h"
36 #include "wx/sizer.h"
37
38 #include "wx/gtk/private.h"
39 #include "wx/gtk/win_gtk.h"
40
41 // ----------------------------------------------------------------------------
42 // event tables
43 // ----------------------------------------------------------------------------
44
45 BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
46 EVT_SCROLLWIN(wxScrolledWindow::OnScroll)
47 EVT_SIZE(wxScrolledWindow::OnSize)
48 EVT_PAINT(wxScrolledWindow::OnPaint)
49 EVT_CHAR(wxScrolledWindow::OnChar)
50 END_EVENT_TABLE()
51
52 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 //-----------------------------------------------------------------------------
59 // data
60 //-----------------------------------------------------------------------------
61
62 extern bool g_blockEventsOnDrag;
63 extern bool g_blockEventsOnScroll;
64
65 //-----------------------------------------------------------------------------
66 // idle system
67 //-----------------------------------------------------------------------------
68
69 extern void wxapp_install_idle_handler();
70 extern bool g_isIdle;
71
72 //-----------------------------------------------------------------------------
73 // "value_changed" from m_vAdjust
74 //-----------------------------------------------------------------------------
75
76 static void gtk_scrolled_window_vscroll_callback( GtkAdjustment *adjust,
77 SCROLLBAR_CBACK_ARG
78 wxScrolledWindow *win )
79 {
80 if (g_isIdle)
81 wxapp_install_idle_handler();
82
83 if (g_blockEventsOnDrag) return;
84
85 if (!win->m_hasVMT) return;
86
87 win->GtkVScroll( adjust->value,
88 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win->m_widget)->vscrollbar) );
89 }
90
91 //-----------------------------------------------------------------------------
92 // "value_changed" from m_hAdjust
93 //-----------------------------------------------------------------------------
94
95 static void gtk_scrolled_window_hscroll_callback( GtkAdjustment *adjust,
96 SCROLLBAR_CBACK_ARG
97 wxScrolledWindow *win )
98 {
99 if (g_isIdle)
100 wxapp_install_idle_handler();
101
102 if (g_blockEventsOnDrag) return;
103 if (!win->m_hasVMT) return;
104
105 win->GtkHScroll( adjust->value,
106 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win->m_widget)->hscrollbar) );
107 }
108
109 //-----------------------------------------------------------------------------
110 // "button_press_event" from scrollbar
111 //-----------------------------------------------------------------------------
112
113 static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
114 GdkEventButton *gdk_event,
115 wxWindowGTK *win)
116 {
117 if (g_isIdle)
118 wxapp_install_idle_handler();
119
120 g_blockEventsOnScroll = TRUE;
121
122 // FIXME: there is no slider field any more, what was meant here?
123 #ifndef __WXGTK20__
124 win->m_isScrolling = (gdk_event->window == widget->slider);
125 #endif
126
127 return FALSE;
128 }
129
130 //-----------------------------------------------------------------------------
131 // "button_release_event" from scrollbar
132 //-----------------------------------------------------------------------------
133
134 static gint gtk_scrollbar_button_release_callback( GtkRange *widget,
135 GdkEventButton *WXUNUSED(gdk_event),
136 wxWindowGTK *win)
137 {
138 // don't test here as we can release the mouse while being over
139 // a different window than the slider
140 //
141 // if (gdk_event->window != widget->slider) return FALSE;
142
143 g_blockEventsOnScroll = FALSE;
144
145 if (win->m_isScrolling)
146 {
147 wxEventType command = wxEVT_SCROLLWIN_THUMBRELEASE;
148 int value = -1;
149 int dir = -1;
150
151 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(win->m_widget);
152 if (widget == GTK_RANGE(scrolledWindow->hscrollbar))
153 {
154 value = (int)(win->m_hAdjust->value+0.5);
155 dir = wxHORIZONTAL;
156 }
157 if (widget == GTK_RANGE(scrolledWindow->vscrollbar))
158 {
159 value = (int)(win->m_vAdjust->value+0.5);
160 dir = wxVERTICAL;
161 }
162
163 wxScrollWinEvent event( command, value, dir );
164 event.SetEventObject( win );
165 win->GetEventHandler()->ProcessEvent( event );
166 }
167
168 win->m_isScrolling = FALSE;
169
170 return FALSE;
171 }
172
173 //-----------------------------------------------------------------------------
174 // InsertChild for wxScrolledWindow
175 //-----------------------------------------------------------------------------
176
177 static void wxInsertChildInScrolledWindow( wxWindow* parent, wxWindow* child )
178 {
179 // The window might have been scrolled already, do we
180 // have to adapt the position.
181 GtkPizza *pizza = GTK_PIZZA(parent->m_wxwindow);
182 child->m_x += pizza->xoffset;
183 child->m_y += pizza->yoffset;
184
185 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
186 GTK_WIDGET(child->m_widget),
187 child->m_x,
188 child->m_y,
189 child->m_width,
190 child->m_height );
191 }
192
193 // ----------------------------------------------------------------------------
194 // wxScrolledWindow creation
195 // ----------------------------------------------------------------------------
196
197 void wxScrolledWindow::Init()
198 {
199 m_xScrollPixelsPerLine = 0;
200 m_yScrollPixelsPerLine = 0;
201 m_xScrollingEnabled = TRUE;
202 m_yScrollingEnabled = TRUE;
203 m_xScrollPosition = 0;
204 m_yScrollPosition = 0;
205 m_xScrollLines = 0;
206 m_yScrollLines = 0;
207 m_xScrollLinesPerPage = 0;
208 m_yScrollLinesPerPage = 0;
209 m_targetWindow = (wxWindow*) NULL;
210 m_scaleX = 1.0;
211 m_scaleY = 1.0;
212 m_hasScrolling = TRUE;
213 }
214
215 bool wxScrolledWindow::Create(wxWindow *parent,
216 wxWindowID id,
217 const wxPoint& pos,
218 const wxSize& size,
219 long style,
220 const wxString& name)
221 {
222 Init();
223
224 if (!PreCreation( parent, pos, size ) ||
225 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
226 {
227 wxFAIL_MSG( wxT("wxWindow creation failed") );
228 return FALSE;
229 }
230
231 m_insertCallback = wxInsertChildInScrolledWindow;
232
233 m_targetWindow = this;
234
235 m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
236 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
237
238 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(m_widget);
239
240 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
241 scroll_class->scrollbar_spacing = 0;
242
243 gtk_scrolled_window_set_policy( scrolledWindow, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
244
245 m_hAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->hscrollbar) );
246 m_vAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->vscrollbar) );
247
248 m_wxwindow = gtk_pizza_new();
249
250 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
251
252 GtkPizza *pizza = GTK_PIZZA(m_wxwindow);
253
254 if (HasFlag(wxRAISED_BORDER))
255 {
256 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_OUT );
257 }
258 else if (HasFlag(wxSUNKEN_BORDER))
259 {
260 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_IN );
261 }
262 else if (HasFlag(wxSIMPLE_BORDER))
263 {
264 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_THIN );
265 }
266 else
267 {
268 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_NONE );
269 }
270
271 GTK_WIDGET_SET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
272 m_acceptsFocus = TRUE;
273
274 // I _really_ don't want scrollbars in the beginning
275 m_vAdjust->lower = 0.0;
276 m_vAdjust->upper = 1.0;
277 m_vAdjust->value = 0.0;
278 m_vAdjust->step_increment = 1.0;
279 m_vAdjust->page_increment = 1.0;
280 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
281 m_hAdjust->lower = 0.0;
282 m_hAdjust->upper = 1.0;
283 m_hAdjust->value = 0.0;
284 m_hAdjust->step_increment = 1.0;
285 m_hAdjust->page_increment = 1.0;
286 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
287
288 // Handlers for new scrollbar values
289 GtkVConnectEvent();
290 GtkHConnectEvent();
291
292 // these handlers block mouse events to any window during scrolling such as
293 // motion events and prevent GTK and wxWindows from fighting over where the
294 // slider should be
295
296 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_press_event",
297 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
298
299 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_press_event",
300 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
301
302 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_release_event",
303 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
304
305 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_release_event",
306 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
307
308 gtk_widget_show( m_wxwindow );
309
310 if (m_parent)
311 m_parent->DoAddChild( this );
312
313 m_focusWidget = m_wxwindow;
314
315 PostCreation();
316
317 Show( TRUE );
318
319 return TRUE;
320 }
321
322 // ----------------------------------------------------------------------------
323 // setting scrolling parameters
324 // ----------------------------------------------------------------------------
325
326 /*
327 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
328 * noUnitsX/noUnitsY: : no. units per scrollbar
329 */
330 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
331 int noUnitsX, int noUnitsY,
332 int xPos, int yPos, bool noRefresh )
333 {
334 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
335 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
336
337 m_xScrollPixelsPerLine = pixelsPerUnitX;
338 m_yScrollPixelsPerLine = pixelsPerUnitY;
339 m_xScrollLines = noUnitsX;
340 m_yScrollLines = noUnitsY;
341 m_xScrollPosition = xPos;
342 m_yScrollPosition = yPos;
343
344 m_hAdjust->lower = 0.0;
345 m_hAdjust->upper = noUnitsX;
346 m_hAdjust->value = xPos;
347 m_hAdjust->step_increment = 1.0;
348 m_hAdjust->page_increment = 2.0;
349
350 m_vAdjust->lower = 0.0;
351 m_vAdjust->upper = noUnitsY;
352 m_vAdjust->value = yPos;
353 m_vAdjust->step_increment = 1.0;
354 m_vAdjust->page_increment = 2.0;
355
356 AdjustScrollbars();
357
358 if (!noRefresh)
359 {
360 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
361 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
362
363 m_targetWindow->ScrollWindow( old_x-new_x, old_y-new_y );
364 }
365 }
366
367 void wxScrolledWindow::AdjustScrollbars()
368 {
369 int w, h;
370 m_targetWindow->GetClientSize( &w, &h );
371
372 if (m_xScrollPixelsPerLine == 0)
373 m_hAdjust->page_size = 1.0;
374 else
375 m_hAdjust->page_size = (w / m_xScrollPixelsPerLine);
376
377 if (m_yScrollPixelsPerLine == 0)
378 m_vAdjust->page_size = 1.0;
379 else
380 m_vAdjust->page_size = (h / m_yScrollPixelsPerLine);
381
382 m_xScrollLinesPerPage = (int)(m_hAdjust->page_size + 0.5);
383 m_yScrollLinesPerPage = (int)(m_vAdjust->page_size + 0.5);
384
385 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
386 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
387 }
388
389 // ----------------------------------------------------------------------------
390 // target window handling
391 // ----------------------------------------------------------------------------
392
393 void wxScrolledWindow::SetTargetWindow( wxWindow *target, bool WXUNUSED(pushEventHandler) )
394 {
395 wxASSERT_MSG( target, wxT("target window must not be NULL") );
396 m_targetWindow = target;
397 }
398
399 wxWindow *wxScrolledWindow::GetTargetWindow()
400 {
401 return m_targetWindow;
402 }
403
404 // Override this function if you don't want to have wxScrolledWindow
405 // automatically change the origin according to the scroll position.
406 void wxScrolledWindow::PrepareDC(wxDC& dc)
407 {
408 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
409 -m_yScrollPosition * m_yScrollPixelsPerLine );
410 }
411
412 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
413 {
414 if ( x_unit )
415 *x_unit = m_xScrollPixelsPerLine;
416 if ( y_unit )
417 *y_unit = m_yScrollPixelsPerLine;
418 }
419
420 int wxScrolledWindow::GetScrollPageSize(int orient) const
421 {
422 if ( orient == wxHORIZONTAL )
423 return m_xScrollLinesPerPage;
424 else
425 return m_yScrollLinesPerPage;
426 }
427
428 void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
429 {
430 if ( orient == wxHORIZONTAL )
431 m_xScrollLinesPerPage = pageSize;
432 else
433 m_yScrollLinesPerPage = pageSize;
434 }
435
436 void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
437 {
438 int orient = event.GetOrientation();
439
440 int nScrollInc = CalcScrollInc(event);
441 if (nScrollInc == 0) return;
442
443 if (orient == wxHORIZONTAL)
444 {
445 int newPos = m_xScrollPosition + nScrollInc;
446 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
447 }
448 else
449 {
450 int newPos = m_yScrollPosition + nScrollInc;
451 SetScrollPos(wxVERTICAL, newPos, TRUE );
452 }
453
454 if (orient == wxHORIZONTAL)
455 {
456 m_xScrollPosition += nScrollInc;
457 }
458 else
459 {
460 m_yScrollPosition += nScrollInc;
461 }
462
463 if (orient == wxHORIZONTAL)
464 {
465 if (m_xScrollingEnabled)
466 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
467 else
468 m_targetWindow->Refresh();
469 }
470 else
471 {
472 if (m_yScrollingEnabled)
473 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
474 else
475 m_targetWindow->Refresh();
476 }
477 }
478
479 void wxScrolledWindow::Scroll( int x_pos, int y_pos )
480 {
481 if (!m_targetWindow)
482 return;
483
484 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
485 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
486
487 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
488 {
489 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
490 if (max < 0) max = 0;
491 if (x_pos > max) x_pos = max;
492 if (x_pos < 0) x_pos = 0;
493
494 int old_x = m_xScrollPosition;
495 m_xScrollPosition = x_pos;
496 m_hAdjust->value = x_pos;
497
498 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
499
500 // Just update the scrollbar, don't send any wxWindows event
501 GtkHDisconnectEvent();
502 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
503 GtkHConnectEvent();
504 }
505
506 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
507 {
508 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
509 if (max < 0) max = 0;
510 if (y_pos > max) y_pos = max;
511 if (y_pos < 0) y_pos = 0;
512
513 int old_y = m_yScrollPosition;
514 m_yScrollPosition = y_pos;
515 m_vAdjust->value = y_pos;
516
517 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
518
519 // Just update the scrollbar, don't send any wxWindows event
520 GtkVDisconnectEvent();
521 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
522 GtkVConnectEvent();
523 }
524 }
525
526 // TODO: [VH]Scroll functions should be combined
527
528 void wxScrolledWindow::GtkVScroll( float value, unsigned int scroll_type )
529 {
530 if (!m_targetWindow)
531 return;
532
533 if (m_yScrollPixelsPerLine == 0)
534 return;
535
536 int y_pos = (int)(value+0.5);
537
538 if (y_pos == m_yScrollPosition)
539 return;
540
541 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
542
543 wxScrollWinEvent event( command, y_pos, wxVERTICAL );
544 event.SetEventObject( this );
545 GetEventHandler()->ProcessEvent( event );
546 }
547
548 void wxScrolledWindow::GtkHScroll( float value, unsigned int scroll_type )
549 {
550 if (!m_targetWindow)
551 return;
552
553 if (m_xScrollPixelsPerLine == 0)
554 return;
555
556 int x_pos = (int)(value+0.5);
557
558 if (x_pos == m_xScrollPosition)
559 return;
560
561 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
562
563 wxScrollWinEvent event( command, x_pos, wxHORIZONTAL );
564 event.SetEventObject( this );
565 GetEventHandler()->ProcessEvent( event );
566 }
567
568 void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
569 {
570 m_xScrollingEnabled = x_scroll;
571 m_yScrollingEnabled = y_scroll;
572 }
573
574 void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
575 {
576 wxSize sz(0, 0);
577 if (m_targetWindow)
578 sz = m_targetWindow->GetClientSize();
579
580 if ( x )
581 {
582 if (m_xScrollPixelsPerLine == 0)
583 *x = sz.x;
584 else
585 *x = m_xScrollPixelsPerLine * m_xScrollLines;
586 }
587 if ( y )
588 {
589 if (m_yScrollPixelsPerLine == 0)
590 *y = sz.y;
591 else
592 *y = m_yScrollPixelsPerLine * m_yScrollLines;
593 }
594 }
595
596 // Where the current view starts from
597 void wxScrolledWindow::GetViewStart (int *x, int *y) const
598 {
599 if ( x )
600 *x = m_xScrollPosition;
601 if ( y )
602 *y = m_yScrollPosition;
603 }
604
605 void wxScrolledWindow::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
606 {
607 if ( xx )
608 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
609 if ( yy )
610 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
611 }
612
613 void wxScrolledWindow::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
614 {
615 if ( xx )
616 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
617 if ( yy )
618 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
619 }
620
621 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
622 {
623 int pos = event.GetPosition();
624 int orient = event.GetOrientation();
625
626 int nScrollInc = 0;
627 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
628 {
629 if (orient == wxHORIZONTAL)
630 nScrollInc = - m_xScrollPosition;
631 else
632 nScrollInc = - m_yScrollPosition;
633 } else
634 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
635 {
636 if (orient == wxHORIZONTAL)
637 nScrollInc = m_xScrollLines - m_xScrollPosition;
638 else
639 nScrollInc = m_yScrollLines - m_yScrollPosition;
640 } else
641 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
642 {
643 nScrollInc = -1;
644 } else
645 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
646 {
647 nScrollInc = 1;
648 } else
649 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
650 {
651 if (orient == wxHORIZONTAL)
652 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
653 else
654 nScrollInc = -GetScrollPageSize(wxVERTICAL);
655 } else
656 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
657 {
658 if (orient == wxHORIZONTAL)
659 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
660 else
661 nScrollInc = GetScrollPageSize(wxVERTICAL);
662 } else
663 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
664 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
665 {
666 if (orient == wxHORIZONTAL)
667 nScrollInc = pos - m_xScrollPosition;
668 else
669 nScrollInc = pos - m_yScrollPosition;
670 }
671
672 if (orient == wxHORIZONTAL)
673 {
674 if (m_xScrollPixelsPerLine > 0)
675 {
676 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
677 if (max < 0) max = 0;
678
679 if ( (m_xScrollPosition + nScrollInc) < 0 )
680 nScrollInc = -m_xScrollPosition; // As -ve as we can go
681 else if ( (m_xScrollPosition + nScrollInc) > max )
682 nScrollInc = max - m_xScrollPosition; // As +ve as we can go
683 }
684 else
685 m_targetWindow->Refresh();
686 }
687 else
688 {
689 if (m_yScrollPixelsPerLine > 0)
690 {
691 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
692 if (max < 0) max = 0;
693
694 if ( (m_yScrollPosition + nScrollInc) < 0 )
695 nScrollInc = -m_yScrollPosition; // As -ve as we can go
696 else if ( (m_yScrollPosition + nScrollInc) > max )
697 nScrollInc = max - m_yScrollPosition; // As +ve as we can go
698 }
699 else
700 m_targetWindow->Refresh();
701 }
702
703 return nScrollInc;
704 }
705
706 void wxScrolledWindow::SetScrollPos( int orient, int pos, bool refresh )
707 {
708 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
709
710 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
711
712 if (orient == wxHORIZONTAL)
713 {
714 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
715 if (max < 0) max = 0;
716
717 if (pos > max) pos = 0;
718 if (pos < 0) pos = 0;
719
720 if (pos == (int)(m_hAdjust->value+0.5)) return;
721 m_hAdjust->value = pos;
722 }
723 else
724 {
725 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
726 if (max < 0) max = 0;
727
728 if (pos > max) pos = 0;
729 if (pos < 0) pos = 0;
730
731 if (pos == (int)(m_vAdjust->value+0.5)) return;
732 m_vAdjust->value = pos;
733 }
734
735 if (m_wxwindow->window)
736 {
737 if (orient == wxHORIZONTAL)
738 {
739 // Just update the scrollbar, don't send any wxWindows event
740 GtkHDisconnectEvent();
741 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
742 GtkHConnectEvent();
743 }
744 else
745 {
746 // Just update the scrollbar, don't send any wxWindows event
747 GtkVDisconnectEvent();
748 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
749 GtkVConnectEvent();
750 }
751 }
752 }
753
754 void wxScrolledWindow::GtkVConnectEvent()
755 {
756 gtk_signal_connect( GTK_OBJECT(m_vAdjust), "value_changed",
757 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
758 }
759
760 void wxScrolledWindow::GtkHConnectEvent()
761 {
762 gtk_signal_connect( GTK_OBJECT(m_hAdjust), "value_changed",
763 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
764 }
765
766 void wxScrolledWindow::GtkHDisconnectEvent()
767 {
768 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust),
769 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
770 }
771
772 void wxScrolledWindow::GtkVDisconnectEvent()
773 {
774 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust),
775 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
776 }
777
778
779 bool wxScrolledWindow::Layout()
780 {
781 if (GetSizer())
782 {
783 // Take into account the virtual size and scrolled position of the window
784 int x, y, w, h;
785 CalcScrolledPosition(0,0, &x,&y);
786 GetVirtualSize(&w, &h);
787 GetSizer()->SetDimension(x, y, w, h);
788 return TRUE;
789 }
790 else
791 return wxPanel::Layout(); // fall back to default for LayoutConstraints
792 }
793
794 // ----------------------------------------------------------------------------
795 // event handlers
796 // ----------------------------------------------------------------------------
797
798 // Default OnSize resets scrollbars, if any
799 void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
800 {
801 #if wxUSE_CONSTRAINTS
802 if (GetAutoLayout())
803 Layout();
804 #endif
805
806 AdjustScrollbars();
807 }
808
809 // This calls OnDraw, having adjusted the origin according to the current
810 // scroll position
811 void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
812 {
813 wxPaintDC dc(this);
814 PrepareDC(dc);
815
816 OnDraw(dc);
817 }
818
819 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
820 // compatibility here - if we used OnKeyDown(), the programs which process
821 // arrows themselves in their OnChar() would never get the message and like
822 // this they always have the priority
823 void wxScrolledWindow::OnChar(wxKeyEvent& event)
824 {
825 int stx, sty, // view origin
826 szx, szy, // view size (total)
827 clix, cliy; // view size (on screen)
828
829 ViewStart(&stx, &sty);
830 GetClientSize(&clix, &cliy);
831 GetVirtualSize(&szx, &szy);
832
833 if( m_xScrollPixelsPerLine )
834 {
835 clix /= m_xScrollPixelsPerLine;
836 szx /= m_xScrollPixelsPerLine;
837 }
838 else
839 {
840 clix = 0;
841 szx = -1;
842 }
843 if( m_yScrollPixelsPerLine )
844 {
845 cliy /= m_yScrollPixelsPerLine;
846 szy /= m_yScrollPixelsPerLine;
847 }
848 else
849 {
850 cliy = 0;
851 szy = -1;
852 }
853
854 int xScrollOld = GetScrollPos(wxHORIZONTAL),
855 yScrollOld = GetScrollPos(wxVERTICAL);
856
857 int dsty;
858 switch ( event.KeyCode() )
859 {
860 case WXK_PAGEUP:
861 case WXK_PRIOR:
862 dsty = sty - (5 * cliy / 6);
863 Scroll(-1, (dsty == -1) ? 0 : dsty);
864 break;
865
866 case WXK_PAGEDOWN:
867 case WXK_NEXT:
868 Scroll(-1, sty + (5 * cliy / 6));
869 break;
870
871 case WXK_HOME:
872 Scroll(0, event.ControlDown() ? 0 : -1);
873 break;
874
875 case WXK_END:
876 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
877 break;
878
879 case WXK_UP:
880 Scroll(-1, sty - 1);
881 break;
882
883 case WXK_DOWN:
884 Scroll(-1, sty + 1);
885 break;
886
887 case WXK_LEFT:
888 Scroll(stx - 1, -1);
889 break;
890
891 case WXK_RIGHT:
892 Scroll(stx + 1, -1);
893 break;
894
895 default:
896 // not for us
897 event.Skip();
898 return;
899 }
900
901 int xScroll = GetScrollPos(wxHORIZONTAL);
902 if ( xScroll != xScrollOld )
903 {
904 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, xScroll,
905 wxHORIZONTAL);
906 event.SetEventObject(this);
907 GetEventHandler()->ProcessEvent(event);
908 }
909
910 int yScroll = GetScrollPos(wxVERTICAL);
911 if ( yScroll != yScrollOld )
912 {
913 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, yScroll,
914 wxVERTICAL);
915 event.SetEventObject(this);
916 GetEventHandler()->ProcessEvent(event);
917 }
918 }
919