1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/scrolwin.cpp
3 // Purpose: wxScrolledWindow implementation
4 // Author: Robert Roebling
5 // Modified by: Ron Lee
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "scrolwin.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/scrolwin.h"
33 #include "wx/dcclient.h"
37 #include "wx/gtk/private.h"
38 #include "wx/gtk/win_gtk.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
45 EVT_SCROLLWIN(wxScrolledWindow::OnScroll
)
46 EVT_SIZE(wxScrolledWindow::OnSize
)
47 EVT_PAINT(wxScrolledWindow::OnPaint
)
48 EVT_CHAR(wxScrolledWindow::OnChar
)
51 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
53 // ============================================================================
55 // ============================================================================
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 extern bool g_blockEventsOnDrag
;
62 extern bool g_blockEventsOnScroll
;
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 extern void wxapp_install_idle_handler();
71 //-----------------------------------------------------------------------------
72 // "value_changed" from m_vAdjust
73 //-----------------------------------------------------------------------------
75 static void gtk_scrolled_window_vscroll_callback( GtkAdjustment
*adjust
,
77 wxScrolledWindow
*win
)
80 wxapp_install_idle_handler();
82 if (g_blockEventsOnDrag
) return;
84 if (!win
->m_hasVMT
) return;
86 win
->GtkVScroll( adjust
->value
,
87 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win
->m_widget
)->vscrollbar
) );
90 //-----------------------------------------------------------------------------
91 // "value_changed" from m_hAdjust
92 //-----------------------------------------------------------------------------
94 static void gtk_scrolled_window_hscroll_callback( GtkAdjustment
*adjust
,
96 wxScrolledWindow
*win
)
99 wxapp_install_idle_handler();
101 if (g_blockEventsOnDrag
) return;
102 if (!win
->m_hasVMT
) return;
104 win
->GtkHScroll( adjust
->value
,
105 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win
->m_widget
)->hscrollbar
) );
108 //-----------------------------------------------------------------------------
109 // "button_press_event" from scrollbar
110 //-----------------------------------------------------------------------------
112 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
113 GdkEventButton
*gdk_event
,
117 wxapp_install_idle_handler();
119 g_blockEventsOnScroll
= TRUE
;
121 // FIXME: there is no slider field any more, what was meant here?
123 win
->m_isScrolling
= (gdk_event
->window
== widget
->slider
);
129 //-----------------------------------------------------------------------------
130 // "button_release_event" from scrollbar
131 //-----------------------------------------------------------------------------
133 static gint
gtk_scrollbar_button_release_callback( GtkRange
*widget
,
134 GdkEventButton
*WXUNUSED(gdk_event
),
137 // don't test here as we can release the mouse while being over
138 // a different window than the slider
140 // if (gdk_event->window != widget->slider) return FALSE;
142 g_blockEventsOnScroll
= FALSE
;
144 if (win
->m_isScrolling
)
146 wxEventType command
= wxEVT_SCROLLWIN_THUMBRELEASE
;
150 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(win
->m_widget
);
151 if (widget
== GTK_RANGE(scrolledWindow
->hscrollbar
))
153 value
= (int)(win
->m_hAdjust
->value
+0.5);
156 if (widget
== GTK_RANGE(scrolledWindow
->vscrollbar
))
158 value
= (int)(win
->m_vAdjust
->value
+0.5);
162 wxScrollWinEvent
event( command
, value
, dir
);
163 event
.SetEventObject( win
);
164 win
->GetEventHandler()->ProcessEvent( event
);
167 win
->m_isScrolling
= FALSE
;
172 //-----------------------------------------------------------------------------
173 // InsertChild for wxScrolledWindow
174 //-----------------------------------------------------------------------------
176 static void wxInsertChildInScrolledWindow( wxWindow
* parent
, wxWindow
* child
)
178 // The window might have been scrolled already, do we
179 // have to adapt the position.
180 GtkPizza
*pizza
= GTK_PIZZA(parent
->m_wxwindow
);
181 child
->m_x
+= pizza
->xoffset
;
182 child
->m_y
+= pizza
->yoffset
;
184 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
185 GTK_WIDGET(child
->m_widget
),
192 // ----------------------------------------------------------------------------
193 // wxScrolledWindow creation
194 // ----------------------------------------------------------------------------
196 void wxScrolledWindow::Init()
198 m_xScrollPixelsPerLine
= 0;
199 m_yScrollPixelsPerLine
= 0;
200 m_xScrollingEnabled
= TRUE
;
201 m_yScrollingEnabled
= TRUE
;
202 m_xScrollPosition
= 0;
203 m_yScrollPosition
= 0;
204 m_xScrollLinesPerPage
= 0;
205 m_yScrollLinesPerPage
= 0;
206 m_targetWindow
= (wxWindow
*) NULL
;
209 m_hasScrolling
= TRUE
;
212 bool wxScrolledWindow::Create(wxWindow
*parent
,
217 const wxString
& name
)
221 if (!PreCreation( parent
, pos
, size
) ||
222 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
224 wxFAIL_MSG( wxT("wxWindow creation failed") );
228 m_insertCallback
= wxInsertChildInScrolledWindow
;
230 m_targetWindow
= this;
232 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
233 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
235 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
237 GtkScrolledWindowClass
*scroll_class
= GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
238 scroll_class
->scrollbar_spacing
= 0;
240 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
242 m_hAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->hscrollbar
) );
243 m_vAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->vscrollbar
) );
245 m_wxwindow
= gtk_pizza_new();
247 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
249 GtkPizza
*pizza
= GTK_PIZZA(m_wxwindow
);
251 if (HasFlag(wxRAISED_BORDER
))
253 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_OUT
);
255 else if (HasFlag(wxSUNKEN_BORDER
))
257 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_IN
);
259 else if (HasFlag(wxSIMPLE_BORDER
))
261 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_THIN
);
265 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_NONE
);
268 GTK_WIDGET_SET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
269 m_acceptsFocus
= TRUE
;
271 // I _really_ don't want scrollbars in the beginning
272 m_vAdjust
->lower
= 0.0;
273 m_vAdjust
->upper
= 1.0;
274 m_vAdjust
->value
= 0.0;
275 m_vAdjust
->step_increment
= 1.0;
276 m_vAdjust
->page_increment
= 2.0;
277 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
278 m_hAdjust
->lower
= 0.0;
279 m_hAdjust
->upper
= 1.0;
280 m_hAdjust
->value
= 0.0;
281 m_hAdjust
->step_increment
= 1.0;
282 m_hAdjust
->page_increment
= 2.0;
283 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
285 // Handlers for new scrollbar values
289 // these handlers block mouse events to any window during scrolling such as
290 // motion events and prevent GTK and wxWidgets from fighting over where the
293 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->vscrollbar
), "button_press_event",
294 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
296 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->hscrollbar
), "button_press_event",
297 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
299 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->vscrollbar
), "button_release_event",
300 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
302 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->hscrollbar
), "button_release_event",
303 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
305 gtk_widget_show( m_wxwindow
);
308 m_parent
->DoAddChild( this );
310 m_focusWidget
= m_wxwindow
;
319 // ----------------------------------------------------------------------------
320 // setting scrolling parameters
321 // ----------------------------------------------------------------------------
323 void wxScrolledWindow::DoSetVirtualSize( int x
, int y
)
325 wxPanel::DoSetVirtualSize( x
, y
);
333 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
334 * noUnitsX/noUnitsY: : no. units per scrollbar
336 void wxScrolledWindow::SetScrollbars( int pixelsPerUnitX
, int pixelsPerUnitY
,
337 int noUnitsX
, int noUnitsY
,
338 int xPos
, int yPos
, bool noRefresh
)
341 GetViewStart (& xs
, & ys
);
343 int old_x
= m_xScrollPixelsPerLine
* xs
;
344 int old_y
= m_yScrollPixelsPerLine
* ys
;
346 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
347 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
349 m_hAdjust
->value
= m_xScrollPosition
= xPos
;
350 m_vAdjust
->value
= m_yScrollPosition
= yPos
;
352 // Setting hints here should arguably be deprecated, but without it
353 // a sizer might override this manual scrollbar setting in old code.
354 m_targetWindow
->SetVirtualSizeHints( noUnitsX
* pixelsPerUnitX
, noUnitsY
* pixelsPerUnitY
);
356 m_targetWindow
->SetVirtualSize( noUnitsX
* pixelsPerUnitX
, noUnitsY
* pixelsPerUnitY
);
360 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
361 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
363 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
367 void wxScrolledWindow::AdjustScrollbars()
372 m_targetWindow
->GetClientSize( &w
, &h
);
373 m_targetWindow
->GetVirtualSize( &vw
, &vh
);
375 if (m_xScrollPixelsPerLine
== 0)
377 m_hAdjust
->upper
= 1.0;
378 m_hAdjust
->page_increment
= 1.0;
379 m_hAdjust
->page_size
= 1.0;
383 m_hAdjust
->upper
= vw
/ m_xScrollPixelsPerLine
;
384 m_hAdjust
->page_increment
= (w
/ m_xScrollPixelsPerLine
);
385 m_hAdjust
->page_size
= m_hAdjust
->page_increment
;
387 // If the scrollbar hits the right side, move the window
388 // right to keep it from over extending.
390 if ((m_hAdjust
->value
!= 0.0) && (m_hAdjust
->value
+ m_hAdjust
->page_size
> m_hAdjust
->upper
))
392 m_hAdjust
->value
= m_hAdjust
->upper
- m_hAdjust
->page_size
;
393 if (m_hAdjust
->value
< 0.0)
394 m_hAdjust
->value
= 0.0;
396 if (GetChildren().GetCount() == 0)
397 m_xScrollPosition
= (int)m_hAdjust
->value
; // This is enough without child windows
399 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" ); // Actually scroll window
403 if (m_yScrollPixelsPerLine
== 0)
405 m_vAdjust
->upper
= 1.0;
406 m_vAdjust
->page_increment
= 1.0;
407 m_vAdjust
->page_size
= 1.0;
411 m_vAdjust
->upper
= vh
/ m_yScrollPixelsPerLine
;
412 m_vAdjust
->page_increment
= (h
/ m_yScrollPixelsPerLine
);
413 m_vAdjust
->page_size
= m_vAdjust
->page_increment
;
415 if ((m_vAdjust
->value
!= 0.0) && (m_vAdjust
->value
+ m_vAdjust
->page_size
> m_vAdjust
->upper
))
417 m_vAdjust
->value
= m_vAdjust
->upper
- m_vAdjust
->page_size
;
418 if (m_vAdjust
->value
< 0.0)
419 m_vAdjust
->value
= 0.0;
421 if (GetChildren().GetCount() == 0)
422 m_yScrollPosition
= (int)m_vAdjust
->value
;
424 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
428 m_xScrollLinesPerPage
= (int)(m_hAdjust
->page_increment
+ 0.5);
429 m_yScrollLinesPerPage
= (int)(m_vAdjust
->page_increment
+ 0.5);
431 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
432 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
436 // ----------------------------------------------------------------------------
437 // target window handling
438 // ----------------------------------------------------------------------------
440 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
, bool WXUNUSED(pushEventHandler
) )
442 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
443 m_targetWindow
= target
;
446 wxWindow
*wxScrolledWindow::GetTargetWindow() const
448 return m_targetWindow
;
451 // Override this function if you don't want to have wxScrolledWindow
452 // automatically change the origin according to the scroll position.
453 void wxScrolledWindow::DoPrepareDC(wxDC
& dc
)
455 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
456 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
459 void wxScrolledWindow::SetScrollRate( int xstep
, int ystep
)
461 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
462 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
464 m_xScrollPixelsPerLine
= xstep
;
465 m_yScrollPixelsPerLine
= ystep
;
467 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
468 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
470 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
475 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
478 *x_unit
= m_xScrollPixelsPerLine
;
480 *y_unit
= m_yScrollPixelsPerLine
;
483 int wxScrolledWindow::GetScrollPageSize(int orient
) const
485 if ( orient
== wxHORIZONTAL
)
486 return m_xScrollLinesPerPage
;
488 return m_yScrollLinesPerPage
;
491 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
493 if ( orient
== wxHORIZONTAL
)
494 m_xScrollLinesPerPage
= pageSize
;
496 m_yScrollLinesPerPage
= pageSize
;
499 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
501 int orient
= event
.GetOrientation();
503 int nScrollInc
= CalcScrollInc(event
);
504 if (nScrollInc
== 0) return;
506 if (orient
== wxHORIZONTAL
)
508 int newPos
= m_xScrollPosition
+ nScrollInc
;
509 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
513 int newPos
= m_yScrollPosition
+ nScrollInc
;
514 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
517 if (orient
== wxHORIZONTAL
)
519 m_xScrollPosition
+= nScrollInc
;
523 m_yScrollPosition
+= nScrollInc
;
526 if (orient
== wxHORIZONTAL
)
528 if (m_xScrollingEnabled
)
529 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
531 m_targetWindow
->Refresh();
535 if (m_yScrollingEnabled
)
536 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
538 m_targetWindow
->Refresh();
542 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
544 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
546 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
547 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
549 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
551 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
552 if (max
< 0) max
= 0;
553 if (x_pos
> max
) x_pos
= max
;
554 if (x_pos
< 0) x_pos
= 0;
556 int old_x
= m_xScrollPosition
;
557 m_xScrollPosition
= x_pos
;
558 m_hAdjust
->value
= x_pos
;
560 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
562 // Just update the scrollbar, don't send any wxWidgets event
563 GtkHDisconnectEvent();
564 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" );
568 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
570 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
571 if (max
< 0) max
= 0;
572 if (y_pos
> max
) y_pos
= max
;
573 if (y_pos
< 0) y_pos
= 0;
575 int old_y
= m_yScrollPosition
;
576 m_yScrollPosition
= y_pos
;
577 m_vAdjust
->value
= y_pos
;
579 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
581 // Just update the scrollbar, don't send any wxWidgets event
582 GtkVDisconnectEvent();
583 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
588 // TODO: [VH]Scroll functions should be combined
590 void wxScrolledWindow::GtkVScroll( float value
, unsigned int scroll_type
)
592 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
594 if (m_yScrollPixelsPerLine
== 0)
597 int y_pos
= (int)(value
+0.5);
599 if (y_pos
== m_yScrollPosition
)
602 wxEventType command
= GtkScrollWinTypeToWx(scroll_type
);
604 wxScrollWinEvent
event( command
, y_pos
, wxVERTICAL
);
605 event
.SetEventObject( this );
606 GetEventHandler()->ProcessEvent( event
);
609 void wxScrolledWindow::GtkHScroll( float value
, unsigned int scroll_type
)
611 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
613 if (m_xScrollPixelsPerLine
== 0)
616 int x_pos
= (int)(value
+0.5);
618 if (x_pos
== m_xScrollPosition
)
621 wxEventType command
= GtkScrollWinTypeToWx(scroll_type
);
623 wxScrollWinEvent
event( command
, x_pos
, wxHORIZONTAL
);
624 event
.SetEventObject( this );
625 GetEventHandler()->ProcessEvent( event
);
628 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
630 m_xScrollingEnabled
= x_scroll
;
631 m_yScrollingEnabled
= y_scroll
;
634 // Where the current view starts from
635 void wxScrolledWindow::GetViewStart (int *x
, int *y
) const
638 *x
= m_xScrollPosition
;
640 *y
= m_yScrollPosition
;
643 void wxScrolledWindow::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
646 GetViewStart (& xs
, & ys
);
649 *xx
= x
- xs
* m_xScrollPixelsPerLine
;
651 *yy
= y
- ys
* m_yScrollPixelsPerLine
;
654 void wxScrolledWindow::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
657 GetViewStart (& xs
, & ys
);
660 *xx
= x
+ xs
* m_xScrollPixelsPerLine
;
662 *yy
= y
+ ys
* m_yScrollPixelsPerLine
;
665 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
667 int pos
= event
.GetPosition();
668 int orient
= event
.GetOrientation();
671 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
673 if (orient
== wxHORIZONTAL
)
674 nScrollInc
= - m_xScrollPosition
;
676 nScrollInc
= - m_yScrollPosition
;
678 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
680 if (orient
== wxHORIZONTAL
)
681 nScrollInc
= GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine
- m_xScrollPosition
;
683 nScrollInc
= GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine
- m_yScrollPosition
;
685 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
689 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
693 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
695 if (orient
== wxHORIZONTAL
)
696 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
698 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
700 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
702 if (orient
== wxHORIZONTAL
)
703 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
705 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
707 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
708 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
710 if (orient
== wxHORIZONTAL
)
711 nScrollInc
= pos
- m_xScrollPosition
;
713 nScrollInc
= pos
- m_yScrollPosition
;
716 if (orient
== wxHORIZONTAL
)
718 if (m_xScrollPixelsPerLine
> 0)
720 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
721 if (max
< 0) max
= 0;
723 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
724 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
725 else if ( (m_xScrollPosition
+ nScrollInc
) > max
)
726 nScrollInc
= max
- m_xScrollPosition
; // As +ve as we can go
729 m_targetWindow
->Refresh();
733 if (m_yScrollPixelsPerLine
> 0)
735 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
736 if (max
< 0) max
= 0;
738 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
739 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
740 else if ( (m_yScrollPosition
+ nScrollInc
) > max
)
741 nScrollInc
= max
- m_yScrollPosition
; // As +ve as we can go
744 m_targetWindow
->Refresh();
750 void wxScrolledWindow::SetScrollPos( int orient
, int pos
, bool refresh
)
752 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
754 wxCHECK_RET( m_wxwindow
!= NULL
, wxT("window needs client area for scrolling") );
756 if (orient
== wxHORIZONTAL
)
758 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
759 if (max
< 0) max
= 0;
761 if (pos
> max
) pos
= 0;
762 if (pos
< 0) pos
= 0;
764 if (pos
== (int)(m_hAdjust
->value
+0.5)) return;
765 m_hAdjust
->value
= pos
;
769 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
770 if (max
< 0) max
= 0;
772 if (pos
> max
) pos
= 0;
773 if (pos
< 0) pos
= 0;
775 if (pos
== (int)(m_vAdjust
->value
+0.5)) return;
776 m_vAdjust
->value
= pos
;
779 if (m_wxwindow
->window
)
781 if (orient
== wxHORIZONTAL
)
783 // Just update the scrollbar, don't send any wxWidgets event
784 GtkHDisconnectEvent();
785 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" );
790 // Just update the scrollbar, don't send any wxWidgets event
791 GtkVDisconnectEvent();
792 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
798 void wxScrolledWindow::GtkVConnectEvent()
800 gtk_signal_connect( GTK_OBJECT(m_vAdjust
), "value_changed",
801 (GtkSignalFunc
) gtk_scrolled_window_vscroll_callback
, (gpointer
) this );
804 void wxScrolledWindow::GtkHConnectEvent()
806 gtk_signal_connect( GTK_OBJECT(m_hAdjust
), "value_changed",
807 (GtkSignalFunc
) gtk_scrolled_window_hscroll_callback
, (gpointer
) this );
810 void wxScrolledWindow::GtkHDisconnectEvent()
812 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust
),
813 (GtkSignalFunc
) gtk_scrolled_window_hscroll_callback
, (gpointer
) this );
816 void wxScrolledWindow::GtkVDisconnectEvent()
818 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust
),
819 (GtkSignalFunc
) gtk_scrolled_window_vscroll_callback
, (gpointer
) this );
822 bool wxScrolledWindow::Layout()
824 if (GetSizer() && m_targetWindow
== this)
826 // If we're the scroll target, take into account the
827 // virtual size and scrolled position of the window.
830 CalcScrolledPosition(0,0, &x
,&y
);
831 GetVirtualSize(&w
, &h
);
832 GetSizer()->SetDimension(x
, y
, w
, h
);
836 return wxPanel::Layout(); // fall back to default for LayoutConstraints
839 // ----------------------------------------------------------------------------
841 // ----------------------------------------------------------------------------
843 // Default OnSize resets scrollbars, if any
844 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
846 if( GetAutoLayout() || m_targetWindow
->GetAutoLayout() )
848 if( m_targetWindow
!= this )
849 m_targetWindow
->FitInside();
853 // FIXME: Something is really weird here... This should be
854 // called by FitInside above (and apparently is), yet the
855 // scrollsub sample will get the scrollbar wrong if resized
856 // quickly. This masks the bug, but is surely not the right
866 // This calls OnDraw, having adjusted the origin according to the current
868 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
876 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
877 // compatibility here - if we used OnKeyDown(), the programs which process
878 // arrows themselves in their OnChar() would never get the message and like
879 // this they always have the priority
880 void wxScrolledWindow::OnChar(wxKeyEvent
& event
)
882 int stx
, sty
, // view origin
883 szx
, szy
, // view size (total)
884 clix
, cliy
; // view size (on screen)
886 GetViewStart(&stx
, &sty
);
887 GetClientSize(&clix
, &cliy
);
888 GetVirtualSize(&szx
, &szy
);
890 if( m_xScrollPixelsPerLine
)
892 clix
/= m_xScrollPixelsPerLine
;
893 szx
/= m_xScrollPixelsPerLine
;
900 if( m_yScrollPixelsPerLine
)
902 cliy
/= m_yScrollPixelsPerLine
;
903 szy
/= m_yScrollPixelsPerLine
;
911 int xScrollOld
= GetScrollPos(wxHORIZONTAL
),
912 yScrollOld
= GetScrollPos(wxVERTICAL
);
915 switch ( event
.GetKeyCode() )
919 dsty
= sty
- (5 * cliy
/ 6);
920 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
925 Scroll(-1, sty
+ (5 * cliy
/ 6));
929 Scroll(0, event
.ControlDown() ? 0 : -1);
933 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
958 int xScroll
= GetScrollPos(wxHORIZONTAL
);
959 if ( xScroll
!= xScrollOld
)
961 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, xScroll
,
963 event
.SetEventObject(this);
964 GetEventHandler()->ProcessEvent(event
);
967 int yScroll
= GetScrollPos(wxVERTICAL
);
968 if ( yScroll
!= yScrollOld
)
970 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, yScroll
,
972 event
.SetEventObject(this);
973 GetEventHandler()->ProcessEvent(event
);