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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/scrolwin.h"
29 #include "wx/dcclient.h"
33 #include "wx/gtk/private.h"
34 #include "wx/gtk/win_gtk.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
41 EVT_SCROLLWIN(wxScrolledWindow::OnScroll
)
42 EVT_SIZE(wxScrolledWindow::OnSize
)
43 EVT_PAINT(wxScrolledWindow::OnPaint
)
44 EVT_CHAR(wxScrolledWindow::OnChar
)
47 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
49 // ============================================================================
51 // ============================================================================
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 extern bool g_blockEventsOnDrag
;
58 extern bool g_blockEventsOnScroll
;
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 extern void wxapp_install_idle_handler();
67 //-----------------------------------------------------------------------------
68 // "value_changed" from m_vAdjust
69 //-----------------------------------------------------------------------------
72 static void gtk_scrolled_window_vscroll_callback( GtkAdjustment
*adjust
,
74 wxScrolledWindow
*win
)
77 wxapp_install_idle_handler();
79 if (g_blockEventsOnDrag
) return;
81 if (!win
->m_hasVMT
) return;
83 win
->GtkVScroll( adjust
->value
,
84 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win
->m_widget
)->vscrollbar
) );
88 //-----------------------------------------------------------------------------
89 // "value_changed" from m_hAdjust
90 //-----------------------------------------------------------------------------
93 static void gtk_scrolled_window_hscroll_callback( GtkAdjustment
*adjust
,
95 wxScrolledWindow
*win
)
98 wxapp_install_idle_handler();
100 if (g_blockEventsOnDrag
) return;
101 if (!win
->m_hasVMT
) return;
103 win
->GtkHScroll( adjust
->value
,
104 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win
->m_widget
)->hscrollbar
) );
108 //-----------------------------------------------------------------------------
109 // "button_press_event" from scrollbar
110 //-----------------------------------------------------------------------------
113 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
114 GdkEventButton
*gdk_event
,
118 wxapp_install_idle_handler();
120 g_blockEventsOnScroll
= TRUE
;
122 // FIXME: there is no slider field any more, what was meant here?
124 win
->m_isScrolling
= (gdk_event
->window
== widget
->slider
);
131 //-----------------------------------------------------------------------------
132 // "button_release_event" from scrollbar
133 //-----------------------------------------------------------------------------
136 static gint
gtk_scrollbar_button_release_callback( GtkRange
*widget
,
137 GdkEventButton
*WXUNUSED(gdk_event
),
140 // don't test here as we can release the mouse while being over
141 // a different window than the slider
143 // if (gdk_event->window != widget->slider) return FALSE;
145 g_blockEventsOnScroll
= FALSE
;
147 if (win
->m_isScrolling
)
149 wxEventType command
= wxEVT_SCROLLWIN_THUMBRELEASE
;
153 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(win
->m_widget
);
154 if (widget
== GTK_RANGE(scrolledWindow
->hscrollbar
))
156 value
= (int)(win
->m_hAdjust
->value
+0.5);
159 if (widget
== GTK_RANGE(scrolledWindow
->vscrollbar
))
161 value
= (int)(win
->m_vAdjust
->value
+0.5);
165 wxScrollWinEvent
event( command
, value
, dir
);
166 event
.SetEventObject( win
);
167 win
->GetEventHandler()->ProcessEvent( event
);
170 win
->m_isScrolling
= FALSE
;
176 //-----------------------------------------------------------------------------
177 // InsertChild for wxScrolledWindow
178 //-----------------------------------------------------------------------------
180 static void wxInsertChildInScrolledWindow( wxWindow
* parent
, wxWindow
* child
)
182 // The window might have been scrolled already, do we
183 // have to adapt the position.
184 GtkPizza
*pizza
= GTK_PIZZA(parent
->m_wxwindow
);
185 child
->m_x
+= pizza
->xoffset
;
186 child
->m_y
+= pizza
->yoffset
;
188 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
189 GTK_WIDGET(child
->m_widget
),
196 // ----------------------------------------------------------------------------
197 // wxScrolledWindow creation
198 // ----------------------------------------------------------------------------
200 void wxScrolledWindow::Init()
202 m_xScrollPixelsPerLine
= 0;
203 m_yScrollPixelsPerLine
= 0;
204 m_xScrollingEnabled
= TRUE
;
205 m_yScrollingEnabled
= TRUE
;
206 m_xScrollPosition
= 0;
207 m_yScrollPosition
= 0;
208 m_xScrollLinesPerPage
= 0;
209 m_yScrollLinesPerPage
= 0;
210 m_targetWindow
= (wxWindow
*) NULL
;
213 m_hasScrolling
= TRUE
;
216 bool wxScrolledWindow::Create(wxWindow
*parent
,
221 const wxString
& name
)
225 if (!PreCreation( parent
, pos
, size
) ||
226 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
228 wxFAIL_MSG( wxT("wxWindow creation failed") );
232 m_insertCallback
= wxInsertChildInScrolledWindow
;
234 m_targetWindow
= this;
236 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
237 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
239 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
241 GtkScrolledWindowClass
*scroll_class
= GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
242 scroll_class
->scrollbar_spacing
= 0;
244 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
246 m_hAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->hscrollbar
) );
247 m_vAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->vscrollbar
) );
249 m_wxwindow
= gtk_pizza_new();
251 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
253 GtkPizza
*pizza
= GTK_PIZZA(m_wxwindow
);
255 if (HasFlag(wxRAISED_BORDER
))
257 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_OUT
);
259 else if (HasFlag(wxSUNKEN_BORDER
))
261 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_IN
);
263 else if (HasFlag(wxSIMPLE_BORDER
))
265 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_THIN
);
269 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_NONE
);
272 GTK_WIDGET_SET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
273 m_acceptsFocus
= TRUE
;
275 // I _really_ don't want scrollbars in the beginning
276 m_vAdjust
->lower
= 0.0;
277 m_vAdjust
->upper
= 1.0;
278 m_vAdjust
->value
= 0.0;
279 m_vAdjust
->step_increment
= 1.0;
280 m_vAdjust
->page_increment
= 2.0;
281 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
282 m_hAdjust
->lower
= 0.0;
283 m_hAdjust
->upper
= 1.0;
284 m_hAdjust
->value
= 0.0;
285 m_hAdjust
->step_increment
= 1.0;
286 m_hAdjust
->page_increment
= 2.0;
287 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
289 // Handlers for new scrollbar values
293 // these handlers block mouse events to any window during scrolling such as
294 // motion events and prevent GTK and wxWidgets from fighting over where the
297 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->vscrollbar
), "button_press_event",
298 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
300 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->hscrollbar
), "button_press_event",
301 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
303 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->vscrollbar
), "button_release_event",
304 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
306 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->hscrollbar
), "button_release_event",
307 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
309 gtk_widget_show( m_wxwindow
);
312 m_parent
->DoAddChild( this );
314 m_focusWidget
= m_wxwindow
;
323 // ----------------------------------------------------------------------------
324 // setting scrolling parameters
325 // ----------------------------------------------------------------------------
327 void wxScrolledWindow::DoSetVirtualSize( int x
, int y
)
329 wxPanel::DoSetVirtualSize( x
, y
);
336 // wxWindow's GetBestVirtualSize returns the actual window size,
337 // whereas we want to return the virtual size
338 wxSize
wxScrolledWindow::GetBestVirtualSize() const
340 wxSize
clientSize( GetClientSize() );
343 wxSize
minSize( GetSizer()->CalcMin() );
345 return wxSize( wxMax( clientSize
.x
, minSize
.x
), wxMax( clientSize
.y
, minSize
.y
) );
351 // return the size best suited for the current window
352 // (this isn't a virtual size, this is a sensible size for the window)
353 wxSize
wxScrolledWindow::DoGetBestSize() const
359 wxSize b
= GetSizer()->GetMinSize();
361 // Only use the content to set the window size in the direction
362 // where there's no scrolling; otherwise we're going to get a huge
363 // window in the direction in which scrolling is enabled
365 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
368 if ( GetMinSize().IsFullySpecified() )
369 minSize
= GetMinSize();
380 return wxWindow::DoGetBestSize();
382 // Add any difference between size and client size
383 wxSize diff
= GetSize() - GetClientSize();
384 best
.x
+= wxMax(0, diff
.x
);
385 best
.y
+= wxMax(0, diff
.y
);
391 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
392 * noUnitsX/noUnitsY: : no. units per scrollbar
394 void wxScrolledWindow::SetScrollbars( int pixelsPerUnitX
, int pixelsPerUnitY
,
395 int noUnitsX
, int noUnitsY
,
396 int xPos
, int yPos
, bool noRefresh
)
399 GetViewStart (& xs
, & ys
);
401 int old_x
= m_xScrollPixelsPerLine
* xs
;
402 int old_y
= m_yScrollPixelsPerLine
* ys
;
404 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
405 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
407 m_hAdjust
->value
= m_xScrollPosition
= xPos
;
408 m_vAdjust
->value
= m_yScrollPosition
= yPos
;
410 // Setting hints here should arguably be deprecated, but without it
411 // a sizer might override this manual scrollbar setting in old code.
412 // m_targetWindow->SetVirtualSizeHints( noUnitsX * pixelsPerUnitX, noUnitsY * pixelsPerUnitY );
414 int w
= noUnitsX
* pixelsPerUnitX
;
415 int h
= noUnitsY
* pixelsPerUnitY
;
416 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
417 h
? h
: wxDefaultCoord
);
421 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
422 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
424 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
428 void wxScrolledWindow::AdjustScrollbars()
433 m_targetWindow
->GetClientSize( &w
, &h
);
434 m_targetWindow
->GetVirtualSize( &vw
, &vh
);
436 if (m_xScrollPixelsPerLine
== 0)
438 m_hAdjust
->upper
= 1.0;
439 m_hAdjust
->page_increment
= 1.0;
440 m_hAdjust
->page_size
= 1.0;
444 m_hAdjust
->upper
= (vw
+m_xScrollPixelsPerLine
-1) / m_xScrollPixelsPerLine
;
445 m_hAdjust
->page_size
= w
/ m_xScrollPixelsPerLine
;
446 m_hAdjust
->page_increment
= w
/ m_xScrollPixelsPerLine
;
448 // Special case. When client and virtual size are very close but
449 // the client is big enough, kill scrollbar.
451 if ((m_hAdjust
->page_size
< m_hAdjust
->upper
) && (w
>= vw
))
452 m_hAdjust
->page_size
+= 1.0;
454 // If the scrollbar hits the right side, move the window
455 // right to keep it from over extending.
457 if ((m_hAdjust
->value
!= 0.0) && (m_hAdjust
->value
+ m_hAdjust
->page_size
> m_hAdjust
->upper
))
459 m_hAdjust
->value
= m_hAdjust
->upper
- m_hAdjust
->page_size
;
460 if (m_hAdjust
->value
< 0.0)
461 m_hAdjust
->value
= 0.0;
463 if (GetChildren().GetCount() == 0)
464 m_xScrollPosition
= (int)m_hAdjust
->value
; // This is enough without child windows
466 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" ); // Actually scroll window
470 if (m_yScrollPixelsPerLine
== 0)
472 m_vAdjust
->upper
= 1.0;
473 m_vAdjust
->page_increment
= 1.0;
474 m_vAdjust
->page_size
= 1.0;
478 m_vAdjust
->upper
= (vh
+m_yScrollPixelsPerLine
-1) / m_yScrollPixelsPerLine
;
479 m_vAdjust
->page_size
= h
/ m_yScrollPixelsPerLine
;
480 m_vAdjust
->page_increment
= h
/ m_yScrollPixelsPerLine
;
482 if ((m_vAdjust
->page_size
< m_vAdjust
->upper
) && (h
>= vh
))
483 m_vAdjust
->page_size
+= 1.0;
485 if ((m_vAdjust
->value
!= 0.0) && (m_vAdjust
->value
+ m_vAdjust
->page_size
> m_vAdjust
->upper
))
487 m_vAdjust
->value
= m_vAdjust
->upper
- m_vAdjust
->page_size
;
488 if (m_vAdjust
->value
< 0.0)
489 m_vAdjust
->value
= 0.0;
491 if (GetChildren().GetCount() == 0)
492 m_yScrollPosition
= (int)m_vAdjust
->value
;
494 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
498 m_xScrollLinesPerPage
= (int)(m_hAdjust
->page_increment
+ 0.5);
499 m_yScrollLinesPerPage
= (int)(m_vAdjust
->page_increment
+ 0.5);
501 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
502 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
506 // ----------------------------------------------------------------------------
507 // target window handling
508 // ----------------------------------------------------------------------------
510 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
, bool WXUNUSED(pushEventHandler
) )
512 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
513 m_targetWindow
= target
;
516 wxWindow
*wxScrolledWindow::GetTargetWindow() const
518 return m_targetWindow
;
521 // Override this function if you don't want to have wxScrolledWindow
522 // automatically change the origin according to the scroll position.
523 void wxScrolledWindow::DoPrepareDC(wxDC
& dc
)
525 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
526 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
529 void wxScrolledWindow::SetScrollRate( int xstep
, int ystep
)
531 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
532 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
534 m_xScrollPixelsPerLine
= xstep
;
535 m_yScrollPixelsPerLine
= ystep
;
537 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
538 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
540 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
545 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
548 *x_unit
= m_xScrollPixelsPerLine
;
550 *y_unit
= m_yScrollPixelsPerLine
;
553 int wxScrolledWindow::GetScrollPageSize(int orient
) const
555 if ( orient
== wxHORIZONTAL
)
556 return m_xScrollLinesPerPage
;
558 return m_yScrollLinesPerPage
;
561 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
563 if ( orient
== wxHORIZONTAL
)
564 m_xScrollLinesPerPage
= pageSize
;
566 m_yScrollLinesPerPage
= pageSize
;
569 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
571 int orient
= event
.GetOrientation();
573 int nScrollInc
= CalcScrollInc(event
);
574 if (nScrollInc
== 0) return;
576 if (orient
== wxHORIZONTAL
)
578 int newPos
= m_xScrollPosition
+ nScrollInc
;
579 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
583 int newPos
= m_yScrollPosition
+ nScrollInc
;
584 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
587 if (orient
== wxHORIZONTAL
)
589 m_xScrollPosition
+= nScrollInc
;
593 m_yScrollPosition
+= nScrollInc
;
596 if (orient
== wxHORIZONTAL
)
598 if (m_xScrollingEnabled
)
599 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
601 m_targetWindow
->Refresh();
605 if (m_yScrollingEnabled
)
606 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
608 m_targetWindow
->Refresh();
612 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
614 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
616 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
617 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
619 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
621 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
622 if (max
< 0) max
= 0;
623 if (x_pos
> max
) x_pos
= max
;
624 if (x_pos
< 0) x_pos
= 0;
626 int old_x
= m_xScrollPosition
;
627 m_xScrollPosition
= x_pos
;
628 m_hAdjust
->value
= x_pos
;
630 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
632 // Just update the scrollbar, don't send any wxWidgets event
633 GtkHDisconnectEvent();
634 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" );
638 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
640 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
641 if (max
< 0) max
= 0;
642 if (y_pos
> max
) y_pos
= max
;
643 if (y_pos
< 0) y_pos
= 0;
645 int old_y
= m_yScrollPosition
;
646 m_yScrollPosition
= y_pos
;
647 m_vAdjust
->value
= y_pos
;
649 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
651 // Just update the scrollbar, don't send any wxWidgets event
652 GtkVDisconnectEvent();
653 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
658 // TODO: [VH]Scroll functions should be combined
660 void wxScrolledWindow::GtkVScroll( float value
, unsigned int scroll_type
)
662 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
664 if (m_yScrollPixelsPerLine
== 0)
667 int y_pos
= (int)(value
+0.5);
669 if (y_pos
== m_yScrollPosition
)
672 wxEventType command
= GtkScrollWinTypeToWx(scroll_type
);
674 wxScrollWinEvent
event( command
, y_pos
, wxVERTICAL
);
675 event
.SetEventObject( this );
676 GetEventHandler()->ProcessEvent( event
);
679 void wxScrolledWindow::GtkHScroll( float value
, unsigned int scroll_type
)
681 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
683 if (m_xScrollPixelsPerLine
== 0)
686 int x_pos
= (int)(value
+0.5);
688 if (x_pos
== m_xScrollPosition
)
691 wxEventType command
= GtkScrollWinTypeToWx(scroll_type
);
693 wxScrollWinEvent
event( command
, x_pos
, wxHORIZONTAL
);
694 event
.SetEventObject( this );
695 GetEventHandler()->ProcessEvent( event
);
698 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
700 m_xScrollingEnabled
= x_scroll
;
701 m_yScrollingEnabled
= y_scroll
;
704 // Where the current view starts from
705 void wxScrolledWindow::GetViewStart (int *x
, int *y
) const
708 *x
= m_xScrollPosition
;
710 *y
= m_yScrollPosition
;
713 void wxScrolledWindow::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
716 GetViewStart (& xs
, & ys
);
719 *xx
= x
- xs
* m_xScrollPixelsPerLine
;
721 *yy
= y
- ys
* m_yScrollPixelsPerLine
;
724 void wxScrolledWindow::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
727 GetViewStart (& xs
, & ys
);
730 *xx
= x
+ xs
* m_xScrollPixelsPerLine
;
732 *yy
= y
+ ys
* m_yScrollPixelsPerLine
;
735 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
737 int pos
= event
.GetPosition();
738 int orient
= event
.GetOrientation();
741 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
743 if (orient
== wxHORIZONTAL
)
744 nScrollInc
= - m_xScrollPosition
;
746 nScrollInc
= - m_yScrollPosition
;
748 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
750 if (orient
== wxHORIZONTAL
)
751 nScrollInc
= GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine
- m_xScrollPosition
;
753 nScrollInc
= GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine
- m_yScrollPosition
;
755 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
759 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
763 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
765 if (orient
== wxHORIZONTAL
)
766 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
768 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
770 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
772 if (orient
== wxHORIZONTAL
)
773 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
775 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
777 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
778 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
780 if (orient
== wxHORIZONTAL
)
781 nScrollInc
= pos
- m_xScrollPosition
;
783 nScrollInc
= pos
- m_yScrollPosition
;
786 if (orient
== wxHORIZONTAL
)
788 if (m_xScrollPixelsPerLine
> 0)
790 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
791 if (max
< 0) max
= 0;
793 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
794 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
795 else if ( (m_xScrollPosition
+ nScrollInc
) > max
)
796 nScrollInc
= max
- m_xScrollPosition
; // As +ve as we can go
799 m_targetWindow
->Refresh();
803 if (m_yScrollPixelsPerLine
> 0)
805 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
806 if (max
< 0) max
= 0;
808 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
809 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
810 else if ( (m_yScrollPosition
+ nScrollInc
) > max
)
811 nScrollInc
= max
- m_yScrollPosition
; // As +ve as we can go
814 m_targetWindow
->Refresh();
820 void wxScrolledWindow::SetScrollPos( int orient
, int pos
, bool refresh
)
822 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
824 wxCHECK_RET( m_wxwindow
!= NULL
, wxT("window needs client area for scrolling") );
826 if (orient
== wxHORIZONTAL
)
828 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
829 if (max
< 0) max
= 0;
831 if (pos
> max
) pos
= 0;
832 if (pos
< 0) pos
= 0;
834 if (pos
== (int)(m_hAdjust
->value
+0.5)) return;
835 m_hAdjust
->value
= pos
;
839 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
840 if (max
< 0) max
= 0;
842 if (pos
> max
) pos
= 0;
843 if (pos
< 0) pos
= 0;
845 if (pos
== (int)(m_vAdjust
->value
+0.5)) return;
846 m_vAdjust
->value
= pos
;
849 if (m_wxwindow
->window
)
851 if (orient
== wxHORIZONTAL
)
853 // Just update the scrollbar, don't send any wxWidgets event
854 GtkHDisconnectEvent();
855 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" );
860 // Just update the scrollbar, don't send any wxWidgets event
861 GtkVDisconnectEvent();
862 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
868 void wxScrolledWindow::GtkVConnectEvent()
870 gtk_signal_connect( GTK_OBJECT(m_vAdjust
), "value_changed",
871 (GtkSignalFunc
) gtk_scrolled_window_vscroll_callback
, (gpointer
) this );
874 void wxScrolledWindow::GtkHConnectEvent()
876 gtk_signal_connect( GTK_OBJECT(m_hAdjust
), "value_changed",
877 (GtkSignalFunc
) gtk_scrolled_window_hscroll_callback
, (gpointer
) this );
880 void wxScrolledWindow::GtkHDisconnectEvent()
882 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust
),
883 (GtkSignalFunc
) gtk_scrolled_window_hscroll_callback
, (gpointer
) this );
886 void wxScrolledWindow::GtkVDisconnectEvent()
888 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust
),
889 (GtkSignalFunc
) gtk_scrolled_window_vscroll_callback
, (gpointer
) this );
892 bool wxScrolledWindow::Layout()
894 if (GetSizer() && m_targetWindow
== this)
896 // If we're the scroll target, take into account the
897 // virtual size and scrolled position of the window.
900 CalcScrolledPosition(0,0, &x
,&y
);
901 GetVirtualSize(&w
, &h
);
902 GetSizer()->SetDimension(x
, y
, w
, h
);
906 return wxPanel::Layout(); // fall back to default for LayoutConstraints
909 // ----------------------------------------------------------------------------
911 // ----------------------------------------------------------------------------
913 // Default OnSize resets scrollbars, if any
914 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
916 if ( m_targetWindow
->GetAutoLayout() )
918 wxSize size
= m_targetWindow
->GetBestVirtualSize();
920 // This will call ::Layout() and ::AdjustScrollbars()
921 SetVirtualSize( size
);
929 // This calls OnDraw, having adjusted the origin according to the current
931 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
939 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
940 // compatibility here - if we used OnKeyDown(), the programs which process
941 // arrows themselves in their OnChar() would never get the message and like
942 // this they always have the priority
943 void wxScrolledWindow::OnChar(wxKeyEvent
& event
)
945 int stx
, sty
, // view origin
946 szx
, szy
, // view size (total)
947 clix
, cliy
; // view size (on screen)
949 GetViewStart(&stx
, &sty
);
950 GetClientSize(&clix
, &cliy
);
951 GetVirtualSize(&szx
, &szy
);
953 if( m_xScrollPixelsPerLine
)
955 clix
/= m_xScrollPixelsPerLine
;
956 szx
/= m_xScrollPixelsPerLine
;
963 if( m_yScrollPixelsPerLine
)
965 cliy
/= m_yScrollPixelsPerLine
;
966 szy
/= m_yScrollPixelsPerLine
;
974 int xScrollOld
= GetScrollPos(wxHORIZONTAL
),
975 yScrollOld
= GetScrollPos(wxVERTICAL
);
978 switch ( event
.GetKeyCode() )
982 dsty
= sty
- (5 * cliy
/ 6);
983 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
988 Scroll(-1, sty
+ (5 * cliy
/ 6));
992 Scroll(0, event
.ControlDown() ? 0 : -1);
996 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1000 Scroll(-1, sty
- 1);
1004 Scroll(-1, sty
+ 1);
1008 Scroll(stx
- 1, -1);
1012 Scroll(stx
+ 1, -1);
1021 int xScroll
= GetScrollPos(wxHORIZONTAL
);
1022 if ( xScroll
!= xScrollOld
)
1024 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, xScroll
,
1026 event
.SetEventObject(this);
1027 GetEventHandler()->ProcessEvent(event
);
1030 int yScroll
= GetScrollPos(wxVERTICAL
);
1031 if ( yScroll
!= yScrollOld
)
1033 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, yScroll
,
1035 event
.SetEventObject(this);
1036 GetEventHandler()->ProcessEvent(event
);