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"
34 #include "wx/gtk/private.h"
35 #include "wx/gtk/win_gtk.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
42 EVT_SCROLLWIN(wxScrolledWindow::OnScroll
)
43 EVT_SIZE(wxScrolledWindow::OnSize
)
44 EVT_PAINT(wxScrolledWindow::OnPaint
)
45 EVT_CHAR(wxScrolledWindow::OnChar
)
48 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
50 // ============================================================================
52 // ============================================================================
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 extern bool g_blockEventsOnDrag
;
59 extern bool g_blockEventsOnScroll
;
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 extern void wxapp_install_idle_handler();
68 //-----------------------------------------------------------------------------
69 // "value_changed" from m_vAdjust
70 //-----------------------------------------------------------------------------
73 static void gtk_scrolled_window_vscroll_callback( GtkAdjustment
*adjust
,
75 wxScrolledWindow
*win
)
78 wxapp_install_idle_handler();
80 if (g_blockEventsOnDrag
) return;
82 if (!win
->m_hasVMT
) return;
84 win
->GtkVScroll( adjust
->value
,
85 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win
->m_widget
)->vscrollbar
) );
89 //-----------------------------------------------------------------------------
90 // "value_changed" from m_hAdjust
91 //-----------------------------------------------------------------------------
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
) );
109 //-----------------------------------------------------------------------------
110 // "button_press_event" from scrollbar
111 //-----------------------------------------------------------------------------
114 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
115 GdkEventButton
*gdk_event
,
119 wxapp_install_idle_handler();
121 g_blockEventsOnScroll
= TRUE
;
123 // FIXME: there is no slider field any more, what was meant here?
125 win
->m_isScrolling
= (gdk_event
->window
== widget
->slider
);
132 //-----------------------------------------------------------------------------
133 // "button_release_event" from scrollbar
134 //-----------------------------------------------------------------------------
137 static gint
gtk_scrollbar_button_release_callback( GtkRange
*widget
,
138 GdkEventButton
*WXUNUSED(gdk_event
),
141 // don't test here as we can release the mouse while being over
142 // a different window than the slider
144 // if (gdk_event->window != widget->slider) return FALSE;
146 g_blockEventsOnScroll
= FALSE
;
148 if (win
->m_isScrolling
)
150 wxEventType command
= wxEVT_SCROLLWIN_THUMBRELEASE
;
154 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(win
->m_widget
);
155 if (widget
== GTK_RANGE(scrolledWindow
->hscrollbar
))
157 value
= (int)(win
->m_hAdjust
->value
+0.5);
160 if (widget
== GTK_RANGE(scrolledWindow
->vscrollbar
))
162 value
= (int)(win
->m_vAdjust
->value
+0.5);
166 wxScrollWinEvent
event( command
, value
, dir
);
167 event
.SetEventObject( win
);
168 win
->GetEventHandler()->ProcessEvent( event
);
171 win
->m_isScrolling
= FALSE
;
177 //-----------------------------------------------------------------------------
178 // InsertChild for wxScrolledWindow
179 //-----------------------------------------------------------------------------
181 static void wxInsertChildInScrolledWindow( wxWindow
* parent
, wxWindow
* child
)
183 // The window might have been scrolled already, do we
184 // have to adapt the position.
185 GtkPizza
*pizza
= GTK_PIZZA(parent
->m_wxwindow
);
186 child
->m_x
+= pizza
->xoffset
;
187 child
->m_y
+= pizza
->yoffset
;
189 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
190 GTK_WIDGET(child
->m_widget
),
197 // ----------------------------------------------------------------------------
198 // wxScrolledWindow creation
199 // ----------------------------------------------------------------------------
201 void wxScrolledWindow::Init()
203 m_xScrollPixelsPerLine
= 0;
204 m_yScrollPixelsPerLine
= 0;
205 m_xScrollingEnabled
= TRUE
;
206 m_yScrollingEnabled
= TRUE
;
207 m_xScrollPosition
= 0;
208 m_yScrollPosition
= 0;
209 m_xScrollLinesPerPage
= 0;
210 m_yScrollLinesPerPage
= 0;
211 m_targetWindow
= (wxWindow
*) NULL
;
214 m_hasScrolling
= TRUE
;
217 bool wxScrolledWindow::Create(wxWindow
*parent
,
222 const wxString
& name
)
226 if (!PreCreation( parent
, pos
, size
) ||
227 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
229 wxFAIL_MSG( wxT("wxWindow creation failed") );
233 m_insertCallback
= wxInsertChildInScrolledWindow
;
235 m_targetWindow
= this;
237 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
238 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
240 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
242 GtkScrolledWindowClass
*scroll_class
= GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
243 scroll_class
->scrollbar_spacing
= 0;
245 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
247 m_hAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->hscrollbar
) );
248 m_vAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->vscrollbar
) );
250 m_wxwindow
= gtk_pizza_new();
252 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
254 GtkPizza
*pizza
= GTK_PIZZA(m_wxwindow
);
256 if (HasFlag(wxRAISED_BORDER
))
258 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_OUT
);
260 else if (HasFlag(wxSUNKEN_BORDER
))
262 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_IN
);
264 else if (HasFlag(wxSIMPLE_BORDER
))
266 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_THIN
);
270 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_NONE
);
273 GTK_WIDGET_SET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
274 m_acceptsFocus
= TRUE
;
276 // I _really_ don't want scrollbars in the beginning
277 m_vAdjust
->lower
= 0.0;
278 m_vAdjust
->upper
= 1.0;
279 m_vAdjust
->value
= 0.0;
280 m_vAdjust
->step_increment
= 1.0;
281 m_vAdjust
->page_increment
= 2.0;
282 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
283 m_hAdjust
->lower
= 0.0;
284 m_hAdjust
->upper
= 1.0;
285 m_hAdjust
->value
= 0.0;
286 m_hAdjust
->step_increment
= 1.0;
287 m_hAdjust
->page_increment
= 2.0;
288 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
290 // Handlers for new scrollbar values
294 // these handlers block mouse events to any window during scrolling such as
295 // motion events and prevent GTK and wxWidgets from fighting over where the
298 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->vscrollbar
), "button_press_event",
299 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
301 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->hscrollbar
), "button_press_event",
302 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
304 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->vscrollbar
), "button_release_event",
305 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
307 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->hscrollbar
), "button_release_event",
308 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
310 gtk_widget_show( m_wxwindow
);
313 m_parent
->DoAddChild( this );
315 m_focusWidget
= m_wxwindow
;
324 // ----------------------------------------------------------------------------
325 // setting scrolling parameters
326 // ----------------------------------------------------------------------------
328 void wxScrolledWindow::DoSetVirtualSize( int x
, int y
)
330 wxPanel::DoSetVirtualSize( x
, y
);
337 // wxWindow's GetBestVirtualSize returns the actual window size,
338 // whereas we want to return the virtual size
339 wxSize
wxScrolledWindow::GetBestVirtualSize() const
341 wxSize
clientSize( GetClientSize() );
344 wxSize
minSize( GetSizer()->CalcMin() );
346 return wxSize( wxMax( clientSize
.x
, minSize
.x
), wxMax( clientSize
.y
, minSize
.y
) );
352 // return the size best suited for the current window
353 // (this isn't a virtual size, this is a sensible size for the window)
354 wxSize
wxScrolledWindow::DoGetBestSize() const
360 wxSize b
= GetSizer()->GetMinSize();
362 // Only use the content to set the window size in the direction
363 // where there's no scrolling; otherwise we're going to get a huge
364 // window in the direction in which scrolling is enabled
366 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
369 if ( GetMinSize().IsFullySpecified() )
370 minSize
= GetMinSize();
381 return wxWindow::DoGetBestSize();
383 // Add any difference between size and client size
384 wxSize diff
= GetSize() - GetClientSize();
385 best
.x
+= wxMax(0, diff
.x
);
386 best
.y
+= wxMax(0, diff
.y
);
392 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
393 * noUnitsX/noUnitsY: : no. units per scrollbar
395 void wxScrolledWindow::SetScrollbars( int pixelsPerUnitX
, int pixelsPerUnitY
,
396 int noUnitsX
, int noUnitsY
,
397 int xPos
, int yPos
, bool noRefresh
)
400 GetViewStart (& xs
, & ys
);
402 int old_x
= m_xScrollPixelsPerLine
* xs
;
403 int old_y
= m_yScrollPixelsPerLine
* ys
;
405 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
406 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
408 m_hAdjust
->value
= m_xScrollPosition
= xPos
;
409 m_vAdjust
->value
= m_yScrollPosition
= yPos
;
411 // Setting hints here should arguably be deprecated, but without it
412 // a sizer might override this manual scrollbar setting in old code.
413 // m_targetWindow->SetVirtualSizeHints( noUnitsX * pixelsPerUnitX, noUnitsY * pixelsPerUnitY );
415 int w
= noUnitsX
* pixelsPerUnitX
;
416 int h
= noUnitsY
* pixelsPerUnitY
;
417 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
418 h
? h
: wxDefaultCoord
);
422 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
423 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
425 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
429 void wxScrolledWindow::AdjustScrollbars()
434 m_targetWindow
->GetClientSize( &w
, &h
);
435 m_targetWindow
->GetVirtualSize( &vw
, &vh
);
437 if (m_xScrollPixelsPerLine
== 0)
439 m_hAdjust
->upper
= 1.0;
440 m_hAdjust
->page_increment
= 1.0;
441 m_hAdjust
->page_size
= 1.0;
445 m_hAdjust
->upper
= (vw
+m_xScrollPixelsPerLine
-1) / m_xScrollPixelsPerLine
;
446 m_hAdjust
->page_size
= w
/ m_xScrollPixelsPerLine
;
447 m_hAdjust
->page_increment
= w
/ m_xScrollPixelsPerLine
;
449 // Special case. When client and virtual size are very close but
450 // the client is big enough, kill scrollbar.
452 if ((m_hAdjust
->page_size
< m_hAdjust
->upper
) && (w
>= vw
))
453 m_hAdjust
->page_size
+= 1.0;
455 // If the scrollbar hits the right side, move the window
456 // right to keep it from over extending.
458 if ( !wxIsNullDouble(m_hAdjust
->value
) &&
459 (m_hAdjust
->value
+ m_hAdjust
->page_size
> m_hAdjust
->upper
) )
461 m_hAdjust
->value
= m_hAdjust
->upper
- m_hAdjust
->page_size
;
462 if (m_hAdjust
->value
< 0.0)
463 m_hAdjust
->value
= 0.0;
465 if (GetChildren().GetCount() == 0)
466 m_xScrollPosition
= (int)m_hAdjust
->value
; // This is enough without child windows
468 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" ); // Actually scroll window
472 if (m_yScrollPixelsPerLine
== 0)
474 m_vAdjust
->upper
= 1.0;
475 m_vAdjust
->page_increment
= 1.0;
476 m_vAdjust
->page_size
= 1.0;
480 m_vAdjust
->upper
= (vh
+m_yScrollPixelsPerLine
-1) / m_yScrollPixelsPerLine
;
481 m_vAdjust
->page_size
= h
/ m_yScrollPixelsPerLine
;
482 m_vAdjust
->page_increment
= h
/ m_yScrollPixelsPerLine
;
484 if ((m_vAdjust
->page_size
< m_vAdjust
->upper
) && (h
>= vh
))
485 m_vAdjust
->page_size
+= 1.0;
487 if ( !wxIsNullDouble(m_vAdjust
->value
) &&
488 (m_vAdjust
->value
+ m_vAdjust
->page_size
> m_vAdjust
->upper
) )
490 m_vAdjust
->value
= m_vAdjust
->upper
- m_vAdjust
->page_size
;
491 if (m_vAdjust
->value
< 0.0)
492 m_vAdjust
->value
= 0.0;
494 if (GetChildren().GetCount() == 0)
495 m_yScrollPosition
= (int)m_vAdjust
->value
;
497 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
501 m_xScrollLinesPerPage
= (int)(m_hAdjust
->page_increment
+ 0.5);
502 m_yScrollLinesPerPage
= (int)(m_vAdjust
->page_increment
+ 0.5);
504 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
505 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
509 // ----------------------------------------------------------------------------
510 // target window handling
511 // ----------------------------------------------------------------------------
513 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
, bool WXUNUSED(pushEventHandler
) )
515 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
516 m_targetWindow
= target
;
519 wxWindow
*wxScrolledWindow::GetTargetWindow() const
521 return m_targetWindow
;
524 // Override this function if you don't want to have wxScrolledWindow
525 // automatically change the origin according to the scroll position.
526 void wxScrolledWindow::DoPrepareDC(wxDC
& dc
)
528 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
529 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
532 void wxScrolledWindow::SetScrollRate( int xstep
, int ystep
)
534 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
535 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
537 m_xScrollPixelsPerLine
= xstep
;
538 m_yScrollPixelsPerLine
= ystep
;
540 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
541 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
543 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
548 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
551 *x_unit
= m_xScrollPixelsPerLine
;
553 *y_unit
= m_yScrollPixelsPerLine
;
556 int wxScrolledWindow::GetScrollPageSize(int orient
) const
558 if ( orient
== wxHORIZONTAL
)
559 return m_xScrollLinesPerPage
;
561 return m_yScrollLinesPerPage
;
564 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
566 if ( orient
== wxHORIZONTAL
)
567 m_xScrollLinesPerPage
= pageSize
;
569 m_yScrollLinesPerPage
= pageSize
;
572 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
574 int orient
= event
.GetOrientation();
576 int nScrollInc
= CalcScrollInc(event
);
577 if (nScrollInc
== 0) return;
579 if (orient
== wxHORIZONTAL
)
581 int newPos
= m_xScrollPosition
+ nScrollInc
;
582 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
586 int newPos
= m_yScrollPosition
+ nScrollInc
;
587 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
590 if (orient
== wxHORIZONTAL
)
592 m_xScrollPosition
+= nScrollInc
;
596 m_yScrollPosition
+= nScrollInc
;
599 if (orient
== wxHORIZONTAL
)
601 if (m_xScrollingEnabled
)
602 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
604 m_targetWindow
->Refresh();
608 if (m_yScrollingEnabled
)
609 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
611 m_targetWindow
->Refresh();
615 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
617 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
619 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
620 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
622 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
624 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
625 if (max
< 0) max
= 0;
626 if (x_pos
> max
) x_pos
= max
;
627 if (x_pos
< 0) x_pos
= 0;
629 int old_x
= m_xScrollPosition
;
630 m_xScrollPosition
= x_pos
;
631 m_hAdjust
->value
= x_pos
;
633 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
635 // Just update the scrollbar, don't send any wxWidgets event
636 GtkHDisconnectEvent();
637 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" );
641 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
643 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
644 if (max
< 0) max
= 0;
645 if (y_pos
> max
) y_pos
= max
;
646 if (y_pos
< 0) y_pos
= 0;
648 int old_y
= m_yScrollPosition
;
649 m_yScrollPosition
= y_pos
;
650 m_vAdjust
->value
= y_pos
;
652 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
654 // Just update the scrollbar, don't send any wxWidgets event
655 GtkVDisconnectEvent();
656 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
661 // TODO: [VH]Scroll functions should be combined
663 void wxScrolledWindow::GtkVScroll( float value
, unsigned int scroll_type
)
665 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
667 if (m_yScrollPixelsPerLine
== 0)
670 int y_pos
= (int)(value
+0.5);
672 if (y_pos
== m_yScrollPosition
)
675 wxEventType command
= GtkScrollWinTypeToWx(scroll_type
);
677 wxScrollWinEvent
event( command
, y_pos
, wxVERTICAL
);
678 event
.SetEventObject( this );
679 GetEventHandler()->ProcessEvent( event
);
682 void wxScrolledWindow::GtkHScroll( float value
, unsigned int scroll_type
)
684 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
686 if (m_xScrollPixelsPerLine
== 0)
689 int x_pos
= (int)(value
+0.5);
691 if (x_pos
== m_xScrollPosition
)
694 wxEventType command
= GtkScrollWinTypeToWx(scroll_type
);
696 wxScrollWinEvent
event( command
, x_pos
, wxHORIZONTAL
);
697 event
.SetEventObject( this );
698 GetEventHandler()->ProcessEvent( event
);
701 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
703 m_xScrollingEnabled
= x_scroll
;
704 m_yScrollingEnabled
= y_scroll
;
707 // Where the current view starts from
708 void wxScrolledWindow::GetViewStart (int *x
, int *y
) const
711 *x
= m_xScrollPosition
;
713 *y
= m_yScrollPosition
;
716 void wxScrolledWindow::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
719 GetViewStart (& xs
, & ys
);
722 *xx
= x
- xs
* m_xScrollPixelsPerLine
;
724 *yy
= y
- ys
* m_yScrollPixelsPerLine
;
727 void wxScrolledWindow::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
730 GetViewStart (& xs
, & ys
);
733 *xx
= x
+ xs
* m_xScrollPixelsPerLine
;
735 *yy
= y
+ ys
* m_yScrollPixelsPerLine
;
738 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
740 int pos
= event
.GetPosition();
741 int orient
= event
.GetOrientation();
744 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
746 if (orient
== wxHORIZONTAL
)
747 nScrollInc
= - m_xScrollPosition
;
749 nScrollInc
= - m_yScrollPosition
;
751 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
753 if (orient
== wxHORIZONTAL
)
754 nScrollInc
= GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine
- m_xScrollPosition
;
756 nScrollInc
= GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine
- m_yScrollPosition
;
758 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
762 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
766 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
768 if (orient
== wxHORIZONTAL
)
769 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
771 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
773 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
775 if (orient
== wxHORIZONTAL
)
776 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
778 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
780 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
781 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
783 if (orient
== wxHORIZONTAL
)
784 nScrollInc
= pos
- m_xScrollPosition
;
786 nScrollInc
= pos
- m_yScrollPosition
;
789 if (orient
== wxHORIZONTAL
)
791 if (m_xScrollPixelsPerLine
> 0)
793 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
794 if (max
< 0) max
= 0;
796 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
797 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
798 else if ( (m_xScrollPosition
+ nScrollInc
) > max
)
799 nScrollInc
= max
- m_xScrollPosition
; // As +ve as we can go
802 m_targetWindow
->Refresh();
806 if (m_yScrollPixelsPerLine
> 0)
808 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
809 if (max
< 0) max
= 0;
811 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
812 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
813 else if ( (m_yScrollPosition
+ nScrollInc
) > max
)
814 nScrollInc
= max
- m_yScrollPosition
; // As +ve as we can go
817 m_targetWindow
->Refresh();
823 void wxScrolledWindow::SetScrollPos( int orient
, int pos
, bool refresh
)
825 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
827 wxCHECK_RET( m_wxwindow
!= NULL
, wxT("window needs client area for scrolling") );
829 if (orient
== wxHORIZONTAL
)
831 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
832 if (max
< 0) max
= 0;
834 if (pos
> max
) pos
= 0;
835 if (pos
< 0) pos
= 0;
837 if (pos
== (int)(m_hAdjust
->value
+0.5)) return;
838 m_hAdjust
->value
= pos
;
842 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
843 if (max
< 0) max
= 0;
845 if (pos
> max
) pos
= 0;
846 if (pos
< 0) pos
= 0;
848 if (pos
== (int)(m_vAdjust
->value
+0.5)) return;
849 m_vAdjust
->value
= pos
;
852 if (m_wxwindow
->window
)
854 if (orient
== wxHORIZONTAL
)
856 // Just update the scrollbar, don't send any wxWidgets event
857 GtkHDisconnectEvent();
858 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" );
863 // Just update the scrollbar, don't send any wxWidgets event
864 GtkVDisconnectEvent();
865 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
871 void wxScrolledWindow::GtkVConnectEvent()
873 gtk_signal_connect( GTK_OBJECT(m_vAdjust
), "value_changed",
874 (GtkSignalFunc
) gtk_scrolled_window_vscroll_callback
, (gpointer
) this );
877 void wxScrolledWindow::GtkHConnectEvent()
879 gtk_signal_connect( GTK_OBJECT(m_hAdjust
), "value_changed",
880 (GtkSignalFunc
) gtk_scrolled_window_hscroll_callback
, (gpointer
) this );
883 void wxScrolledWindow::GtkHDisconnectEvent()
885 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust
),
886 (GtkSignalFunc
) gtk_scrolled_window_hscroll_callback
, (gpointer
) this );
889 void wxScrolledWindow::GtkVDisconnectEvent()
891 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust
),
892 (GtkSignalFunc
) gtk_scrolled_window_vscroll_callback
, (gpointer
) this );
895 bool wxScrolledWindow::Layout()
897 if (GetSizer() && m_targetWindow
== this)
899 // If we're the scroll target, take into account the
900 // virtual size and scrolled position of the window.
903 CalcScrolledPosition(0,0, &x
,&y
);
904 GetVirtualSize(&w
, &h
);
905 GetSizer()->SetDimension(x
, y
, w
, h
);
909 return wxPanel::Layout(); // fall back to default for LayoutConstraints
912 // ----------------------------------------------------------------------------
914 // ----------------------------------------------------------------------------
916 // Default OnSize resets scrollbars, if any
917 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
919 if ( m_targetWindow
->GetAutoLayout() )
921 wxSize size
= m_targetWindow
->GetBestVirtualSize();
923 // This will call ::Layout() and ::AdjustScrollbars()
924 SetVirtualSize( size
);
932 // This calls OnDraw, having adjusted the origin according to the current
934 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
942 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
943 // compatibility here - if we used OnKeyDown(), the programs which process
944 // arrows themselves in their OnChar() would never get the message and like
945 // this they always have the priority
946 void wxScrolledWindow::OnChar(wxKeyEvent
& event
)
948 int stx
, sty
, // view origin
949 szx
, szy
, // view size (total)
950 clix
, cliy
; // view size (on screen)
952 GetViewStart(&stx
, &sty
);
953 GetClientSize(&clix
, &cliy
);
954 GetVirtualSize(&szx
, &szy
);
956 if( m_xScrollPixelsPerLine
)
958 clix
/= m_xScrollPixelsPerLine
;
959 szx
/= m_xScrollPixelsPerLine
;
966 if( m_yScrollPixelsPerLine
)
968 cliy
/= m_yScrollPixelsPerLine
;
969 szy
/= m_yScrollPixelsPerLine
;
977 int xScrollOld
= GetScrollPos(wxHORIZONTAL
),
978 yScrollOld
= GetScrollPos(wxVERTICAL
);
981 switch ( event
.GetKeyCode() )
985 dsty
= sty
- (5 * cliy
/ 6);
986 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
991 Scroll(-1, sty
+ (5 * cliy
/ 6));
995 Scroll(0, event
.ControlDown() ? 0 : -1);
999 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1003 Scroll(-1, sty
- 1);
1007 Scroll(-1, sty
+ 1);
1011 Scroll(stx
- 1, -1);
1015 Scroll(stx
+ 1, -1);
1024 int xScroll
= GetScrollPos(wxHORIZONTAL
);
1025 if ( xScroll
!= xScrollOld
)
1027 wxScrollWinEvent
eventS(wxEVT_SCROLLWIN_THUMBTRACK
, xScroll
,
1029 eventS
.SetEventObject(this);
1030 GetEventHandler()->ProcessEvent(eventS
);
1033 int yScroll
= GetScrollPos(wxVERTICAL
);
1034 if ( yScroll
!= yScrollOld
)
1036 wxScrollWinEvent
eventS(wxEVT_SCROLLWIN_THUMBTRACK
, yScroll
,
1038 eventS
.SetEventObject(this);
1039 GetEventHandler()->ProcessEvent(eventS
);